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.

167 lines
5.3 KiB

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 api
  2. import (
  3. "net/http"
  4. "time"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/gin-gonic/gin"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. )
  10. // Network define status of the network
  11. type Network struct {
  12. LastEthBlock int64 `json:"lastEthereumBlock"`
  13. LastSyncBlock int64 `json:"lastSynchedBlock"`
  14. LastBatch historydb.BatchAPI `json:"lastBatch"`
  15. CurrentSlot int64 `json:"currentSlot"`
  16. NextForgers []NextForger `json:"nextForgers"`
  17. }
  18. // NextForger is a representation of the information of a coordinator and the period will forge
  19. type NextForger struct {
  20. Coordinator historydb.CoordinatorAPI `json:"coordinator"`
  21. Period Period `json:"period"`
  22. }
  23. // Period is a representation of a period
  24. type Period struct {
  25. SlotNum int64 `json:"slotNum"`
  26. FromBlock int64 `json:"fromBlock"`
  27. ToBlock int64 `json:"toBlock"`
  28. FromTimestamp time.Time `json:"fromTimestamp"`
  29. ToTimestamp time.Time `json:"toTimestamp"`
  30. }
  31. var bootCoordinator historydb.CoordinatorAPI = historydb.CoordinatorAPI{
  32. ItemID: 0,
  33. Bidder: ethCommon.HexToAddress("0x111111111111111111111111111111111111111"),
  34. Forger: ethCommon.HexToAddress("0x111111111111111111111111111111111111111"),
  35. URL: "https://bootCoordinator",
  36. }
  37. func (a *API) getState(c *gin.Context) {
  38. // TODO: There are no events for the buckets information, so now this information will be 0
  39. c.JSON(http.StatusOK, a.status)
  40. }
  41. // SC Vars
  42. // SetRollupVariables set Status.Rollup variables
  43. func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
  44. a.status.Rollup = rollupVariables
  45. }
  46. // SetWDelayerVariables set Status.WithdrawalDelayer variables
  47. func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
  48. a.status.WithdrawalDelayer = wDelayerVariables
  49. }
  50. // SetAuctionVariables set Status.Auction variables
  51. func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
  52. a.status.Auction = auctionVariables
  53. }
  54. // Network
  55. // UpdateNetworkInfoBlock update Status.Network block related information
  56. func (a *API) UpdateNetworkInfoBlock(
  57. lastEthBlock, lastSyncBlock common.Block,
  58. ) {
  59. a.status.Network.LastSyncBlock = lastSyncBlock.Num
  60. a.status.Network.LastEthBlock = lastEthBlock.Num
  61. }
  62. // UpdateNetworkInfo update Status.Network information
  63. func (a *API) UpdateNetworkInfo(
  64. lastEthBlock, lastSyncBlock common.Block,
  65. lastBatchNum common.BatchNum, currentSlot int64,
  66. ) error {
  67. a.status.Network.LastSyncBlock = lastSyncBlock.Num
  68. a.status.Network.LastEthBlock = lastEthBlock.Num
  69. lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
  70. if err != nil {
  71. return err
  72. }
  73. a.status.Network.LastBatch = *lastBatch
  74. a.status.Network.CurrentSlot = currentSlot
  75. lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
  76. nextForgers, err := a.GetNextForgers(lastSyncBlock, currentSlot, lastClosedSlot)
  77. if err != nil {
  78. return err
  79. }
  80. a.status.Network.NextForgers = nextForgers
  81. return nil
  82. }
  83. // GetNextForgers returns next forgers
  84. func (a *API) GetNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
  85. secondsPerBlock := int64(15) //nolint:gomnd
  86. // currentSlot and lastClosedSlot included
  87. limit := uint(lastClosedSlot - currentSlot + 1)
  88. bids, _, err := a.h.GetBestBidsAPI(&currentSlot, &lastClosedSlot, nil, &limit, "ASC")
  89. if err != nil {
  90. return nil, err
  91. }
  92. nextForgers := []NextForger{}
  93. // Create nextForger for each slot
  94. for i := currentSlot; i <= lastClosedSlot; i++ {
  95. fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
  96. toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
  97. nextForger := NextForger{
  98. Period: Period{
  99. SlotNum: i,
  100. FromBlock: fromBlock,
  101. ToBlock: toBlock,
  102. FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.Num))),
  103. ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.Num))),
  104. },
  105. }
  106. foundBid := false
  107. // If there is a bid for a slot, get forger (coordinator)
  108. for j := range bids {
  109. if bids[j].SlotNum == i {
  110. foundBid = true
  111. coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
  112. if err != nil {
  113. return nil, err
  114. }
  115. nextForger.Coordinator = *coordinator
  116. break
  117. }
  118. }
  119. // If there is no bid, the coordinator that will forge is boot coordinator
  120. if !foundBid {
  121. nextForger.Coordinator = bootCoordinator
  122. }
  123. nextForgers = append(nextForgers, nextForger)
  124. }
  125. return nextForgers, nil
  126. }
  127. // Metrics
  128. // UpdateMetrics update Status.Metrics information
  129. func (a *API) UpdateMetrics() error {
  130. metrics, err := a.h.GetMetrics(a.status.Network.LastBatch.BatchNum)
  131. if err != nil {
  132. return err
  133. }
  134. a.status.Metrics = *metrics
  135. return nil
  136. }
  137. // Recommended fee
  138. // UpdateRecommendedFee update Status.RecommendedFee information
  139. func (a *API) UpdateRecommendedFee() error {
  140. feeExistingAccount, err := a.h.GetAvgTxFee()
  141. if err != nil {
  142. return err
  143. }
  144. a.status.RecommendedFee.ExistingAccount = feeExistingAccount
  145. a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
  146. a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
  147. return nil
  148. }