You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
11 KiB

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
  1. package eth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. "github.com/ethereum/go-ethereum/accounts"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/ethclient"
  13. "github.com/hermeznetwork/hermez-node/common"
  14. HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
  15. "github.com/hermeznetwork/hermez-node/log"
  16. "github.com/hermeznetwork/tracerr"
  17. )
  18. // ERC20Consts are the constants defined in a particular ERC20 Token instance
  19. type ERC20Consts struct {
  20. Name string
  21. Symbol string
  22. Decimals uint64
  23. }
  24. // EthereumInterface is the interface to Ethereum
  25. type EthereumInterface interface {
  26. EthLastBlock() (int64, error)
  27. // EthHeaderByNumber(context.Context, *big.Int) (*types.Header, error)
  28. EthBlockByNumber(context.Context, int64) (*common.Block, error)
  29. EthAddress() (*ethCommon.Address, error)
  30. EthTransactionReceipt(context.Context, ethCommon.Hash) (*types.Receipt, error)
  31. EthERC20Consts(ethCommon.Address) (*ERC20Consts, error)
  32. EthChainID() (*big.Int, error)
  33. }
  34. var (
  35. // ErrAccountNil is used when the calls can not be made because the account is nil
  36. ErrAccountNil = fmt.Errorf("Authorized calls can't be made when the account is nil")
  37. // ErrReceiptStatusFailed is used when receiving a failed transaction
  38. ErrReceiptStatusFailed = fmt.Errorf("receipt status is failed")
  39. // ErrReceiptNotReceived is used when unable to retrieve a transaction
  40. ErrReceiptNotReceived = fmt.Errorf("receipt not available")
  41. // ErrBlockHashMismatchEvent is used when there's a block hash mismatch
  42. // beetween different events of the same block
  43. ErrBlockHashMismatchEvent = fmt.Errorf("block hash mismatch in event log")
  44. )
  45. const (
  46. errStrDeploy = "deployment of %s failed: %w"
  47. errStrWaitReceipt = "wait receipt of %s deploy failed: %w"
  48. // default values
  49. defaultCallGasLimit = 300000
  50. defaultDeployGasLimit = 1000000
  51. defaultGasPriceDiv = 100
  52. defaultReceiptTimeout = 60
  53. defaultIntervalReceiptLoop = 200
  54. )
  55. // EthereumConfig defines the configuration parameters of the EthereumClient
  56. type EthereumConfig struct {
  57. CallGasLimit uint64
  58. DeployGasLimit uint64
  59. GasPriceDiv uint64
  60. ReceiptTimeout time.Duration
  61. IntervalReceiptLoop time.Duration
  62. }
  63. // EthereumClient is an ethereum client to call Smart Contract methods and check blockchain information.
  64. type EthereumClient struct {
  65. client *ethclient.Client
  66. chainID *big.Int
  67. account *accounts.Account
  68. ks *ethKeystore.KeyStore
  69. ReceiptTimeout time.Duration
  70. config *EthereumConfig
  71. opts *bind.CallOpts
  72. }
  73. // NewEthereumClient creates a EthereumClient instance. The account is not mandatory (it can
  74. // be nil). If the account is nil, CallAuth will fail with ErrAccountNil.
  75. func NewEthereumClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, config *EthereumConfig) (*EthereumClient, error) {
  76. if config == nil {
  77. config = &EthereumConfig{
  78. CallGasLimit: defaultCallGasLimit,
  79. DeployGasLimit: defaultDeployGasLimit,
  80. GasPriceDiv: defaultGasPriceDiv,
  81. ReceiptTimeout: defaultReceiptTimeout,
  82. IntervalReceiptLoop: defaultIntervalReceiptLoop,
  83. }
  84. }
  85. c := &EthereumClient{
  86. client: client,
  87. account: account,
  88. ks: ks,
  89. ReceiptTimeout: config.ReceiptTimeout * time.Second,
  90. config: config,
  91. opts: newCallOpts(),
  92. }
  93. chainID, err := c.EthChainID()
  94. if err != nil {
  95. return nil, tracerr.Wrap(err)
  96. }
  97. c.chainID = chainID
  98. return c, nil
  99. }
  100. // EthChainID returns the ChainID of the ethereum network
  101. func (c *EthereumClient) EthChainID() (*big.Int, error) {
  102. chainID, err := c.client.ChainID(context.Background())
  103. if err != nil {
  104. return nil, tracerr.Wrap(err)
  105. }
  106. return chainID, nil
  107. }
  108. // BalanceAt retieves information about the default account
  109. func (c *EthereumClient) BalanceAt(addr ethCommon.Address) (*big.Int, error) {
  110. return c.client.BalanceAt(context.TODO(), addr, nil)
  111. }
  112. // Account returns the underlying ethereum account
  113. func (c *EthereumClient) Account() *accounts.Account {
  114. return c.account
  115. }
  116. // EthAddress returns the ethereum address of the account loaded into the EthereumClient
  117. func (c *EthereumClient) EthAddress() (*ethCommon.Address, error) {
  118. if c.account == nil {
  119. return nil, tracerr.Wrap(ErrAccountNil)
  120. }
  121. return &c.account.Address, nil
  122. }
  123. // CallAuth performs a Smart Contract method call that requires authorization.
  124. // This call requires a valid account with Ether that can be spend during the
  125. // call.
  126. func (c *EthereumClient) CallAuth(gasLimit uint64,
  127. fn func(*ethclient.Client, *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) {
  128. if c.account == nil {
  129. return nil, tracerr.Wrap(ErrAccountNil)
  130. }
  131. gasPrice, err := c.client.SuggestGasPrice(context.Background())
  132. if err != nil {
  133. return nil, tracerr.Wrap(err)
  134. }
  135. inc := new(big.Int).Set(gasPrice)
  136. inc.Div(inc, new(big.Int).SetUint64(c.config.GasPriceDiv))
  137. gasPrice.Add(gasPrice, inc)
  138. log.Debugw("Transaction metadata", "gasPrice", gasPrice)
  139. auth, err := bind.NewKeyStoreTransactorWithChainID(c.ks, *c.account, c.chainID)
  140. if err != nil {
  141. return nil, tracerr.Wrap(err)
  142. }
  143. auth.Value = big.NewInt(0) // in wei
  144. if gasLimit == 0 {
  145. auth.GasLimit = c.config.CallGasLimit // in units
  146. } else {
  147. auth.GasLimit = gasLimit // in units
  148. }
  149. auth.GasPrice = gasPrice
  150. tx, err := fn(c.client, auth)
  151. if tx != nil {
  152. log.Debugw("Transaction", "tx", tx.Hash().Hex(), "nonce", tx.Nonce())
  153. }
  154. return tx, tracerr.Wrap(err)
  155. }
  156. // ContractData contains the contract data
  157. type ContractData struct {
  158. Address ethCommon.Address
  159. Tx *types.Transaction
  160. Receipt *types.Receipt
  161. }
  162. // Deploy a smart contract. `name` is used to log deployment information. fn
  163. // is a wrapper to the deploy function generated by abigen. In case of error,
  164. // the returned `ContractData` may have some parameters filled depending on the
  165. // kind of error that occurred.
  166. func (c *EthereumClient) Deploy(name string,
  167. fn func(c *ethclient.Client, auth *bind.TransactOpts) (ethCommon.Address, *types.Transaction, interface{}, error)) (ContractData, error) {
  168. var contractData ContractData
  169. log.Infow("Deploying", "contract", name)
  170. tx, err := c.CallAuth(
  171. c.config.DeployGasLimit,
  172. func(client *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  173. addr, tx, _, err := fn(client, auth)
  174. if err != nil {
  175. return nil, tracerr.Wrap(err)
  176. }
  177. contractData.Address = addr
  178. return tx, nil
  179. },
  180. )
  181. if err != nil {
  182. return contractData, tracerr.Wrap(fmt.Errorf(errStrDeploy, name, err))
  183. }
  184. log.Infow("Waiting receipt", "tx", tx.Hash().Hex(), "contract", name)
  185. contractData.Tx = tx
  186. receipt, err := c.WaitReceipt(tx)
  187. if err != nil {
  188. return contractData, tracerr.Wrap(fmt.Errorf(errStrWaitReceipt, name, err))
  189. }
  190. contractData.Receipt = receipt
  191. return contractData, nil
  192. }
  193. // Call performs a read only Smart Contract method call.
  194. func (c *EthereumClient) Call(fn func(*ethclient.Client) error) error {
  195. return fn(c.client)
  196. }
  197. // WaitReceipt will block until a transaction is confirmed. Internally it
  198. // polls the state every 200 milliseconds.
  199. func (c *EthereumClient) WaitReceipt(tx *types.Transaction) (*types.Receipt, error) {
  200. return c.waitReceipt(context.TODO(), tx, c.ReceiptTimeout)
  201. }
  202. // GetReceipt will check if a transaction is confirmed and return
  203. // immediately, waiting at most 1 second and returning error if the transaction
  204. // is still pending.
  205. func (c *EthereumClient) GetReceipt(tx *types.Transaction) (*types.Receipt, error) {
  206. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  207. defer cancel()
  208. return c.waitReceipt(ctx, tx, 0)
  209. }
  210. // EthTransactionReceipt returns the transaction receipt of the given txHash
  211. func (c *EthereumClient) EthTransactionReceipt(ctx context.Context, txHash ethCommon.Hash) (*types.Receipt, error) {
  212. return c.client.TransactionReceipt(ctx, txHash)
  213. }
  214. func (c *EthereumClient) waitReceipt(ctx context.Context, tx *types.Transaction, timeout time.Duration) (*types.Receipt, error) {
  215. var err error
  216. var receipt *types.Receipt
  217. txHash := tx.Hash()
  218. log.Debugw("Waiting for receipt", "tx", txHash.Hex())
  219. start := time.Now()
  220. for {
  221. receipt, err = c.client.TransactionReceipt(ctx, txHash)
  222. if receipt != nil || time.Since(start) >= timeout {
  223. break
  224. }
  225. time.Sleep(c.config.IntervalReceiptLoop * time.Millisecond)
  226. }
  227. if receipt != nil && receipt.Status == types.ReceiptStatusFailed {
  228. log.Errorw("Failed transaction", "tx", txHash.Hex())
  229. return receipt, tracerr.Wrap(ErrReceiptStatusFailed)
  230. }
  231. if receipt == nil {
  232. log.Debugw("Pendingtransaction / Wait receipt timeout", "tx", txHash.Hex(), "lasterr", err)
  233. return receipt, tracerr.Wrap(ErrReceiptNotReceived)
  234. }
  235. log.Debugw("Successful transaction", "tx", txHash.Hex())
  236. return receipt, tracerr.Wrap(err)
  237. }
  238. // EthLastBlock returns the last block number in the blockchain
  239. func (c *EthereumClient) EthLastBlock() (int64, error) {
  240. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  241. defer cancel()
  242. header, err := c.client.HeaderByNumber(ctx, nil)
  243. if err != nil {
  244. return 0, tracerr.Wrap(err)
  245. }
  246. return header.Number.Int64(), nil
  247. }
  248. // EthHeaderByNumber internally calls ethclient.Client HeaderByNumber
  249. // func (c *EthereumClient) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  250. // return c.client.HeaderByNumber(ctx, number)
  251. // }
  252. // EthBlockByNumber internally calls ethclient.Client BlockByNumber and returns
  253. // *common.Block. If number == -1, the latests known block is returned.
  254. func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number int64) (*common.Block, error) {
  255. blockNum := big.NewInt(number)
  256. if number == -1 {
  257. blockNum = nil
  258. }
  259. block, err := c.client.BlockByNumber(ctx, blockNum)
  260. if err != nil {
  261. return nil, tracerr.Wrap(err)
  262. }
  263. b := &common.Block{
  264. Num: block.Number().Int64(),
  265. Timestamp: time.Unix(int64(block.Time()), 0),
  266. ParentHash: block.ParentHash(),
  267. Hash: block.Hash(),
  268. }
  269. return b, nil
  270. }
  271. // EthERC20Consts returns the constants defined for a particular ERC20 Token instance.
  272. func (c *EthereumClient) EthERC20Consts(tokenAddress ethCommon.Address) (*ERC20Consts, error) {
  273. // We use the HEZ token smart contract interfacehere because it's an
  274. // ERC20, which allows us to access the standard ERC20 constants.
  275. instance, err := HEZ.NewHEZ(tokenAddress, c.client)
  276. if err != nil {
  277. return nil, tracerr.Wrap(err)
  278. }
  279. name, err := instance.Name(c.opts)
  280. if err != nil {
  281. return nil, tracerr.Wrap(err)
  282. }
  283. symbol, err := instance.Symbol(c.opts)
  284. if err != nil {
  285. return nil, tracerr.Wrap(err)
  286. }
  287. decimals, err := instance.Decimals(c.opts)
  288. if err != nil {
  289. return nil, tracerr.Wrap(err)
  290. }
  291. return &ERC20Consts{
  292. Name: name,
  293. Symbol: symbol,
  294. Decimals: uint64(decimals),
  295. }, nil
  296. }
  297. // Client returns the internal ethclient.Client
  298. func (c *EthereumClient) Client() *ethclient.Client {
  299. return c.client
  300. }
  301. // newCallOpts returns a CallOpts to be used in ethereum calls with a non-zero
  302. // From address. This is a workaround for a bug in ethereumjs-vm that shows up
  303. // in ganache: https://github.com/hermeznetwork/hermez-node/issues/317
  304. func newCallOpts() *bind.CallOpts {
  305. return &bind.CallOpts{
  306. From: ethCommon.HexToAddress("0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"),
  307. }
  308. }