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.

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