Skip to content

Commit

Permalink
fix safe and finalized l2 block to consider l1 safe and finalized blo…
Browse files Browse the repository at this point in the history
…cks respectively (0xPolygonHermez#2245)
  • Loading branch information
tclemos authored Aug 17, 2023
1 parent c4d4904 commit a1476c9
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 45 deletions.
4 changes: 2 additions & 2 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 Down Expand Up @@ -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 @@ -294,7 +295,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 @@ -434,13 +435,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 @@ -511,7 +512,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 @@ -681,7 +682,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
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.

23 changes: 13 additions & 10 deletions jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ type mockedServer struct {
}

type mocksWrapper struct {
Pool *mocks.PoolMock
State *mocks.StateMock
Storage *storageMock
DbTx *mocks.DBTxMock
Pool *mocks.PoolMock
State *mocks.StateMock
Etherman *mocks.EthermanMock
Storage *storageMock
DbTx *mocks.DBTxMock
}

func newMockedServer(t *testing.T, cfg Config) (*mockedServer, *mocksWrapper, *ethclient.Client) {
pool := mocks.NewPoolMock(t)
st := mocks.NewStateMock(t)
etherman := mocks.NewEthermanMock(t)
storage := newStorageMock(t)
dbTx := mocks.NewDBTxMock(t)
apis := map[string]bool{
Expand All @@ -55,7 +57,7 @@ func newMockedServer(t *testing.T, cfg Config) (*mockedServer, *mocksWrapper, *e
if _, ok := apis[APIEth]; ok {
services = append(services, Service{
Name: APIEth,
Service: NewEthEndpoints(cfg, chainID, pool, st, storage),
Service: NewEthEndpoints(cfg, chainID, pool, st, etherman, storage),
})
}

Expand Down Expand Up @@ -83,7 +85,7 @@ func newMockedServer(t *testing.T, cfg Config) (*mockedServer, *mocksWrapper, *e
if _, ok := apis[APIDebug]; ok {
services = append(services, Service{
Name: APIDebug,
Service: NewDebugEndpoints(cfg, st),
Service: NewDebugEndpoints(cfg, st, etherman),
})
}

Expand Down Expand Up @@ -123,10 +125,11 @@ func newMockedServer(t *testing.T, cfg Config) (*mockedServer, *mocksWrapper, *e
}

mks := &mocksWrapper{
Pool: pool,
State: st,
Storage: storage,
DbTx: dbTx,
Pool: pool,
State: st,
Etherman: etherman,
Storage: storage,
DbTx: dbTx,
}

return msv, mks, ethClient
Expand Down
Loading

0 comments on commit a1476c9

Please sign in to comment.