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.

136 lines
3.4 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.GetAccount(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. accounts, err := a.stateDB.GetAccounts()
  59. if err != nil {
  60. badReq(err, c)
  61. return
  62. }
  63. c.JSON(http.StatusOK, accounts)
  64. }
  65. func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
  66. batchNum, err := a.stateDB.GetCurrentBatch()
  67. if err != nil {
  68. badReq(err, c)
  69. return
  70. }
  71. c.JSON(http.StatusOK, batchNum)
  72. }
  73. func (a *DebugAPI) handleMTRoot(c *gin.Context) {
  74. root := a.stateDB.MTGetRoot()
  75. c.JSON(http.StatusOK, root)
  76. }
  77. func (a *DebugAPI) handleSyncStats(c *gin.Context) {
  78. stats := a.sync.Stats()
  79. c.JSON(http.StatusOK, stats)
  80. }
  81. // Run starts the http server of the DebugAPI. To stop it, pass a context with
  82. // cancelation (see `debugapi_test.go` for an example).
  83. func (a *DebugAPI) Run(ctx context.Context) error {
  84. api := gin.Default()
  85. api.NoRoute(handleNoRoute)
  86. api.Use(cors.Default())
  87. debugAPI := api.Group("/debug")
  88. debugAPI.GET("sdb/batchnum", a.handleCurrentBatch)
  89. debugAPI.GET("sdb/mtroot", a.handleMTRoot)
  90. // Accounts returned by these endpoints will always have BatchNum = 0,
  91. // because the stateDB doesn't store the BatchNum in which an account
  92. // is created.
  93. debugAPI.GET("sdb/accounts", a.handleAccounts)
  94. debugAPI.GET("sdb/accounts/:Idx", a.handleAccount)
  95. debugAPI.GET("sync/stats", a.handleSyncStats)
  96. debugAPIServer := &http.Server{
  97. Addr: a.addr,
  98. Handler: api,
  99. // Use some hardcoded numberes that are suitable for testing
  100. ReadTimeout: 30 * time.Second, //nolint:gomnd
  101. WriteTimeout: 30 * time.Second, //nolint:gomnd
  102. MaxHeaderBytes: 1 << 20, //nolint:gomnd
  103. }
  104. go func() {
  105. log.Infof("DebugAPI is ready at %v", a.addr)
  106. if err := debugAPIServer.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
  107. log.Fatalf("Listen: %s\n", err)
  108. }
  109. }()
  110. <-ctx.Done()
  111. log.Info("Stopping DebugAPI...")
  112. ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:gomnd
  113. defer cancel()
  114. if err := debugAPIServer.Shutdown(ctxTimeout); err != nil {
  115. return tracerr.Wrap(err)
  116. }
  117. log.Info("DebugAPI done")
  118. return nil
  119. }