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, synchronizerStateDB, statedb.TypeBatchBuilder, int(nLevels))
33	if err != nil {
34		return nil, tracerr.Wrap(err)
35	}
36
37	bb := BatchBuilder{
38		localStateDB:   localStateDB,
39		configCircuits: configCircuits,
40	}
41
42	err = bb.Reset(batchNum, true)
43	return &bb, tracerr.Wrap(err)
44}
45
46// Reset tells the BatchBuilder to reset it's internal state to the required
47// `batchNum`.  If `fromSynchronizer` is true, the BatchBuilder must take a
48// copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
49// it can just roll back the internal copy.
50func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
51	return bb.localStateDB.Reset(batchNum, fromSynchronizer)
52}
53
54// BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
55func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) {
56	//nolint:gomnd
57	ptc := statedb.ProcessTxsConfig{ // TODO TMP
58		NLevels:  32,
59		MaxFeeTx: 64,
60		MaxTx:    512,
61		MaxL1Tx:  64,
62	}
63	ptOut, err := bb.localStateDB.ProcessTxs(ptc, coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
64	return ptOut.ZKInputs, tracerr.Wrap(err)
65}
66
67// LocalStateDB returns the underlying LocalStateDB
68func (bb *BatchBuilder) LocalStateDB() *statedb.LocalStateDB {
69	return bb.localStateDB
70}