hermez-node/batchbuilder/batchbuilder.go
batchbuilder/batchbuilder.goRaw
1package batchbuilder
2
3import (
4 ethCommon "github.com/ethereum/go-ethereum/common"
5 "github.com/hermeznetwork/hermez-node/common"
6 "github.com/hermeznetwork/hermez-node/db/statedb"
7)
8
9// ConfigCircuit contains the circuit configuration
10type ConfigCircuit struct {
11 TxsMax uint64
12 L1TxsMax uint64
13 SMTLevelsMax uint64
14}
15
16// BatchBuilder implements the batch builder type, which contains the
17// functionalities
18type BatchBuilder struct {
19 localStateDB *statedb.LocalStateDB
20 configCircuits []ConfigCircuit
21}
22
23// ConfigBatch contains the batch configuration
24type ConfigBatch struct {
25 ForgerAddress ethCommon.Address
26}
27
28// NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
29// method
30func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
31 localStateDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, statedb.TypeBatchBuilder, int(nLevels))
32 if err != nil {
33 return nil, err
34 }
35
36 bb := BatchBuilder{
37 localStateDB: localStateDB,
38 configCircuits: configCircuits,
39 }
40
41 err = bb.Reset(batchNum, true)
42 return &bb, 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, l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
55 zkInputs, _, err := bb.localStateDB.ProcessTxs(coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
56 if err != nil {
57 return nil, err
58 }
59 err = bb.localStateDB.MakeCheckpoint()
60 return zkInputs, err
61}