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.

71 lines
2.4 KiB

  1. package batchbuilder
  2. import (
  3. ethCommon "github.com/ethereum/go-ethereum/common"
  4. "github.com/hermeznetwork/hermez-node/common"
  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. configCircuits []ConfigCircuit
  20. }
  21. // ConfigBatch contains the batch configuration
  22. type ConfigBatch struct {
  23. ForgerAddress ethCommon.Address
  24. TxProcessorConfig txprocessor.Config
  25. }
  26. // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
  27. // method
  28. func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
  29. localStateDB, err := statedb.NewLocalStateDB(dbpath, 128, synchronizerStateDB,
  30. statedb.TypeBatchBuilder, int(nLevels))
  31. if err != nil {
  32. return nil, tracerr.Wrap(err)
  33. }
  34. bb := BatchBuilder{
  35. localStateDB: localStateDB,
  36. configCircuits: configCircuits,
  37. }
  38. err = bb.Reset(batchNum, true)
  39. return &bb, tracerr.Wrap(err)
  40. }
  41. // Reset tells the BatchBuilder to reset it's internal state to the required
  42. // `batchNum`. If `fromSynchronizer` is true, the BatchBuilder must take a
  43. // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
  44. // it can just roll back the internal copy.
  45. func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
  46. return bb.localStateDB.Reset(batchNum, fromSynchronizer)
  47. }
  48. // BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
  49. func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs,
  50. l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx,
  51. tokenIDs []common.TokenID) (*common.ZKInputs, error) {
  52. bbStateDB := bb.localStateDB.StateDB
  53. tp := txprocessor.NewTxProcessor(bbStateDB, configBatch.TxProcessorConfig)
  54. ptOut, err := tp.ProcessTxs(coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
  55. return ptOut.ZKInputs, tracerr.Wrap(err)
  56. }
  57. // LocalStateDB returns the underlying LocalStateDB
  58. func (bb *BatchBuilder) LocalStateDB() *statedb.LocalStateDB {
  59. return bb.localStateDB
  60. }