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.3 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.
4 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. )
  13. func handleNoRoute(c *gin.Context) {
  14. c.JSON(http.StatusNotFound, gin.H{
  15. "error": "404 page not found",
  16. })
  17. }
  18. type errorMsg struct {
  19. Message string
  20. }
  21. func badReq(err error, c *gin.Context) {
  22. log.Errorw("Bad request", "err", err)
  23. c.JSON(http.StatusBadRequest, errorMsg{
  24. Message: err.Error(),
  25. })
  26. }
  27. // DebugAPI is an http API with debugging endpoints
  28. type DebugAPI struct {
  29. addr string
  30. stateDB *statedb.StateDB // synchronizer statedb
  31. sync *synchronizer.Synchronizer
  32. }
  33. // NewDebugAPI creates a new DebugAPI
  34. func NewDebugAPI(addr string, stateDB *statedb.StateDB, sync *synchronizer.Synchronizer) *DebugAPI {
  35. return &DebugAPI{
  36. addr: addr,
  37. stateDB: stateDB,
  38. sync: sync,
  39. }
  40. }
  41. func (a *DebugAPI) handleAccount(c *gin.Context) {
  42. uri := struct {
  43. Idx uint32
  44. }{}
  45. if err := c.ShouldBindUri(&uri); err != nil {
  46. badReq(err, c)
  47. return
  48. }
  49. account, err := a.stateDB.GetAccount(common.Idx(uri.Idx))
  50. if err != nil {
  51. badReq(err, c)
  52. return
  53. }
  54. c.JSON(http.StatusOK, account)
  55. }
  56. func (a *DebugAPI) handleAccounts(c *gin.Context) {
  57. accounts, err := a.stateDB.GetAccounts()
  58. if err != nil {
  59. badReq(err, c)
  60. return
  61. }
  62. c.JSON(http.StatusOK, accounts)
  63. }
  64. func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
  65. batchNum, err := a.stateDB.GetCurrentBatch()
  66. if err != nil {
  67. badReq(err, c)
  68. return
  69. }
  70. c.JSON(http.StatusOK, batchNum)
  71. }
  72. func (a *DebugAPI) handleMTRoot(c *gin.Context) {
  73. root := a.stateDB.MTGetRoot()
  74. c.JSON(http.StatusOK, root)
  75. }
  76. func (a *DebugAPI) handleSyncStats(c *gin.Context) {
  77. stats := a.sync.Stats()
  78. c.JSON(http.StatusOK, stats)
  79. }
  80. // Run starts the http server of the DebugAPI. To stop it, pass a context with
  81. // cancelation (see `debugapi_test.go` for an example).
  82. func (a *DebugAPI) Run(ctx context.Context) error {
  83. api := gin.Default()
  84. api.NoRoute(handleNoRoute)
  85. api.Use(cors.Default())
  86. debugAPI := api.Group("/debug")
  87. debugAPI.GET("sdb/batchnum", a.handleCurrentBatch)
  88. debugAPI.GET("sdb/mtroot", a.handleMTRoot)
  89. // Accounts returned by these endpoints will always have BatchNum = 0,
  90. // because the stateDB doesn't store the BatchNum in which an account
  91. // is created.
  92. debugAPI.GET("sdb/accounts", a.handleAccounts)
  93. debugAPI.GET("sdb/accounts/:Idx", a.handleAccount)
  94. debugAPI.GET("sync/stats", a.handleSyncStats)
  95. debugAPIServer := &http.Server{
  96. Addr: a.addr,
  97. Handler: api,
  98. // Use some hardcoded numberes that are suitable for testing
  99. ReadTimeout: 30 * time.Second, //nolint:gomnd
  100. WriteTimeout: 30 * time.Second, //nolint:gomnd
  101. MaxHeaderBytes: 1 << 20, //nolint:gomnd
  102. }
  103. go func() {
  104. log.Infof("DebugAPI is ready at %v", a.addr)
  105. if err := debugAPIServer.ListenAndServe(); err != nil &&
  106. 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 err
  116. }
  117. log.Info("DebugAPI done")
  118. return nil
  119. }