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.

261 lines
6.6 KiB

  1. package node
  2. import (
  3. "context"
  4. "time"
  5. "github.com/ethereum/go-ethereum/ethclient"
  6. "github.com/hermeznetwork/hermez-node/batchbuilder"
  7. "github.com/hermeznetwork/hermez-node/config"
  8. "github.com/hermeznetwork/hermez-node/coordinator"
  9. dbUtils "github.com/hermeznetwork/hermez-node/db"
  10. "github.com/hermeznetwork/hermez-node/db/historydb"
  11. "github.com/hermeznetwork/hermez-node/db/l2db"
  12. "github.com/hermeznetwork/hermez-node/db/statedb"
  13. "github.com/hermeznetwork/hermez-node/eth"
  14. "github.com/hermeznetwork/hermez-node/log"
  15. "github.com/hermeznetwork/hermez-node/synchronizer"
  16. "github.com/hermeznetwork/hermez-node/txselector"
  17. "github.com/jmoiron/sqlx"
  18. )
  19. // Mode sets the working mode of the node (synchronizer or coordinator)
  20. type Mode string
  21. const (
  22. // ModeCoordinator defines the mode of the HermezNode as Coordinator, which
  23. // means that the node is set to forge (which also will be synchronizing with
  24. // the L1 blockchain state)
  25. ModeCoordinator Mode = "coordinator"
  26. // ModeSynchronizer defines the mode of the HermezNode as Synchronizer, which
  27. // means that the node is set to only synchronize with the L1 blockchain state
  28. // and will not forge
  29. ModeSynchronizer Mode = "synchronizer"
  30. )
  31. // Node is the Hermez Node
  32. type Node struct {
  33. // Coordinator
  34. coord *coordinator.Coordinator
  35. coordCfg *config.Coordinator
  36. stopForge chan bool
  37. stopGetProofCallForge chan bool
  38. stopForgeCallConfirm chan bool
  39. stoppedForge chan bool
  40. stoppedGetProofCallForge chan bool
  41. stoppedForgeCallConfirm chan bool
  42. // Synchronizer
  43. sync *synchronizer.Synchronizer
  44. stopSync chan bool
  45. stoppedSync chan bool
  46. // General
  47. cfg *config.Node
  48. mode Mode
  49. sqlConn *sqlx.DB
  50. }
  51. // NewNode creates a Node
  52. func NewNode(mode Mode, cfg *config.Node, coordCfg *config.Coordinator) (*Node, error) {
  53. // Stablish DB connection
  54. db, err := dbUtils.InitSQLDB(
  55. cfg.PostgreSQL.Port,
  56. cfg.PostgreSQL.Host,
  57. cfg.PostgreSQL.User,
  58. cfg.PostgreSQL.Password,
  59. cfg.PostgreSQL.Name,
  60. )
  61. if err != nil {
  62. return nil, err
  63. }
  64. historyDB := historydb.NewHistoryDB(db)
  65. stateDB, err := statedb.NewStateDB(cfg.StateDB.Path, statedb.TypeSynchronizer, 32)
  66. if err != nil {
  67. return nil, err
  68. }
  69. ethClient, err := ethclient.Dial(cfg.Web3.URL)
  70. if err != nil {
  71. return nil, err
  72. }
  73. client := eth.NewClient(ethClient, nil, nil, nil)
  74. sync, err := synchronizer.NewSynchronizer(client, historyDB, stateDB)
  75. if err != nil {
  76. return nil, err
  77. }
  78. var coord *coordinator.Coordinator
  79. if mode == ModeCoordinator {
  80. l2DB := l2db.NewL2DB(
  81. db,
  82. coordCfg.L2DB.SafetyPeriod,
  83. coordCfg.L2DB.MaxTxs,
  84. coordCfg.L2DB.TTL.Duration,
  85. )
  86. // TODO: Get (maxL1UserTxs, maxL1OperatorTxs, maxTxs) from the smart contract
  87. txSelector, err := txselector.NewTxSelector(coordCfg.TxSelector.Path, stateDB, l2DB, 10, 10, 10)
  88. if err != nil {
  89. return nil, err
  90. }
  91. // TODO: Get (configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) from smart contract
  92. nLevels := uint64(32) //nolint:gomnd
  93. batchBuilder, err := batchbuilder.NewBatchBuilder(coordCfg.BatchBuilder.Path, stateDB, nil, 0, nLevels)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if err != nil {
  98. return nil, err
  99. }
  100. serverProofs := make([]coordinator.ServerProofInterface, len(coordCfg.ServerProofs))
  101. for i, serverProofCfg := range coordCfg.ServerProofs {
  102. serverProofs[i] = coordinator.NewServerProof(serverProofCfg.URL)
  103. }
  104. coord = coordinator.NewCoordinator(
  105. coordinator.Config{
  106. ForgerAddress: coordCfg.ForgerAddress,
  107. },
  108. historyDB,
  109. txSelector,
  110. batchBuilder,
  111. serverProofs,
  112. client,
  113. )
  114. }
  115. return &Node{
  116. coord: coord,
  117. coordCfg: coordCfg,
  118. sync: sync,
  119. cfg: cfg,
  120. mode: mode,
  121. sqlConn: db,
  122. }, nil
  123. }
  124. // StartCoordinator starts the coordinator
  125. func (n *Node) StartCoordinator() {
  126. log.Info("Starting Coordinator...")
  127. n.stopForge = make(chan bool)
  128. n.stopGetProofCallForge = make(chan bool)
  129. n.stopForgeCallConfirm = make(chan bool)
  130. n.stoppedForge = make(chan bool)
  131. n.stoppedGetProofCallForge = make(chan bool)
  132. n.stoppedForgeCallConfirm = make(chan bool)
  133. queueSize := 1
  134. batchCh0 := make(chan *coordinator.BatchInfo, queueSize)
  135. batchCh1 := make(chan *coordinator.BatchInfo, queueSize)
  136. go func() {
  137. defer func() { n.stoppedForge <- true }()
  138. for {
  139. select {
  140. case <-n.stopForge:
  141. return
  142. default:
  143. if forge, err := n.coord.ForgeLoopFn(batchCh0, n.stopForge); err == coordinator.ErrStop {
  144. return
  145. } else if err != nil {
  146. log.Errorw("Coordinator.ForgeLoopFn", "error", err)
  147. } else if !forge {
  148. time.Sleep(n.coordCfg.ForgeLoopInterval.Duration)
  149. }
  150. }
  151. }
  152. }()
  153. go func() {
  154. defer func() { n.stoppedGetProofCallForge <- true }()
  155. for {
  156. select {
  157. case <-n.stopGetProofCallForge:
  158. return
  159. default:
  160. if err := n.coord.GetProofCallForgeLoopFn(
  161. batchCh0, batchCh1, n.stopGetProofCallForge); err == coordinator.ErrStop {
  162. return
  163. } else if err != nil {
  164. log.Errorw("Coordinator.GetProofCallForgeLoopFn", "error", err)
  165. }
  166. }
  167. }
  168. }()
  169. go func() {
  170. defer func() { n.stoppedForgeCallConfirm <- true }()
  171. for {
  172. select {
  173. case <-n.stopForgeCallConfirm:
  174. return
  175. default:
  176. if err := n.coord.ForgeCallConfirmLoopFn(
  177. batchCh1, n.stopForgeCallConfirm); err == coordinator.ErrStop {
  178. return
  179. } else if err != nil {
  180. log.Errorw("Coordinator.ForgeCallConfirmLoopFn", "error", err)
  181. }
  182. }
  183. }
  184. }()
  185. }
  186. // StopCoordinator stops the coordinator
  187. func (n *Node) StopCoordinator() {
  188. log.Info("Stopping Coordinator...")
  189. n.stopForge <- true
  190. n.stopGetProofCallForge <- true
  191. n.stopForgeCallConfirm <- true
  192. <-n.stoppedForge
  193. <-n.stoppedGetProofCallForge
  194. <-n.stoppedForgeCallConfirm
  195. }
  196. // StartSynchronizer starts the synchronizer
  197. func (n *Node) StartSynchronizer() {
  198. log.Info("Starting Synchronizer...")
  199. n.stopSync = make(chan bool)
  200. n.stoppedSync = make(chan bool)
  201. go func() {
  202. defer func() { n.stoppedSync <- true }()
  203. for {
  204. select {
  205. case <-n.stopSync:
  206. log.Info("Coordinator stopped")
  207. return
  208. case <-time.After(n.cfg.Synchronizer.SyncLoopInterval.Duration):
  209. if err := n.sync.Sync(context.TODO()); err != nil {
  210. log.Errorw("Synchronizer.Sync", "error", err)
  211. }
  212. }
  213. }
  214. }()
  215. }
  216. // StopSynchronizer stops the synchronizer
  217. func (n *Node) StopSynchronizer() {
  218. log.Info("Stopping Synchronizer...")
  219. n.stopSync <- true
  220. <-n.stoppedSync
  221. }
  222. // Start the node
  223. func (n *Node) Start() {
  224. log.Infow("Starting node...", "mode", n.mode)
  225. if n.mode == ModeCoordinator {
  226. n.StartCoordinator()
  227. }
  228. n.StartSynchronizer()
  229. }
  230. // Stop the node
  231. func (n *Node) Stop() {
  232. log.Infow("Stopping node...")
  233. if n.mode == ModeCoordinator {
  234. n.StopCoordinator()
  235. }
  236. n.StopSynchronizer()
  237. }