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.

61 lines
2.0 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. )
  7. // ConfigCircuit contains the circuit configuration
  8. type ConfigCircuit struct {
  9. TxsMax uint64
  10. L1TxsMax uint64
  11. SMTLevelsMax uint64
  12. }
  13. // BatchBuilder implements the batch builder type, which contains the
  14. // functionalities
  15. type BatchBuilder struct {
  16. localStateDB *statedb.LocalStateDB
  17. configCircuits []ConfigCircuit
  18. }
  19. // ConfigBatch contains the batch configuration
  20. type ConfigBatch struct {
  21. ForgerAddress ethCommon.Address
  22. }
  23. // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
  24. // method
  25. func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
  26. localStateDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, true, int(nLevels))
  27. if err != nil {
  28. return nil, err
  29. }
  30. bb := BatchBuilder{
  31. localStateDB: localStateDB,
  32. configCircuits: configCircuits,
  33. }
  34. err = bb.Reset(batchNum, true)
  35. return &bb, 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(configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.L2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
  46. zkInputs, _, err := bb.localStateDB.ProcessTxs(false, l1usertxs, l1coordinatortxs, l2txs)
  47. if err != nil {
  48. return nil, err
  49. }
  50. err = bb.localStateDB.MakeCheckpoint()
  51. return zkInputs, err
  52. }