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.

73 lines
2.3 KiB

  1. package batchbuilder
  2. import (
  3. "github.com/hermeznetwork/hermez-node/common"
  4. "github.com/hermeznetwork/hermez-node/db/kvdb"
  5. "github.com/hermeznetwork/hermez-node/db/statedb"
  6. "github.com/hermeznetwork/hermez-node/txprocessor"
  7. "github.com/hermeznetwork/tracerr"
  8. )
  9. // ConfigCircuit contains the circuit configuration
  10. type ConfigCircuit struct {
  11. TxsMax uint64
  12. L1TxsMax uint64
  13. SMTLevelsMax uint64
  14. }
  15. // BatchBuilder implements the batch builder type, which contains the
  16. // functionalities
  17. type BatchBuilder struct {
  18. localStateDB *statedb.LocalStateDB
  19. }
  20. // ConfigBatch contains the batch configuration
  21. type ConfigBatch struct {
  22. TxProcessorConfig txprocessor.Config
  23. }
  24. // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
  25. // method
  26. func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
  27. localStateDB, err := statedb.NewLocalStateDB(
  28. statedb.Config{
  29. Path: dbpath,
  30. Keep: kvdb.DefaultKeep,
  31. Type: statedb.TypeBatchBuilder,
  32. NLevels: int(nLevels),
  33. },
  34. synchronizerStateDB)
  35. if err != nil {
  36. return nil, tracerr.Wrap(err)
  37. }
  38. bb := BatchBuilder{
  39. localStateDB: localStateDB,
  40. }
  41. err = bb.Reset(batchNum, true)
  42. return &bb, tracerr.Wrap(err)
  43. }
  44. // Reset tells the BatchBuilder to reset it's internal state to the required
  45. // `batchNum`. If `fromSynchronizer` is true, the BatchBuilder must take a
  46. // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
  47. // it can just roll back the internal copy.
  48. func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
  49. return bb.localStateDB.Reset(batchNum, fromSynchronizer)
  50. }
  51. // BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
  52. func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs,
  53. l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx) (*common.ZKInputs, error) {
  54. bbStateDB := bb.localStateDB.StateDB
  55. tp := txprocessor.NewTxProcessor(bbStateDB, configBatch.TxProcessorConfig)
  56. ptOut, err := tp.ProcessTxs(coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
  57. return ptOut.ZKInputs, tracerr.Wrap(err)
  58. }
  59. // LocalStateDB returns the underlying LocalStateDB
  60. func (bb *BatchBuilder) LocalStateDB() *statedb.LocalStateDB {
  61. return bb.localStateDB
  62. }