Skip to content

Commit

Permalink
cherry-pick #2245 and #2424 from develop into v0.2.6 (#2447)
Browse files Browse the repository at this point in the history
* fix safe and finalized l2 block to consider l1 safe and finalized blocks respectively (#2245)

* fix and add tests for safe and finalized l2 blocks (#2424)
  • Loading branch information
tclemos authored Aug 23, 2023
1 parent 667d846 commit c1dd19c
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 54 deletions.
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func runJSONRPCServer(c config.Config, etherman *etherman.Client, chainID uint64
if _, ok := apis[jsonrpc.APIEth]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIEth,
Service: jsonrpc.NewEthEndpoints(c.RPC, chainID, pool, st, storage),
Service: jsonrpc.NewEthEndpoints(c.RPC, chainID, pool, st, etherman, storage),
})
}

Expand All @@ -345,7 +345,7 @@ func runJSONRPCServer(c config.Config, etherman *etherman.Client, chainID uint64
if _, ok := apis[jsonrpc.APIZKEVM]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIZKEVM,
Service: jsonrpc.NewZKEVMEndpoints(c.RPC, st),
Service: jsonrpc.NewZKEVMEndpoints(c.RPC, st, etherman),
})
}

Expand All @@ -359,7 +359,7 @@ func runJSONRPCServer(c config.Config, etherman *etherman.Client, chainID uint64
if _, ok := apis[jsonrpc.APIDebug]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIDebug,
Service: jsonrpc.NewDebugEndpoints(c.RPC, st),
Service: jsonrpc.NewDebugEndpoints(c.RPC, st, etherman),
})
}

