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.

161 lines
4.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package api
  2. import (
  3. "database/sql"
  4. "net/http"
  5. "sync"
  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. func (a *API) getState(c *gin.Context) {
  12. stateAPI, err := a.h.GetStateAPI()
  13. if err != nil {
  14. retBadReq(err, c)
  15. return
  16. }
  17. c.JSON(http.StatusOK, stateAPI)
  18. }
  19. // StateAPIUpdater is an utility object to facilitate updating the StateAPI
  20. type StateAPIUpdater struct {
  21. hdb *historydb.HistoryDB
  22. state historydb.StateAPI
  23. config historydb.NodeConfig
  24. vars common.SCVariablesPtr
  25. consts historydb.Constants
  26. rw sync.RWMutex
  27. }
  28. // NewStateAPIUpdater creates a new StateAPIUpdater
  29. func NewStateAPIUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables,
  30. consts *historydb.Constants) *StateAPIUpdater {
  31. u := StateAPIUpdater{
  32. hdb: hdb,
  33. config: *config,
  34. consts: *consts,
  35. }
  36. u.SetSCVars(vars.AsPtr())
  37. return &u
  38. }
  39. // Store the State in the HistoryDB
  40. func (u *StateAPIUpdater) Store() error {
  41. u.rw.RLock()
  42. defer u.rw.RUnlock()
  43. return tracerr.Wrap(u.hdb.SetStateInternalAPI(&u.state))
  44. }
  45. // SetSCVars sets the smart contract vars (ony updates those that are not nil)
  46. func (u *StateAPIUpdater) SetSCVars(vars *common.SCVariablesPtr) {
  47. u.rw.Lock()
  48. defer u.rw.Unlock()
  49. if vars.Rollup != nil {
  50. u.vars.Rollup = vars.Rollup
  51. rollupVars := historydb.NewRollupVariablesAPI(u.vars.Rollup)
  52. u.state.Rollup = *rollupVars
  53. }
  54. if vars.Auction != nil {
  55. u.vars.Auction = vars.Auction
  56. auctionVars := historydb.NewAuctionVariablesAPI(u.vars.Auction)
  57. u.state.Auction = *auctionVars
  58. }
  59. if vars.WDelayer != nil {
  60. u.vars.WDelayer = vars.WDelayer
  61. u.state.WithdrawalDelayer = *u.vars.WDelayer
  62. }
  63. }
  64. // UpdateRecommendedFee update Status.RecommendedFee information
  65. func (u *StateAPIUpdater) UpdateRecommendedFee() error {
  66. recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD)
  67. if err != nil {
  68. return tracerr.Wrap(err)
  69. }
  70. u.rw.Lock()
  71. u.state.RecommendedFee = *recommendedFee
  72. u.rw.Unlock()
  73. return nil
  74. }
  75. // UpdateMetrics update Status.Metrics information
  76. func (u *StateAPIUpdater) UpdateMetrics() error {
  77. u.rw.RLock()
  78. lastBatch := u.state.Network.LastBatch
  79. u.rw.RUnlock()
  80. if lastBatch == nil {
  81. return nil
  82. }
  83. lastBatchNum := lastBatch.BatchNum
  84. metrics, err := u.hdb.GetMetricsInternalAPI(lastBatchNum)
  85. if err != nil {
  86. return tracerr.Wrap(err)
  87. }
  88. u.rw.Lock()
  89. u.state.Metrics = *metrics
  90. u.rw.Unlock()
  91. return nil
  92. }
  93. // UpdateNetworkInfoBlock update Status.Network block related information
  94. func (u *StateAPIUpdater) UpdateNetworkInfoBlock(lastEthBlock, lastSyncBlock common.Block) {
  95. u.rw.Lock()
  96. u.state.Network.LastSyncBlock = lastSyncBlock.Num
  97. u.state.Network.LastEthBlock = lastEthBlock.Num
  98. u.rw.Unlock()
  99. }
  100. // UpdateNetworkInfo update Status.Network information
  101. func (u *StateAPIUpdater) UpdateNetworkInfo(
  102. lastEthBlock, lastSyncBlock common.Block,
  103. lastBatchNum common.BatchNum, currentSlot int64,
  104. ) error {
  105. // Get last batch in API format
  106. lastBatch, err := u.hdb.GetBatchInternalAPI(lastBatchNum)
  107. if tracerr.Unwrap(err) == sql.ErrNoRows {
  108. lastBatch = nil
  109. } else if err != nil {
  110. return tracerr.Wrap(err)
  111. }
  112. u.rw.RLock()
  113. auctionVars := u.vars.Auction
  114. u.rw.RUnlock()
  115. // Get next forgers
  116. lastClosedSlot := currentSlot + int64(auctionVars.ClosedAuctionSlots)
  117. nextForgers, err := u.hdb.GetNextForgersInternalAPI(auctionVars, &u.consts.Auction,
  118. lastSyncBlock, currentSlot, lastClosedSlot)
  119. if tracerr.Unwrap(err) == sql.ErrNoRows {
  120. nextForgers = nil
  121. } else if err != nil {
  122. return tracerr.Wrap(err)
  123. }
  124. bucketUpdates, err := u.hdb.GetBucketUpdatesInternalAPI()
  125. if err == sql.ErrNoRows {
  126. bucketUpdates = nil
  127. } else if err != nil {
  128. return tracerr.Wrap(err)
  129. }
  130. u.rw.Lock()
  131. // Update NodeInfo struct
  132. for i, bucketParams := range u.state.Rollup.Buckets {
  133. for _, bucketUpdate := range bucketUpdates {
  134. if bucketUpdate.NumBucket == i {
  135. bucketParams.Withdrawals = bucketUpdate.Withdrawals
  136. u.state.Rollup.Buckets[i] = bucketParams
  137. break
  138. }
  139. }
  140. }
  141. u.state.Network.LastSyncBlock = lastSyncBlock.Num
  142. u.state.Network.LastEthBlock = lastEthBlock.Num
  143. u.state.Network.LastBatch = lastBatch
  144. u.state.Network.CurrentSlot = currentSlot
  145. u.state.Network.NextForgers = nextForgers
  146. u.rw.Unlock()
  147. return nil
  148. }