mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Wrap all errors with tracerr
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
@@ -73,7 +74,7 @@ func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
|
||||
hdb.db, block,
|
||||
"SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
|
||||
)
|
||||
return block, err
|
||||
return block, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAllBlocks retrieve all blocks from the DB
|
||||
@@ -83,7 +84,7 @@ func (hdb *HistoryDB) GetAllBlocks() ([]common.Block, error) {
|
||||
hdb.db, &blocks,
|
||||
"SELECT * FROM block;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(blocks).([]common.Block), err
|
||||
return db.SlicePtrsToSlice(blocks).([]common.Block), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
|
||||
@@ -94,7 +95,7 @@ func (hdb *HistoryDB) GetBlocks(from, to int64) ([]common.Block, error) {
|
||||
"SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2;",
|
||||
from, to,
|
||||
)
|
||||
return db.SlicePtrsToSlice(blocks).([]common.Block), err
|
||||
return db.SlicePtrsToSlice(blocks).([]common.Block), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetLastBlock retrieve the block with the highest block number from the DB
|
||||
@@ -103,7 +104,7 @@ func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
|
||||
)
|
||||
return block, err
|
||||
return block, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddBatch insert a Batch into the DB
|
||||
@@ -128,13 +129,13 @@ func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
|
||||
tokenIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
if err := meddler.QueryAll(
|
||||
hdb.db, &tokenPrices, query, args...,
|
||||
); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
// Calculate total collected
|
||||
@@ -159,7 +160,7 @@ func (hdb *HistoryDB) AddBatches(batches []common.Batch) error {
|
||||
func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
|
||||
for i := 0; i < len(batches); i++ {
|
||||
if err := hdb.addBatch(d, &batches[i]); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -258,11 +259,11 @@ func (hdb *HistoryDB) GetBatchesAPI(
|
||||
// log.Debug(query)
|
||||
batchPtrs := []*BatchAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &batchPtrs, query, args...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
batches := db.SlicePtrsToSlice(batchPtrs).([]BatchAPI)
|
||||
if len(batches) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return batches, batches[0].TotalItems - uint64(len(batches)), nil
|
||||
}
|
||||
@@ -276,7 +277,7 @@ func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
|
||||
batch.fee_idxs_coordinator, 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
|
||||
return db.SlicePtrsToSlice(batches).([]common.Batch), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBatches retrieve batches from the DB, given a range of batch numbers defined by from and to
|
||||
@@ -287,7 +288,7 @@ func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, erro
|
||||
"SELECT * FROM batch WHERE $1 <= batch_num AND batch_num < $2;",
|
||||
from, to,
|
||||
)
|
||||
return db.SlicePtrsToSlice(batches).([]common.Batch), err
|
||||
return db.SlicePtrsToSlice(batches).([]common.Batch), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBatchesLen retrieve number of batches from the DB, given a slotNum
|
||||
@@ -331,7 +332,7 @@ func (hdb *HistoryDB) Reorg(lastValidBlock int64) error {
|
||||
} else {
|
||||
_, err = hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
|
||||
}
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddBids insert Bids into the DB
|
||||
@@ -352,7 +353,7 @@ func (hdb *HistoryDB) GetAllBids() ([]common.Bid, error) {
|
||||
hdb.db, &bids,
|
||||
`SELECT bid.slot_num, bid.bid_value, bid.eth_block_num, bid.bidder_addr FROM bid;`,
|
||||
)
|
||||
return db.SlicePtrsToSlice(bids).([]common.Bid), err
|
||||
return db.SlicePtrsToSlice(bids).([]common.Bid), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBestBidAPI returns the best bid in specific slot by slotNum
|
||||
@@ -364,7 +365,7 @@ func (hdb *HistoryDB) GetBestBidAPI(slotNum *int64) (BidAPI, error) {
|
||||
INNER JOIN coordinator ON bid.bidder_addr = coordinator.bidder_addr
|
||||
WHERE slot_num = $1 ORDER BY item_id DESC LIMIT 1;`, slotNum,
|
||||
)
|
||||
return *bid, err
|
||||
return *bid, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBestBidCoordinator returns the forger address of the highest bidder in a slot by slotNum
|
||||
@@ -385,7 +386,7 @@ func (hdb *HistoryDB) GetBestBidCoordinator(slotNum int64) (*common.BidCoordinat
|
||||
WHERE bid.slot_num = $1 ORDER BY bid.item_id DESC LIMIT 1;`,
|
||||
slotNum)
|
||||
|
||||
return bidCoord, err
|
||||
return bidCoord, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetBestBidsAPI returns the best bid in specific slot by slotNum
|
||||
@@ -424,12 +425,12 @@ func (hdb *HistoryDB) GetBestBidsAPI(
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
bidPtrs := []*BidAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &bidPtrs, query, args...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
// log.Debug(query)
|
||||
bids := db.SlicePtrsToSlice(bidPtrs).([]BidAPI)
|
||||
if len(bids) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return bids, bids[0].TotalItems - uint64(len(bids)), nil
|
||||
}
|
||||
@@ -492,15 +493,15 @@ func (hdb *HistoryDB) GetBidsAPI(
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query, argsQ, err := sqlx.In(queryStr, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
bids := []*BidAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &bids, query, argsQ...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(bids) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return db.SlicePtrsToSlice(bids).([]BidAPI), bids[0].TotalItems - uint64(len(bids)), nil
|
||||
}
|
||||
@@ -592,7 +593,7 @@ func (hdb *HistoryDB) updateExitTree(d sqlx.Ext, blockNum int64,
|
||||
`
|
||||
if len(withdrawals) > 0 {
|
||||
if _, err := sqlx.NamedQuery(d, query, withdrawals); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,7 +628,7 @@ func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error
|
||||
"UPDATE token SET usd = $1 WHERE symbol = $2;",
|
||||
value, tokenSymbol,
|
||||
)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetToken returns a token from the DB given a TokenID
|
||||
@@ -636,7 +637,7 @@ func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenWithUSD, error) {
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
|
||||
)
|
||||
return token, err
|
||||
return token, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAllTokens returns all tokens from the DB
|
||||
@@ -646,7 +647,7 @@ func (hdb *HistoryDB) GetAllTokens() ([]TokenWithUSD, error) {
|
||||
hdb.db, &tokens,
|
||||
"SELECT * FROM token ORDER BY token_id;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), err
|
||||
return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetTokens returns a list of tokens from the DB
|
||||
@@ -707,15 +708,15 @@ func (hdb *HistoryDB) GetTokens(
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query, argsQ, err := sqlx.In(queryStr, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
tokens := []*TokenWithUSD{}
|
||||
if err := meddler.QueryAll(hdb.db, &tokens, query, argsQ...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(tokens) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), uint64(len(tokens)) - tokens[0].TotalItems, nil
|
||||
}
|
||||
@@ -725,13 +726,13 @@ func (hdb *HistoryDB) GetTokenSymbols() ([]string, error) {
|
||||
var tokenSymbols []string
|
||||
rows, err := hdb.db.Query("SELECT symbol FROM token;")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
sym := new(string)
|
||||
for rows.Next() {
|
||||
err = rows.Scan(sym)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
tokenSymbols = append(tokenSymbols, *sym)
|
||||
}
|
||||
@@ -763,7 +764,7 @@ func (hdb *HistoryDB) GetAllAccounts() ([]common.Account, error) {
|
||||
hdb.db, &accs,
|
||||
"SELECT * FROM account ORDER BY idx;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(accs).([]common.Account), err
|
||||
return db.SlicePtrsToSlice(accs).([]common.Account), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddL1Txs inserts L1 txs to the DB. USD and LoadAmountUSD will be set automatically before storing the tx.
|
||||
@@ -895,7 +896,7 @@ func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*TxAPI, error) {
|
||||
INNER JOIN block ON tx.eth_block_num = block.eth_block_num
|
||||
WHERE tx.id = $1;`, txID,
|
||||
)
|
||||
return tx, err
|
||||
return tx, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetHistoryTxs returns a list of txs from the DB using the HistoryTx struct
|
||||
@@ -906,7 +907,7 @@ func (hdb *HistoryDB) GetHistoryTxs(
|
||||
fromItem, limit *uint, order string,
|
||||
) ([]TxAPI, uint64, error) {
|
||||
if ethAddr != nil && bjj != nil {
|
||||
return nil, 0, errors.New("ethAddr and bjj are incompatible")
|
||||
return nil, 0, tracerr.Wrap(errors.New("ethAddr and bjj are incompatible"))
|
||||
}
|
||||
var query string
|
||||
var args []interface{}
|
||||
@@ -1010,11 +1011,11 @@ func (hdb *HistoryDB) GetHistoryTxs(
|
||||
// log.Debug(query)
|
||||
txsPtrs := []*TxAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &txsPtrs, query, args...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
txs := db.SlicePtrsToSlice(txsPtrs).([]TxAPI)
|
||||
if len(txs) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return txs, txs[0].TotalItems - uint64(len(txs)), nil
|
||||
}
|
||||
@@ -1028,7 +1029,7 @@ func (hdb *HistoryDB) GetAllExits() ([]common.ExitInfo, error) {
|
||||
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
|
||||
return db.SlicePtrsToSlice(exits).([]common.ExitInfo), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetExitAPI returns a exit from the DB
|
||||
@@ -1046,7 +1047,7 @@ func (hdb *HistoryDB) GetExitAPI(batchNum *uint, idx *common.Idx) (*ExitAPI, err
|
||||
INNER JOIN token ON account.token_id = token.token_id
|
||||
WHERE exit_tree.batch_num = $1 AND exit_tree.account_idx = $2;`, batchNum, idx,
|
||||
)
|
||||
return exit, err
|
||||
return exit, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetExitsAPI returns a list of exits from the DB and pagination info
|
||||
@@ -1056,7 +1057,7 @@ func (hdb *HistoryDB) GetExitsAPI(
|
||||
fromItem, limit *uint, order string,
|
||||
) ([]ExitAPI, uint64, error) {
|
||||
if ethAddr != nil && bjj != nil {
|
||||
return nil, 0, errors.New("ethAddr and bjj are incompatible")
|
||||
return nil, 0, tracerr.Wrap(errors.New("ethAddr and bjj are incompatible"))
|
||||
}
|
||||
var query string
|
||||
var args []interface{}
|
||||
@@ -1152,10 +1153,10 @@ func (hdb *HistoryDB) GetExitsAPI(
|
||||
// log.Debug(query)
|
||||
exits := []*ExitAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(exits) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return db.SlicePtrsToSlice(exits).([]ExitAPI), exits[0].TotalItems - uint64(len(exits)), nil
|
||||
}
|
||||
@@ -1171,7 +1172,7 @@ func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
|
||||
tx.eth_block_num, tx.type, tx.batch_num
|
||||
FROM tx WHERE is_l1 = TRUE AND user_origin = TRUE;`,
|
||||
)
|
||||
return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
|
||||
return db.SlicePtrsToSlice(txs).([]common.L1Tx), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAllL1CoordinatorTxs returns all L1CoordinatorTxs from the DB
|
||||
@@ -1185,7 +1186,7 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
|
||||
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
|
||||
return db.SlicePtrsToSlice(txs).([]common.L1Tx), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAllL2Txs returns all L2Txs from the DB
|
||||
@@ -1198,7 +1199,7 @@ func (hdb *HistoryDB) GetAllL2Txs() ([]common.L2Tx, error) {
|
||||
tx.type, tx.eth_block_num
|
||||
FROM tx WHERE is_l1 = FALSE;`,
|
||||
)
|
||||
return db.SlicePtrsToSlice(txs).([]common.L2Tx), err
|
||||
return db.SlicePtrsToSlice(txs).([]common.L2Tx), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
|
||||
@@ -1213,7 +1214,7 @@ func (hdb *HistoryDB) GetL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error)
|
||||
FROM tx WHERE to_forge_l1_txs_num = $1 AND is_l1 = TRUE AND user_origin = TRUE;`,
|
||||
toForgeL1TxsNum,
|
||||
)
|
||||
return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
|
||||
return db.SlicePtrsToSlice(txs).([]common.L1Tx), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// TODO: Think about chaning all the queries that return a last value, to queries that return the next valid value.
|
||||
@@ -1233,15 +1234,15 @@ func (hdb *HistoryDB) GetSCVars() (*common.RollupVariables, *common.AuctionVaria
|
||||
var wDelayer common.WDelayerVariables
|
||||
if err := meddler.QueryRow(hdb.db, &rollup,
|
||||
"SELECT * FROM rollup_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &auction,
|
||||
"SELECT * FROM auction_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &wDelayer,
|
||||
"SELECT * FROM wdelayer_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
return &rollup, &auction, &wDelayer, nil
|
||||
}
|
||||
@@ -1266,7 +1267,7 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
auction *common.AuctionVariables, wDelayer *common.WDelayerVariables) error {
|
||||
txn, err := hdb.db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
@@ -1280,13 +1281,13 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
wDelayer.EthBlockNum = 0
|
||||
auction.DefaultSlotSetBidSlotNum = 0
|
||||
if err := hdb.setRollupVars(txn, rollup); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if err := hdb.setAuctionVars(txn, auction); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if err := hdb.setWDelayerVars(txn, wDelayer); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return txn.Commit()
|
||||
@@ -1299,7 +1300,7 @@ func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
txn, err := hdb.db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
@@ -1309,27 +1310,27 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
|
||||
// Add block
|
||||
if err := hdb.addBlock(txn, &blockData.Block); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// Add Coordinators
|
||||
if len(blockData.Auction.Coordinators) > 0 {
|
||||
if err := hdb.addCoordinators(txn, blockData.Auction.Coordinators); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add Bids
|
||||
if len(blockData.Auction.Bids) > 0 {
|
||||
if err := hdb.addBids(txn, blockData.Auction.Bids); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add Tokens
|
||||
if len(blockData.Rollup.AddedTokens) > 0 {
|
||||
if err := hdb.addTokens(txn, blockData.Rollup.AddedTokens); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1366,69 +1367,69 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
// Add Batch: this will trigger an update on the DB
|
||||
// that will set the batch num of forged L1 txs in this batch
|
||||
if err = hdb.addBatch(txn, &batch.Batch); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// Add accounts
|
||||
if len(batch.CreatedAccounts) > 0 {
|
||||
if err := hdb.addAccounts(txn, batch.CreatedAccounts); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add forged l1 coordinator Txs
|
||||
if len(batch.L1CoordinatorTxs) > 0 {
|
||||
if err := hdb.addL1Txs(txn, batch.L1CoordinatorTxs); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add l2 Txs
|
||||
if len(batch.L2Txs) > 0 {
|
||||
if err := hdb.addL2Txs(txn, batch.L2Txs); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add user L1 txs that will be forged in next batch
|
||||
if userlL1s, ok := userL1s[batch.Batch.BatchNum]; ok {
|
||||
if err := hdb.addL1Txs(txn, userlL1s); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Add exit tree
|
||||
if len(batch.ExitTree) > 0 {
|
||||
if err := hdb.addExitTree(txn, batch.ExitTree); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add user L1 txs that won't be forged in this block
|
||||
if userL1sNotForgedInThisBlock, ok := userL1s[0]; ok {
|
||||
if err := hdb.addL1Txs(txn, userL1sNotForgedInThisBlock); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
if blockData.Rollup.Vars != nil {
|
||||
if err := hdb.setRollupVars(txn, blockData.Rollup.Vars); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
if blockData.Auction.Vars != nil {
|
||||
if err := hdb.setAuctionVars(txn, blockData.Auction.Vars); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
if blockData.WDelayer.Vars != nil {
|
||||
if err := hdb.setWDelayerVars(txn, blockData.WDelayer.Vars); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := hdb.updateExitTree(txn, blockData.Block.Num,
|
||||
blockData.Rollup.Withdrawals, blockData.WDelayer.Withdrawals); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return txn.Commit()
|
||||
@@ -1438,7 +1439,7 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
func (hdb *HistoryDB) GetCoordinatorAPI(bidderAddr ethCommon.Address) (*CoordinatorAPI, error) {
|
||||
coordinator := &CoordinatorAPI{}
|
||||
err := meddler.QueryRow(hdb.db, coordinator, "SELECT * FROM coordinator WHERE bidder_addr = $1;", bidderAddr)
|
||||
return coordinator, err
|
||||
return coordinator, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetCoordinatorsAPI returns a list of coordinators from the DB and pagination info
|
||||
@@ -1470,10 +1471,10 @@ func (hdb *HistoryDB) GetCoordinatorsAPI(fromItem, limit *uint, order string) ([
|
||||
|
||||
coordinators := []*CoordinatorAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(coordinators) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
return db.SlicePtrsToSlice(coordinators).([]CoordinatorAPI),
|
||||
coordinators[0].TotalItems - uint64(len(coordinators)), nil
|
||||
@@ -1490,7 +1491,7 @@ func (hdb *HistoryDB) GetAuctionVars() (*common.AuctionVariables, error) {
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, auctionVars, `SELECT * FROM auction_vars;`,
|
||||
)
|
||||
return auctionVars, err
|
||||
return auctionVars, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAccountAPI returns an account by its index
|
||||
@@ -1503,7 +1504,7 @@ func (hdb *HistoryDB) GetAccountAPI(idx common.Idx) (*AccountAPI, error) {
|
||||
FROM account INNER JOIN token ON account.token_id = token.token_id WHERE idx = $1;`, idx)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return account, nil
|
||||
@@ -1515,7 +1516,7 @@ func (hdb *HistoryDB) GetAccountsAPI(
|
||||
bjj *babyjub.PublicKey, fromItem, limit *uint, order string,
|
||||
) ([]AccountAPI, uint64, error) {
|
||||
if ethAddr != nil && bjj != nil {
|
||||
return nil, 0, errors.New("ethAddr and bjj are incompatible")
|
||||
return nil, 0, tracerr.Wrap(errors.New("ethAddr and bjj are incompatible"))
|
||||
}
|
||||
var query string
|
||||
var args []interface{}
|
||||
@@ -1570,16 +1571,16 @@ func (hdb *HistoryDB) GetAccountsAPI(
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query, argsQ, err := sqlx.In(queryStr, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
|
||||
accounts := []*AccountAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &accounts, query, argsQ...); err != nil {
|
||||
return nil, 0, err
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(accounts) == 0 {
|
||||
return nil, 0, sql.ErrNoRows
|
||||
return nil, 0, tracerr.Wrap(sql.ErrNoRows)
|
||||
}
|
||||
|
||||
return db.SlicePtrsToSlice(accounts).([]AccountAPI),
|
||||
@@ -1595,7 +1596,7 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
|
||||
FROM tx INNER JOIN block ON tx.eth_block_num = block.eth_block_num
|
||||
WHERE block.timestamp >= NOW() - INTERVAL '24 HOURS';`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
metrics.TransactionsPerSecond = float64(metricsTotals.TotalTransactions / (24 * 60 * 60))
|
||||
@@ -1611,7 +1612,7 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
|
||||
SUM(total_fees_usd) AS total_fees FROM batch
|
||||
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if metricsTotals.TotalBatches > 0 {
|
||||
metrics.BatchFrequency = float64((24 * 60 * 60) / metricsTotals.TotalBatches)
|
||||
@@ -1627,7 +1628,7 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
|
||||
hdb.db, metrics,
|
||||
`SELECT COUNT(*) AS total_bjjs, COUNT(DISTINCT(bjj)) AS total_accounts FROM account;`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return metrics, nil
|
||||
@@ -1641,14 +1642,14 @@ func (hdb *HistoryDB) GetAvgTxFee() (float64, error) {
|
||||
FROM tx INNER JOIN block ON tx.eth_block_num = block.eth_block_num
|
||||
WHERE block.timestamp >= NOW() - INTERVAL '1 HOURS';`)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, tracerr.Wrap(err)
|
||||
}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches,
|
||||
SUM(total_fees_usd) AS total_fees FROM batch
|
||||
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
var avgTransactionFee float64
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/hermez-node/test"
|
||||
"github.com/hermeznetwork/hermez-node/test/til"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -78,7 +79,7 @@ func TestBlocks(t *testing.T) {
|
||||
}
|
||||
// Add block 0 to the generated blocks
|
||||
blocks = append(
|
||||
[]common.BlockData{common.BlockData{Block: test.Block0}}, //nolint:gofmt
|
||||
[]common.BlockData{{Block: test.Block0}}, //nolint:gofmt
|
||||
blocks...,
|
||||
)
|
||||
// Get all blocks from DB
|
||||
@@ -680,7 +681,7 @@ func exampleInitSCVars() (*common.RollupVariables, *common.AuctionVariables, *co
|
||||
func TestSetInitialSCVars(t *testing.T) {
|
||||
test.WipeDB(historyDB.DB())
|
||||
_, _, _, err := historyDB.GetSCVars()
|
||||
assert.Equal(t, sql.ErrNoRows, err)
|
||||
assert.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
|
||||
rollup, auction, wDelayer := exampleInitSCVars()
|
||||
err = historyDB.SetInitialSCVars(rollup, auction, wDelayer)
|
||||
require.Nil(t, err)
|
||||
@@ -859,7 +860,7 @@ func TestGetBestBidCoordinator(t *testing.T) {
|
||||
require.Equal(t, coords[1].URL, forger10.URL)
|
||||
|
||||
_, err = historyDB.GetBestBidCoordinator(11)
|
||||
require.Equal(t, sql.ErrNoRows, err)
|
||||
require.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
|
||||
}
|
||||
|
||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
//nolint:errcheck // driver for postgres DB
|
||||
@@ -51,7 +52,7 @@ func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error
|
||||
VALUES ($1, $2, $3);`,
|
||||
auth.EthAddr, auth.BJJ, auth.Signature,
|
||||
)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// GetAccountCreationAuth returns an account creation authorization from the DB
|
||||
@@ -170,7 +171,7 @@ func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
|
||||
selectPoolTxCommon+"WHERE state = $1",
|
||||
common.PoolL2TxStatePending,
|
||||
)
|
||||
return db.SlicePtrsToSlice(txs).([]common.PoolL2Tx), err
|
||||
return db.SlicePtrsToSlice(txs).([]common.PoolL2Tx), tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// StartForging updates the state of the transactions that will begin the forging process.
|
||||
@@ -186,11 +187,11 @@ func (l2db *L2DB) StartForging(txIDs []common.TxID, batchNum common.BatchNum) er
|
||||
txIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
query = l2db.db.Rebind(query)
|
||||
_, err = l2db.db.Exec(query, args...)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// DoneForging updates the state of the transactions that have been forged
|
||||
@@ -206,11 +207,11 @@ func (l2db *L2DB) DoneForging(txIDs []common.TxID, batchNum common.BatchNum) err
|
||||
txIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
query = l2db.db.Rebind(query)
|
||||
_, err = l2db.db.Exec(query, args...)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// InvalidateTxs updates the state of the transactions that are invalid.
|
||||
@@ -225,11 +226,11 @@ func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) e
|
||||
txIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
query = l2db.db.Rebind(query)
|
||||
_, err = l2db.db.Exec(query, args...)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// CheckNonces invalidate txs with nonces that are smaller or equal than their respective accounts nonces.
|
||||
@@ -237,7 +238,7 @@ func (l2db *L2DB) InvalidateTxs(txIDs []common.TxID, batchNum common.BatchNum) e
|
||||
func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.BatchNum) (err error) {
|
||||
txn, err := l2db.db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
// Rollback the transaction if there was an error.
|
||||
@@ -257,7 +258,7 @@ func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.
|
||||
updatedAccounts[i].Nonce,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
return txn.Commit()
|
||||
@@ -274,7 +275,7 @@ func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
|
||||
common.PoolL2TxStateInvalid,
|
||||
lastValidBatch,
|
||||
)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// Purge deletes transactions that have been forged or marked as invalid for longer than the safety period
|
||||
@@ -282,7 +283,7 @@ func (l2db *L2DB) Reorg(lastValidBatch common.BatchNum) error {
|
||||
func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
|
||||
txn, err := l2db.db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
// Rollback the transaction if there was an error.
|
||||
@@ -298,7 +299,7 @@ func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
|
||||
time.Unix(now-int64(l2db.ttl.Seconds()), 0),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// Delete txs that have been marked as forged / invalid after the safety period
|
||||
_, err = txn.Exec(
|
||||
@@ -309,7 +310,7 @@ func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
|
||||
common.PoolL2TxStateInvalid,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
return txn.Commit()
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/hermez-node/test"
|
||||
"github.com/hermeznetwork/hermez-node/test/til"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -69,12 +70,12 @@ func prepareHistoryDB(historyDB *historydb.HistoryDB) error {
|
||||
}
|
||||
blocks, err := tc.GenerateBlocks(setBlockchain)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
tokens = make(map[common.TokenID]historydb.TokenWithUSD)
|
||||
tokensValue = make(map[common.TokenID]float64)
|
||||
@@ -85,7 +86,7 @@ func prepareHistoryDB(historyDB *historydb.HistoryDB) error {
|
||||
for i := range blocks[:len(blocks)-1] {
|
||||
err = historyDB.AddBlockSCData(&blocks[i])
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
for _, batch := range blocks[i].Rollup.Batches {
|
||||
for _, account := range batch.CreatedAccounts {
|
||||
@@ -110,7 +111,7 @@ func prepareHistoryDB(historyDB *historydb.HistoryDB) error {
|
||||
tokenSymbol := ""
|
||||
err := historyDB.UpdateTokenValue(tokenSymbol, value)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -135,7 +136,7 @@ func generatePoolL2Txs() ([]common.PoolL2Tx, error) {
|
||||
`
|
||||
poolL2Txs, err := tc.GeneratePoolL2Txs(setPool)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
return poolL2Txs, nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-merkletree"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
"github.com/iden3/go-merkletree/db/pebble"
|
||||
@@ -92,18 +93,18 @@ func NewStateDB(path string, typ TypeStateDB, nLevels int) (*StateDB, error) {
|
||||
var err error
|
||||
sto, err = pebble.NewPebbleStorage(path+PathCurrent, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
var mt *merkletree.MerkleTree = nil
|
||||
if typ == TypeSynchronizer || typ == TypeBatchBuilder {
|
||||
mt, err = merkletree.NewMerkleTree(sto.WithPrefix(PrefixKeyMT), nLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
if typ == TypeTxSelector && nLevels != 0 {
|
||||
return nil, fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0")
|
||||
return nil, tracerr.Wrap(fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0"))
|
||||
}
|
||||
|
||||
sdb := &StateDB{
|
||||
@@ -116,13 +117,13 @@ func NewStateDB(path string, typ TypeStateDB, nLevels int) (*StateDB, error) {
|
||||
// load currentBatch
|
||||
sdb.currentBatch, err = sdb.GetCurrentBatch()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// make reset (get checkpoint) at currentBatch
|
||||
err = sdb.reset(sdb.currentBatch, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return sdb, nil
|
||||
@@ -136,11 +137,11 @@ func (s *StateDB) DB() *pebble.PebbleStorage {
|
||||
// GetCurrentBatch returns the current BatchNum stored in the StateDB
|
||||
func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
|
||||
cbBytes, err := s.db.Get(KeyCurrentBatch)
|
||||
if err == db.ErrNotFound {
|
||||
if tracerr.Unwrap(err) == db.ErrNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, tracerr.Wrap(err)
|
||||
}
|
||||
return common.BatchNumFromBytes(cbBytes)
|
||||
}
|
||||
@@ -149,14 +150,14 @@ func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
|
||||
func (s *StateDB) setCurrentBatch() error {
|
||||
tx, err := s.db.NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Put(KeyCurrentBatch, s.currentBatch.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -171,23 +172,23 @@ func (s *StateDB) MakeCheckpoint() error {
|
||||
|
||||
err := s.setCurrentBatch()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// if checkpoint BatchNum already exist in disk, delete it
|
||||
if _, err := os.Stat(checkpointPath); !os.IsNotExist(err) {
|
||||
err := os.RemoveAll(checkpointPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
} else if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// execute Checkpoint
|
||||
err = s.db.Pebble().Checkpoint(checkpointPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -198,7 +199,7 @@ func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error {
|
||||
checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum))
|
||||
|
||||
if _, err := os.Stat(checkpointPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Checkpoint with batchNum %d does not exist in DB", batchNum)
|
||||
return tracerr.Wrap(fmt.Errorf("Checkpoint with batchNum %d does not exist in DB", batchNum))
|
||||
}
|
||||
|
||||
return os.RemoveAll(checkpointPath)
|
||||
@@ -209,15 +210,15 @@ func pebbleMakeCheckpoint(source, dest string) error {
|
||||
if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
||||
err := os.RemoveAll(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
} else if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
sto, err := pebble.NewPebbleStorage(source, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
errClose := sto.Pebble().Close()
|
||||
@@ -229,7 +230,7 @@ func pebbleMakeCheckpoint(source, dest string) error {
|
||||
// execute Checkpoint
|
||||
err = sto.Pebble().Checkpoint(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -253,19 +254,19 @@ func (s *StateDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
|
||||
|
||||
if closeCurrent {
|
||||
if err := s.db.Pebble().Close(); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
// remove 'current'
|
||||
err := os.RemoveAll(currentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if batchNum == 0 {
|
||||
// if batchNum == 0, open the new fresh 'current'
|
||||
sto, err := pebble.NewPebbleStorage(currentPath, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
s.db = sto
|
||||
s.idx = 255
|
||||
@@ -275,7 +276,7 @@ func (s *StateDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
|
||||
// open the MT for the current s.db
|
||||
mt, err := merkletree.NewMerkleTree(s.db.WithPrefix(PrefixKeyMT), s.mt.MaxLevels())
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
s.mt = mt
|
||||
}
|
||||
@@ -286,32 +287,32 @@ func (s *StateDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
|
||||
// copy 'BatchNumX' to 'current'
|
||||
err = pebbleMakeCheckpoint(checkpointPath, currentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// open the new 'current'
|
||||
sto, err := pebble.NewPebbleStorage(currentPath, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
s.db = sto
|
||||
|
||||
// get currentBatch num
|
||||
s.currentBatch, err = s.GetCurrentBatch()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// idx is obtained from the statedb reset
|
||||
s.idx, err = s.getIdx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if s.mt != nil {
|
||||
// open the MT for the current s.db
|
||||
mt, err := merkletree.NewMerkleTree(s.db.WithPrefix(PrefixKeyMT), s.mt.MaxLevels())
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
s.mt = mt
|
||||
}
|
||||
@@ -335,18 +336,18 @@ func (s *StateDB) GetAccounts() ([]common.Account, error) {
|
||||
if err := idxDB.Iterate(func(k []byte, v []byte) (bool, error) {
|
||||
idx, err := common.IdxFromBytes(k)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, tracerr.Wrap(err)
|
||||
}
|
||||
idxs = append(idxs, idx)
|
||||
return true, nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accs := []common.Account{}
|
||||
for i := range idxs {
|
||||
acc, err := s.GetAccount(idxs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accs = append(accs, *acc)
|
||||
}
|
||||
@@ -358,21 +359,21 @@ func (s *StateDB) GetAccounts() ([]common.Account, error) {
|
||||
func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error) {
|
||||
idxBytes, err := idx.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
vBytes, err := sto.Get(append(PrefixKeyIdx, idxBytes[:]...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accBytes, err := sto.Get(append(PrefixKeyAccHash, vBytes...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
var b [32 * common.NLeafElems]byte
|
||||
copy(b[:], accBytes)
|
||||
account, err := common.AccountFromBytes(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
account.Idx = idx
|
||||
return account, nil
|
||||
@@ -384,11 +385,11 @@ func getAccountInTreeDB(sto db.Storage, idx common.Idx) (*common.Account, error)
|
||||
func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
|
||||
cpp, err := createAccountInTreeDB(s.db, s.mt, idx, account)
|
||||
if err != nil {
|
||||
return cpp, err
|
||||
return cpp, tracerr.Wrap(err)
|
||||
}
|
||||
// store idx by EthAddr & BJJ
|
||||
err = s.setIdxByEthAddrBJJ(idx, account.EthAddr, account.PublicKey, account.TokenID)
|
||||
return cpp, err
|
||||
return cpp, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// createAccountInTreeDB is abstracted from StateDB to be used from StateDB and
|
||||
@@ -399,39 +400,39 @@ func createAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common
|
||||
// store at the DB the key: v, and value: leaf.Bytes()
|
||||
v, err := account.HashValue()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accountBytes, err := account.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// store the Leaf value
|
||||
tx, err := sto.NewTx()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
idxBytes, err := idx.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
_, err = tx.Get(append(PrefixKeyIdx, idxBytes[:]...))
|
||||
if err != db.ErrNotFound {
|
||||
return nil, ErrAccountAlreadyExists
|
||||
if tracerr.Unwrap(err) != db.ErrNotFound {
|
||||
return nil, tracerr.Wrap(ErrAccountAlreadyExists)
|
||||
}
|
||||
|
||||
err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if mt != nil {
|
||||
@@ -456,32 +457,32 @@ func updateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common
|
||||
// store at the DB the key: v, and value: account.Bytes()
|
||||
v, err := account.HashValue()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accountBytes, err := account.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
tx, err := sto.NewTx()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Put(append(PrefixKeyAccHash, v.Bytes()...), accountBytes[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
idxBytes, err := idx.Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Put(append(PrefixKeyIdx, idxBytes[:]...), v.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if mt != nil {
|
||||
@@ -493,7 +494,7 @@ func updateAccountInTreeDB(sto db.Storage, mt *merkletree.MerkleTree, idx common
|
||||
// MTGetProof returns the CircomVerifierProof for a given Idx
|
||||
func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error) {
|
||||
if s.mt == nil {
|
||||
return nil, ErrStateDBWithoutMT
|
||||
return nil, tracerr.Wrap(ErrStateDBWithoutMT)
|
||||
}
|
||||
return s.mt.GenerateCircomVerifierProof(idx.BigInt(), s.mt.Root())
|
||||
}
|
||||
@@ -516,7 +517,7 @@ type LocalStateDB struct {
|
||||
func NewLocalStateDB(path string, synchronizerDB *StateDB, typ TypeStateDB, nLevels int) (*LocalStateDB, error) {
|
||||
s, err := NewStateDB(path, typ, nLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
return &LocalStateDB{
|
||||
s,
|
||||
@@ -540,44 +541,44 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
|
||||
// use checkpoint from SynchronizerStateDB
|
||||
if _, err := os.Stat(synchronizerCheckpointPath); os.IsNotExist(err) {
|
||||
// if synchronizerStateDB does not have checkpoint at batchNum, return err
|
||||
return fmt.Errorf("Checkpoint not exist in Synchronizer")
|
||||
return tracerr.Wrap(fmt.Errorf("Checkpoint not exist in Synchronizer"))
|
||||
}
|
||||
|
||||
if err := l.db.Pebble().Close(); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// remove 'current'
|
||||
err := os.RemoveAll(currentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// copy synchronizer'BatchNumX' to 'current'
|
||||
err = pebbleMakeCheckpoint(synchronizerCheckpointPath, currentPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// copy synchronizer'BatchNumX' to 'BatchNumX'
|
||||
err = pebbleMakeCheckpoint(synchronizerCheckpointPath, checkpointPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// open the new 'current'
|
||||
sto, err := pebble.NewPebbleStorage(currentPath, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
l.db = sto
|
||||
|
||||
// get currentBatch num
|
||||
l.currentBatch, err = l.GetCurrentBatch()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// open the MT for the current s.db
|
||||
mt, err := merkletree.NewMerkleTree(l.db.WithPrefix(PrefixKeyMT), l.mt.MaxLevels())
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
l.mt = mt
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
ethCrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -69,7 +70,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
v, err = sdb.db.Get(k0)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
|
||||
assert.Nil(t, v)
|
||||
|
||||
// store the same data from the beginning that has ben lost since last NewStateDB
|
||||
@@ -116,7 +117,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
|
||||
|
||||
v, err = sdb.db.Get(k1)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
|
||||
assert.Nil(t, v)
|
||||
}
|
||||
|
||||
@@ -138,7 +139,7 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
unexistingAccount := common.Idx(1)
|
||||
_, err = sdb.GetAccount(unexistingAccount)
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
|
||||
|
||||
// add test accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
@@ -159,7 +160,7 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.CreateAccount(common.Idx(256), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, tracerr.Unwrap(err))
|
||||
|
||||
// update accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
@@ -171,7 +172,7 @@ func TestStateDBWithoutMT(t *testing.T) {
|
||||
|
||||
_, err = sdb.MTGetProof(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrStateDBWithoutMT, err)
|
||||
assert.Equal(t, ErrStateDBWithoutMT, tracerr.Unwrap(err))
|
||||
}
|
||||
|
||||
func TestStateDBWithMT(t *testing.T) {
|
||||
@@ -191,7 +192,7 @@ func TestStateDBWithMT(t *testing.T) {
|
||||
// get non-existing account, expecting an error
|
||||
_, err = sdb.GetAccount(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
assert.Equal(t, db.ErrNotFound, tracerr.Unwrap(err))
|
||||
|
||||
// add test accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
@@ -210,7 +211,7 @@ func TestStateDBWithMT(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.CreateAccount(common.Idx(256), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, tracerr.Unwrap(err))
|
||||
|
||||
_, err = sdb.MTGetProof(common.Idx(256))
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/iden3/go-merkletree"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
@@ -70,12 +71,12 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
var createdAccounts []common.Account
|
||||
|
||||
if s.zki != nil {
|
||||
return nil, errors.New("Expected StateDB.zki==nil, something went wrong and it's not empty")
|
||||
return nil, tracerr.Wrap(errors.New("Expected StateDB.zki==nil, something went wrong and it's not empty"))
|
||||
}
|
||||
defer s.resetZKInputs()
|
||||
|
||||
if len(coordIdxs) >= int(ptc.MaxFeeTx) {
|
||||
return nil, fmt.Errorf("CoordIdxs (%d) length must be smaller than MaxFeeTx (%d)", len(coordIdxs), ptc.MaxFeeTx)
|
||||
return nil, tracerr.Wrap(fmt.Errorf("CoordIdxs (%d) length must be smaller than MaxFeeTx (%d)", len(coordIdxs), ptc.MaxFeeTx))
|
||||
}
|
||||
|
||||
s.accumulatedFees = make(map[common.Idx]*big.Int)
|
||||
@@ -104,7 +105,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
|
||||
tmpDir, err := ioutil.TempDir("", "hermez-statedb-exittree")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := os.RemoveAll(tmpDir); err != nil {
|
||||
@@ -113,11 +114,11 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
}()
|
||||
sto, err := pebble.NewPebbleStorage(tmpDir, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
exitTree, err = merkletree.NewMerkleTree(sto, s.mt.MaxLevels())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +127,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
// assumption: l1usertx are sorted by L1Tx.Position
|
||||
exitIdx, exitAccount, newExit, createdAccount, err := s.processL1Tx(exitTree, &l1usertxs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if s.typ == TypeSynchronizer && createdAccount != nil {
|
||||
createdAccounts = append(createdAccounts, *createdAccount)
|
||||
@@ -135,13 +136,13 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
if s.zki != nil {
|
||||
l1TxData, err := l1usertxs[i].BytesGeneric()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.Metadata.L1TxsData = append(s.zki.Metadata.L1TxsData, l1TxData)
|
||||
|
||||
l1TxDataAvailability, err := l1usertxs[i].BytesDataAvailability(s.zki.Metadata.NLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.Metadata.L1TxsDataAvailability = append(s.zki.Metadata.L1TxsDataAvailability, l1TxDataAvailability)
|
||||
|
||||
@@ -167,7 +168,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
for i := 0; i < len(l1coordinatortxs); i++ {
|
||||
exitIdx, _, _, createdAccount, err := s.processL1Tx(exitTree, &l1coordinatortxs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if exitIdx != nil {
|
||||
log.Error("Unexpected Exit in L1CoordinatorTx")
|
||||
@@ -178,7 +179,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
if s.zki != nil {
|
||||
l1TxData, err := l1coordinatortxs[i].BytesGeneric()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.Metadata.L1TxsData = append(s.zki.Metadata.L1TxsData, l1TxData)
|
||||
|
||||
@@ -200,7 +201,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
// created in the current batch, at this point the Idx will be created
|
||||
coordIdxsMap, err := s.getTokenIDsFromIdxs(coordIdxs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
// collectedFees will contain the amount of fee collected for each
|
||||
// TokenID
|
||||
@@ -217,7 +218,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
feePlanTokens, err := s.getFeePlanTokens(coordIdxs, l2txs)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
copy(s.zki.FeePlanTokens, feePlanTokens)
|
||||
}
|
||||
@@ -226,12 +227,12 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
for i := 0; i < len(l2txs); i++ {
|
||||
exitIdx, exitAccount, newExit, err := s.processL2Tx(coordIdxsMap, collectedFees, exitTree, &l2txs[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
l2TxData, err := l2txs[i].L2Tx().BytesDataAvailability(s.zki.Metadata.NLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.Metadata.L2TxsData = append(s.zki.Metadata.L2TxsData, l2TxData)
|
||||
|
||||
@@ -271,13 +272,13 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
accCoord, err := s.GetAccount(idx)
|
||||
if err != nil {
|
||||
log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx)
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee)
|
||||
pFee, err := s.UpdateAccount(idx, accCoord)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt()
|
||||
@@ -315,7 +316,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
// 0. generate MerkleProof
|
||||
p, err := exitTree.GenerateCircomVerifierProof(exitIdx.BigInt(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
// 1. generate common.ExitInfo
|
||||
ei := common.ExitInfo{
|
||||
@@ -389,7 +390,7 @@ func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx, l2txs []common.PoolL2
|
||||
acc, err := s.GetAccount(coordIdxs[i])
|
||||
if err != nil {
|
||||
log.Errorf("could not get account to determine TokenID of CoordIdx %d not found: %s", coordIdxs[i], err.Error())
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
coordTokenIDs[acc.TokenID] = true
|
||||
}
|
||||
@@ -401,7 +402,7 @@ func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx, l2txs []common.PoolL2
|
||||
acc, err := s.GetAccount(l2txs[i].FromIdx)
|
||||
if err != nil {
|
||||
log.Errorf("could not get account to determine TokenID of L2Tx: FromIdx %d not found: %s", l2txs[i].FromIdx, err.Error())
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
if _, ok := coordTokenIDs[acc.TokenID]; ok {
|
||||
tokenIDs[acc.TokenID] = true
|
||||
@@ -429,7 +430,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
s.zki.TxCompressedData[s.i], err = tx.TxCompressedData()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
|
||||
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
|
||||
@@ -460,7 +461,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
err := s.applyTransfer(nil, nil, tx.Tx(), 0)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
case common.TxTypeCreateAccountDeposit:
|
||||
s.computeEffectiveAmounts(tx)
|
||||
@@ -469,7 +470,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
err := s.applyCreateAccount(tx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
// TODO applyCreateAccount will return the created account,
|
||||
// which in the case type==TypeSynchronizer will be added to an
|
||||
@@ -481,7 +482,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
err := s.applyDeposit(tx, false)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
case common.TxTypeDepositTransfer:
|
||||
s.computeEffectiveAmounts(tx)
|
||||
@@ -491,7 +492,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
err := s.applyDeposit(tx, true)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
case common.TxTypeCreateAccountDepositTransfer:
|
||||
s.computeEffectiveAmounts(tx)
|
||||
@@ -501,7 +502,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
err := s.applyCreateAccountDepositTransfer(tx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
case common.TxTypeForceExit:
|
||||
s.computeEffectiveAmounts(tx)
|
||||
@@ -511,7 +512,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
exitAccount, newExit, err := s.applyExit(nil, nil, exitTree, tx.Tx())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
return &tx.FromIdx, exitAccount, newExit, nil, nil
|
||||
default:
|
||||
@@ -523,7 +524,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx)
|
||||
createdAccount, err = s.GetAccount(s.idx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, nil, err
|
||||
return nil, nil, false, nil, tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,12 +543,12 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collec
|
||||
if s.typ == TypeSynchronizer {
|
||||
// this should never be reached
|
||||
log.Error("WARNING: In StateDB with Synchronizer mode L2.ToIdx can't be 0")
|
||||
return nil, nil, false, fmt.Errorf("In StateDB with Synchronizer mode L2.ToIdx can't be 0")
|
||||
return nil, nil, false, tracerr.Wrap(fmt.Errorf("In StateDB with Synchronizer mode L2.ToIdx can't be 0"))
|
||||
}
|
||||
// case when tx.Type== common.TxTypeTransferToEthAddr or common.TxTypeTransferToBJJ
|
||||
tx.AuxToIdx, err = s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ, tx.TokenID)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -556,11 +557,11 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collec
|
||||
// Txs
|
||||
s.zki.TxCompressedData[s.i], err = tx.TxCompressedData()
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.TxCompressedDataV2[s.i], err = tx.TxCompressedDataV2()
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
|
||||
s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
|
||||
@@ -589,7 +590,7 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collec
|
||||
signature, err := tx.Signature.Decompress()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
s.zki.S[s.i] = signature.S
|
||||
s.zki.R8x[s.i] = signature.R8.X
|
||||
@@ -602,7 +603,7 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collec
|
||||
acc, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
log.Errorw("GetAccount", "fromIdx", tx.FromIdx, "err", err)
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
tx.Nonce = acc.Nonce + 1
|
||||
tx.TokenID = acc.TokenID
|
||||
@@ -615,14 +616,14 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collec
|
||||
err = s.applyTransfer(coordIdxsMap, collectedFees, tx.Tx(), tx.AuxToIdx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
case common.TxTypeExit:
|
||||
// execute exit flow
|
||||
exitAccount, newExit, err := s.applyExit(coordIdxsMap, collectedFees, exitTree, tx.Tx())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, nil, false, err
|
||||
return nil, nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
return &tx.FromIdx, exitAccount, newExit, nil
|
||||
default:
|
||||
@@ -643,7 +644,7 @@ func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
|
||||
|
||||
p, err := s.CreateAccount(common.Idx(s.idx+1), account)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
|
||||
@@ -683,7 +684,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// deposit the tx.EffectiveLoadAmount into the sender account
|
||||
accSender, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
accSender.Balance = new(big.Int).Add(accSender.Balance, tx.EffectiveLoadAmount)
|
||||
|
||||
@@ -692,7 +693,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
if transfer {
|
||||
accReceiver, err = s.GetAccount(tx.ToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// subtract amount to the sender
|
||||
accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
|
||||
@@ -702,7 +703,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// update sender account in localStateDB
|
||||
p, err := s.UpdateAccount(tx.FromIdx, accSender)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
|
||||
@@ -722,7 +723,7 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
|
||||
// update receiver account in localStateDB
|
||||
p, err := s.UpdateAccount(tx.ToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
|
||||
@@ -757,7 +758,7 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
accSender, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if !tx.IsL1 {
|
||||
// increment nonce
|
||||
@@ -766,7 +767,7 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
// compute fee and subtract it from the accSender
|
||||
fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
feeAndAmount := new(big.Int).Add(tx.Amount, fee)
|
||||
accSender.Balance = new(big.Int).Sub(accSender.Balance, feeAndAmount)
|
||||
@@ -798,7 +799,7 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
accReceiver, err = s.GetAccount(auxToIdx)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,7 +810,7 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
pSender, err := s.UpdateAccount(tx.FromIdx, accSender)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
|
||||
@@ -826,7 +827,7 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
// update receiver account in localStateDB
|
||||
pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
|
||||
@@ -855,7 +856,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
}
|
||||
accReceiver, err := s.GetAccount(tx.ToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// subtract amount to the sender
|
||||
accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
|
||||
@@ -865,7 +866,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
// create Account of the Sender
|
||||
p, err := s.CreateAccount(common.Idx(s.idx+1), accSender)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
|
||||
@@ -895,7 +896,7 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
|
||||
// update receiver account in localStateDB
|
||||
p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
|
||||
@@ -922,7 +923,7 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
// add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
|
||||
acc, err := s.GetAccount(tx.FromIdx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
return nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
if !tx.IsL1 {
|
||||
@@ -932,7 +933,7 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
// compute fee and subtract it from the accSender
|
||||
fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
return nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
feeAndAmount := new(big.Int).Add(tx.Amount, fee)
|
||||
acc.Balance = new(big.Int).Sub(acc.Balance, feeAndAmount)
|
||||
@@ -956,7 +957,7 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
|
||||
p, err := s.UpdateAccount(tx.FromIdx, acc)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
return nil, false, tracerr.Wrap(err)
|
||||
}
|
||||
if s.zki != nil {
|
||||
s.zki.TokenID1[s.i] = acc.TokenID.BigInt()
|
||||
@@ -974,7 +975,7 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
return nil, false, nil
|
||||
}
|
||||
exitAccount, err := getAccountInTreeDB(exitTree.DB(), tx.FromIdx)
|
||||
if err == db.ErrNotFound {
|
||||
if tracerr.Unwrap(err) == db.ErrNotFound {
|
||||
// 1a. if idx does not exist in exitTree:
|
||||
// add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance = exitAmount (exitAmount=tx.Amount)
|
||||
exitAccount := &common.Account{
|
||||
@@ -985,16 +986,16 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
|
||||
EthAddr: acc.EthAddr,
|
||||
}
|
||||
_, err = createAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
|
||||
return exitAccount, true, err
|
||||
return exitAccount, true, tracerr.Wrap(err)
|
||||
} else if err != nil {
|
||||
return exitAccount, false, err
|
||||
return exitAccount, false, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// 1b. if idx already exist in exitTree:
|
||||
// update account, where account.Balance += exitAmount
|
||||
exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
|
||||
_, err = updateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
|
||||
return exitAccount, false, err
|
||||
return exitAccount, false, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// computeEffectiveAmounts checks that the L1Tx data is correct
|
||||
@@ -1084,11 +1085,11 @@ func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
|
||||
// used for an Account in the localStateDB.
|
||||
func (s *StateDB) getIdx() (common.Idx, error) {
|
||||
idxBytes, err := s.DB().Get(keyidx)
|
||||
if err == db.ErrNotFound {
|
||||
if tracerr.Unwrap(err) == db.ErrNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, tracerr.Wrap(err)
|
||||
}
|
||||
return common.IdxFromBytes(idxBytes[:])
|
||||
}
|
||||
@@ -1097,18 +1098,18 @@ func (s *StateDB) getIdx() (common.Idx, error) {
|
||||
func (s *StateDB) setIdx(idx common.Idx) error {
|
||||
tx, err := s.DB().NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
idxBytes, err := idx.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Put(keyidx, idxBytes[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/iden3/go-merkletree"
|
||||
)
|
||||
@@ -46,7 +47,7 @@ func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr ethCommon.Address, pk
|
||||
}
|
||||
|
||||
if pk == nil {
|
||||
return fmt.Errorf("BabyJubJub pk not defined")
|
||||
return tracerr.Wrap(fmt.Errorf("BabyJubJub pk not defined"))
|
||||
}
|
||||
|
||||
// store idx for EthAddr & BJJ assuming that EthAddr & BJJ still don't
|
||||
@@ -55,28 +56,28 @@ func (s *StateDB) setIdxByEthAddrBJJ(idx common.Idx, addr ethCommon.Address, pk
|
||||
// (smaller)
|
||||
tx, err := s.db.NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
idxBytes, err := idx.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
// store Addr&BJJ-idx
|
||||
k := concatEthAddrBJJTokenID(addr, pk, tokenID)
|
||||
err = tx.Put(append(PrefixKeyAddrBJJ, k...), idxBytes[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// store Addr-idx
|
||||
k = concatEthAddrTokenID(addr, tokenID)
|
||||
err = tx.Put(append(PrefixKeyAddr, k...), idxBytes[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -88,11 +89,11 @@ func (s *StateDB) GetIdxByEthAddr(addr ethCommon.Address, tokenID common.TokenID
|
||||
k := concatEthAddrTokenID(addr, tokenID)
|
||||
b, err := s.db.Get(append(PrefixKeyAddr, k...))
|
||||
if err != nil {
|
||||
return common.Idx(0), fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d", ErrToIdxNotFound, addr.Hex(), tokenID)
|
||||
return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d", ErrToIdxNotFound, addr.Hex(), tokenID))
|
||||
}
|
||||
idx, err := common.IdxFromBytes(b)
|
||||
if err != nil {
|
||||
return common.Idx(0), fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d", err, addr.Hex(), tokenID)
|
||||
return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddr: %s: ToEthAddr: %s, TokenID: %d", err, addr.Hex(), tokenID))
|
||||
}
|
||||
return idx, nil
|
||||
}
|
||||
@@ -112,16 +113,16 @@ func (s *StateDB) GetIdxByEthAddrBJJ(addr ethCommon.Address, pk *babyjub.PublicK
|
||||
k := concatEthAddrBJJTokenID(addr, pk, tokenID)
|
||||
b, err := s.db.Get(append(PrefixKeyAddrBJJ, k...))
|
||||
if err != nil {
|
||||
return common.Idx(0), fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrToIdxNotFound, addr.Hex(), pk, tokenID)
|
||||
return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrToIdxNotFound, addr.Hex(), pk, tokenID))
|
||||
}
|
||||
idx, err := common.IdxFromBytes(b)
|
||||
if err != nil {
|
||||
return common.Idx(0), fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", err, addr.Hex(), pk, tokenID)
|
||||
return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", err, addr.Hex(), pk, tokenID))
|
||||
}
|
||||
return idx, nil
|
||||
}
|
||||
// rest of cases (included case ToEthAddr==0) are not possible
|
||||
return common.Idx(0), fmt.Errorf("GetIdxByEthAddrBJJ: Not found, %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrGetIdxNoCase, addr.Hex(), pk, tokenID)
|
||||
return common.Idx(0), tracerr.Wrap(fmt.Errorf("GetIdxByEthAddrBJJ: Not found, %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrGetIdxNoCase, addr.Hex(), pk, tokenID))
|
||||
}
|
||||
|
||||
func (s *StateDB) getTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]common.Idx, error) {
|
||||
@@ -129,7 +130,7 @@ func (s *StateDB) getTokenIDsFromIdxs(idxs []common.Idx) (map[common.TokenID]com
|
||||
for i := 0; i < len(idxs); i++ {
|
||||
a, err := s.GetAccount(idxs[i])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getTokenIDsFromIdxs error on GetAccount with Idx==%d: %s", idxs[i], err.Error())
|
||||
return nil, tracerr.Wrap(fmt.Errorf("getTokenIDsFromIdxs error on GetAccount with Idx==%d: %s", idxs[i], err.Error()))
|
||||
}
|
||||
m[a.TokenID] = idxs[i]
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -84,7 +85,7 @@ func TestGetIdx(t *testing.T) {
|
||||
idxR, err = sdb.GetIdxByEthAddrBJJ(addr2, pk2, tokenID0)
|
||||
assert.NotNil(t, err)
|
||||
expectedErr := fmt.Errorf("GetIdxByEthAddrBJJ: %s: ToEthAddr: %s, ToBJJ: %s, TokenID: %d", ErrToIdxNotFound, addr2.Hex(), pk2, tokenID0)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
assert.Equal(t, expectedErr, tracerr.Unwrap(err))
|
||||
assert.Equal(t, common.Idx(0), idxR)
|
||||
// expect error when trying to get Idx by addr with not used TokenID
|
||||
_, err = sdb.GetIdxByEthAddr(addr, tokenID1)
|
||||
|
||||
13
db/utils.go
13
db/utils.go
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/jmoiron/sqlx"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
"github.com/russross/meddler"
|
||||
@@ -29,7 +30,7 @@ func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) {
|
||||
)
|
||||
db, err := sqlx.Connect("postgres", psqlconn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
// Run DB migrations
|
||||
migrations := &migrate.PackrMigrationSource{
|
||||
@@ -37,7 +38,7 @@ func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) {
|
||||
}
|
||||
nMigrations, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
log.Info("successfully ran ", nMigrations, " migrations")
|
||||
return db, nil
|
||||
@@ -64,7 +65,7 @@ func BulkInsert(db meddler.DB, q string, args interface{}) error {
|
||||
arg := arrayValue.Index(i).Addr().Interface()
|
||||
elemArglist, err := meddler.Default.Values(arg, true)
|
||||
if err != nil {
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
arglist = append(arglist, elemArglist...)
|
||||
value := "("
|
||||
@@ -76,7 +77,7 @@ func BulkInsert(db meddler.DB, q string, args interface{}) error {
|
||||
}
|
||||
stmt := fmt.Sprintf(q, strings.Join(valueStrings, ","))
|
||||
_, err := db.Exec(stmt, arglist...)
|
||||
return err
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// BigIntMeddler encodes or decodes the field value to or from JSON
|
||||
@@ -92,7 +93,7 @@ func (b BigIntMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, e
|
||||
func (b BigIntMeddler) PostRead(fieldPtr, scanTarget interface{}) error {
|
||||
ptr := scanTarget.(*string)
|
||||
if ptr == nil {
|
||||
return fmt.Errorf("BigIntMeddler.PostRead: nil pointer")
|
||||
return tracerr.Wrap(fmt.Errorf("BigIntMeddler.PostRead: nil pointer"))
|
||||
}
|
||||
field := fieldPtr.(**big.Int)
|
||||
*field = new(big.Int).SetBytes([]byte(*ptr))
|
||||
@@ -126,7 +127,7 @@ func (b BigIntNullMeddler) PostRead(fieldPtr, scanTarget interface{}) error {
|
||||
// not null
|
||||
ptr := (*ptrPtr).([]byte)
|
||||
if ptr == nil {
|
||||
return fmt.Errorf("BigIntMeddler.PostRead: nil pointer")
|
||||
return tracerr.Wrap(fmt.Errorf("BigIntMeddler.PostRead: nil pointer"))
|
||||
}
|
||||
*field = new(big.Int).SetBytes(ptr)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user