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.

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