Test L2Txs, ExitTree in synchronizer

This commit is contained in:
Eduard S
2020-10-27 12:49:07 +01:00
parent c1b9b0be90
commit 558de2737e
8 changed files with 401 additions and 169 deletions

View File

@@ -272,7 +272,9 @@ func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
var batches []*common.Batch
err := meddler.QueryAll(
hdb.db, &batches,
"SELECT * FROM batch;",
`SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr, batch.fees_collected,
batch.state_root, batch.num_accounts, batch.last_idx, batch.exit_root,
batch.forge_l1_txs_num, batch.slot_num, batch.total_fees_usd FROM batch;`,
)
return db.SlicePtrsToSlice(batches).([]common.Batch), err
}
@@ -796,6 +798,18 @@ func (hdb *HistoryDB) GetHistoryTxs(
}, nil
}
// GetAllExits returns all exit from the DB
func (hdb *HistoryDB) GetAllExits() ([]common.ExitInfo, error) {
var exits []*common.ExitInfo
err := meddler.QueryAll(
hdb.db, &exits,
`SELECT exit_tree.batch_num, exit_tree.account_idx, exit_tree.merkle_proof,
exit_tree.balance, exit_tree.instant_withdrawn, exit_tree.delayed_withdraw_request,
exit_tree.delayed_withdrawn FROM exit_tree;`,
)
return db.SlicePtrsToSlice(exits).([]common.ExitInfo), err
}
// GetExit returns a exit from the DB
func (hdb *HistoryDB) GetExit(batchNum *uint, idx *common.Idx) (*HistoryExit, error) {
exit := &HistoryExit{}
@@ -931,6 +945,32 @@ func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
}
// GetAllL1CoordinatorTxs returns all L1CoordinatorTxs from the DB
func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
var txs []*common.L1Tx
err := meddler.QueryAll(
hdb.db, &txs,
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id, tx.amount,
tx.load_amount, tx.eth_block_num, tx.type, tx.batch_num
FROM tx WHERE is_l1 = TRUE AND user_origin = FALSE;`,
)
return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
}
// GetAllL2Txs returns all L2Txs from the DB
func (hdb *HistoryDB) GetAllL2Txs() ([]common.L2Tx, error) {
var txs []*common.L2Tx
err := meddler.QueryAll(
hdb.db, &txs,
`SELECT tx.id, tx.batch_num, tx.position,
tx.from_idx, tx.to_idx, tx.amount, tx.fee, tx.nonce,
tx.type, tx.eth_block_num
FROM tx WHERE is_l1 = FALSE;`,
)
return db.SlicePtrsToSlice(txs).([]common.L2Tx), err
}
// GetL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
func (hdb *HistoryDB) GetL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
var txs []*common.L1Tx

View File

@@ -1,7 +1,6 @@
package historydb
import (
"fmt"
"math"
"math/big"
"os"
@@ -409,11 +408,6 @@ func TestGetL1UserTxs(t *testing.T) {
toForgeL1TxsNum := int64(1)
for i := range blocks {
fmt.Printf("DBG %+v\n", blocks[i])
// fmt.Printf("DBG Batches %+v\n", blocks[i].Batches)
// for _, l1Tx := range blocks[i].L1UserTxs {
// fmt.Printf("DBG l1UserTx %+v\n", l1Tx)
// }
err = historyDB.AddBlockSCData(&blocks[i])
require.Nil(t, err)
}

View File

@@ -429,10 +429,10 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, exitTr
// as type==TypeSynchronizer, always tx.ToIdx!=0
acc, err := s.GetAccount(tx.FromIdx)
if err != nil {
log.Error(err)
log.Errorw("GetAccount", "fromIdx", tx.FromIdx, "err", err)
return nil, nil, false, err
}
tx.Nonce = acc.Nonce
tx.Nonce = acc.Nonce + 1
tx.TokenID = acc.TokenID
}
@@ -592,14 +592,14 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, tx c
// send the fee to the Idx of the Coordinator for the TokenID
accCoord, err := s.GetAccount(coordIdxsMap[tx.TokenID])
if err != nil {
log.Errorf("applyTransfer error: Tx=%s, error: %s", tx.String(), err)
return err
}
accCoord.Balance = new(big.Int).Add(accCoord.Balance, fee)
_, err = s.UpdateAccount(coordIdxsMap[tx.TokenID], accCoord)
if err != nil {
log.Error(err)
return err
log.Debugw("No coord Idx to receive fee", "tx", tx)
} else {
accCoord.Balance = new(big.Int).Add(accCoord.Balance, fee)
_, err = s.UpdateAccount(coordIdxsMap[tx.TokenID], accCoord)
if err != nil {
log.Error(err)
return err
}
}
}
@@ -726,14 +726,14 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx, exitTree
// send the fee to the Idx of the Coordinator for the TokenID
accCoord, err := s.GetAccount(coordIdxsMap[tx.TokenID])
if err != nil {
log.Errorf("applyExit error: Tx=%s, error: %s", tx.String(), err)
return nil, false, err
}
accCoord.Balance = new(big.Int).Add(accCoord.Balance, fee)
_, err = s.UpdateAccount(coordIdxsMap[tx.TokenID], accCoord)
if err != nil {
log.Error(err)
return nil, false, err
log.Debugw("No coord Idx to receive fee", "tx", tx)
} else {
accCoord.Balance = new(big.Int).Add(accCoord.Balance, fee)
_, err = s.UpdateAccount(coordIdxsMap[tx.TokenID], accCoord)
if err != nil {
log.Error(err)
return nil, false, err
}
}
}