Skip to content

Commit 8400167

Browse files
piersyezdac
authored andcommitted
Allow use of "finalized" tag in rpc requests (#2298)
* Allow use of finalized tag in rpc requests This PR allows the use of the "finalized" tag to retrieve blocks or headers via the RPC. Using the "finalized" tag is equivalent to using the "latest" tag because all blocks are final in celo. This is to supprt integration with chainlink since their system depends on retrieving finalized blocks.
1 parent bd82ab1 commit 8400167

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

e2e_test/e2e_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,37 @@ func TestSettingGingerbreadOnGenesisBlock(t *testing.T) {
881881
require.Greater(t, block.BaseFee().Uint64(), uint64(0))
882882
require.Greater(t, block.GasLimit(), uint64(0))
883883
}
884+
885+
// This test checks that retreiveing the "finalized" block results in the same response as retrieving the "latest" block.
886+
func TestGetFinalizedBlock(t *testing.T) {
887+
ac := test.AccountConfig(2, 2)
888+
gingerbreadBlock := common.Big0
889+
gc, ec, err := test.BuildConfig(ac, gingerbreadBlock)
890+
require.NoError(t, err)
891+
network, shutdown, err := test.NewNetwork(ac, gc, ec)
892+
require.NoError(t, err)
893+
defer shutdown()
894+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
895+
defer cancel()
896+
897+
// Wait for at least one block to be built.
898+
err = network.AwaitBlock(ctx, 1)
899+
require.NoError(t, err)
900+
901+
// Stop one of the two validators, so no more blocks can be created.
902+
err = network[1].Close()
903+
require.NoError(t, err)
904+
905+
c := network[0].WsClient.GetRPCClient()
906+
h := types.Header{}
907+
err = c.CallContext(ctx, &h, "eth_getHeaderByNumber", "latest")
908+
require.NoError(t, err)
909+
require.GreaterOrEqual(t, h.Number.Uint64(), uint64(1))
910+
911+
h2 := types.Header{}
912+
err = c.CallContext(ctx, &h2, "eth_getHeaderByNumber", "finalized")
913+
require.NoError(t, err)
914+
915+
// Check latest and finalzed block are the same
916+
require.Equal(t, h.Hash(), h2.Hash())
917+
}

internal/jsre/deps/web3.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/types.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ type RPCTransaction struct {
8484
EthCompatible bool `json:"ethCompatible"`
8585
}
8686

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

111116
blckNum, err := hexutil.DecodeUint64(input)
@@ -172,6 +177,10 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
172177
bn := LatestBlockNumber
173178
bnh.BlockNumber = &bn
174179
return nil
180+
case "finalized":
181+
bn := LatestBlockNumber
182+
bnh.BlockNumber = &bn
183+
return nil
175184
case "pending":
176185
bn := PendingBlockNumber
177186
bnh.BlockNumber = &bn

rpc/types_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) {
4545
11: {`"pending"`, false, PendingBlockNumber},
4646
12: {`"latest"`, false, LatestBlockNumber},
4747
13: {`"earliest"`, false, EarliestBlockNumber},
48-
14: {`someString`, true, BlockNumber(0)},
49-
15: {`""`, true, BlockNumber(0)},
50-
16: {``, true, BlockNumber(0)},
48+
14: {`"finalized"`, false, LatestBlockNumber},
49+
15: {`someString`, true, BlockNumber(0)},
50+
16: {`""`, true, BlockNumber(0)},
51+
17: {``, true, BlockNumber(0)},
5152
}
5253

5354
for i, test := range tests {
@@ -87,18 +88,20 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) {
8788
11: {`"pending"`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
8889
12: {`"latest"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
8990
13: {`"earliest"`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
90-
14: {`someString`, true, BlockNumberOrHash{}},
91-
15: {`""`, true, BlockNumberOrHash{}},
92-
16: {``, true, BlockNumberOrHash{}},
93-
17: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
94-
18: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
95-
19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
96-
20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
97-
21: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
98-
22: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
99-
23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
100-
24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
101-
25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
91+
14: {`"finalized"`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
92+
15: {`someString`, true, BlockNumberOrHash{}},
93+
16: {`""`, true, BlockNumberOrHash{}},
94+
17: {``, true, BlockNumberOrHash{}},
95+
18: {`"0x0000000000000000000000000000000000000000000000000000000000000000"`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
96+
19: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
97+
20: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":false}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), false)},
98+
21: {`{"blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","requireCanonical":true}`, false, BlockNumberOrHashWithHash(common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), true)},
99+
22: {`{"blockNumber":"0x1"}`, false, BlockNumberOrHashWithNumber(1)},
100+
23: {`{"blockNumber":"pending"}`, false, BlockNumberOrHashWithNumber(PendingBlockNumber)},
101+
24: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
102+
25: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)},
103+
26: {`{"blockNumber":"finalized"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)},
104+
27: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}},
102105
}
103106

104107
for i, test := range tests {

0 commit comments

Comments
 (0)