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