From 3374a4754d6a5386a057f184a19d981ae59c2dcc Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 1 Oct 2020 17:59:50 +0200 Subject: [PATCH] Add StateDB compute nonces on ProcessTx L2Tx Add StateDB compute nonces on ProcessTx L2Tx, and update StateDB for type TypeSynchronizer, TypeTxSelector, TypeBatchBuilder --- api/api_test.go | 2 +- batchbuilder/batchbuilder.go | 4 +- batchbuilder/batchbuilder_test.go | 2 +- coordinator/coordinator_test.go | 17 +++++-- db/statedb/statedb.go | 67 ++++++++++++++++--------- db/statedb/statedb_test.go | 16 +++--- db/statedb/txprocessors.go | 83 ++++++++++++++++++------------- db/statedb/txprocessors_test.go | 75 ++++++++++++++++++++++++---- db/statedb/utils_test.go | 2 +- node/node.go | 2 +- synchronizer/synchronizer.go | 6 +-- synchronizer/synchronizer_test.go | 2 +- txselector/txselector.go | 6 +-- txselector/txselector_test.go | 2 +- 14 files changed, 192 insertions(+), 94 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 1fabbf1..1ea3d4d 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -96,7 +96,7 @@ func TestMain(m *testing.M) { if err != nil { panic(err) } - sdb, err := statedb.NewStateDB(dir, false, 0) + sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0) if err != nil { panic(err) } diff --git a/batchbuilder/batchbuilder.go b/batchbuilder/batchbuilder.go index 35c5ad3..c17063f 100644 --- a/batchbuilder/batchbuilder.go +++ b/batchbuilder/batchbuilder.go @@ -28,7 +28,7 @@ type ConfigBatch struct { // NewBatchBuilder constructs a new BatchBuilder, and executes the bb.Reset // method func NewBatchBuilder(dbpath string, synchronizerStateDB *statedb.StateDB, configCircuits []ConfigCircuit, batchNum common.BatchNum, nLevels uint64) (*BatchBuilder, error) { - localStateDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, true, int(nLevels)) + localStateDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, statedb.TypeBatchBuilder, int(nLevels)) if err != nil { return nil, err } @@ -52,7 +52,7 @@ func (bb *BatchBuilder) Reset(batchNum common.BatchNum, fromSynchronizer bool) e // BuildBatch takes the transactions and returns the common.ZKInputs of the next batch func (bb *BatchBuilder) BuildBatch(configBatch *ConfigBatch, l1usertxs, l1coordinatortxs []*common.L1Tx, pooll2txs []*common.PoolL2Tx, tokenIDs []common.TokenID) (*common.ZKInputs, error) { - zkInputs, _, err := bb.localStateDB.ProcessTxs(false, true, l1usertxs, l1coordinatortxs, pooll2txs) + zkInputs, _, err := bb.localStateDB.ProcessTxs(l1usertxs, l1coordinatortxs, pooll2txs) if err != nil { return nil, err } diff --git a/batchbuilder/batchbuilder_test.go b/batchbuilder/batchbuilder_test.go index 1c6c26c..922e988 100644 --- a/batchbuilder/batchbuilder_test.go +++ b/batchbuilder/batchbuilder_test.go @@ -13,7 +13,7 @@ func TestBatchBuilder(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - synchDB, err := statedb.NewStateDB(dir, false, 0) + synchDB, err := statedb.NewStateDB(dir, statedb.TypeBatchBuilder, 0) assert.Nil(t, err) bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB") diff --git a/coordinator/coordinator_test.go b/coordinator/coordinator_test.go index 3adb136..b275425 100644 --- a/coordinator/coordinator_test.go +++ b/coordinator/coordinator_test.go @@ -21,11 +21,20 @@ import ( func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBuilder) { // FUTURE once Synchronizer is ready, should return it also nLevels := 32 - synchDB, err := ioutil.TempDir("", "tmpSynchDB") + synchDBPath, err := ioutil.TempDir("", "tmpSynchDB") require.Nil(t, err) - sdb, err := statedb.NewStateDB(synchDB, true, nLevels) + synchSdb, err := statedb.NewStateDB(synchDBPath, statedb.TypeSynchronizer, nLevels) assert.Nil(t, err) + // txselDBPath, err := ioutil.TempDir("", "tmpTxSelDB") + // require.Nil(t, err) + // bbDBPath, err := ioutil.TempDir("", "tmpBBDB") + // require.Nil(t, err) + // txselSdb, err := statedb.NewLocalStateDB(txselDBPath, synchSdb, statedb.TypeTxSelector, nLevels) + // assert.Nil(t, err) + // bbSdb, err := statedb.NewLocalStateDB(bbDBPath, synchSdb, statedb.TypeBatchBuilder, nLevels) + // assert.Nil(t, err) + pass := os.Getenv("POSTGRES_PASS") db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez") require.Nil(t, err) @@ -33,12 +42,12 @@ func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBu txselDir, err := ioutil.TempDir("", "tmpTxSelDB") require.Nil(t, err) - txsel, err := txselector.NewTxSelector(txselDir, sdb, l2DB, 10, 10, 10) + txsel, err := txselector.NewTxSelector(txselDir, synchSdb, l2DB, 10, 10, 10) assert.Nil(t, err) bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB") require.Nil(t, err) - bb, err := batchbuilder.NewBatchBuilder(bbDir, sdb, nil, 0, uint64(nLevels)) + bb, err := batchbuilder.NewBatchBuilder(bbDir, synchSdb, nil, 0, uint64(nLevels)) assert.Nil(t, err) // l1Txs, coordinatorL1Txs, poolL2Txs := test.GenerateTestTxsFromSet(t, test.SetTest0) diff --git a/db/statedb/statedb.go b/db/statedb/statedb.go index 4ab5769..084ca43 100644 --- a/db/statedb/statedb.go +++ b/db/statedb/statedb.go @@ -15,31 +15,45 @@ import ( // TODO(Edu): Document here how StateDB is kept consistent -// ErrStateDBWithoutMT is used when a method that requires a MerkleTree is -// called in a StateDB that does not have a MerkleTree defined -var ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a StateDB without MerkleTree") +var ( + // ErrStateDBWithoutMT is used when a method that requires a MerkleTree + // is called in a StateDB that does not have a MerkleTree defined + ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a StateDB without MerkleTree") -// ErrAccountAlreadyExists is used when CreateAccount is called and the Account -// already exists -var ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists") + // ErrAccountAlreadyExists is used when CreateAccount is called and the + // Account already exists + ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists") -// ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr or -// ToEthAddr&ToBJJ -var ErrToIdxNotFound = errors.New("ToIdx can not be found") + // ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr + // or ToEthAddr&ToBJJ + ErrToIdxNotFound = errors.New("ToIdx can not be found") -// KeyCurrentBatch is used as key in the db to store the current BatchNum -var KeyCurrentBatch = []byte("currentbatch") - -// PathStateDB defines the subpath of the StateDB -const PathStateDB = "/statedb" + // KeyCurrentBatch is used as key in the db to store the current BatchNum + KeyCurrentBatch = []byte("currentbatch") +) -// PathBatchNum defines the subpath of the Batch Checkpoint in the subpath of -// the StateDB -const PathBatchNum = "/BatchNum" +const ( + // PathStateDB defines the subpath of the StateDB + PathStateDB = "/statedb" + // PathBatchNum defines the subpath of the Batch Checkpoint in the + // subpath of the StateDB + PathBatchNum = "/BatchNum" + // PathCurrent defines the subpath of the current Batch in the subpath + // of the StateDB + PathCurrent = "/current" + // TypeSynchronizer defines a StateDB used by the Synchronizer, that + // generates the ExitTree when processing the txs + TypeSynchronizer = "synchronizer" + // TypeTxSelector defines a StateDB used by the TxSelector, without + // computing ExitTree neither the ZKInputs + TypeTxSelector = "txselector" + // TypeBatchBuilder defines a StateDB used by the BatchBuilder, that + // generates the ExitTree and the ZKInput when processing the txs + TypeBatchBuilder = "batchbuilder" +) -// PathCurrent defines the subpath of the current Batch in the subpath of the -// StateDB -const PathCurrent = "/current" +// TypeStateDB determines the type of StateDB +type TypeStateDB string // StateDB represents the StateDB object type StateDB struct { @@ -47,6 +61,7 @@ type StateDB struct { currentBatch common.BatchNum db *pebble.PebbleStorage mt *merkletree.MerkleTree + typ TypeStateDB // idx holds the current Idx that the BatchBuilder is using idx common.Idx zki *common.ZKInputs @@ -55,7 +70,7 @@ type StateDB struct { // NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk // storage -func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) { +func NewStateDB(path string, typ TypeStateDB, nLevels int) (*StateDB, error) { var sto *pebble.PebbleStorage var err error sto, err = pebble.NewPebbleStorage(path+PathStateDB+PathCurrent, false) @@ -64,17 +79,21 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) { } var mt *merkletree.MerkleTree = nil - if withMT { + if typ == TypeSynchronizer || typ == TypeBatchBuilder { mt, err = merkletree.NewMerkleTree(sto, nLevels) if err != nil { return nil, err } } + if typ == TypeTxSelector && nLevels != 0 { + return nil, fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0") + } sdb := &StateDB{ path: path + PathStateDB, db: sto, mt: mt, + typ: typ, } // load currentBatch @@ -382,8 +401,8 @@ type LocalStateDB struct { // NewLocalStateDB returns a new LocalStateDB connected to the given // synchronizerDB -func NewLocalStateDB(path string, synchronizerDB *StateDB, withMT bool, nLevels int) (*LocalStateDB, error) { - s, err := NewStateDB(path, withMT, nLevels) +func NewLocalStateDB(path string, synchronizerDB *StateDB, typ TypeStateDB, nLevels int) (*LocalStateDB, error) { + s, err := NewStateDB(path, typ, nLevels) if err != nil { return nil, err } diff --git a/db/statedb/statedb_test.go b/db/statedb/statedb_test.go index 25ee263..1950af6 100644 --- a/db/statedb/statedb_test.go +++ b/db/statedb/statedb_test.go @@ -38,7 +38,7 @@ func TestNewStateDBIntermediateState(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, false, 0) + sdb, err := NewStateDB(dir, TypeTxSelector, 0) assert.Nil(t, err) // test values @@ -60,7 +60,7 @@ func TestNewStateDBIntermediateState(t *testing.T) { // call NewStateDB which should get the db at the last checkpoint state // executing a Reset (discarding the last 'testkey0'&'testvalue0' data) - sdb, err = NewStateDB(dir, false, 0) + sdb, err = NewStateDB(dir, TypeTxSelector, 0) assert.Nil(t, err) v, err = sdb.db.Get(k0) assert.NotNil(t, err) @@ -102,7 +102,7 @@ func TestNewStateDBIntermediateState(t *testing.T) { // call NewStateDB which should get the db at the last checkpoint state // executing a Reset (discarding the last 'testkey1'&'testvalue1' data) - sdb, err = NewStateDB(dir, false, 0) + sdb, err = NewStateDB(dir, TypeTxSelector, 0) assert.Nil(t, err) v, err = sdb.db.Get(k0) @@ -119,7 +119,7 @@ func TestStateDBWithoutMT(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, false, 0) + sdb, err := NewStateDB(dir, TypeTxSelector, 0) assert.Nil(t, err) // create test accounts @@ -168,7 +168,7 @@ func TestStateDBWithMT(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, true, 32) + sdb, err := NewStateDB(dir, TypeSynchronizer, 32) assert.Nil(t, err) // create test accounts @@ -219,7 +219,7 @@ func TestCheckpoints(t *testing.T) { dir, err := ioutil.TempDir("", "sdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, true, 32) + sdb, err := NewStateDB(dir, TypeSynchronizer, 32) assert.Nil(t, err) // create test accounts @@ -285,7 +285,7 @@ func TestCheckpoints(t *testing.T) { // Create a LocalStateDB from the initial StateDB dirLocal, err := ioutil.TempDir("", "ldb") require.Nil(t, err) - ldb, err := NewLocalStateDB(dirLocal, sdb, true, 32) + ldb, err := NewLocalStateDB(dirLocal, sdb, TypeBatchBuilder, 32) assert.Nil(t, err) // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB) @@ -305,7 +305,7 @@ func TestCheckpoints(t *testing.T) { // Create a 2nd LocalStateDB from the initial StateDB dirLocal2, err := ioutil.TempDir("", "ldb2") require.Nil(t, err) - ldb2, err := NewLocalStateDB(dirLocal2, sdb, true, 32) + ldb2, err := NewLocalStateDB(dirLocal2, sdb, TypeBatchBuilder, 32) assert.Nil(t, err) // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB) diff --git a/db/statedb/txprocessors.go b/db/statedb/txprocessors.go index d47f274..76c951a 100644 --- a/db/statedb/txprocessors.go +++ b/db/statedb/txprocessors.go @@ -30,11 +30,13 @@ type processedExit struct { } // ProcessTxs process the given L1Txs & L2Txs applying the needed updates to -// the StateDB depending on the transaction Type. Returns the common.ZKInputs -// to generate the SnarkProof later used by the BatchBuilder, and if -// cmpExitTree is set to true, returns common.ExitTreeLeaf that is later used -// by the Synchronizer to update the HistoryDB. -func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx) (*common.ZKInputs, []*common.ExitInfo, error) { +// the StateDB depending on the transaction Type. If StateDB +// type==TypeBatchBuilder, returns the common.ZKInputs to generate the +// SnarkProof later used by the BatchBuilder. If StateDB +// type==TypeSynchronizer, assumes that the call is done from the Synchronizer, +// returns common.ExitTreeLeaf that is later used by the Synchronizer to update +// the HistoryDB, and adds Nonce & TokenID to the L2Txs. +func (s *StateDB) ProcessTxs(l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx) (*common.ZKInputs, []common.ExitInfo, error) { var err error var exitTree *merkletree.MerkleTree @@ -50,7 +52,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin } exits := make([]processedExit, nTx) - if cmpZKInputs { + if s.typ == TypeBatchBuilder { s.zki = common.NewZKInputs(nTx, 24, 32) // TODO this values will be parameters of the function, taken from config file/coordinator call s.zki.OldLastIdx = (s.idx - 1).BigInt() s.zki.OldStateRoot = s.mt.Root().BigInt() @@ -58,7 +60,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin // TBD if ExitTree is only in memory or stored in disk, for the moment // only needed in memory - if cmpExitTree { + if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { exitTree, err = merkletree.NewMerkleTree(memory.NewMemoryStorage(), s.mt.MaxLevels()) if err != nil { return nil, nil, err @@ -71,15 +73,15 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin if err != nil { return nil, nil, err } - if exitIdx != nil && cmpExitTree { - exits[s.i] = processedExit{ - exit: true, - newExit: newExit, - idx: *exitIdx, - acc: *exitAccount, + if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { + if exitIdx != nil && exitTree != nil { + exits[s.i] = processedExit{ + exit: true, + newExit: newExit, + idx: *exitIdx, + acc: *exitAccount, + } } - } - if s.zki != nil { s.i++ } } @@ -91,15 +93,15 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin if exitIdx != nil { log.Error("Unexpected Exit in L1CoordinatorTx") } - if exitIdx != nil && cmpExitTree { - exits[s.i] = processedExit{ - exit: true, - newExit: newExit, - idx: *exitIdx, - acc: *exitAccount, + if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { + if exitIdx != nil && exitTree != nil { + exits[s.i] = processedExit{ + exit: true, + newExit: newExit, + idx: *exitIdx, + acc: *exitAccount, + } } - } - if s.zki != nil { s.i++ } } @@ -108,26 +110,26 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin if err != nil { return nil, nil, err } - if exitIdx != nil && cmpExitTree { - exits[s.i] = processedExit{ - exit: true, - newExit: newExit, - idx: *exitIdx, - acc: *exitAccount, + if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { + if exitIdx != nil && exitTree != nil { + exits[s.i] = processedExit{ + exit: true, + newExit: newExit, + idx: *exitIdx, + acc: *exitAccount, + } } - } - if s.zki != nil { s.i++ } } - if !cmpExitTree && !cmpZKInputs { + if s.typ == TypeTxSelector { return nil, nil, nil } // once all txs processed (exitTree root frozen), for each Exit, // generate common.ExitInfo data - var exitInfos []*common.ExitInfo + var exitInfos []common.ExitInfo for i := 0; i < nTx; i++ { if !exits[i].exit { continue @@ -141,7 +143,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin return nil, nil, err } // 1. generate common.ExitInfo - ei := &common.ExitInfo{ + ei := common.ExitInfo{ AccountIdx: exitIdx, MerkleProof: p, Balance: exitAccount.Balance, @@ -168,7 +170,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin s.zki.OldValue2[i] = p.OldValue.BigInt() } } - if !cmpZKInputs { + if s.typ == TypeSynchronizer { return nil, exitInfos, nil } @@ -345,6 +347,17 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2 s.zki.R8y[s.i] = tx.Signature.R8.Y } + // if StateDB type==TypeSynchronizer, will need to add Nonce and + // TokenID to the transaction + if s.typ == TypeSynchronizer { + acc, err := s.GetAccount(tx.ToIdx) + if err != nil { + return nil, nil, false, err + } + tx.Nonce = acc.Nonce + tx.TokenID = acc.TokenID + } + switch tx.Type { case common.TxTypeTransfer: // go to the MT account of sender and receiver, and update diff --git a/db/statedb/txprocessors_test.go b/db/statedb/txprocessors_test.go index 79bed68..05407b6 100644 --- a/db/statedb/txprocessors_test.go +++ b/db/statedb/txprocessors_test.go @@ -19,7 +19,7 @@ func TestProcessTxs(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, true, 32) + sdb, err := NewStateDB(dir, TypeBatchBuilder, 32) assert.Nil(t, err) // generate test transactions from test.SetTest0 code @@ -36,7 +36,7 @@ func TestProcessTxs(t *testing.T) { for i := 0; i < len(l1Txs); i++ { // l2Txs := common.PoolL2TxsToL2Txs(poolL2Txs[i]) - _, _, err := sdb.ProcessTxs(true, true, l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i]) + _, _, err := sdb.ProcessTxs(l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i]) require.Nil(t, err) } @@ -45,11 +45,68 @@ func TestProcessTxs(t *testing.T) { assert.Equal(t, "23", acc.Balance.String()) } -func TestProcessTxsBatchByBatch(t *testing.T) { +func TestProcessTxsSynchronizer(t *testing.T) { + // TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for + // the Synchronizer tests + + dir, err := ioutil.TempDir("", "tmpdb") + require.Nil(t, err) + + sdb, err := NewStateDB(dir, TypeSynchronizer, 32) + assert.Nil(t, err) + + // generate test transactions from test.SetTest0 code + parser := test.NewParser(strings.NewReader(test.SetTest0)) + instructions, err := parser.Parse() + assert.Nil(t, err) + + l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions) + assert.Equal(t, 29, len(l1Txs[0])) + assert.Equal(t, 0, len(coordinatorL1Txs[0])) + assert.Equal(t, 21, len(poolL2Txs[0])) + assert.Equal(t, 5, len(l1Txs[1])) + assert.Equal(t, 1, len(coordinatorL1Txs[1])) + assert.Equal(t, 55, len(poolL2Txs[1])) + assert.Equal(t, 10, len(l1Txs[2])) + assert.Equal(t, 0, len(coordinatorL1Txs[2])) + assert.Equal(t, 7, len(poolL2Txs[2])) + + // use first batch + // l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0]) + _, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0]) + require.Nil(t, err) + // TODO once TTGL is updated, add a check that a input poolL2Tx with + // Nonce & TokenID =0, after ProcessTxs call has the expected value + + assert.Equal(t, 0, len(exitInfos)) + acc, err := sdb.GetAccount(common.Idx(256)) + assert.Nil(t, err) + assert.Equal(t, "28", acc.Balance.String()) + + // use second batch + // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1]) + _, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1]) + require.Nil(t, err) + assert.Equal(t, 5, len(exitInfos)) + acc, err = sdb.GetAccount(common.Idx(256)) + require.Nil(t, err) + assert.Equal(t, "48", acc.Balance.String()) + + // use third batch + // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2]) + _, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2]) + require.Nil(t, err) + assert.Equal(t, 1, len(exitInfos)) + acc, err = sdb.GetAccount(common.Idx(256)) + assert.Nil(t, err) + assert.Equal(t, "23", acc.Balance.String()) +} + +func TestProcessTxsBatchBuilder(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, true, 32) + sdb, err := NewStateDB(dir, TypeBatchBuilder, 32) assert.Nil(t, err) // generate test transactions from test.SetTest0 code @@ -70,7 +127,7 @@ func TestProcessTxsBatchByBatch(t *testing.T) { // use first batch // l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0]) - _, exitInfos, err := sdb.ProcessTxs(true, true, l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0]) + _, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0]) require.Nil(t, err) assert.Equal(t, 0, len(exitInfos)) acc, err := sdb.GetAccount(common.Idx(256)) @@ -79,7 +136,7 @@ func TestProcessTxsBatchByBatch(t *testing.T) { // use second batch // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1]) - _, exitInfos, err = sdb.ProcessTxs(true, true, l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1]) + _, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1]) require.Nil(t, err) assert.Equal(t, 5, len(exitInfos)) acc, err = sdb.GetAccount(common.Idx(256)) @@ -88,7 +145,7 @@ func TestProcessTxsBatchByBatch(t *testing.T) { // use third batch // l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2]) - _, exitInfos, err = sdb.ProcessTxs(true, true, l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2]) + _, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2]) require.Nil(t, err) assert.Equal(t, 1, len(exitInfos)) acc, err = sdb.GetAccount(common.Idx(256)) @@ -100,7 +157,7 @@ func TestZKInputsGeneration(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, true, 32) + sdb, err := NewStateDB(dir, TypeBatchBuilder, 32) assert.Nil(t, err) // generate test transactions from test.SetTest0 code @@ -113,7 +170,7 @@ func TestZKInputsGeneration(t *testing.T) { assert.Equal(t, 0, len(coordinatorL1Txs[0])) assert.Equal(t, 21, len(poolL2Txs[0])) - zki, _, err := sdb.ProcessTxs(false, true, l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0]) + zki, _, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0]) require.Nil(t, err) s, err := json.Marshal(zki) diff --git a/db/statedb/utils_test.go b/db/statedb/utils_test.go index 29193ff..821cea3 100644 --- a/db/statedb/utils_test.go +++ b/db/statedb/utils_test.go @@ -16,7 +16,7 @@ func TestGetIdx(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := NewStateDB(dir, false, 0) + sdb, err := NewStateDB(dir, TypeTxSelector, 0) assert.Nil(t, err) var sk babyjub.PrivateKey diff --git a/node/node.go b/node/node.go index 273b0e5..122976d 100644 --- a/node/node.go +++ b/node/node.go @@ -72,7 +72,7 @@ func NewNode(mode Mode, cfg *config.Node, coordCfg *config.Coordinator) (*Node, historyDB := historydb.NewHistoryDB(db) - stateDB, err := statedb.NewStateDB(cfg.StateDB.Path, true, 32) + stateDB, err := statedb.NewStateDB(cfg.StateDB.Path, statedb.TypeSynchronizer, 32) if err != nil { return nil, err } diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index 7646718..66216b4 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -58,7 +58,7 @@ type BatchData struct { l1CoordinatorTxs []*common.L1Tx l2Txs []*common.L2Tx createdAccounts []*common.Account - exitTree []*common.ExitInfo + exitTree []common.ExitInfo batch *common.Batch } @@ -69,7 +69,7 @@ func NewBatchData() *BatchData { l1CoordinatorTxs: make([]*common.L1Tx, 0), l2Txs: make([]*common.L2Tx, 0), createdAccounts: make([]*common.Account, 0), - exitTree: make([]*common.ExitInfo, 0), + exitTree: make([]common.ExitInfo, 0), } } @@ -435,7 +435,7 @@ func (s *Synchronizer) rollupSync(blockNum int64) (*rollupData, error) { // TODO: Get createdAccounts from ProcessTxs() // TODO: Get CollectedFees from ProcessTxs() // TODO: Pass forgeBatchArgs.FeeIdxCoordinator to ProcessTxs() - _, exitInfo, err := s.stateDB.ProcessTxs(true, false, batchData.l1UserTxs, batchData.l1CoordinatorTxs, poolL2Txs) + _, exitInfo, err := s.stateDB.ProcessTxs(batchData.l1UserTxs, batchData.l1CoordinatorTxs, poolL2Txs) if err != nil { return nil, err } diff --git a/synchronizer/synchronizer_test.go b/synchronizer/synchronizer_test.go index f87e3da..9f75209 100644 --- a/synchronizer/synchronizer_test.go +++ b/synchronizer/synchronizer_test.go @@ -19,7 +19,7 @@ func Test(t *testing.T) { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := statedb.NewStateDB(dir, true, 32) + sdb, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32) assert.Nil(t, err) // Init History DB diff --git a/txselector/txselector.go b/txselector/txselector.go index 0a2aa21..3e17fba 100644 --- a/txselector/txselector.go +++ b/txselector/txselector.go @@ -43,7 +43,7 @@ type TxSelector struct { // NewTxSelector returns a *TxSelector func NewTxSelector(dbpath string, synchronizerStateDB *statedb.StateDB, l2 *l2db.L2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs uint64) (*TxSelector, error) { - localAccountsDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, false, 0) // without merkletree + localAccountsDB, err := statedb.NewLocalStateDB(dbpath, synchronizerStateDB, statedb.TypeTxSelector, 0) // without merkletree if err != nil { return nil, err } @@ -89,7 +89,7 @@ func (txsel *TxSelector) GetL2TxSelection(batchNum common.BatchNum) ([]*common.P txs := txsel.getL2Profitable(validTxs, txsel.MaxTxs) // process the txs in the local AccountsDB - _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, nil, nil, txs) + _, _, err = txsel.localAccountsDB.ProcessTxs(nil, nil, txs) if err != nil { return nil, err } @@ -238,7 +238,7 @@ func (txsel *TxSelector) GetL1L2TxSelection(batchNum common.BatchNum, l1Txs []*c l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs) // process the txs in the local AccountsDB - _, _, err = txsel.localAccountsDB.ProcessTxs(false, false, l1Txs, l1CoordinatorTxs, l2Txs) + _, _, err = txsel.localAccountsDB.ProcessTxs(l1Txs, l1CoordinatorTxs, l2Txs) if err != nil { return nil, nil, nil, err } diff --git a/txselector/txselector_test.go b/txselector/txselector_test.go index a5aea8f..7575503 100644 --- a/txselector/txselector_test.go +++ b/txselector/txselector_test.go @@ -25,7 +25,7 @@ func initTest(t *testing.T, testSet string) *TxSelector { dir, err := ioutil.TempDir("", "tmpdb") require.Nil(t, err) - sdb, err := statedb.NewStateDB(dir, false, 0) + sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0) require.Nil(t, err) txselDir, err := ioutil.TempDir("", "tmpTxSelDB")