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.

237 lines
5.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. "fmt"
  5. "math/big"
  6. "net/http"
  7. "sync"
  8. "time"
  9. "github.com/gin-contrib/cors"
  10. "github.com/gin-gonic/gin"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/hermeznetwork/hermez-node/db/historydb"
  13. "github.com/hermeznetwork/hermez-node/db/statedb"
  14. "github.com/hermeznetwork/hermez-node/log"
  15. "github.com/hermeznetwork/hermez-node/synchronizer"
  16. "github.com/hermeznetwork/tracerr"
  17. )
  18. func handleNoRoute(c *gin.Context) {
  19. c.JSON(http.StatusNotFound, gin.H{
  20. "error": "404 page not found",
  21. })
  22. }
  23. type errorMsg struct {
  24. Message string
  25. }
  26. func badReq(err error, c *gin.Context) {
  27. log.Errorw("Bad request", "err", err)
  28. c.JSON(http.StatusBadRequest, errorMsg{
  29. Message: err.Error(),
  30. })
  31. }
  32. const (
  33. statusUpdating = "updating"
  34. statusOK = "ok"
  35. )
  36. type tokenBalances struct {
  37. sync.RWMutex
  38. Value struct {
  39. Status string
  40. Block *common.Block
  41. Batch *common.Batch
  42. Balances map[common.TokenID]*big.Int
  43. }
  44. }
  45. func (t *tokenBalances) Update(historyDB *historydb.HistoryDB, sdb *statedb.StateDB) (err error) {
  46. var block *common.Block
  47. var batch *common.Batch
  48. var balances map[common.TokenID]*big.Int
  49. defer func() {
  50. t.Lock()
  51. if err == nil {
  52. t.Value.Status = statusOK
  53. t.Value.Block = block
  54. t.Value.Batch = batch
  55. t.Value.Balances = balances
  56. } else {
  57. t.Value.Status = fmt.Sprintf("tokenBalances.Update: %v", err)
  58. t.Value.Block = nil
  59. t.Value.Batch = nil
  60. t.Value.Balances = nil
  61. }
  62. t.Unlock()
  63. }()
  64. if block, err = historyDB.GetLastBlock(); err != nil {
  65. return tracerr.Wrap(err)
  66. }
  67. if batch, err = historyDB.GetLastBatch(); err != nil {
  68. return tracerr.Wrap(err)
  69. }
  70. balances = make(map[common.TokenID]*big.Int)
  71. sdb.LastRead(func(sdbLast *statedb.Last) error {
  72. return tracerr.Wrap(
  73. statedb.AccountsIter(sdbLast.DB(), func(a *common.Account) (bool, error) {
  74. if balance, ok := balances[a.TokenID]; !ok {
  75. balances[a.TokenID] = a.Balance
  76. } else {
  77. balance.Add(balance, a.Balance)
  78. }
  79. return true, nil
  80. }),
  81. )
  82. })
  83. return nil
  84. }
  85. // DebugAPI is an http API with debugging endpoints
  86. type DebugAPI struct {
  87. addr string
  88. historyDB *historydb.HistoryDB
  89. stateDB *statedb.StateDB // synchronizer statedb
  90. sync *synchronizer.Synchronizer
  91. tokenBalances tokenBalances
  92. }
  93. // NewDebugAPI creates a new DebugAPI
  94. func NewDebugAPI(addr string, historyDB *historydb.HistoryDB, stateDB *statedb.StateDB,
  95. sync *synchronizer.Synchronizer) *DebugAPI {
  96. return &DebugAPI{
  97. addr: addr,
  98. historyDB: historyDB,
  99. stateDB: stateDB,
  100. sync: sync,
  101. }
  102. }
  103. // SyncBlockHook is a hook function that the node will call after every new synchronized block
  104. func (a *DebugAPI) SyncBlockHook() {
  105. a.tokenBalances.RLock()
  106. updateTokenBalances := a.tokenBalances.Value.Status == statusUpdating
  107. a.tokenBalances.RUnlock()
  108. if updateTokenBalances {
  109. if err := a.tokenBalances.Update(a.historyDB, a.stateDB); err != nil {
  110. log.Errorw("DebugAPI.tokenBalances.Upate", "err", err)
  111. }
  112. }
  113. }
  114. func (a *DebugAPI) handleTokenBalances(c *gin.Context) {
  115. a.tokenBalances.RLock()
  116. tokenBalances := a.tokenBalances.Value
  117. a.tokenBalances.RUnlock()
  118. c.JSON(http.StatusOK, tokenBalances)
  119. }
  120. func (a *DebugAPI) handlePostTokenBalances(c *gin.Context) {
  121. a.tokenBalances.Lock()
  122. a.tokenBalances.Value.Status = statusUpdating
  123. a.tokenBalances.Unlock()
  124. c.JSON(http.StatusOK, nil)
  125. }
  126. func (a *DebugAPI) handleAccount(c *gin.Context) {
  127. uri := struct {
  128. Idx uint32
  129. }{}
  130. if err := c.ShouldBindUri(&uri); err != nil {
  131. badReq(err, c)
  132. return
  133. }
  134. account, err := a.stateDB.LastGetAccount(common.Idx(uri.Idx))
  135. if err != nil {
  136. badReq(err, c)
  137. return
  138. }
  139. c.JSON(http.StatusOK, account)
  140. }
  141. func (a *DebugAPI) handleAccounts(c *gin.Context) {
  142. var accounts []common.Account
  143. if err := a.stateDB.LastRead(func(sdb *statedb.Last) error {
  144. var err error
  145. accounts, err = sdb.GetAccounts()
  146. return err
  147. }); err != nil {
  148. badReq(err, c)
  149. return
  150. }
  151. c.JSON(http.StatusOK, accounts)
  152. }
  153. func (a *DebugAPI) handleCurrentBatch(c *gin.Context) {
  154. batchNum, err := a.stateDB.LastGetCurrentBatch()
  155. if err != nil {
  156. badReq(err, c)
  157. return
  158. }
  159. c.JSON(http.StatusOK, batchNum)
  160. }
  161. func (a *DebugAPI) handleMTRoot(c *gin.Context) {
  162. root, err := a.stateDB.LastMTGetRoot()
  163. if err != nil {
  164. badReq(err, c)
  165. return
  166. }
  167. c.JSON(http.StatusOK, root)
  168. }
  169. func (a *DebugAPI) handleSyncStats(c *gin.Context) {
  170. stats := a.sync.Stats()
  171. c.JSON(http.StatusOK, stats)
  172. }
  173. // Run starts the http server of the DebugAPI. To stop it, pass a context with
  174. // cancelation (see `debugapi_test.go` for an example).
  175. func (a *DebugAPI) Run(ctx context.Context) error {
  176. api := gin.Default()
  177. api.NoRoute(handleNoRoute)
  178. api.Use(cors.Default())
  179. debugAPI := api.Group("/debug")
  180. debugAPI.GET("sdb/batchnum", a.handleCurrentBatch)
  181. debugAPI.GET("sdb/mtroot", a.handleMTRoot)
  182. // Accounts returned by these endpoints will always have BatchNum = 0,
  183. // because the stateDB doesn't store the BatchNum in which an account
  184. // is created.
  185. debugAPI.GET("sdb/accounts", a.handleAccounts)
  186. debugAPI.GET("sdb/accounts/:Idx", a.handleAccount)
  187. debugAPI.POST("sdb/tokenbalances", a.handlePostTokenBalances)
  188. debugAPI.GET("sdb/tokenbalances", a.handleTokenBalances)
  189. debugAPI.GET("sync/stats", a.handleSyncStats)
  190. debugAPIServer := &http.Server{
  191. Addr: a.addr,
  192. Handler: api,
  193. // Use some hardcoded numberes that are suitable for testing
  194. ReadTimeout: 30 * time.Second, //nolint:gomnd
  195. WriteTimeout: 30 * time.Second, //nolint:gomnd
  196. MaxHeaderBytes: 1 << 20, //nolint:gomnd
  197. }
  198. go func() {
  199. log.Infof("DebugAPI is ready at %v", a.addr)
  200. if err := debugAPIServer.ListenAndServe(); err != nil && tracerr.Unwrap(err) != http.ErrServerClosed {
  201. log.Fatalf("Listen: %s\n", err)
  202. }
  203. }()
  204. <-ctx.Done()
  205. log.Info("Stopping DebugAPI...")
  206. ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:gomnd
  207. defer cancel()
  208. if err := debugAPIServer.Shutdown(ctxTimeout); err != nil {
  209. return tracerr.Wrap(err)
  210. }
  211. log.Info("DebugAPI done")
  212. return nil
  213. }