Skip to content

Commit 53e9961

Browse files
authored
Node/EVM: Add GetLatestBlocks API (#4274)
* Node/EVM: Add GetLatestBlocks API * Code review rework
1 parent 94895c9 commit 53e9961

File tree

11 files changed

+102
-844
lines changed

11 files changed

+102
-844
lines changed

node/pkg/adminrpc/adminserver_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (m mockEVMConnector) SubscribeForBlocks(ctx context.Context, errC chan erro
7272
panic("unimplemented")
7373
}
7474

75+
func (e mockEVMConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
76+
panic("unimplemented")
77+
}
78+
7579
func (m mockEVMConnector) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
7680
panic("unimplemented")
7781
}

node/pkg/watchers/evm/ccq_backfill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (w *Watcher) ccqBackfillStart(ctx context.Context, errC chan error) {
7474
// ccqBackfillInit determines the maximum batch size to be used for backfilling the cache. It also loads the initial batch of timestamps.
7575
func (w *Watcher) ccqBackfillInit(ctx context.Context) error {
7676
// Get the latest block so we can use that as the starting point in our cache.
77-
latestBlock, err := connectors.GetLatestBlock(ctx, w.ccqLogger, w.ethConn)
77+
latestBlock, err := connectors.GetLatestBlock(ctx, w.ethConn)
7878
if err != nil {
7979
return fmt.Errorf("failed to look up latest block: %w", err)
8080
}

node/pkg/watchers/evm/connectors/batch_poller.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@ func (b *BatchPollConnector) SubscribeForBlocks(ctx context.Context, errC chan e
135135
return headerSubscription, nil
136136
}
137137

138+
func (b *BatchPollConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
139+
block, err := GetBlockByFinality(ctx, b.Connector, Latest)
140+
if err != nil {
141+
return 0, 0, 0, fmt.Errorf("failed to get latest block: %w", err)
142+
}
143+
latest = block.Number.Uint64()
144+
145+
block, err = GetBlockByFinality(ctx, b.Connector, Finalized)
146+
if err != nil {
147+
return 0, 0, 0, fmt.Errorf("failed to get finalized block: %w", err)
148+
}
149+
finalized = block.Number.Uint64()
150+
151+
if b.generateSafe {
152+
safe = finalized
153+
} else {
154+
block, err = GetBlockByFinality(ctx, b.Connector, Safe)
155+
if err != nil {
156+
return 0, 0, 0, fmt.Errorf("failed to get safe block: %w", err)
157+
}
158+
safe = block.Number.Uint64()
159+
}
160+
161+
return
162+
}
163+
138164
// pollBlocks polls for the latest blocks (finalized, safe and latest), compares them to the last ones, and publishes any new ones.
139165
// In the case of an error, it returns the last blocks that were passed in, otherwise it returns the new blocks.
140166
func (b *BatchPollConnector) pollBlocks(ctx context.Context, sink chan<- *NewBlock, prevBlocks Blocks) (Blocks, error) {

node/pkg/watchers/evm/connectors/batch_poller_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func (e *mockConnectorForBatchPoller) SubscribeForBlocks(ctx context.Context, er
9191
return e.sub, fmt.Errorf("not implemented")
9292
}
9393

94+
func (e *mockConnectorForBatchPoller) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
95+
return e.prevLatest, e.prevFinalized, e.prevSafe, nil
96+
}
97+
9498
func (e *mockConnectorForBatchPoller) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
9599
panic("method not implemented by mockConnectorForBatchPoller")
96100
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package connectors
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/big"
7+
"time"
8+
)
9+
10+
func GetLatestBlock(ctx context.Context, conn Connector) (*NewBlock, error) {
11+
return GetBlockByFinality(ctx, conn, Latest)
12+
}
13+
14+
func GetBlockByFinality(ctx context.Context, conn Connector, blockFinality FinalityLevel) (*NewBlock, error) {
15+
return GetBlock(ctx, conn, blockFinality.String(), blockFinality)
16+
}
17+
18+
func GetBlockByNumberUint64(ctx context.Context, conn Connector, blockNum uint64, blockFinality FinalityLevel) (*NewBlock, error) {
19+
return GetBlock(ctx, conn, "0x"+fmt.Sprintf("%x", blockNum), blockFinality)
20+
}
21+
22+
func GetBlock(ctx context.Context, conn Connector, str string, blockFinality FinalityLevel) (*NewBlock, error) {
23+
timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
24+
defer cancel()
25+
26+
var m BlockMarshaller
27+
err := conn.RawCallContext(timeout, &m, "eth_getBlockByNumber", str, false)
28+
if err != nil {
29+
return nil, fmt.Errorf("failed to get block for %s: %w", str, err)
30+
}
31+
if m.Number == nil {
32+
return nil, fmt.Errorf("failed to unmarshal block for %s: Number is nil", str)
33+
}
34+
n := big.Int(*m.Number)
35+
36+
var l1bn *big.Int
37+
if m.L1BlockNumber != nil {
38+
bn := big.Int(*m.L1BlockNumber)
39+
l1bn = &bn
40+
}
41+
42+
return &NewBlock{
43+
Number: &n,
44+
Time: uint64(m.Time),
45+
Hash: m.Hash,
46+
L1BlockNumber: l1bn,
47+
Finality: blockFinality,
48+
}, nil
49+
}

node/pkg/watchers/evm/connectors/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Connector interface {
5656
TimeOfBlockByHash(ctx context.Context, hash common.Hash) (uint64, error)
5757
ParseLogMessagePublished(log types.Log) (*ethabi.AbiLogMessagePublished, error)
5858
SubscribeForBlocks(ctx context.Context, errC chan error, sink chan<- *NewBlock) (ethereum.Subscription, error)
59+
GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error)
5960
RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
6061
RawBatchCallContext(ctx context.Context, b []rpc.BatchElem) error
6162
Client() *ethClient.Client

node/pkg/watchers/evm/connectors/ethereum.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func (e *EthereumBaseConnector) SubscribeForBlocks(ctx context.Context, errC cha
101101
panic("not implemented")
102102
}
103103

104+
func (e *EthereumBaseConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
105+
panic("not implemented")
106+
}
107+
104108
func (e *EthereumBaseConnector) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
105109
return e.rawClient.CallContext(ctx, result, method, args...)
106110
}

node/pkg/watchers/evm/connectors/finalizer_poller.go

Lines changed: 0 additions & 243 deletions
This file was deleted.

0 commit comments

Comments
 (0)