Skip to content

Allow use of "finalized" tag in rpc requests #2298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,37 @@ func TestSettingGingerbreadOnGenesisBlock(t *testing.T) {
require.Greater(t, block.BaseFee().Uint64(), uint64(0))
require.Greater(t, block.GasLimit(), uint64(0))
}

// This test checks that retreiveing the "finalized" block results in the same response as retrieving the "latest" block.
func TestGetFinalizedBlock(t *testing.T) {
ac := test.AccountConfig(2, 2)
gingerbreadBlock := common.Big0
gc, ec, err := test.BuildConfig(ac, gingerbreadBlock)
require.NoError(t, err)
network, shutdown, err := test.NewNetwork(ac, gc, ec)
require.NoError(t, err)
defer shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

// Wait for at least one block to be built.
err = network.AwaitBlock(ctx, 1)
require.NoError(t, err)

// Stop one of the two validators, so no more blocks can be created.
err = network[1].Close()
require.NoError(t, err)

c := network[0].WsClient.GetRPCClient()
h := types.Header{}
err = c.CallContext(ctx, &h, "eth_getHeaderByNumber", "latest")
require.NoError(t, err)
require.GreaterOrEqual(t, h.Number.Uint64(), uint64(1))

h2 := types.Header{}
err = c.CallContext(ctx, &h2, "eth_getHeaderByNumber", "finalized")
require.NoError(t, err)

// Check latest and finalzed block are the same
require.Equal(t, h.Hash(), h2.Hash())
}
2 changes: 1 addition & 1 deletion internal/jsre/deps/web3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ type RPCTransaction struct {
EthCompatible bool `json:"ethCompatible"`
}

// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
// - "latest", "earliest" or "pending" as string arguments
// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It
// supports: - "finalized", latest", "earliest" or "pending" as string
// arguments where finalized is equivalent to latest since all blocks are final
// in celo.
// - the block number
// Returned errors:
// - an invalid block number error when the given argument isn't a known strings
Expand All @@ -106,6 +108,9 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
case "pending":
*bn = PendingBlockNumber
return nil
case "finalized":
*bn = LatestBlockNumber
return nil
}

blckNum, err := hexutil.DecodeUint64(input)
Expand Down Expand Up @@ -172,6 +177,10 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
bn := LatestBlockNumber
bnh.BlockNumber = &bn
return nil
case "finalized":
bn := LatestBlockNumber
bnh.BlockNumber = &bn
return nil
case "pending":
bn := PendingBlockNumber
bnh.BlockNumber = &bn
Expand Down
33 changes: 18 additions & 15 deletions rpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) {
11: {`"pending"`, false, PendingBlockNumber},
12: {`"latest"`, false, LatestBlockNumber},
13: {`"earliest"`, false, EarliestBlockNumber},
14: {`someString`, true, BlockNumber(0)},
15: {`""`, true, BlockNumber(0)},
16: {``, true, BlockNumber(0)},
14: {`"finalized"`, false, LatestBlockNumber},
15: {`someString`, true, BlockNumber(0)},
16: {`""`, true, BlockNumber(0)},
17: {``, true, BlockNumber(0)},
}

for i, test := range tests {
Expand Down Expand Up @@ -87,18 +88,20 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
14: {`someString`, true, BlockNumberOrHash{}},
15: {`""`, true, BlockNumberOrHash{}},
16: {``, true, BlockNumberOrHash{}},
17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
14: {`"finalized"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
15: {`someString`, true, BlockNumberOrHash{}},
16: {`""`, true, BlockNumberOrHash{}},
17: {``, true, BlockNumberOrHash{}},
18: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
21: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
22: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
23: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
24: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
25: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
26: {`{"blockNumber":"finalized"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
27: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
}

for i, test := range tests {
Expand Down