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.

70 lines
2.3 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/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. configCircuits []ConfigCircuit
  19. }
  20. // ConfigBatch contains the batch configuration
  21. type ConfigBatch struct {
  22. ForgerAddress ethCommon.Address
  23. }
  24. // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
  25. // method
  26. func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
  27. localStateDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, statedb.TypeBatchBuilder, int(nLevels))
  28. if err != nil {
  29. return nil, tracerr.Wrap(err)
  30. }
  31. bb := BatchBuilder{
  32. localStateDB: localStateDB,
  33. configCircuits: configCircuits,
  34. }
  35. err = bb.Reset(batchNum, true)
  36. return &bb, tracerr.Wrap(err)
  37. }
  38. // Reset tells the BatchBuilder to reset it's internal state to the required
  39. // `batchNum`. If `fromSynchronizer` is true, the BatchBuilder must take a
  40. // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
  41. // it can just roll back the internal copy.
  42. func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
  43. return bb.localStateDB.Reset(batchNum, fromSynchronizer)
  44. }
  45. // BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
  46. func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
  47. //nolint:gomnd
  48. ptc := statedb.ProcessTxsConfig{ // TODO TMP
  49. NLevels: 32,
  50. MaxFeeTx: 64,
  51. MaxTx: 512,
  52. MaxL1Tx: 64,
  53. }
  54. ptOut, err := bb.localStateDB.ProcessTxs(ptc, 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. }