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.

226 lines
6.4 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 poolL2Txs []*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, poolL2Txs, 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. poolL2Txs, 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 poolL2Txs 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(poolL2Txs)
  111. if err != nil {
  112. return err
  113. }
  114. // 3. Save metadata from TxSelector output for BatchNum
  115. batchInfo.SetTxsInfo(l1UserTxsExtra, l1OperatorTxs, poolL2Txs) // TODO feesInfo
  116. // 4. Call BatchBuilder with TxSelector output
  117. configBatch := &batchbuilder.ConfigBatch{
  118. ForgerAddress: c.config.ForgerAddress,
  119. }
  120. l2Txs := common.PoolL2TxsToL2Txs(poolL2Txs)
  121. zkInputs, err := c.batchBuilder.BuildBatch(configBatch, l1UserTxsExtra, l1OperatorTxs, l2Txs, nil) // TODO []common.TokenID --> feesInfo
  122. if err != nil {
  123. return err
  124. }
  125. // 5. Save metadata from BatchBuilder output for BatchNum
  126. batchInfo.SetZKInputs(zkInputs)
  127. // 6. Call an idle server proof with BatchBuilder output, save server proof info for batchNum
  128. err = batchInfo.serverProof.CalculateProof(zkInputs)
  129. if err != nil {
  130. return err
  131. }
  132. c.batchQueue.Push(&batchInfo)
  133. return nil
  134. }
  135. // proveSequence gets the generated zkProof & sends it to the SmartContract
  136. func (c *Coordinator) proveSequence() error {
  137. batchInfo := c.batchQueue.Pop()
  138. if batchInfo == nil {
  139. // no batches in queue, return
  140. return common.ErrBatchQueueEmpty
  141. }
  142. serverProofInfo := batchInfo.serverProof
  143. proof, err := serverProofInfo.GetProof() // blocking call, until not resolved don't continue. Returns when the proof server has calculated the proof
  144. if err != nil {
  145. return err
  146. }
  147. batchInfo.SetProof(proof)
  148. callData := c.prepareCallDataForge(batchInfo)
  149. _, err = c.ethClient.ForgeCall(callData)
  150. if err != nil {
  151. return err
  152. }
  153. // TODO once tx data type is defined, store ethTx (returned by ForgeCall)
  154. // TBD if use ethTxStore as a disk k-v database, or use a Queue
  155. // tx, err := c.ethTxStore.NewTx()
  156. // if err != nil {
  157. // return err
  158. // }
  159. // tx.Put(ethTx.Hash(), ethTx.Bytes())
  160. // if err := tx.Commit(); err!=nil {
  161. // return nil
  162. // }
  163. return nil
  164. }
  165. func (c *Coordinator) forgeConfirmationSequence() error {
  166. // TODO strategy of this sequence TBD
  167. // confirm eth txs and mark them as accepted sequence
  168. // ethTx := ethTxStore.GetFirstPending()
  169. // waitForAccepted(ethTx) // blocking call, returns once the ethTx is mined
  170. // ethTxStore.MarkAccepted(ethTx)
  171. return nil
  172. }
  173. func (c *Coordinator) handleReorg() error {
  174. return nil
  175. }
  176. // isForgeSequence returns true if the node is the Forger in the current ethereum block
  177. func (c *Coordinator) isForgeSequence() bool {
  178. return false
  179. }
  180. func (c *Coordinator) purgeRemoveByTimeout() error {
  181. return nil
  182. }
  183. func (c *Coordinator) purgeInvalidDueToL2TxsSelection(l2Txs []*common.PoolL2Tx) error {
  184. return nil
  185. }
  186. func (c *Coordinator) shouldL1L2Batch() bool {
  187. return false
  188. }
  189. func (c *Coordinator) prepareCallDataForge(batchInfo *BatchInfo) *common.CallDataForge {
  190. return nil
  191. }