Get ChainID from ethereum node

This commit is contained in:
Eduard S
2020-12-23 16:26:56 +01:00
parent 150597c282
commit 086dc85104
6 changed files with 60 additions and 26 deletions

View File

@@ -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"`

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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()