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.

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