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	TxProcessorConfig txprocessor.Config
29}
30
31// NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset
32// method
33func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) {
34	localStateDB, err := statedb.NewLocalStateDB(dbpath, 128, synchronizerStateDB,
35		statedb.TypeBatchBuilder, int(nLevels))
36	if err != nil {
37		return nil, tracerr.Wrap(err)
38	}
39
40	bb := BatchBuilder{
41		localStateDB:   localStateDB,
42		configCircuits: configCircuits,
43	}
44
45	err = bb.Reset(batchNum, true)
46	return &bb, tracerr.Wrap(err)
47}
48
49// Reset tells the BatchBuilder to reset it's internal state to the required
50// `batchNum`.  If `fromSynchronizer` is true, the BatchBuilder must take a
51// copy of the rollup state from the Synchronizer at that `batchNum`, otherwise
52// it can just roll back the internal copy.
53func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) error {
54	return bb.localStateDB.Reset(batchNum, fromSynchronizer)
55}
56
57// BuildBatch takes the transactions and returns the common.ZKInputs of the next batch
58func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBatch, l1usertxs,
59	l1coordinatortxs []common.L1Tx, pooll2txs []common.PoolL2Tx,
60	tokenIDs []common.TokenID) (*common.ZKInputs, error) {
61	bbStateDB := bb.localStateDB.StateDB
62	tp := txprocessor.NewTxProcessor(bbStateDB, configBatch.TxProcessorConfig)
63
64	ptOut, err := tp.ProcessTxs(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}