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.

161 lines
4.2 KiB

  1. package batchbuilder
  2. import (
  3. "math/big"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/iden3/go-merkletree"
  7. "github.com/iden3/go-merkletree/db"
  8. "github.com/iden3/go-merkletree/db/memory"
  9. )
  10. type ConfigCircuit struct {
  11. TxsMax uint64
  12. L1TxsMax uint64
  13. SMTLevelsMax uint64
  14. }
  15. type BatchBuilder struct {
  16. StateDB db.Storage // where the MTs will be stored by the Synchronizer
  17. idx uint64
  18. mt *merkletree.MerkleTree
  19. configCircuits []ConfigCircuit
  20. }
  21. type ConfigBatch struct {
  22. CoordinatorAddress ethCommon.Address
  23. }
  24. // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
  25. // method
  26. func NewBatchBuilder(stateDB db.Storage, configCircuits []ConfigCircuit, batchNum int, idx, nLevels uint64) (*BatchBuilder, error) {
  27. localMt, err := merkletree.NewMerkleTree(memory.NewMemoryStorage(), int(nLevels))
  28. if err != nil {
  29. return nil, err
  30. }
  31. bb := BatchBuilder{
  32. StateDB: stateDB,
  33. idx: idx,
  34. mt: localMt,
  35. configCircuits: configCircuits,
  36. }
  37. bb.Reset(batchNum, idx, true)
  38. return &bb, nil
  39. }
  40. // Reset tells the BatchBuilder to reset it's internal state to the required
  41. // `batchNum`. If `fromSynchronizer` is true, the BatchBuilder must take a
  42. // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
  43. // it can just roll back the internal copy.
  44. func (bb *BatchBuilder) Reset(batchNum int, idx uint64, fromSynchronizer bool) error {
  45. return nil
  46. }
  47. func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
  48. for _, tx := range l1usertxs {
  49. bb.processL1Tx(tx)
  50. }
  51. for _, tx := range l1coordinatortxs {
  52. bb.processL1Tx(tx)
  53. }
  54. for _, tx := range l2txs {
  55. switch tx.Type {
  56. case common.TxTypeTransfer:
  57. // go to the MT leaf of sender and receiver, and update
  58. // balance & nonce
  59. bb.applyTransfer(tx.Tx)
  60. case common.TxTypeExit:
  61. // execute exit flow
  62. default:
  63. }
  64. }
  65. return nil, nil
  66. }
  67. func (bb *BatchBuilder) processL1Tx(tx common.L1Tx) error {
  68. switch tx.Type {
  69. case common.TxTypeForceTransfer, common.TxTypeTransfer:
  70. // go to the MT leaf of sender and receiver, and update balance
  71. // & nonce
  72. bb.applyTransfer(tx.Tx)
  73. case common.TxTypeCreateAccountDeposit:
  74. // add new leaf to the MT, update balance of the MT leaf
  75. bb.applyCreateLeaf(tx)
  76. case common.TxTypeDeposit:
  77. // update balance of the MT leaf
  78. bb.applyDeposit(tx)
  79. case common.TxTypeDepositAndTransfer:
  80. // update balance in MT leaf, update balance & nonce of sender
  81. // & receiver
  82. bb.applyDeposit(tx) // this after v0, can be done by bb.applyDepositAndTransfer in a single step
  83. bb.applyTransfer(tx.Tx)
  84. case common.TxTypeCreateAccountDepositAndTransfer:
  85. // add new leaf to the merkletree, update balance in MT leaf,
  86. // update balance & nonce of sender & receiver
  87. bb.applyCreateLeaf(tx)
  88. bb.applyTransfer(tx.Tx)
  89. case common.TxTypeExit:
  90. // execute exit flow
  91. default:
  92. }
  93. return nil
  94. }
  95. // applyCreateLeaf creates a new leaf in the leaf of the depositer, it stores
  96. // the deposit value
  97. func (bb *BatchBuilder) applyCreateLeaf(tx common.L1Tx) error {
  98. k := big.NewInt(int64(bb.idx + 1))
  99. leaf := common.Leaf{
  100. TokenID: tx.TokenID,
  101. Nonce: 0, // TODO check always that a new leaf is created nonce is at 0
  102. Balance: tx.LoadAmount,
  103. Ax: tx.FromBJJ.X,
  104. Ay: tx.FromBJJ.Y,
  105. EthAddr: tx.FromEthAddr,
  106. }
  107. v, err := leaf.Value()
  108. if err != nil {
  109. return err
  110. }
  111. // store at the DB the key: v, and value: leaf.Bytes()
  112. dbTx, err := bb.mt.DB().NewTx()
  113. if err != nil {
  114. return err
  115. }
  116. leafBytes := leaf.Bytes()
  117. dbTx.Put(v.Bytes(), leafBytes[:])
  118. // Add k & v into the MT
  119. err = bb.mt.Add(k, v)
  120. if err != nil {
  121. return err
  122. }
  123. // if everything is fine, increment idx
  124. bb.idx = bb.idx + 1
  125. return nil
  126. }
  127. // applyDeposit updates the balance in the leaf of the depositer
  128. func (bb *BatchBuilder) applyDeposit(tx common.L1Tx) error {
  129. return nil
  130. }
  131. // applyTransfer updates the balance & nonce in the leaf of the sender, and the
  132. // balance in the leaf of the receiver
  133. func (bb *BatchBuilder) applyTransfer(tx common.Tx) error {
  134. return nil
  135. }