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.

123 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package api
  2. import (
  3. "database/sql"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. "github.com/hermeznetwork/tracerr"
  9. )
  10. func (a *API) getState(c *gin.Context) {
  11. stateAPI, err := a.h.GetStateAPI()
  12. if err != nil {
  13. retBadReq(err, c)
  14. return
  15. }
  16. c.JSON(http.StatusOK, stateAPI)
  17. }
  18. type APIStateUpdater struct {
  19. hdb *historydb.HistoryDB
  20. state historydb.StateAPI
  21. config historydb.NodeConfig
  22. vars common.SCVariablesPtr
  23. consts historydb.Constants
  24. }
  25. func NewAPIStateUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables,
  26. consts *historydb.Constants) *APIStateUpdater {
  27. u := APIStateUpdater{
  28. hdb: hdb,
  29. config: *config,
  30. consts: *consts,
  31. }
  32. u.SetSCVars(&common.SCVariablesPtr{&vars.Rollup, &vars.Auction, &vars.WDelayer})
  33. return &u
  34. }
  35. func (u *APIStateUpdater) Store() error {
  36. return tracerr.Wrap(u.hdb.SetAPIState(&u.state))
  37. }
  38. func (u *APIStateUpdater) SetSCVars(vars *common.SCVariablesPtr) {
  39. if vars.Rollup != nil {
  40. u.vars.Rollup = vars.Rollup
  41. rollupVars := historydb.NewRollupVariablesAPI(u.vars.Rollup)
  42. u.state.Rollup = *rollupVars
  43. }
  44. if vars.Auction != nil {
  45. u.vars.Auction = vars.Auction
  46. auctionVars := historydb.NewAuctionVariablesAPI(u.vars.Auction)
  47. u.state.Auction = *auctionVars
  48. }
  49. if vars.WDelayer != nil {
  50. u.vars.WDelayer = vars.WDelayer
  51. u.state.WithdrawalDelayer = *u.vars.WDelayer
  52. }
  53. }
  54. func (u *APIStateUpdater) UpdateMetrics() error {
  55. if u.state.Network.LastBatch == nil {
  56. return nil
  57. }
  58. lastBatchNum := u.state.Network.LastBatch.BatchNum
  59. metrics, err := u.hdb.GetMetricsInternalAPI(lastBatchNum)
  60. if err != nil {
  61. return tracerr.Wrap(err)
  62. }
  63. u.state.Metrics = *metrics
  64. return nil
  65. }
  66. func (u *APIStateUpdater) UpdateNetworkInfoBlock(lastEthBlock, lastSyncBlock common.Block) {
  67. u.state.Network.LastSyncBlock = lastSyncBlock.Num
  68. u.state.Network.LastEthBlock = lastEthBlock.Num
  69. }
  70. func (u *APIStateUpdater) UpdateNetworkInfo(
  71. lastEthBlock, lastSyncBlock common.Block,
  72. lastBatchNum common.BatchNum, currentSlot int64,
  73. ) error {
  74. // Get last batch in API format
  75. lastBatch, err := u.hdb.GetBatchInternalAPI(lastBatchNum)
  76. if tracerr.Unwrap(err) == sql.ErrNoRows {
  77. lastBatch = nil
  78. } else if err != nil {
  79. return tracerr.Wrap(err)
  80. }
  81. // Get next forgers
  82. lastClosedSlot := currentSlot + int64(u.state.Auction.ClosedAuctionSlots)
  83. nextForgers, err := u.hdb.GetNextForgersInternalAPI(u.vars.Auction, &u.consts.Auction,
  84. lastSyncBlock, currentSlot, lastClosedSlot)
  85. if tracerr.Unwrap(err) == sql.ErrNoRows {
  86. nextForgers = nil
  87. } else if err != nil {
  88. return tracerr.Wrap(err)
  89. }
  90. bucketUpdates, err := u.hdb.GetBucketUpdatesInternalAPI()
  91. if err == sql.ErrNoRows {
  92. bucketUpdates = nil
  93. } else if err != nil {
  94. return tracerr.Wrap(err)
  95. }
  96. // Update NodeInfo struct
  97. for i, bucketParams := range u.state.Rollup.Buckets {
  98. for _, bucketUpdate := range bucketUpdates {
  99. if bucketUpdate.NumBucket == i {
  100. bucketParams.Withdrawals = bucketUpdate.Withdrawals
  101. u.state.Rollup.Buckets[i] = bucketParams
  102. break
  103. }
  104. }
  105. }
  106. u.state.Network.LastSyncBlock = lastSyncBlock.Num
  107. u.state.Network.LastEthBlock = lastEthBlock.Num
  108. u.state.Network.LastBatch = lastBatch
  109. u.state.Network.CurrentSlot = currentSlot
  110. u.state.Network.NextForgers = nextForgers
  111. return nil
  112. }