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.

257 lines
6.5 KiB

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