@@ -2,12 +2,13 @@ package l1
2
2
3
3
import (
4
4
"context"
5
- "encoding/json "
5
+ "errors "
6
6
"fmt"
7
7
"math/big"
8
8
"time"
9
9
10
10
"github.com/NethermindEth/juno/l1/contract"
11
+ "github.com/ethereum/go-ethereum"
11
12
"github.com/ethereum/go-ethereum/accounts/abi/bind"
12
13
"github.com/ethereum/go-ethereum/common"
13
14
"github.com/ethereum/go-ethereum/core/types"
@@ -16,6 +17,8 @@ import (
16
17
"github.com/ethereum/go-ethereum/rpc"
17
18
)
18
19
20
+ var finalizedBlockNumber = new (big.Int ).SetInt64 (rpc .FinalizedBlockNumber .Int64 ())
21
+
19
22
type EthSubscriber struct {
20
23
ethClient * ethclient.Client
21
24
client * rpc.Client
@@ -62,23 +65,16 @@ func (s *EthSubscriber) ChainID(ctx context.Context) (*big.Int, error) {
62
65
}
63
66
64
67
func (s * EthSubscriber ) FinalisedHeight (ctx context.Context ) (uint64 , error ) {
65
- const method = "eth_getBlockByNumber"
66
68
reqTimer := time .Now ()
67
-
68
- var raw json.RawMessage
69
- if err := s .client .CallContext (ctx , & raw , method , "finalized" , false ); err != nil { //nolint:misspell
69
+ head , err := s .ethClient .HeaderByNumber (ctx , finalizedBlockNumber )
70
+ if err != nil {
71
+ if errors .Is (err , ethereum .NotFound ) {
72
+ s .listener .OnL1Call ("eth_getBlockByNumber" , time .Since (reqTimer ))
73
+ return 0 , fmt .Errorf ("finalised block not found" )
74
+ }
70
75
return 0 , fmt .Errorf ("get finalised Ethereum block: %w" , err )
71
76
}
72
- s .listener .OnL1Call (method , time .Since (reqTimer ))
73
-
74
- var head * types.Header
75
- if err := json .Unmarshal (raw , & head ); err != nil {
76
- return 0 , err
77
- }
78
-
79
- if head == nil {
80
- return 0 , fmt .Errorf ("finalised block not found" )
81
- }
77
+ s .listener .OnL1Call ("eth_getBlockByNumber" , time .Since (reqTimer ))
82
78
83
79
return head .Number .Uint64 (), nil
84
80
}
0 commit comments