arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Get ChainID from ethereum node
Eduard S eduard@iden3.io
Wed, 23 Dec 2020 16:26:56 +0100
commit

086dc85104fead9969bc5c8bd385f11a30e54c3b

parent

150597c282b43d4f35bb3e07da98b4a6cfed7de1

6 files changed, 60 insertions(+), 26 deletions(-)

jump to
M config/config.go → config/config.go

@@ -48,9 +48,7 @@ ProofServerPollInterval Duration `validate:"required"`

// 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"`
M eth/auction.go → eth/auction.go

@@ -265,6 +265,7 @@

// 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 @@ tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client())

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 @@ return nil, tracerr.Wrap(err)

} 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 @@ return nil, tracerr.Wrap(err)

} 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)
M eth/ethereum.go → eth/ethereum.go

@@ -34,6 +34,7 @@ EthAddress() (*ethCommon.Address, error)

EthTransactionReceipt(context.Context, ethCommon.Hash) (*types.Receipt, error) EthERC20Consts(ethCommon.Address) (*ERC20Consts, error) + EthChainID() (*big.Int, error) } var (

@@ -99,6 +100,15 @@ ReceiptTimeout: config.ReceiptTimeout * time.Second,

config: config, opts: newCallOpts(), } +} + +// 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
M eth/rollup.go → eth/rollup.go

@@ -283,6 +283,7 @@ hermez *Hermez.Hermez

tokenHEZ *HEZ.HEZ contractAbi abi.ABI opts *bind.CallOpts + consts *common.RollupConstants } // NewRollupClient creates a new RollupClient

@@ -299,11 +300,11 @@ tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client())

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 @@ hermez: hermez,

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.Transaction, err error) {

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 @@ L1CoordinatorTxsAuths: [][]byte{},

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
M node/node.go → node/node.go

@@ -83,11 +83,6 @@ }

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)

@@ -118,6 +113,25 @@ WDelayer: eth.WDelayerConfig{

Address: cfg.SmartContracts.WDelayer, }, }) + if err != nil { + 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) }

@@ -232,7 +246,7 @@ RollupConstants: scConsts.Rollup,

AuctionConstants: scConsts.Auction, WDelayerConstants: scConsts.WDelayer, }, - cfg.Coordinator.ChainID, + chainIDU16, ) if err != nil { return nil, tracerr.Wrap(err)
M test/ethclient.go → test/ethclient.go

@@ -272,6 +272,7 @@ AuctionVariables *common.AuctionVariables

WDelayerConstants *common.WDelayerConstants WDelayerVariables *common.WDelayerVariables VerifyProof bool + ChainID *big.Int } // NewClientSetupExample returns a ClientSetup example with hardcoded realistic

@@ -357,6 +358,8 @@ AuctionConstants: auctionConstants,

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

@@ -600,6 +604,11 @@

currentBlock := c.currentBlock() e := currentBlock.Rollup 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