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