From 9309722dfcf705961c8de37f341762be39228a63 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Wed, 26 Aug 2020 15:49:00 +0200 Subject: [PATCH] Update StateDB to new & ExitInfo struct & BatchNum Update StateDB to new & ExitInfo struct & BatchNum Also a small fix at txselector & log packages --- common/exittree.go | 4 +++- db/statedb/statedb.go | 6 +++--- db/statedb/statedb_test.go | 8 ++++---- db/statedb/txprocessors.go | 26 +++++++++----------------- log/log.go | 3 ++- txselector/txselector.go | 9 ++++++--- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/common/exittree.go b/common/exittree.go index 201d3da..4f6dfcf 100644 --- a/common/exittree.go +++ b/common/exittree.go @@ -2,11 +2,13 @@ package common import ( "math/big" + + "github.com/iden3/go-merkletree" ) type ExitInfo struct { AccountIdx Idx - MerkleProof []byte + MerkleProof *merkletree.CircomVerifierProof Balance *big.Int Nullifier *big.Int } diff --git a/db/statedb/statedb.go b/db/statedb/statedb.go index f608383..7f4c231 100644 --- a/db/statedb/statedb.go +++ b/db/statedb/statedb.go @@ -130,7 +130,7 @@ func (s *StateDB) MakeCheckpoint() error { } // DeleteCheckpoint removes if exist the checkpoint of the given batchNum -func (s *StateDB) DeleteCheckpoint(batchNum uint64) error { +func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error { checkpointPath := s.path + PATHBATCHNUM + strconv.Itoa(int(batchNum)) if _, err := os.Stat(checkpointPath); os.IsNotExist(err) { @@ -144,7 +144,7 @@ func (s *StateDB) DeleteCheckpoint(batchNum uint64) error { // does not delete the checkpoints between old current and the new current, // those checkpoints will remain in the storage, and eventually will be // deleted when MakeCheckpoint overwrites them. -func (s *StateDB) Reset(batchNum uint64) error { +func (s *StateDB) Reset(batchNum common.BatchNum) error { if batchNum == 0 { s.idx = 0 return nil @@ -331,7 +331,7 @@ func NewLocalStateDB(path string, synchronizerDB *StateDB, withMT bool, nLevels // Reset performs a reset in the LocaStateDB. If fromSynchronizer is true, it // gets the state from LocalStateDB.synchronizerStateDB for the given batchNum. If fromSynchronizer is false, get the state from LocalStateDB checkpoints. -func (l *LocalStateDB) Reset(batchNum uint64, fromSynchronizer bool) error { +func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) error { if batchNum == 0 { l.idx = 0 return nil diff --git a/db/statedb/statedb_test.go b/db/statedb/statedb_test.go index df35909..39ec264 100644 --- a/db/statedb/statedb_test.go +++ b/db/statedb/statedb_test.go @@ -193,13 +193,13 @@ func TestCheckpoints(t *testing.T) { assert.Nil(t, err) assert.Equal(t, common.BatchNum(4), cb) - err = sdb.DeleteCheckpoint(uint64(9)) + err = sdb.DeleteCheckpoint(common.BatchNum(9)) assert.Nil(t, err) - err = sdb.DeleteCheckpoint(uint64(10)) + err = sdb.DeleteCheckpoint(common.BatchNum(10)) assert.Nil(t, err) - err = sdb.DeleteCheckpoint(uint64(9)) // does not exist, should return err + err = sdb.DeleteCheckpoint(common.BatchNum(9)) // does not exist, should return err assert.NotNil(t, err) - err = sdb.DeleteCheckpoint(uint64(11)) // does not exist, should return err + err = sdb.DeleteCheckpoint(common.BatchNum(11)) // does not exist, should return err assert.NotNil(t, err) // Create a LocalStateDB from the initial StateDB diff --git a/db/statedb/txprocessors.go b/db/statedb/txprocessors.go index e7f38e6..7db03af 100644 --- a/db/statedb/txprocessors.go +++ b/db/statedb/txprocessors.go @@ -13,20 +13,12 @@ import ( // keyidx is used as key in the db to store the current Idx var keyidx = []byte("idx") -// FUTURE This will be used from common once pending PR is merged -type ExitInfo struct { - Idx *common.Idx - Proof *merkletree.CircomVerifierProof - Nullifier *big.Int - Balance *big.Int -} - // 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 bool, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.L2Tx) (*common.ZKInputs, []*ExitInfo, error) { +func (s *StateDB) ProcessTxs(cmpExitTree bool, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.L2Tx) (*common.ZKInputs, []*common.ExitInfo, error) { var err error var exitTree *merkletree.MerkleTree exits := make(map[common.Idx]common.Account) @@ -71,8 +63,8 @@ func (s *StateDB) ProcessTxs(cmpExitTree bool, l1usertxs, l1coordinatortxs []*co } // once all txs processed (exitTree root frozen), for each leaf - // generate ExitInfo data - var exitInfos []*ExitInfo + // generate common.ExitInfo data + var exitInfos []*common.ExitInfo for exitIdx, exitAccount := range exits { // 0. generate MerkleProof p, err := exitTree.GenerateCircomVerifierProof(exitIdx.BigInt(), nil) @@ -92,12 +84,12 @@ func (s *StateDB) ProcessTxs(cmpExitTree bool, l1usertxs, l1coordinatortxs []*co if err != nil { return nil, nil, err } - // 2. generate ExitInfo - ei := &ExitInfo{ - Idx: &exitIdx, - Proof: p, - Nullifier: nullifier, - Balance: exitAccount.Balance, + // 2. generate common.ExitInfo + ei := &common.ExitInfo{ + AccountIdx: exitIdx, + MerkleProof: p, + Nullifier: nullifier, + Balance: exitAccount.Balance, } exitInfos = append(exitInfos, ei) } diff --git a/log/log.go b/log/log.go index c86a709..53fe7e1 100644 --- a/log/log.go +++ b/log/log.go @@ -57,7 +57,8 @@ func Init(levelStr, errorsPath string) { } //nolint:errcheck defer logger.Sync() - log = logger.Sugar() + withOptions := logger.WithOptions(zap.AddCallerSkip(1)) + log = withOptions.Sugar() if errorsPath != "" { log.Infof("file where errors will be written: %s", errorsPath) diff --git a/txselector/txselector.go b/txselector/txselector.go index 360e595..c5f6f63 100644 --- a/txselector/txselector.go +++ b/txselector/txselector.go @@ -52,7 +52,7 @@ func NewTxSelector(dbpath string, synchronizerStateDB *statedb.StateDB, l2 *l2db // Reset tells the TxSelector to get it's internal AccountsDB // from the required `batchNum` -func (txsel *TxSelector) Reset(batchNum uint64) error { +func (txsel *TxSelector) Reset(batchNum common.BatchNum) error { err := txsel.localAccountsDB.Reset(batchNum, true) if err != nil { return err @@ -61,7 +61,7 @@ func (txsel *TxSelector) Reset(batchNum uint64) error { } // GetL2TxSelection returns a selection of the L2Txs for the next batch, from the L2DB pool -func (txsel *TxSelector) GetL2TxSelection(batchNum uint64) ([]*common.PoolL2Tx, error) { +func (txsel *TxSelector) GetL2TxSelection(batchNum common.BatchNum) ([]*common.PoolL2Tx, error) { // get pending l2-tx from tx-pool l2TxsRaw, err := txsel.l2db.GetPendingTxs() // once l2db ready, maybe use parameter 'batchNum' if err != nil { @@ -90,7 +90,7 @@ func (txsel *TxSelector) GetL2TxSelection(batchNum uint64) ([]*common.PoolL2Tx, } // GetL1L2TxSelection returns the selection of L1 + L2 txs -func (txsel *TxSelector) GetL1L2TxSelection(batchNum uint64, l1txs []*common.L1Tx) ([]*common.L1Tx, []*common.L1Tx, []*common.PoolL2Tx, error) { +func (txsel *TxSelector) GetL1L2TxSelection(batchNum common.BatchNum, l1txs []*common.L1Tx) ([]*common.L1Tx, []*common.L1Tx, []*common.PoolL2Tx, error) { // apply l1-user-tx to localAccountDB // create new leaves // update balances @@ -153,6 +153,9 @@ func (txsel *TxSelector) checkIfAccountExistOrPending(idx common.Idx) bool { func (txsel *TxSelector) getL2Profitable(txs txs, max uint64) txs { sort.Sort(txs) + if len(txs) < int(max) { + return txs + } return txs[:max] } func (txsel *TxSelector) createL1OperatorTxForL2Tx(accounts []*common.Account) []*common.L1Tx {