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.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, 128, synchronizerStateDB,
  28. statedb.TypeBatchBuilder, int(nLevels))
  29. if err != nil {
  30. return nil, tracerr.Wrap(err)
  31. }
  32. bb := BatchBuilder{
  33. localStateDB: localStateDB,
  34. configCircuits: configCircuits,
  35. }
  36. err = bb.Reset(batchNum, true)
  37. return &bb, tracerr.Wrap(err)
  38. }
  39. // Reset tells the BatchBuilder to reset it's internal state to the required
  40. // `batchNum`. If `fromSynchronizer` is true, the BatchBuilder must take a
  41. // copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
  42. // it can just roll back the internal copy.
  43. func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
  44. return bb.localStateDB.Reset(batchNum, fromSynchronizer)
  45. }
  46. // BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
  47. func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
  48. //nolint:gomnd
  49. ptc := statedb.ProcessTxsConfig{ // TODO TMP
  50. NLevels: 32,
  51. MaxFeeTx: 64,
  52. MaxTx: 512,
  53. MaxL1Tx: 64,
  54. }
  55. ptOut, err := bb.localStateDB.ProcessTxs(ptc, coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
  56. return ptOut.ZKInputs, tracerr.Wrap(err)
  57. }
  58. // LocalStateDB returns the underlying LocalStateDB
  59. func (bb *BatchBuilder) LocalStateDB() *statedb.LocalStateDB {
  60. return bb.localStateDB
  61. }