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.

144 lines
3.5 KiB

Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
  1. package debugapi
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "github.com/gin-contrib/cors"
  7. "github.com/gin-gonic/gin"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/hermeznetwork/hermez-node/db/statedb"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. "github.com/hermeznetwork/hermez-node/synchronizer"
  12. "github.com/hermeznetwork/tracerr"
  13. )
  14. func handleNoRoute(c *gin.Context) {
  15. c.JSON(http.StatusNotFound, gin.H{
  16. "error": "404 page not found",
  17. })
  18. }
  19. type errorMsg struct {
  20. Message string
  21. }
  22. func badReq(err error, c *gin.Context) {
  23. log.Errorw("Bad request", "err", err)
  24. c.JSON(http.StatusBadRequest, errorMsg{
  25. Message: err.Error(),
  26. })
  27. }
  28. // DebugAPI is an http API with debugging endpoints
  29. type DebugAPI struct {
  30. addr string
  31. stateDB *statedb.StateDB // synchronizer statedb
  32. sync *synchronizer.Synchronizer
  33. }
  34. // NewDebugAPI creates a new DebugAPI
  35. func NewDebugAPI(addr string, stateDB *statedb.StateDB, sync *synchronizer.Synchronizer) *DebugAPI {
  36. return &DebugAPI{
  37. addr: addr,
  38. stateDB: stateDB,
  39. sync: sync,
  40. }
  41. }
  42. func (a *DebugAPI) handleAccount(c *gin.Context) {
  43. uri := struct {
  44. Idx uint32
  45. }{}
  46. if err := c.ShouldBindUri(&uri); err != nil {
  47. badReq(err, c)
  48. return
  49. }
  50. account, err := a.stateDB.LastGetAccount(common.Idx(uri.Idx))
  51. if err != nil {
  52. badReq(err, c)
  53. return
  54. }
  55. c.JSON(http.StatusOK, account)
  56. }
  57. func (a *DebugAPI) handleAccounts(c *gin.Context) {
  58. var accounts []common.Account
  59. if err := a.stateDB.LastRead(func(sdb *statedb.Last) error {
  60. var err error
  61. accounts, err = sdb.GetAccounts()
  62. return err
  63. }); err != nil {
  64. badReq(err, c)
  65. return
  66. }
  67. c.JSON(http.StatusOK, accounts)
  68. }
  69. func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
  70. batchNum, err := a.stateDB.LastGetCurrentBatch()
  71. if err != nil {
  72. badReq(err, c)
  73. return
  74. }
  75. c.JSON(http.StatusOK, batchNum)
  76. }
  77. func (a *DebugAPI) handleMTRoot(c *gin.Context) {
  78. root, err := a.stateDB.LastMTGetRoot()
  79. if err != nil {
  80. badReq(err, c)
  81. return
  82. }
  83. c.JSON(http.StatusOK, root)
  84. }
  85. func (a *DebugAPI) handleSyncStats(c *gin.Context) {
  86. stats := a.sync.Stats()
  87. c.JSON(http.StatusOK, stats)
  88. }
  89. // Run starts the http server of the DebugAPI. To stop it, pass a context with
  90. // cancelation (see `debugapi_test.go` for an example).
  91. func (a *DebugAPI) Run(ctx context.Context) error {
  92. api := gin.Default()
  93. api.NoRoute(handleNoRoute)
  94. api.Use(cors.Default())
  95. debugAPI := api.Group("/debug")
  96. debugAPI.GET("sdb/batchnum", a.handleCurrentBatch)
  97. debugAPI.GET("sdb/mtroot", a.handleMTRoot)
  98. // Accounts returned by these endpoints will always have BatchNum = 0,
  99. // because the stateDB doesn't store the BatchNum in which an account
  100. // is created.
  101. debugAPI.GET("sdb/accounts", a.handleAccounts)
  102. debugAPI.GET("sdb/accounts/:Idx", a.handleAccount)
  103. debugAPI.GET("sync/stats", a.handleSyncStats)
  104. debugAPIServer := &http.Server{
  105. Addr: a.addr,
  106. Handler: api,
  107. // Use some hardcoded numberes that are suitable for testing
  108. ReadTimeout: 30 * time.Second, //nolint:gomnd
  109. WriteTimeout: 30 * time.Second, //nolint:gomnd
  110. MaxHeaderBytes: 1 << 20, //nolint:gomnd
  111. }
  112. go func() {
  113. log.Infof("DebugAPI is ready at %v", a.addr)
  114. if err := debugAPIServer.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
  115. log.Fatalf("Listen: %s\n", err)
  116. }
  117. }()
  118. <-ctx.Done()
  119. log.Info("Stopping DebugAPI...")
  120. ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:gomnd
  121. defer cancel()
  122. if err := debugAPIServer.Shutdown(ctxTimeout); err != nil {
  123. return tracerr.Wrap(err)
  124. }
  125. log.Info("DebugAPI done")
  126. return nil
  127. }