Redo coordinator structure, connect API to node
- API:
- Modify the constructor so that hardcoded rollup constants don't need
to be passed (introduce a `Config` and use `configAPI` internally)
- Common:
- Update rollup constants with proper *big.Int when required
- Add BidCoordinator and Slot structs used by the HistoryDB and
Synchronizer.
- Add helper methods to AuctionConstants
- AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL
table: `default_slot_set_bid_slot_num`), which indicates at which
slotNum does the `DefaultSlotSetBid` specified starts applying.
- Config:
- Move coordinator exclusive configuration from the node config to the
coordinator config
- Coordinator:
- Reorganize the code towards having the goroutines started and stopped
from the coordinator itself instead of the node.
- Remove all stop and stopped channels, and use context.Context and
sync.WaitGroup instead.
- Remove BatchInfo setters and assing variables directly
- In ServerProof and ServerProofPool use context instead stop channel.
- Use message passing to notify the coordinator about sync updates and
reorgs
- Introduce the Pipeline, which can be started and stopped by the
Coordinator
- Introduce the TxManager, which manages ethereum transactions (the
TxManager is also in charge of making the forge call to the rollup
smart contract). The TxManager keeps ethereum transactions and:
1. Waits for the transaction to be accepted
2. Waits for the transaction to be confirmed for N blocks
- In forge logic, first prepare a batch and then wait for an available
server proof to have all work ready once the proof server is ready.
- Remove the `isForgeSequence` method which was querying the smart
contract, and instead use notifications sent by the Synchronizer to
figure out if it's forging time.
- Update test (which is a minimal test to manually see if the
coordinator starts)
- HistoryDB:
- Add method to get the number of batches in a slot (used to detect when
a slot has passed the bid winner forging deadline)
- Add method to get the best bid and associated coordinator of a slot
(used to detect the forgerAddress that can forge the slot)
- General:
- Rename some instances of `currentBlock` to `lastBlock` to be more
clear.
- Node:
- Connect the API to the node and call the methods to update cached
state when the sync advances blocks.
- Call methods to update Coordinator state when the sync advances blocks
and finds reorgs.
- Synchronizer:
- Add Auction field in the Stats, which contain the current slot with
info about highest bidder and other related info required to know who
can forge in the current block.
- Better organization of cached state:
- On Sync, update the internal cached state
- On Init or Reorg, load the state from HistoryDB into the
internal cached state.
4 years ago Redo coordinator structure, connect API to node
- API:
- Modify the constructor so that hardcoded rollup constants don't need
to be passed (introduce a `Config` and use `configAPI` internally)
- Common:
- Update rollup constants with proper *big.Int when required
- Add BidCoordinator and Slot structs used by the HistoryDB and
Synchronizer.
- Add helper methods to AuctionConstants
- AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL
table: `default_slot_set_bid_slot_num`), which indicates at which
slotNum does the `DefaultSlotSetBid` specified starts applying.
- Config:
- Move coordinator exclusive configuration from the node config to the
coordinator config
- Coordinator:
- Reorganize the code towards having the goroutines started and stopped
from the coordinator itself instead of the node.
- Remove all stop and stopped channels, and use context.Context and
sync.WaitGroup instead.
- Remove BatchInfo setters and assing variables directly
- In ServerProof and ServerProofPool use context instead stop channel.
- Use message passing to notify the coordinator about sync updates and
reorgs
- Introduce the Pipeline, which can be started and stopped by the
Coordinator
- Introduce the TxManager, which manages ethereum transactions (the
TxManager is also in charge of making the forge call to the rollup
smart contract). The TxManager keeps ethereum transactions and:
1. Waits for the transaction to be accepted
2. Waits for the transaction to be confirmed for N blocks
- In forge logic, first prepare a batch and then wait for an available
server proof to have all work ready once the proof server is ready.
- Remove the `isForgeSequence` method which was querying the smart
contract, and instead use notifications sent by the Synchronizer to
figure out if it's forging time.
- Update test (which is a minimal test to manually see if the
coordinator starts)
- HistoryDB:
- Add method to get the number of batches in a slot (used to detect when
a slot has passed the bid winner forging deadline)
- Add method to get the best bid and associated coordinator of a slot
(used to detect the forgerAddress that can forge the slot)
- General:
- Rename some instances of `currentBlock` to `lastBlock` to be more
clear.
- Node:
- Connect the API to the node and call the methods to update cached
state when the sync advances blocks.
- Call methods to update Coordinator state when the sync advances blocks
and finds reorgs.
- Synchronizer:
- Add Auction field in the Stats, which contain the current slot with
info about highest bidder and other related info required to know who
can forge in the current block.
- Better organization of cached state:
- On Sync, update the internal cached state
- On Init or Reorg, load the state from HistoryDB into the
internal cached state.
4 years ago Update coordinator, call all api update functions
- Common:
- Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition
- API:
- Add UpdateNetworkInfoBlock to update just block information, to be
used when the node is not yet synchronized
- Node:
- Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with
configurable time intervals
- Synchronizer:
- When mapping events by TxHash, use an array to support the possibility
of multiple calls of the same function happening in the same
transaction (for example, a smart contract in a single transaction
could call withdraw with delay twice, which would generate 2 withdraw
events, and 2 deposit events).
- In Stats, keep entire LastBlock instead of just the blockNum
- In Stats, add lastL1BatchBlock
- Test Stats and SCVars
- Coordinator:
- Enable writing the BatchInfo in every step of the pipeline to disk
(with JSON text files) for debugging purposes.
- Move the Pipeline functionality from the Coordinator to its own struct
(Pipeline)
- Implement shouldL1lL2Batch
- In TxManager, implement logic to perform several attempts when doing
ethereum node RPC calls before considering the error. (Both for calls
to forgeBatch and transaction receipt)
- In TxManager, reorganize the flow and note the specific points in
which actions are made when err != nil
- HistoryDB:
- Implement GetLastL1BatchBlockNum: returns the blockNum of the latest
forged l1Batch, to help the coordinator decide when to forge an
L1Batch.
- EthereumClient and test.Client:
- Update EthBlockByNumber to return the last block when the passed
number is -1.
4 years ago Update coordinator, call all api update functions
- Common:
- Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition
- API:
- Add UpdateNetworkInfoBlock to update just block information, to be
used when the node is not yet synchronized
- Node:
- Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with
configurable time intervals
- Synchronizer:
- When mapping events by TxHash, use an array to support the possibility
of multiple calls of the same function happening in the same
transaction (for example, a smart contract in a single transaction
could call withdraw with delay twice, which would generate 2 withdraw
events, and 2 deposit events).
- In Stats, keep entire LastBlock instead of just the blockNum
- In Stats, add lastL1BatchBlock
- Test Stats and SCVars
- Coordinator:
- Enable writing the BatchInfo in every step of the pipeline to disk
(with JSON text files) for debugging purposes.
- Move the Pipeline functionality from the Coordinator to its own struct
(Pipeline)
- Implement shouldL1lL2Batch
- In TxManager, implement logic to perform several attempts when doing
ethereum node RPC calls before considering the error. (Both for calls
to forgeBatch and transaction receipt)
- In TxManager, reorganize the flow and note the specific points in
which actions are made when err != nil
- HistoryDB:
- Implement GetLastL1BatchBlockNum: returns the blockNum of the latest
forged l1Batch, to help the coordinator decide when to forge an
L1Batch.
- EthereumClient and test.Client:
- Update EthBlockByNumber to return the last block when the passed
number is -1.
4 years ago Update coordinator, call all api update functions
- Common:
- Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition
- API:
- Add UpdateNetworkInfoBlock to update just block information, to be
used when the node is not yet synchronized
- Node:
- Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with
configurable time intervals
- Synchronizer:
- When mapping events by TxHash, use an array to support the possibility
of multiple calls of the same function happening in the same
transaction (for example, a smart contract in a single transaction
could call withdraw with delay twice, which would generate 2 withdraw
events, and 2 deposit events).
- In Stats, keep entire LastBlock instead of just the blockNum
- In Stats, add lastL1BatchBlock
- Test Stats and SCVars
- Coordinator:
- Enable writing the BatchInfo in every step of the pipeline to disk
(with JSON text files) for debugging purposes.
- Move the Pipeline functionality from the Coordinator to its own struct
(Pipeline)
- Implement shouldL1lL2Batch
- In TxManager, implement logic to perform several attempts when doing
ethereum node RPC calls before considering the error. (Both for calls
to forgeBatch and transaction receipt)
- In TxManager, reorganize the flow and note the specific points in
which actions are made when err != nil
- HistoryDB:
- Implement GetLastL1BatchBlockNum: returns the blockNum of the latest
forged l1Batch, to help the coordinator decide when to forge an
L1Batch.
- EthereumClient and test.Client:
- Update EthBlockByNumber to return the last block when the passed
number is -1.
4 years ago |
|
package eth
import ( "context" "fmt" "math/big" "time"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/abi/bind" ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore" ethCommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/hermeznetwork/hermez-node/common" HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ" "github.com/hermeznetwork/hermez-node/log" )
// ERC20Consts are the constants defined in a particular ERC20 Token instance
type ERC20Consts struct { Name string Symbol string Decimals uint64 }
// EthereumInterface is the interface to Ethereum
type EthereumInterface interface { EthLastBlock() (int64, error) // EthHeaderByNumber(context.Context, *big.Int) (*types.Header, error)
EthBlockByNumber(context.Context, int64) (*common.Block, error) EthAddress() (*ethCommon.Address, error) EthTransactionReceipt(context.Context, ethCommon.Hash) (*types.Receipt, error)
EthERC20Consts(ethCommon.Address) (*ERC20Consts, error) }
var ( // ErrAccountNil is used when the calls can not be made because the account is nil
ErrAccountNil = fmt.Errorf("Authorized calls can't be made when the account is nil") // ErrReceiptStatusFailed is used when receiving a failed transaction
ErrReceiptStatusFailed = fmt.Errorf("receipt status is failed") // ErrReceiptNotReceived is used when unable to retrieve a transaction
ErrReceiptNotReceived = fmt.Errorf("receipt not available") // ErrBlockHashMismatchEvent is used when there's a block hash mismatch
// beetween different events of the same block
ErrBlockHashMismatchEvent = fmt.Errorf("block hash mismatch in event log") )
const ( errStrDeploy = "deployment of %s failed: %w" errStrWaitReceipt = "wait receipt of %s deploy failed: %w"
// default values
defaultCallGasLimit = 300000 defaultDeployGasLimit = 1000000 defaultGasPriceDiv = 100 defaultReceiptTimeout = 60 defaultIntervalReceiptLoop = 200 )
// EthereumConfig defines the configuration parameters of the EthereumClient
type EthereumConfig struct { CallGasLimit uint64 DeployGasLimit uint64 GasPriceDiv uint64 ReceiptTimeout time.Duration // in seconds
IntervalReceiptLoop time.Duration // in milliseconds
}
// EthereumClient is an ethereum client to call Smart Contract methods and check blockchain information.
type EthereumClient struct { client *ethclient.Client account *accounts.Account ks *ethKeystore.KeyStore ReceiptTimeout time.Duration config *EthereumConfig }
// NewEthereumClient creates a EthereumClient instance. The account is not mandatory (it can
// be nil). If the account is nil, CallAuth will fail with ErrAccountNil.
func NewEthereumClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, config *EthereumConfig) *EthereumClient { if config == nil { config = &EthereumConfig{ CallGasLimit: defaultCallGasLimit, DeployGasLimit: defaultDeployGasLimit, GasPriceDiv: defaultGasPriceDiv, ReceiptTimeout: defaultReceiptTimeout, IntervalReceiptLoop: defaultIntervalReceiptLoop, } } return &EthereumClient{client: client, account: account, ks: ks, ReceiptTimeout: config.ReceiptTimeout * time.Second, config: config} }
// 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) }
// Account returns the underlying ethereum account
func (c *EthereumClient) Account() *accounts.Account { return c.account }
// EthAddress returns the ethereum address of the account loaded into the EthereumClient
func (c *EthereumClient) EthAddress() (*ethCommon.Address, error) { if c.account == nil { return nil, ErrAccountNil } return &c.account.Address, nil }
// CallAuth performs a Smart Contract method call that requires authorization.
// This call requires a valid account with Ether that can be spend during the
// call.
func (c *EthereumClient) CallAuth(gasLimit uint64, fn func(*ethclient.Client, *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) { if c.account == nil { return nil, ErrAccountNil }
gasPrice, err := c.client.SuggestGasPrice(context.Background()) if err != nil { return nil, err } inc := new(big.Int).Set(gasPrice) inc.Div(inc, new(big.Int).SetUint64(c.config.GasPriceDiv)) gasPrice.Add(gasPrice, inc) log.Debugw("Transaction metadata", "gasPrice", gasPrice)
auth, err := bind.NewKeyStoreTransactor(c.ks, *c.account) if err != nil { return nil, err } auth.Value = big.NewInt(0) // in wei
if gasLimit == 0 { auth.GasLimit = c.config.CallGasLimit // in units
} else { auth.GasLimit = gasLimit // in units
} auth.GasPrice = gasPrice
tx, err := fn(c.client, auth) if tx != nil { log.Debugw("Transaction", "tx", tx.Hash().Hex(), "nonce", tx.Nonce()) } return tx, err }
// ContractData contains the contract data
type ContractData struct { Address ethCommon.Address Tx *types.Transaction Receipt *types.Receipt }
// Deploy a smart contract. `name` is used to log deployment information. fn
// is a wrapper to the deploy function generated by abigen. In case of error,
// the returned `ContractData` may have some parameters filled depending on the
// kind of error that occurred.
func (c *EthereumClient) Deploy(name string, fn func(c *ethclient.Client, auth *bind.TransactOpts) (ethCommon.Address, *types.Transaction, interface{}, error)) (ContractData, error) { var contractData ContractData log.Infow("Deploying", "contract", name) tx, err := c.CallAuth( c.config.DeployGasLimit, func(client *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) { addr, tx, _, err := fn(client, auth) if err != nil { return nil, err } contractData.Address = addr return tx, nil }, ) if err != nil { return contractData, fmt.Errorf(errStrDeploy, name, err) } log.Infow("Waiting receipt", "tx", tx.Hash().Hex(), "contract", name) contractData.Tx = tx receipt, err := c.WaitReceipt(tx) if err != nil { return contractData, fmt.Errorf(errStrWaitReceipt, name, err) } contractData.Receipt = receipt return contractData, nil }
// Call performs a read only Smart Contract method call.
func (c *EthereumClient) Call(fn func(*ethclient.Client) error) error { return fn(c.client) }
// WaitReceipt will block until a transaction is confirmed. Internally it
// polls the state every 200 milliseconds.
func (c *EthereumClient) WaitReceipt(tx *types.Transaction) (*types.Receipt, error) { return c.waitReceipt(context.TODO(), tx, c.ReceiptTimeout) }
// GetReceipt will check if a transaction is confirmed and return
// immediately, waiting at most 1 second and returning error if the transaction
// is still pending.
func (c *EthereumClient) GetReceipt(tx *types.Transaction) (*types.Receipt, error) { ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second) defer cancel() return c.waitReceipt(ctx, tx, 0) }
// EthTransactionReceipt returns the transaction receipt of the given txHash
func (c *EthereumClient) EthTransactionReceipt(ctx context.Context, txHash ethCommon.Hash) (*types.Receipt, error) { return c.client.TransactionReceipt(ctx, txHash) }
func (c *EthereumClient) waitReceipt(ctx context.Context, tx *types.Transaction, timeout time.Duration) (*types.Receipt, error) { var err error var receipt *types.Receipt
txHash := tx.Hash() log.Debugw("Waiting for receipt", "tx", txHash.Hex())
start := time.Now() for { receipt, err = c.client.TransactionReceipt(ctx, txHash) if receipt != nil || time.Since(start) >= timeout { break } time.Sleep(c.config.IntervalReceiptLoop * time.Millisecond) }
if receipt != nil && receipt.Status == types.ReceiptStatusFailed { log.Errorw("Failed transaction", "tx", txHash.Hex()) return receipt, ErrReceiptStatusFailed }
if receipt == nil { log.Debugw("Pendingtransaction / Wait receipt timeout", "tx", txHash.Hex(), "lasterr", err) return receipt, ErrReceiptNotReceived } log.Debugw("Successful transaction", "tx", txHash.Hex())
return receipt, err }
// EthLastBlock returns the last block number in the blockchain
func (c *EthereumClient) EthLastBlock() (int64, error) { ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second) defer cancel() header, err := c.client.HeaderByNumber(ctx, nil) if err != nil { return 0, err } return header.Number.Int64(), nil }
// EthHeaderByNumber internally calls ethclient.Client HeaderByNumber
// func (c *EthereumClient) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
// return c.client.HeaderByNumber(ctx, number)
// }
// EthBlockByNumber internally calls ethclient.Client BlockByNumber and returns
// *common.Block. If number == -1, the latests known block is returned.
func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number int64) (*common.Block, error) { blockNum := big.NewInt(number) if number == -1 { blockNum = nil } block, err := c.client.BlockByNumber(ctx, blockNum) if err != nil { return nil, err } b := &common.Block{ Num: block.Number().Int64(), Timestamp: time.Unix(int64(block.Time()), 0), ParentHash: block.ParentHash(), Hash: block.Hash(), } return b, nil }
// EthERC20Consts returns the constants defined for a particular ERC20 Token instance.
func (c *EthereumClient) EthERC20Consts(tokenAddress ethCommon.Address) (*ERC20Consts, error) { // We use the HEZ token smart contract interfacehere because it's an
// ERC20, which allows us to access the standard ERC20 constants.
instance, err := HEZ.NewHEZ(tokenAddress, c.client) if err != nil { return nil, err } name, err := instance.Name(nil) if err != nil { return nil, err }
symbol, err := instance.Symbol(nil) if err != nil { return nil, err }
decimals, err := instance.Decimals(nil) if err != nil { return nil, err } return &ERC20Consts{ Name: name, Symbol: symbol, Decimals: uint64(decimals), }, nil }
// Client returns the internal ethclient.Client
func (c *EthereumClient) Client() *ethclient.Client { return c.client }
|