Skip to content

Commit

Permalink
Node/EVM: Add GetLatestBlocks API
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-riley committed Feb 21, 2025
1 parent 2ea519c commit 3c8fbd5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 844 deletions.
4 changes: 4 additions & 0 deletions node/pkg/adminrpc/adminserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (m mockEVMConnector) SubscribeForBlocks(ctx context.Context, errC chan erro
panic("unimplemented")
}

func (e mockEVMConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
panic("unimplemented")
}

func (m mockEVMConnector) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
panic("unimplemented")
}
Expand Down
2 changes: 1 addition & 1 deletion node/pkg/watchers/evm/ccq_backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (w *Watcher) ccqBackfillStart(ctx context.Context, errC chan error) {
// ccqBackfillInit determines the maximum batch size to be used for backfilling the cache. It also loads the initial batch of timestamps.
func (w *Watcher) ccqBackfillInit(ctx context.Context) error {
// Get the latest block so we can use that as the starting point in our cache.
latestBlock, err := connectors.GetLatestBlock(ctx, w.ccqLogger, w.ethConn)
latestBlock, err := connectors.GetLatestBlock(ctx, w.ethConn)
if err != nil {
return fmt.Errorf("failed to look up latest block: %w", err)
}
Expand Down
26 changes: 26 additions & 0 deletions node/pkg/watchers/evm/connectors/batch_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ func (b *BatchPollConnector) SubscribeForBlocks(ctx context.Context, errC chan e
return headerSubscription, nil
}

func (b *BatchPollConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
block, err := GetBlockByFinality(ctx, b.Connector, Latest)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to get latest block: %w", err)
}
latest = block.Number.Uint64()

block, err = GetBlockByFinality(ctx, b.Connector, Finalized)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to get finalized block: %w", err)
}
finalized = block.Number.Uint64()

if b.generateSafe {
safe = finalized
} else {
block, err = GetBlockByFinality(ctx, b.Connector, Safe)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to get safe block: %w", err)
}
safe = block.Number.Uint64()
}

return
}

// pollBlocks polls for the latest blocks (finalized, safe and latest), compares them to the last ones, and publishes any new ones.
// In the case of an error, it returns the last blocks that were passed in, otherwise it returns the new blocks.
func (b *BatchPollConnector) pollBlocks(ctx context.Context, sink chan<- *NewBlock, prevBlocks Blocks) (Blocks, error) {
Expand Down
4 changes: 4 additions & 0 deletions node/pkg/watchers/evm/connectors/batch_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (e *mockConnectorForBatchPoller) SubscribeForBlocks(ctx context.Context, er
return e.sub, fmt.Errorf("not implemented")
}

func (e *mockConnectorForBatchPoller) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
return e.prevLatest, e.prevSafe, e.prevFinalized, nil
}

func (e *mockConnectorForBatchPoller) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
panic("method not implemented by mockConnectorForBatchPoller")
}
Expand Down
49 changes: 49 additions & 0 deletions node/pkg/watchers/evm/connectors/block_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package connectors

import (
"context"
"fmt"
"math/big"
"time"
)

func GetLatestBlock(ctx context.Context, conn Connector) (*NewBlock, error) {
return GetBlockByFinality(ctx, conn, Latest)
}

func GetBlockByFinality(ctx context.Context, conn Connector, blockFinality FinalityLevel) (*NewBlock, error) {
return GetBlock(ctx, conn, blockFinality.String(), blockFinality)
}

func GetBlockByNumberUint64(ctx context.Context, conn Connector, blockNum uint64, blockFinality FinalityLevel) (*NewBlock, error) {
return GetBlock(ctx, conn, "0x"+fmt.Sprintf("%x", blockNum), blockFinality)
}

func GetBlock(ctx context.Context, conn Connector, str string, blockFinality FinalityLevel) (*NewBlock, error) {
timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()

var m BlockMarshaller
err := conn.RawCallContext(timeout, &m, "eth_getBlockByNumber", str, false)
if err != nil {
return nil, fmt.Errorf("failed to get block for %s: %w", str, err)
}
if m.Number == nil {
return nil, fmt.Errorf("failed to unmarshal block for %s: Number is nil", str)
}
n := big.Int(*m.Number)

var l1bn *big.Int
if m.L1BlockNumber != nil {
bn := big.Int(*m.L1BlockNumber)
l1bn = &bn
}

return &NewBlock{
Number: &n,
Time: uint64(m.Time),
Hash: m.Hash,
L1BlockNumber: l1bn,
Finality: blockFinality,
}, nil
}
1 change: 1 addition & 0 deletions node/pkg/watchers/evm/connectors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Connector interface {
TimeOfBlockByHash(ctx context.Context, hash common.Hash) (uint64, error)
ParseLogMessagePublished(log types.Log) (*ethabi.AbiLogMessagePublished, error)
SubscribeForBlocks(ctx context.Context, errC chan error, sink chan<- *NewBlock) (ethereum.Subscription, error)
GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error)
RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
RawBatchCallContext(ctx context.Context, b []rpc.BatchElem) error
Client() *ethClient.Client
Expand Down
4 changes: 4 additions & 0 deletions node/pkg/watchers/evm/connectors/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (e *EthereumBaseConnector) SubscribeForBlocks(ctx context.Context, errC cha
panic("not implemented")
}

func (e *EthereumBaseConnector) GetLatest(ctx context.Context) (latest, finalized, safe uint64, err error) {
panic("not implemented")
}

func (e *EthereumBaseConnector) RawCallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
return e.rawClient.CallContext(ctx, result, method, args...)
}
Expand Down
243 changes: 0 additions & 243 deletions node/pkg/watchers/evm/connectors/finalizer_poller.go

This file was deleted.

Loading

0 comments on commit 3c8fbd5

Please sign in to comment.