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.

159 lines
5.1 KiB

  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. // UpdateNetworkInfo update Status.Network information
  56. func (a *API) UpdateNetworkInfo(
  57. lastEthBlock, lastSyncBlock common.Block,
  58. lastBatchNum common.BatchNum, currentSlot int64,
  59. ) error {
  60. a.status.Network.LastSyncBlock = lastSyncBlock.EthBlockNum
  61. a.status.Network.LastEthBlock = lastEthBlock.EthBlockNum
  62. lastBatch, err := a.h.GetBatchAPI(lastBatchNum)
  63. if err != nil {
  64. return err
  65. }
  66. a.status.Network.LastBatch = *lastBatch
  67. a.status.Network.CurrentSlot = currentSlot
  68. lastClosedSlot := currentSlot + int64(a.status.Auction.ClosedAuctionSlots)
  69. nextForgers, err := a.GetNextForgers(lastSyncBlock, currentSlot, lastClosedSlot)
  70. if err != nil {
  71. return err
  72. }
  73. a.status.Network.NextForgers = nextForgers
  74. return nil
  75. }
  76. // GetNextForgers returns next forgers
  77. func (a *API) GetNextForgers(lastBlock common.Block, currentSlot, lastClosedSlot int64) ([]NextForger, error) {
  78. secondsPerBlock := int64(15) //nolint:gomnd
  79. // currentSlot and lastClosedSlot included
  80. limit := uint(lastClosedSlot - currentSlot + 1)
  81. bids, _, err := a.h.GetBestBidsAPI(&currentSlot, &lastClosedSlot, nil, &limit, "ASC")
  82. if err != nil {
  83. return nil, err
  84. }
  85. nextForgers := []NextForger{}
  86. // Create nextForger for each slot
  87. for i := currentSlot; i <= lastClosedSlot; i++ {
  88. fromBlock := i*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum
  89. toBlock := (i+1)*int64(a.cg.AuctionConstants.BlocksPerSlot) + a.cg.AuctionConstants.GenesisBlockNum - 1
  90. nextForger := NextForger{
  91. Period: Period{
  92. SlotNum: i,
  93. FromBlock: fromBlock,
  94. ToBlock: toBlock,
  95. FromTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(fromBlock-lastBlock.EthBlockNum))),
  96. ToTimestamp: lastBlock.Timestamp.Add(time.Second * time.Duration(secondsPerBlock*(toBlock-lastBlock.EthBlockNum))),
  97. },
  98. }
  99. foundBid := false
  100. // If there is a bid for a slot, get forger (coordinator)
  101. for j := range bids {
  102. if bids[j].SlotNum == i {
  103. foundBid = true
  104. coordinator, err := a.h.GetCoordinatorAPI(bids[j].Bidder)
  105. if err != nil {
  106. return nil, err
  107. }
  108. nextForger.Coordinator = *coordinator
  109. break
  110. }
  111. }
  112. // If there is no bid, the coordinator that will forge is boot coordinator
  113. if !foundBid {
  114. nextForger.Coordinator = bootCoordinator
  115. }
  116. nextForgers = append(nextForgers, nextForger)
  117. }
  118. return nextForgers, nil
  119. }
  120. // Metrics
  121. // UpdateMetrics update Status.Metrics information
  122. func (a *API) UpdateMetrics() error {
  123. metrics, err := a.h.GetMetrics(a.status.Network.LastBatch.BatchNum)
  124. if err != nil {
  125. return err
  126. }
  127. a.status.Metrics = *metrics
  128. return nil
  129. }
  130. // Recommended fee
  131. // UpdateRecommendedFee update Status.RecommendedFee information
  132. func (a *API) UpdateRecommendedFee() error {
  133. feeExistingAccount, err := a.h.GetAvgTxFee()
  134. if err != nil {
  135. return err
  136. }
  137. a.status.RecommendedFee.ExistingAccount = feeExistingAccount
  138. a.status.RecommendedFee.CreatesAccount = createAccountExtraFeePercentage * feeExistingAccount
  139. a.status.RecommendedFee.CreatesAccountAndRegister = createAccountInternalExtraFeePercentage * feeExistingAccount
  140. return nil
  141. }