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.

185 lines
5.7 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
  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. a.status.RLock()
  40. status := a.status //nolint
  41. a.status.RUnlock()
  42. c.JSON(http.StatusOK, status) //nolint
  43. }
  44. // SC Vars
  45. // SetRollupVariables set Status.Rollup variables
  46. func (a *API) SetRollupVariables(rollupVariables common.RollupVariables) {
  47. a.status.Lock()
  48. a.status.Rollup = rollupVariables
  49. a.status.Unlock()
  50. }
  51. // SetWDelayerVariables set Status.WithdrawalDelayer variables
  52. func (a *API) SetWDelayerVariables(wDelayerVariables common.WDelayerVariables) {
  53. a.status.Lock()
  54. a.status.WithdrawalDelayer = wDelayerVariables
  55. a.status.Unlock()
  56. }
  57. // SetAuctionVariables set Status.Auction variables
  58. func (a *API) SetAuctionVariables(auctionVariables common.AuctionVariables) {
  59. a.status.Lock()
  60. a.status.Auction = auctionVariables
  61. a.status.Unlock()
  62. }
  63. // Network
  64. // UpdateNetworkInfoBlock update Status.Network block related information
  65. func (a *API) UpdateNetworkInfoBlock(
  66. lastEthBlock, lastSyncBlock common.Block,
  67. ) {
  68. a.status.Network.LastSyncBlock = lastSyncBlock.Num
  69. a.status.Network.LastEthBlock = lastEthBlock.Num
  70. }
  71. // UpdateNetworkInfo update Status.Network information
  72. func (a *API) UpdateNetworkInfo(
  73. lastEthBlock, lastSyncBlock common.Block,
  74. lastBatchNum common.BatchNum, currentSlot int64,
  75. ) error {
  76. lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
  77. if err != nil {
  78. return err
  79. }
  80. lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
  81. nextForgers, err := a.getNextForgers(lastSyncBlock, currentSlot, lastClosedSlot)
  82. if err != nil {
  83. return err
  84. }
  85. a.status.Lock()
  86. a.status.Network.LastSyncBlock = lastSyncBlock.Num
  87. a.status.Network.LastEthBlock = lastEthBlock.Num
  88. a.status.Network.LastBatch = *lastBatch
  89. a.status.Network.CurrentSlot = currentSlot
  90. a.status.Network.NextForgers = nextForgers
  91. a.status.Unlock()
  92. return nil
  93. }
  94. // getNextForgers returns next forgers
  95. func (a *API) getNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
  96. secondsPerBlock := int64(15) //nolint:gomnd
  97. // currentSlot and lastClosedSlot included
  98. limit := uint(lastClosedSlot - currentSlot + 1)
  99. bids, _, err := a.h.GetBestBidsAPI(&currentSlot, &lastClosedSlot, nil, &limit, "ASC")
  100. if err != nil {
  101. return nil, err
  102. }
  103. nextForgers := []NextForger{}
  104. // Create nextForger for each slot
  105. for i := currentSlot; i <= lastClosedSlot; i++ {
  106. fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
  107. toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
  108. nextForger := NextForger{
  109. Period: Period{
  110. SlotNum: i,
  111. FromBlock: fromBlock,
  112. ToBlock: toBlock,
  113. FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.Num))),
  114. ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.Num))),
  115. },
  116. }
  117. foundBid := false
  118. // If there is a bid for a slot, get forger (coordinator)
  119. for j := range bids {
  120. if bids[j].SlotNum == i {
  121. foundBid = true
  122. coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
  123. if err != nil {
  124. return nil, err
  125. }
  126. nextForger.Coordinator = *coordinator
  127. break
  128. }
  129. }
  130. // If there is no bid, the coordinator that will forge is boot coordinator
  131. if !foundBid {
  132. nextForger.Coordinator = bootCoordinator
  133. }
  134. nextForgers = append(nextForgers, nextForger)
  135. }
  136. return nextForgers, nil
  137. }
  138. // Metrics
  139. // UpdateMetrics update Status.Metrics information
  140. func (a *API) UpdateMetrics() error {
  141. a.status.RLock()
  142. batchNum := a.status.Network.LastBatch.BatchNum
  143. a.status.RUnlock()
  144. metrics, err := a.h.GetMetrics(batchNum)
  145. if err != nil {
  146. return err
  147. }
  148. a.status.Lock()
  149. a.status.Metrics = *metrics
  150. a.status.Unlock()
  151. return nil
  152. }
  153. // Recommended fee
  154. // UpdateRecommendedFee update Status.RecommendedFee information
  155. func (a *API) UpdateRecommendedFee() error {
  156. feeExistingAccount, err := a.h.GetAvgTxFee()
  157. if err != nil {
  158. return err
  159. }
  160. a.status.Lock()
  161. a.status.RecommendedFee.ExistingAccount = feeExistingAccount
  162. a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
  163. a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
  164. a.status.Unlock()
  165. return nil
  166. }