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.

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