arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Update StateDB to new & ExitInfo struct & BatchNum

Update StateDB to new & ExitInfo struct & BatchNum
Also a small fix at txselector & log packages
arnaucube root@arnaucube.com
Wed, 26 Aug 2020 15:49:00 +0200
commit

9309722dfcf705961c8de37f341762be39228a63

parent

361af765ab6701ad415785d0c70e2047dd518c25

M common/exittree.go → 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 }
M db/statedb/statedb.go → db/statedb/statedb.go

@@ -130,7 +130,7 @@ return nil

} // 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 @@ // Reset resets the StateDB to the checkpoint at the given batchNum. Reset

// 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 @@ }

// 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
M db/statedb/statedb_test.go → db/statedb/statedb_test.go

@@ -193,13 +193,13 @@ cb, err = sdb.GetCurrentBatch()

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
M db/statedb/txprocessors.go → db/statedb/txprocessors.go

@@ -13,20 +13,12 @@

// 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 @@ return nil, nil, nil

} // 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 @@ })

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) }
M log/log.go → log/log.go

@@ -57,7 +57,8 @@ panic(err)

} //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)
M txselector/txselector.go → txselector/txselector.go

@@ -52,7 +52,7 @@ }

// 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 @@ return nil

} // 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 @@ return txs, nil

} // 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) 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 {