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.

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