Expand Down
18 changes: 17 additions & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -959,7 +960,22 @@ func (etherMan *Client) GetLatestBatchNumber() (uint64, error) {

// GetLatestBlockNumber gets the latest block number from the ethereum
func (etherMan *Client) GetLatestBlockNumber(ctx context.Context) (uint64, error) {
header, err := etherMan.EthClient.HeaderByNumber(ctx, nil)
return etherMan.getBlockNumber(ctx, rpc.LatestBlockNumber)
}

// GetSafeBlockNumber gets the safe block number from the ethereum
func (etherMan *Client) GetSafeBlockNumber(ctx context.Context) (uint64, error) {
return etherMan.getBlockNumber(ctx, rpc.SafeBlockNumber)
}

// GetFinalizedBlockNumber gets the Finalized block number from the ethereum
func (etherMan *Client) GetFinalizedBlockNumber(ctx context.Context) (uint64, error) {
return etherMan.getBlockNumber(ctx, rpc.FinalizedBlockNumber)
}

// getBlockNumber gets the block header by the provided block number from the ethereum
func (etherMan *Client) getBlockNumber(ctx context.Context, blockNumber rpc.BlockNumber) (uint64, error) {
header, err := etherMan.EthClient.HeaderByNumber(ctx, big.NewInt(int64(blockNumber)))
if err != nil || header == nil {
return 0, err
}
Expand Down
16 changes: 9 additions & 7 deletions jsonrpc/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ var defaultTraceConfig = &traceConfig{

// DebugEndpoints is the debug jsonrpc endpoint
type DebugEndpoints struct {
cfg Config
state types.StateInterface
txMan DBTxManager
cfg Config
state types.StateInterface
etherman types.EthermanInterface
txMan DBTxManager
}

// NewDebugEndpoints returns DebugEndpoints
func NewDebugEndpoints(cfg Config, state types.StateInterface) *DebugEndpoints {
func NewDebugEndpoints(cfg Config, state types.StateInterface, etherman types.EthermanInterface) *DebugEndpoints {
return &DebugEndpoints{
cfg: cfg,
state: state,
cfg: cfg,
state: state,
etherman: etherman,
}
}

Expand Down Expand Up @@ -98,7 +100,7 @@ func (d *DebugEndpoints) TraceTransaction(hash types.ArgHash, cfg *traceConfig)
// See https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug#debugtraceblockbynumber
func (d *DebugEndpoints) TraceBlockByNumber(number types.BlockNumber, cfg *traceConfig) (interface{}, types.Error) {
return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, d.state, dbTx)
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, d.state, d.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down
29 changes: 15 additions & 14 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ const (

// EthEndpoints contains implementations for the "eth" RPC endpoints
type EthEndpoints struct {
chainID uint64
cfg Config
pool types.PoolInterface
state types.StateInterface
storage storageInterface
txMan DBTxManager
cfg Config
chainID uint64
pool types.PoolInterface
state types.StateInterface
etherman types.EthermanInterface
storage storageInterface
txMan DBTxManager
}

// NewEthEndpoints creates an new instance of Eth
func NewEthEndpoints(cfg Config, chainID uint64, p types.PoolInterface, s types.StateInterface, storage storageInterface) *EthEndpoints {
e := &EthEndpoints{cfg: cfg, chainID: chainID, pool: p, state: s, storage: storage}
func NewEthEndpoints(cfg Config, chainID uint64, p types.PoolInterface, s types.StateInterface, etherman types.EthermanInterface, storage storageInterface) *EthEndpoints {
e := &EthEndpoints{cfg: cfg, chainID: chainID, pool: p, state: s, etherman: etherman, storage: storage}
s.RegisterNewL2BlockEventHandler(e.onNewL2Block)

return e
Expand Down Expand Up @@ -245,7 +246,7 @@ func (e *EthEndpoints) getBlockByArg(ctx context.Context, blockArg *types.BlockN
}

// Otherwise, try to get the block by number
blockNum, rpcErr := blockArg.Number().GetNumericBlockNumber(ctx, e.state, dbTx)
blockNum, rpcErr := blockArg.Number().GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -310,7 +311,7 @@ func (e *EthEndpoints) GetBlockByNumber(number types.BlockNumber, fullTx bool) (
return rpcBlock, nil
}
var err error
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, dbTx)
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -463,13 +464,13 @@ func (e *EthEndpoints) internalGetLogs(ctx context.Context, dbTx pgx.Tx, filter
var fromBlock uint64 = 0
if filter.FromBlock != nil {
var rpcErr types.Error
fromBlock, rpcErr = filter.FromBlock.GetNumericBlockNumber(ctx, e.state, dbTx)
fromBlock, rpcErr = filter.FromBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
}

toBlock, rpcErr := filter.ToBlock.GetNumericBlockNumber(ctx, e.state, dbTx)
toBlock, rpcErr := filter.ToBlock.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -544,7 +545,7 @@ func (e *EthEndpoints) GetTransactionByBlockHashAndIndex(hash types.ArgHash, ind
func (e *EthEndpoints) GetTransactionByBlockNumberAndIndex(number *types.BlockNumber, index types.Index) (interface{}, types.Error) {
return e.txMan.NewDbTxScope(e.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
var err error
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, dbTx)
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -726,7 +727,7 @@ func (e *EthEndpoints) GetBlockTransactionCountByNumber(number *types.BlockNumbe
}

var err error
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, dbTx)
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, e.state, e.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down
16 changes: 9 additions & 7 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import (

// ZKEVMEndpoints contains implementations for the "zkevm" RPC endpoints
type ZKEVMEndpoints struct {
cfg Config
state types.StateInterface
txMan DBTxManager
cfg Config
state types.StateInterface
etherman types.EthermanInterface
txMan DBTxManager
}

// NewZKEVMEndpoints returns ZKEVMEndpoints
func NewZKEVMEndpoints(cfg Config, state types.StateInterface) *ZKEVMEndpoints {
func NewZKEVMEndpoints(cfg Config, state types.StateInterface, etherman types.EthermanInterface) *ZKEVMEndpoints {
return &ZKEVMEndpoints{
cfg: cfg,
state: state,
cfg: cfg,
state: state,
etherman: etherman,
}
}

Expand Down Expand Up @@ -205,7 +207,7 @@ func (z *ZKEVMEndpoints) GetFullBlockByNumber(number types.BlockNumber, fullTx b
return rpcBlock, nil
}
var err error
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, z.state, dbTx)
blockNumber, rpcErr := number.GetNumericBlockNumber(ctx, z.state, z.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down
77 changes: 77 additions & 0 deletions jsonrpc/mocks/mock_etherman.go

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

48 changes: 48 additions & 0 deletions jsonrpc/mocks/mock_state.go

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

Loading

0 comments on commit c1dd19c

Please sign in to comment.