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.

166 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. type ZKInputs struct{} // TMP
  48. func (bb *BatchBuilder) BuildBatch(configBatch ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.L2Tx, tokenIDs []common.TokenID) (*ZKInputs, error) {
  49. for _, tx := range l1usertxs {
  50. bb.processL1Tx(tx)
  51. }
  52. for _, tx := range l1coordinatortxs {
  53. bb.processL1Tx(tx)
  54. }
  55. for _, tx := range l2txs {
  56. switch tx.Type {
  57. case common.TxTypeTransfer:
  58. // go to the MT leaf of sender and receiver, and update
  59. // balance & nonce
  60. bb.applyTransfer(tx.Tx)
  61. case common.TxTypeExit:
  62. // execute exit flow
  63. default:
  64. }
  65. }
  66. return nil, nil
  67. }
  68. func (bb *BatchBuilder) processL1Tx(tx common.L1Tx) error {
  69. switch tx.Type {
  70. case common.TxTypeForceTransfer, common.TxTypeTransfer:
  71. // go to the MT leaf of sender and receiver, and update balance
  72. // & nonce
  73. bb.applyTransfer(tx.Tx)
  74. case common.TxTypeCreateAccountDeposit:
  75. // add new leaf to the MT, update balance of the MT leaf
  76. bb.applyCreateLeaf(tx)
  77. case common.TxTypeDeposit:
  78. // update balance of the MT leaf
  79. bb.applyDeposit(tx)
  80. case common.TxTypeDepositAndTransfer:
  81. // update balance in MT leaf, update balance & nonce of sender
  82. // & receiver
  83. bb.applyDeposit(tx) // this after v0, can be done by bb.applyDepositAndTransfer in a single step
  84. bb.applyTransfer(tx.Tx)
  85. case common.TxTypeCreateAccountDepositAndTransfer:
  86. // add new leaf to the merkletree, update balance in MT leaf,
  87. // update balance & nonce of sender & receiver
  88. bb.applyCreateLeaf(tx)
  89. bb.applyTransfer(tx.Tx)
  90. case common.TxTypeExit:
  91. // execute exit flow
  92. default:
  93. }
  94. return nil
  95. }
  96. // applyCreateLeaf creates a new leaf in the leaf of the depositer, it stores
  97. // the deposit value
  98. func (bb *BatchBuilder) applyCreateLeaf(tx common.L1Tx) error {
  99. k := big.NewInt(int64(bb.idx + 1))
  100. leaf := common.Leaf{
  101. TokenID: tx.TokenID,
  102. Nonce: 0, // TODO check always that a new leaf is created nonce is at 0
  103. Balance: tx.LoadAmount,
  104. Ax: tx.FromBJJ.X,
  105. Ay: tx.FromBJJ.Y,
  106. EthAddr: tx.FromEthAddr,
  107. }
  108. v, err := leaf.HashValue()
  109. if err != nil {
  110. return err
  111. }
  112. // store at the DB the key: v, and value: leaf.Bytes()
  113. dbTx, err := bb.mt.DB().NewTx()
  114. if err != nil {
  115. return err
  116. }
  117. leafBytes, err := leaf.Bytes()
  118. if err != nil {
  119. return err
  120. }
  121. dbTx.Put(v.Bytes(), leafBytes[:])
  122. // Add k & v into the MT
  123. err = bb.mt.Add(k, v)
  124. if err != nil {
  125. return err
  126. }
  127. // if everything is fine, increment idx
  128. bb.idx = bb.idx + 1
  129. return nil
  130. }
  131. // applyDeposit updates the balance in the leaf of the depositer
  132. func (bb *BatchBuilder) applyDeposit(tx common.L1Tx) error {
  133. return nil
  134. }
  135. // applyTransfer updates the balance & nonce in the leaf of the sender, and the
  136. // balance in the leaf of the receiver
  137. func (bb *BatchBuilder) applyTransfer(tx common.Tx) error {
  138. return nil
  139. }