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.

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