Skip to content

Commit

Permalink
fix: support ethermint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jia authored and Stonepapa committed Feb 22, 2024
1 parent 6c864ad commit 04a6471
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 20 deletions.
46 changes: 41 additions & 5 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ type Client struct {
Pol *pol.Pol
SCAddresses []common.Address

RollupID uint32
RollupID uint32
RpcClient *rpc.Client

GasProviders externalGasProviders

Expand Down Expand Up @@ -261,6 +262,7 @@ func NewClient(cfg Config, l1Config L1Config) (*Client, error) {
OldGlobalExitRootManager: oldGlobalExitRoot,
SCAddresses: scAddresses,
RollupID: rollupID,
RpcClient: ethClient.Client(),
GasProviders: externalGasProviders{
MultiGasProvider: cfg.MultiGasProvider,
Providers: gProviders,
Expand Down Expand Up @@ -1543,20 +1545,40 @@ func hash(data ...[32]byte) [32]byte {

// HeaderByNumber returns a block header from the current canonical chain. If number is
// nil, the latest known header is returned.
func (etherMan *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return etherMan.EthClient.HeaderByNumber(ctx, number)
func (etherMan *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*EtherHeader, error) {
header, err := etherMan.EthClient.HeaderByNumber(ctx, number)
if err != nil {
return nil, err
}
ethermintBlock, err := etherMan.GetEthermintBlockByNum(ctx, header.Number.Uint64())
if err != nil {
return nil, err
}
etherHeader := &EtherHeader{
Header: header,
BlockHash: ethermintBlock.BlockHash,
}
return etherHeader, nil
}

// EthBlockByNumber function retrieves the ethereum block information by ethereum block number.
func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64) (*types.Block, error) {
func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64) (*EtherBlock, error) {
block, err := etherMan.EthClient.BlockByNumber(ctx, new(big.Int).SetUint64(blockNumber))
if err != nil {
if errors.Is(err, ethereum.NotFound) || err.Error() == "block does not exist in blockchain" {
return nil, ErrNotFound
}
return nil, err
}
return block, nil
ethermintBlock, err := etherMan.GetEthermintBlockByNum(ctx, blockNumber)
if err != nil {
return nil, err
}
etherBlock := &EtherBlock{
Block: block,
BlockHash: ethermintBlock.BlockHash,
}
return etherBlock, nil
}

// GetLatestBatchNumber function allows to retrieve the latest proposed batch in the smc
Expand Down Expand Up @@ -1864,3 +1886,17 @@ func (etherMan *Client) generateRandomAuth() (bind.TransactOpts, error) {

return *auth, nil
}

func (etherMan *Client) GetEthermintBlockByNum(ctx context.Context, blockNumber uint64) (*EthermintBlock, error) {
var raw json.RawMessage
err := etherMan.RpcClient.CallContext(ctx, &raw, "eth_getBlockByNumber", toBlockNumArg(new(big.Int).SetUint64(blockNumber)), true)
if err != nil {
return nil, err
}
block := &EthermintBlock{}
if err := json.Unmarshal(raw, &block); err != nil {
fmt.Println(err)
return nil, err
}
return block, nil
}
59 changes: 59 additions & 0 deletions etherman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ package etherman

import (
"time"
"math/big"
"encoding/json"
"fmt"
"strings"

"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/oldpolygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)

// Block struct
Expand Down Expand Up @@ -93,3 +100,55 @@ type ForkID struct {
ForkID uint64
Version string
}

type EtherHeader struct {
*types.Header
BlockHash common.Hash
}

func (header *EtherHeader) Hash() common.Hash { return header.BlockHash }

type EtherBlock struct {
*types.Block
BlockHash common.Hash
}

func (block *EtherBlock) Hash() common.Hash { return block.BlockHash }

type EthermintBlock struct {
BlockNumber *BigInt `json:"number"`
BlockHash common.Hash `json:"hash"`
BlockParentHash common.Hash `json:"parentHash"`
}

type BigInt struct {
big.Int
}

func (i *BigInt) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
if err != nil {
return err
}
_, success := i.SetString(strings.TrimPrefix(val, "0x"), 16)
if !success {
return fmt.Errorf("cannot convert string to big integer")
}
return nil
}

func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
if number.Sign() >= 0 {
return hexutil.EncodeBig(number)
}
// It's negative.
if number.IsInt64() {
return rpc.BlockNumber(number.Int64()).String()
}
// It's negative and large, which is invalid.
return fmt.Sprintf("<invalid %d>", number)
}
5 changes: 2 additions & 3 deletions synchronizer/common/syncinterfaces/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (

"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
)

// EthermanFullInterface contains the methods required to interact with ethereum.
type EthermanFullInterface interface {
HeaderByNumber(ctx context.Context, number *big.Int) (*ethTypes.Header, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*etherman.EtherHeader, error)
GetRollupInfoByBlockRange(ctx context.Context, fromBlock uint64, toBlock *uint64) ([]etherman.Block, map[common.Hash][]etherman.Order, error)
EthBlockByNumber(ctx context.Context, blockNumber uint64) (*ethTypes.Block, error)
EthBlockByNumber(ctx context.Context, blockNumber uint64) (*etherman.EtherBlock, error)
GetLatestBatchNumber() (uint64, error)
GetTrustedSequencerURL() (string, error)
VerifyGenBlockNumber(ctx context.Context, genBlockNumber uint64) (bool, error)
Expand Down
3 changes: 0 additions & 3 deletions synchronizer/interfaces.go

This file was deleted.

7 changes: 4 additions & 3 deletions synchronizer/l1_parallel_sync/l1_etherman_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
)

// L1ParallelEthermanInterface is an interface for the etherman package
type L1ParallelEthermanInterface interface {
HeaderByNumber(ctx context.Context, number *big.Int) (*ethTypes.Header, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*etherman.EtherHeader, error)
// HeaderByNumber(ctx context.Context, number *big.Int) (*ethTypes.Header, error)
GetRollupInfoByBlockRange(ctx context.Context, fromBlock uint64, toBlock *uint64) ([]etherman.Block, map[common.Hash][]etherman.Order, error)
EthBlockByNumber(ctx context.Context, blockNumber uint64) (*ethTypes.Block, error)
// EthBlockByNumber(ctx context.Context, blockNumber uint64) (*ethTypes.Block, error)
EthBlockByNumber(ctx context.Context, blockNumber uint64) (*etherman.EtherBlock, error)
GetLatestBatchNumber() (uint64, error)
GetTrustedSequencerURL() (string, error)
VerifyGenBlockNumber(ctx context.Context, genBlockNumber uint64) (bool, error)
Expand Down
5 changes: 2 additions & 3 deletions synchronizer/l1_parallel_sync/l1_rollup_info_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
types "github.com/ethereum/go-ethereum/core/types"
)

const (
Expand Down Expand Up @@ -264,7 +263,7 @@ func logBlocks(blocks []etherman.Block) {
}
}

func convertL1BlockToEthBlock(fb *types.Block) etherman.Block {
func convertL1BlockToEthBlock(fb *etherman.EtherBlock) etherman.Block {
return etherman.Block{
BlockNumber: fb.NumberU64(),
BlockHash: fb.Hash(),
Expand All @@ -273,7 +272,7 @@ func convertL1BlockToEthBlock(fb *types.Block) etherman.Block {
}
}

func convertL1BlockToStateBlock(fb *types.Block) state.Block {
func convertL1BlockToStateBlock(fb *etherman.EtherBlock) state.Block {
return state.Block{
BlockNumber: fb.NumberU64(),
BlockHash: fb.Hash(),
Expand Down
5 changes: 2 additions & 3 deletions synchronizer/l1_parallel_sync/l1_worker_etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
types "github.com/ethereum/go-ethereum/core/types"
)

type ethermanStatusEnum int8
Expand Down Expand Up @@ -108,8 +107,8 @@ type rollupInfoByBlockRangeResult struct {
order map[common.Hash][]etherman.Order
// If there are no blocks in this range, it gets the last one
// so it could be nil if there are no blocks.
lastBlockOfRange *types.Block
previousBlockOfRange *types.Block
lastBlockOfRange *etherman.EtherBlock
previousBlockOfRange *etherman.EtherBlock
}

func (r *rollupInfoByBlockRangeResult) toStringBrief() string {
Expand Down

0 comments on commit 04a6471

Please sign in to comment.