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.

225 lines
6.3 KiB

  1. package coordinator
  2. import (
  3. "time"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/hermeznetwork/hermez-node/batchbuilder"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/eth"
  8. "github.com/hermeznetwork/hermez-node/txselector"
  9. kvdb "github.com/iden3/go-merkletree/db"
  10. "github.com/iden3/go-merkletree/db/memory"
  11. )
  12. // CoordinatorConfig contains the Coordinator configuration
  13. type CoordinatorConfig struct {
  14. ForgerAddress ethCommon.Address
  15. }
  16. // Coordinator implements the Coordinator type
  17. type Coordinator struct {
  18. config CoordinatorConfig
  19. batchNum uint64
  20. batchQueue *BatchQueue
  21. serverProofPool ServerProofPool
  22. // synchronizer *synchronizer.Synchronizer
  23. txsel *txselector.TxSelector
  24. batchBuilder *batchbuilder.BatchBuilder
  25. ethClient *eth.Client
  26. ethTxStore kvdb.Storage
  27. }
  28. // NewCoordinator creates a new Coordinator
  29. func NewCoordinator() *Coordinator { // once synchronizer is ready, synchronizer.Synchronizer will be passed as parameter here
  30. var c *Coordinator
  31. // c.ethClient = eth.NewClient() // TBD
  32. c.ethTxStore = memory.NewMemoryStorage()
  33. return c
  34. }
  35. // Start starts the Coordinator service
  36. func (c *Coordinator) Start() {
  37. // TODO TBD note: the sequences & loops & errors & logging & goroutines
  38. // & channels approach still needs to be defined, the current code is a
  39. // wip draft
  40. // TBD: goroutines strategy
  41. // if in Forge Sequence:
  42. if c.isForgeSequence() {
  43. // c.batchNum = c.synchronizer.LastBatchNum()
  44. _ = c.txsel.Reset(c.batchNum)
  45. _ = c.batchBuilder.Reset(c.batchNum, true)
  46. c.batchQueue = NewBatchQueue()
  47. go func() {
  48. for {
  49. _ = c.forgeSequence()
  50. time.Sleep(1 * time.Second)
  51. }
  52. }()
  53. go func() {
  54. for {
  55. _ = c.proveSequence()
  56. time.Sleep(1 * time.Second)
  57. }
  58. }()
  59. go func() {
  60. for {
  61. _ = c.forgeConfirmationSequence()
  62. time.Sleep(1 * time.Second)
  63. }
  64. }()
  65. }
  66. }
  67. func (c *Coordinator) forgeSequence() error {
  68. // TODO once synchronizer has this method ready:
  69. // If there's been a reorg, handle it
  70. // handleReorg() function decides if the reorg must restart the pipeline or not
  71. // if c.synchronizer.Reorg():
  72. _ = c.handleReorg()
  73. // 0. If there's an available server proof: Start pipeline for batchNum = batchNum + 1
  74. serverProofInfo, err := c.serverProofPool.GetNextAvailable() // blocking call, returns when a server proof is available
  75. if err != nil {
  76. return err
  77. }
  78. // remove transactions from the pool that have been there for too long
  79. err = c.purgeRemoveByTimeout()
  80. if err != nil {
  81. return err
  82. }
  83. c.batchNum = c.batchNum + 1
  84. batchInfo := NewBatchInfo(c.batchNum, serverProofInfo) // to accumulate metadata of the batch
  85. var l2Txs []*common.PoolL2Tx
  86. // var feesInfo
  87. var l1UserTxsExtra, l1OperatorTxs []*common.L1Tx
  88. // 1. Decide if we forge L2Tx or L1+L2Tx
  89. if c.shouldL1L2Batch() {
  90. // 2a: L1+L2 txs
  91. // l1UserTxs, toForgeL1TxsNumber := c.synchronizer.GetNextL1UserTxs() // TODO once synchronizer is ready, uncomment
  92. var l1UserTxs []*common.L1Tx = nil // tmp, depends on synchronizer
  93. l1UserTxsExtra, l1OperatorTxs, l2Txs, err = c.txsel.GetL1L2TxSelection(c.batchNum, l1UserTxs) // TODO once feesInfo is added to method return, add the var
  94. if err != nil {
  95. return err
  96. }
  97. } else {
  98. // 2b: only L2 txs
  99. l2Txs, err = c.txsel.GetL2TxSelection(c.batchNum) // TODO once feesInfo is added to method return, add the var
  100. if err != nil {
  101. return err
  102. }
  103. l1UserTxsExtra = nil
  104. l1OperatorTxs = nil
  105. }
  106. // Run purger to invalidate transactions that become invalid beause of
  107. // the l2Txs selected. Will mark as invalid the txs that have a
  108. // (fromIdx, nonce) which already appears in the selected txs (includes
  109. // all the nonces smaller than the current one)
  110. err = c.purgeInvalidDueToL2TxsSelection(l2Txs)
  111. if err != nil {
  112. return err
  113. }
  114. // 3. Save metadata from TxSelector output for BatchNum
  115. batchInfo.SetTxsInfo(l1UserTxsExtra, l1OperatorTxs, l2Txs) // TODO feesInfo
  116. // 4. Call BatchBuilder with TxSelector output
  117. configBatch := &batchbuilder.ConfigBatch{
  118. ForgerAddress: c.config.ForgerAddress,
  119. }
  120. zkInputs, err := c.batchBuilder.BuildBatch(configBatch, l1UserTxsExtra, l1OperatorTxs, l2Txs, nil) // TODO []common.TokenID --> feesInfo
  121. if err != nil {
  122. return err
  123. }
  124. // 5. Save metadata from BatchBuilder output for BatchNum
  125. batchInfo.SetZKInputs(zkInputs)
  126. // 6. Call an idle server proof with BatchBuilder output, save server proof info for batchNum
  127. err = batchInfo.serverProof.CalculateProof(zkInputs)
  128. if err != nil {
  129. return err
  130. }
  131. c.batchQueue.Push(&batchInfo)
  132. return nil
  133. }
  134. // proveSequence gets the generated zkProof & sends it to the SmartContract
  135. func (c *Coordinator) proveSequence() error {
  136. batchInfo := c.batchQueue.Pop()
  137. if batchInfo == nil {
  138. // no batches in queue, return
  139. return common.ErrBatchQueueEmpty
  140. }
  141. serverProofInfo := batchInfo.serverProof
  142. proof, err := serverProofInfo.GetProof() // blocking call, until not resolved don't continue. Returns when the proof server has calculated the proof
  143. if err != nil {
  144. return err
  145. }
  146. batchInfo.SetProof(proof)
  147. callData := c.prepareCallDataForge(batchInfo)
  148. _, err = c.ethClient.ForgeCall(callData)
  149. if err != nil {
  150. return err
  151. }
  152. // TODO once tx data type is defined, store ethTx (returned by ForgeCall)
  153. // TBD if use ethTxStore as a disk k-v database, or use a Queue
  154. // tx, err := c.ethTxStore.NewTx()
  155. // if err != nil {
  156. // return err
  157. // }
  158. // tx.Put(ethTx.Hash(), ethTx.Bytes())
  159. // if err := tx.Commit(); err!=nil {
  160. // return nil
  161. // }
  162. return nil
  163. }
  164. func (c *Coordinator) forgeConfirmationSequence() error {
  165. // TODO strategy of this sequence TBD
  166. // confirm eth txs and mark them as accepted sequence
  167. // ethTx := ethTxStore.GetFirstPending()
  168. // waitForAccepted(ethTx) // blocking call, returns once the ethTx is mined
  169. // ethTxStore.MarkAccepted(ethTx)
  170. return nil
  171. }
  172. func (c *Coordinator) handleReorg() error {
  173. return nil
  174. }
  175. // isForgeSequence returns true if the node is the Forger in the current ethereum block
  176. func (c *Coordinator) isForgeSequence() bool {
  177. return false
  178. }
  179. func (c *Coordinator) purgeRemoveByTimeout() error {
  180. return nil
  181. }
  182. func (c *Coordinator) purgeInvalidDueToL2TxsSelection(l2Txs []*common.PoolL2Tx) error {
  183. return nil
  184. }
  185. func (c *Coordinator) shouldL1L2Batch() bool {
  186. return false
  187. }
  188. func (c *Coordinator) prepareCallDataForge(batchInfo *BatchInfo) *common.CallDataForge {
  189. return nil
  190. }