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