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