From 086dc85104fead9969bc5c8bd385f11a30e54c3b Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 23 Dec 2020 16:26:56 +0100 Subject: [PATCH] Get ChainID from ethereum node --- config/config.go | 4 +--- eth/auction.go | 12 ++++++++---- eth/ethereum.go | 10 ++++++++++ eth/rollup.go | 25 ++++++++++++------------- node/node.go | 26 ++++++++++++++++++++------ test/ethclient.go | 9 +++++++++ 6 files changed, 60 insertions(+), 26 deletions(-) diff --git a/config/config.go b/config/config.go index eece824..b3dc03c 100644 --- a/config/config.go +++ b/config/config.go @@ -48,9 +48,7 @@ type Coordinator struct { // SyncRetryInterval is the waiting interval between calls to the main // handler of a synced block after an error SyncRetryInterval Duration `validate:"required"` - // ChainID is the ChainID from the Ethereum chain - ChainID uint16 `validate:"required"` - L2DB struct { + L2DB struct { SafetyPeriod common.BatchNum `validate:"required"` MaxTxs uint32 `validate:"required"` TTL Duration `validate:"required"` diff --git a/eth/auction.go b/eth/auction.go index d502904..73592a9 100644 --- a/eth/auction.go +++ b/eth/auction.go @@ -265,6 +265,7 @@ type AuctionInterface interface { // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum. type AuctionClient struct { client *EthereumClient + chainID *big.Int address ethCommon.Address tokenHEZCfg TokenConfig auction *HermezAuctionProtocol.HermezAuctionProtocol @@ -287,8 +288,13 @@ func NewAuctionClient(client *EthereumClient, address ethCommon.Address, tokenHE if err != nil { return nil, tracerr.Wrap(err) } + chainID, err := client.EthChainID() + if err != nil { + return nil, tracerr.Wrap(err) + } return &AuctionClient{ client: client, + chainID: chainID, address: address, tokenHEZCfg: tokenHEZCfg, auction: auction, @@ -580,8 +586,7 @@ func (c *AuctionClient) AuctionBid(amount *big.Int, slot int64, bidAmount *big.I } tokenName := c.tokenHEZCfg.Name tokenAddr := c.tokenHEZCfg.Address - chainid, _ := c.client.Client().ChainID(context.Background()) - digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenName) + digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, amount, nonce, deadline, tokenName) signature, _ := c.client.ks.SignHash(*c.client.account, digest) permit := createPermit(owner, spender, amount, deadline, digest, signature) _slot := big.NewInt(slot) @@ -607,9 +612,8 @@ func (c *AuctionClient) AuctionMultiBid(amount *big.Int, startingSlot, endingSlo } tokenName := c.tokenHEZCfg.Name tokenAddr := c.tokenHEZCfg.Address - chainid, _ := c.client.Client().ChainID(context.Background()) - digest, _ := createPermitDigest(tokenAddr, owner, spender, chainid, amount, nonce, deadline, tokenName) + digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, amount, nonce, deadline, tokenName) signature, _ := c.client.ks.SignHash(*c.client.account, digest) permit := createPermit(owner, spender, amount, deadline, digest, signature) _startingSlot := big.NewInt(startingSlot) diff --git a/eth/ethereum.go b/eth/ethereum.go index ed3e1a9..4327307 100644 --- a/eth/ethereum.go +++ b/eth/ethereum.go @@ -34,6 +34,7 @@ type EthereumInterface interface { EthTransactionReceipt(context.Context, ethCommon.Hash) (*types.Receipt, error) EthERC20Consts(ethCommon.Address) (*ERC20Consts, error) + EthChainID() (*big.Int, error) } var ( @@ -101,6 +102,15 @@ func NewEthereumClient(client *ethclient.Client, account *accounts.Account, ks * } } +// EthChainID returns the ChainID of the ethereum network +func (c *EthereumClient) EthChainID() (*big.Int, error) { + chainID, err := c.client.ChainID(context.Background()) + if err != nil { + return nil, tracerr.Wrap(err) + } + return chainID, nil +} + // BalanceAt retieves information about the default account func (c *EthereumClient) BalanceAt(addr ethCommon.Address) (*big.Int, error) { return c.client.BalanceAt(context.TODO(), addr, nil) diff --git a/eth/rollup.go b/eth/rollup.go index f5550ce..1fedc68 100644 --- a/eth/rollup.go +++ b/eth/rollup.go @@ -283,6 +283,7 @@ type RollupClient struct { tokenHEZ *HEZ.HEZ contractAbi abi.ABI opts *bind.CallOpts + consts *common.RollupConstants } // NewRollupClient creates a new RollupClient @@ -299,11 +300,11 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ if err != nil { return nil, tracerr.Wrap(err) } - chainID, err := client.client.ChainID(context.Background()) + chainID, err := client.EthChainID() if err != nil { return nil, tracerr.Wrap(err) } - return &RollupClient{ + c := &RollupClient{ client: client, chainID: chainID, address: address, @@ -312,7 +313,13 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZ tokenHEZ: tokenHEZ, contractAbi: contractAbi, opts: newCallOpts(), - }, nil + } + consts, err := c.RollupConstants() + if err != nil { + return nil, tracerr.Wrap(err) + } + c.consts = consts + return c, nil } // RollupForgeBatch is the interface to call the smart contract function @@ -320,11 +327,7 @@ func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (tx *types.T if tx, err = c.client.CallAuth( 1000000, //nolint:gomnd func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { - rollupConst, err := c.RollupConstants() - if err != nil { - return nil, tracerr.Wrap(err) - } - nLevels := rollupConst.Verifiers[args.VerifierIdx].NLevels + nLevels := c.consts.Verifiers[args.VerifierIdx].NLevels lenBytes := nLevels / 8 //nolint:gomnd newLastIdx := big.NewInt(int64(args.NewLastIdx)) // L1CoordinatorBytes @@ -915,11 +918,7 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash, l1UserTxsL L2TxsData: []common.L2Tx{}, FeeIdxCoordinator: []common.Idx{}, } - rollupConsts, err := c.RollupConstants() - if err != nil { - return nil, nil, tracerr.Wrap(err) - } - nLevels := rollupConsts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels + nLevels := c.consts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels lenL1L2TxsBytes := int((nLevels/8)*2 + 2 + 1) numBytesL1TxUser := int(l1UserTxsLen) * lenL1L2TxsBytes numTxsL1Coord := len(aux.EncodedL1CoordinatorTx) / common.L1CoordinatorTxBytesLen diff --git a/node/node.go b/node/node.go index 1ffc41a..9e38083 100644 --- a/node/node.go +++ b/node/node.go @@ -83,11 +83,6 @@ func NewNode(mode Mode, cfg *config.Node) (*Node, error) { historyDB := historydb.NewHistoryDB(db) - stateDB, err := statedb.NewStateDB(cfg.StateDB.Path, statedb.TypeSynchronizer, 32, cfg.Coordinator.ChainID) - if err != nil { - return nil, tracerr.Wrap(err) - } - ethClient, err := ethclient.Dial(cfg.Web3.URL) if err != nil { return nil, tracerr.Wrap(err) @@ -122,6 +117,25 @@ func NewNode(mode Mode, cfg *config.Node) (*Node, error) { return nil, tracerr.Wrap(err) } + chainID, err := client.EthChainID() + if err != nil { + return nil, tracerr.Wrap(err) + } + if !chainID.IsUint64() { + return nil, tracerr.Wrap(fmt.Errorf("chainID cannot be represented as uint64")) + } + chainIDU64 := chainID.Uint64() + const maxUint16 uint64 = 0xffff + if chainIDU64 > maxUint16 { + return nil, tracerr.Wrap(fmt.Errorf("chainID overflows uint16")) + } + chainIDU16 := uint16(chainIDU64) + + stateDB, err := statedb.NewStateDB(cfg.StateDB.Path, statedb.TypeSynchronizer, 32, chainIDU16) + if err != nil { + return nil, tracerr.Wrap(err) + } + sync, err := synchronizer.NewSynchronizer(client, historyDB, stateDB, synchronizer.Config{ StatsRefreshPeriod: cfg.Synchronizer.StatsRefreshPeriod.Duration, }) @@ -232,7 +246,7 @@ func NewNode(mode Mode, cfg *config.Node) (*Node, error) { AuctionConstants: scConsts.Auction, WDelayerConstants: scConsts.WDelayer, }, - cfg.Coordinator.ChainID, + chainIDU16, ) if err != nil { return nil, tracerr.Wrap(err) diff --git a/test/ethclient.go b/test/ethclient.go index f96749e..655f8cd 100644 --- a/test/ethclient.go +++ b/test/ethclient.go @@ -272,6 +272,7 @@ type ClientSetup struct { WDelayerConstants *common.WDelayerConstants WDelayerVariables *common.WDelayerVariables VerifyProof bool + ChainID *big.Int } // NewClientSetupExample returns a ClientSetup example with hardcoded realistic @@ -357,6 +358,8 @@ func NewClientSetupExample() *ClientSetup { AuctionVariables: auctionVariables, WDelayerConstants: wDelayerConstants, WDelayerVariables: wDelayerVariables, + VerifyProof: false, + ChainID: big.NewInt(0), } } @@ -383,6 +386,7 @@ type Client struct { rw *sync.RWMutex log bool addr *ethCommon.Address + chainID *big.Int rollupConstants *common.RollupConstants auctionConstants *common.AuctionConstants wDelayerConstants *common.WDelayerConstants @@ -602,6 +606,11 @@ func (c *Client) CtlLastForgedBatch() int64 { return int64(len(e.State.ExitRoots)) - 1 } +// EthChainID returns the ChainID of the ethereum network +func (c *Client) EthChainID() (*big.Int, error) { + return c.chainID, nil +} + // EthLastBlock returns the last blockNum func (c *Client) EthLastBlock() (int64, error) { c.rw.RLock()