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.

66 lines
2.2 KiB

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