mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Diferentiate SQL connections by read write
This commit is contained in:
@@ -34,7 +34,7 @@ func (hdb *HistoryDB) GetBatchAPI(batchNum common.BatchNum) (*BatchAPI, error) {
|
||||
defer hdb.apiConnCon.Release()
|
||||
batch := &BatchAPI{}
|
||||
return batch, tracerr.Wrap(meddler.QueryRow(
|
||||
hdb.db, batch,
|
||||
hdb.dbRead, batch,
|
||||
`SELECT batch.item_id, batch.batch_num, batch.eth_block_num,
|
||||
batch.forger_addr, batch.fees_collected, batch.total_fees_usd, batch.state_root,
|
||||
batch.num_accounts, batch.exit_root, batch.forge_l1_txs_num, batch.slot_num,
|
||||
@@ -133,10 +133,10 @@ func (hdb *HistoryDB) GetBatchesAPI(
|
||||
queryStr += " DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
query = hdb.dbRead.Rebind(queryStr)
|
||||
// log.Debug(query)
|
||||
batchPtrs := []*BatchAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &batchPtrs, query, args...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &batchPtrs, query, args...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
batches := db.SlicePtrsToSlice(batchPtrs).([]BatchAPI)
|
||||
@@ -156,7 +156,7 @@ func (hdb *HistoryDB) GetBestBidAPI(slotNum *int64) (BidAPI, error) {
|
||||
}
|
||||
defer hdb.apiConnCon.Release()
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, bid, `SELECT bid.*, block.timestamp, coordinator.forger_addr, coordinator.url
|
||||
hdb.dbRead, bid, `SELECT bid.*, block.timestamp, coordinator.forger_addr, coordinator.url
|
||||
FROM bid INNER JOIN block ON bid.eth_block_num = block.eth_block_num
|
||||
INNER JOIN (
|
||||
SELECT bidder_addr, MAX(item_id) AS item_id FROM coordinator
|
||||
@@ -212,9 +212,9 @@ func (hdb *HistoryDB) GetBestBidsAPI(
|
||||
if limit != nil {
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
}
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
query = hdb.dbRead.Rebind(queryStr)
|
||||
bidPtrs := []*BidAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &bidPtrs, query, args...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &bidPtrs, query, args...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
// log.Debug(query)
|
||||
@@ -296,9 +296,9 @@ func (hdb *HistoryDB) GetBidsAPI(
|
||||
if err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
query = hdb.dbRead.Rebind(query)
|
||||
bids := []*BidAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &bids, query, argsQ...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &bids, query, argsQ...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(bids) == 0 {
|
||||
@@ -384,9 +384,9 @@ func (hdb *HistoryDB) GetTokensAPI(
|
||||
if err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
query = hdb.dbRead.Rebind(query)
|
||||
tokens := []*TokenWithUSD{}
|
||||
if err := meddler.QueryAll(hdb.db, &tokens, query, argsQ...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &tokens, query, argsQ...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(tokens) == 0 {
|
||||
@@ -408,7 +408,7 @@ func (hdb *HistoryDB) GetTxAPI(txID common.TxID) (*TxAPI, error) {
|
||||
defer hdb.apiConnCon.Release()
|
||||
tx := &TxAPI{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, tx, `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
|
||||
hdb.dbRead, tx, `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position,
|
||||
hez_idx(tx.effective_from_idx, token.symbol) AS from_idx, tx.from_eth_addr, tx.from_bjj,
|
||||
hez_idx(tx.to_idx, token.symbol) AS to_idx, tx.to_eth_addr, tx.to_bjj,
|
||||
tx.amount, tx.amount_success, tx.token_id, tx.amount_usd,
|
||||
@@ -541,10 +541,10 @@ func (hdb *HistoryDB) GetTxsAPI(
|
||||
queryStr += " DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
query = hdb.dbRead.Rebind(queryStr)
|
||||
// log.Debug(query)
|
||||
txsPtrs := []*TxAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &txsPtrs, query, args...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &txsPtrs, query, args...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
txs := db.SlicePtrsToSlice(txsPtrs).([]TxAPI)
|
||||
@@ -564,7 +564,7 @@ func (hdb *HistoryDB) GetExitAPI(batchNum *uint, idx *common.Idx) (*ExitAPI, err
|
||||
defer hdb.apiConnCon.Release()
|
||||
exit := &ExitAPI{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, exit, `SELECT exit_tree.item_id, exit_tree.batch_num,
|
||||
hdb.dbRead, exit, `SELECT exit_tree.item_id, exit_tree.batch_num,
|
||||
hez_idx(exit_tree.account_idx, token.symbol) AS account_idx,
|
||||
account.bjj, account.eth_addr,
|
||||
exit_tree.merkle_proof, exit_tree.balance, exit_tree.instant_withdrawn,
|
||||
@@ -685,10 +685,10 @@ func (hdb *HistoryDB) GetExitsAPI(
|
||||
queryStr += " DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
query = hdb.dbRead.Rebind(queryStr)
|
||||
// log.Debug(query)
|
||||
exits := []*ExitAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &exits, query, args...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(exits) == 0 {
|
||||
@@ -707,7 +707,7 @@ func (hdb *HistoryDB) GetBucketUpdatesAPI() ([]BucketUpdateAPI, error) {
|
||||
defer hdb.apiConnCon.Release()
|
||||
var bucketUpdates []*BucketUpdateAPI
|
||||
err = meddler.QueryAll(
|
||||
hdb.db, &bucketUpdates,
|
||||
hdb.dbRead, &bucketUpdates,
|
||||
`SELECT num_bucket, withdrawals FROM bucket_update
|
||||
WHERE item_id in(SELECT max(item_id) FROM bucket_update
|
||||
group by num_bucket)
|
||||
@@ -772,10 +772,10 @@ func (hdb *HistoryDB) GetCoordinatorsAPI(
|
||||
queryStr += " DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
query = hdb.dbRead.Rebind(queryStr)
|
||||
|
||||
coordinators := []*CoordinatorAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &coordinators, query, args...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(coordinators) == 0 {
|
||||
@@ -795,7 +795,7 @@ func (hdb *HistoryDB) GetAuctionVarsAPI() (*common.AuctionVariables, error) {
|
||||
defer hdb.apiConnCon.Release()
|
||||
auctionVars := &common.AuctionVariables{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, auctionVars, `SELECT * FROM auction_vars;`,
|
||||
hdb.dbRead, auctionVars, `SELECT * FROM auction_vars;`,
|
||||
)
|
||||
return auctionVars, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -816,7 +816,7 @@ func (hdb *HistoryDB) GetAuctionVarsUntilSetSlotNumAPI(slotNum int64, maxItems i
|
||||
ORDER BY default_slot_set_bid_slot_num DESC
|
||||
LIMIT $2;
|
||||
`
|
||||
err = meddler.QueryAll(hdb.db, &auctionVars, query, slotNum, maxItems)
|
||||
err = meddler.QueryAll(hdb.dbRead, &auctionVars, query, slotNum, maxItems)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -832,7 +832,7 @@ func (hdb *HistoryDB) GetAccountAPI(idx common.Idx) (*AccountAPI, error) {
|
||||
}
|
||||
defer hdb.apiConnCon.Release()
|
||||
account := &AccountAPI{}
|
||||
err = meddler.QueryRow(hdb.db, account, `SELECT account.item_id, hez_idx(account.idx,
|
||||
err = meddler.QueryRow(hdb.dbRead, account, `SELECT account.item_id, hez_idx(account.idx,
|
||||
token.symbol) as idx, account.batch_num, account.bjj, account.eth_addr,
|
||||
token.token_id, token.item_id AS token_item_id, token.eth_block_num AS token_block,
|
||||
token.eth_addr as token_eth_addr, token.name, token.symbol, token.decimals, token.usd,
|
||||
@@ -927,10 +927,10 @@ func (hdb *HistoryDB) GetAccountsAPI(
|
||||
if err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
query = hdb.dbRead.Rebind(query)
|
||||
|
||||
accounts := []*AccountAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &accounts, query, argsQ...); err != nil {
|
||||
if err := meddler.QueryAll(hdb.dbRead, &accounts, query, argsQ...); err != nil {
|
||||
return nil, 0, tracerr.Wrap(err)
|
||||
}
|
||||
if len(accounts) == 0 {
|
||||
@@ -952,7 +952,7 @@ func (hdb *HistoryDB) GetMetricsAPI(lastBatchNum common.BatchNum) (*Metrics, err
|
||||
metricsTotals := &MetricsTotals{}
|
||||
metrics := &Metrics{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metricsTotals, `SELECT COUNT(tx.*) as total_txs,
|
||||
hdb.dbRead, metricsTotals, `SELECT COUNT(tx.*) as total_txs,
|
||||
COALESCE (MIN(tx.batch_num), 0) as batch_num, COALESCE (MIN(block.timestamp),
|
||||
NOW()) AS min_timestamp, COALESCE (MAX(block.timestamp), NOW()) AS max_timestamp
|
||||
FROM tx INNER JOIN block ON tx.eth_block_num = block.eth_block_num
|
||||
@@ -977,7 +977,7 @@ func (hdb *HistoryDB) GetMetricsAPI(lastBatchNum common.BatchNum) (*Metrics, err
|
||||
}
|
||||
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches,
|
||||
hdb.dbRead, metricsTotals, `SELECT COUNT(*) AS total_batches,
|
||||
COALESCE (SUM(total_fees_usd), 0) AS total_fees FROM batch
|
||||
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
|
||||
if err != nil {
|
||||
@@ -994,7 +994,7 @@ func (hdb *HistoryDB) GetMetricsAPI(lastBatchNum common.BatchNum) (*Metrics, err
|
||||
metrics.AvgTransactionFee = 0
|
||||
}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metrics,
|
||||
hdb.dbRead, metrics,
|
||||
`SELECT COUNT(*) AS total_bjjs, COUNT(DISTINCT(bjj)) AS total_accounts FROM account;`)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
@@ -1013,7 +1013,7 @@ func (hdb *HistoryDB) GetAvgTxFeeAPI() (float64, error) {
|
||||
defer hdb.apiConnCon.Release()
|
||||
metricsTotals := &MetricsTotals{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metricsTotals, `SELECT COUNT(tx.*) as total_txs,
|
||||
hdb.dbRead, metricsTotals, `SELECT COUNT(tx.*) as total_txs,
|
||||
COALESCE (MIN(tx.batch_num), 0) as batch_num
|
||||
FROM tx INNER JOIN block ON tx.eth_block_num = block.eth_block_num
|
||||
WHERE block.timestamp >= NOW() - INTERVAL '1 HOURS';`)
|
||||
@@ -1021,7 +1021,7 @@ func (hdb *HistoryDB) GetAvgTxFeeAPI() (float64, error) {
|
||||
return 0, tracerr.Wrap(err)
|
||||
}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches,
|
||||
hdb.dbRead, metricsTotals, `SELECT COUNT(*) AS total_batches,
|
||||
COALESCE (SUM(total_fees_usd), 0) AS total_fees FROM batch
|
||||
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
|
||||
if err != nil {
|
||||
@@ -1048,7 +1048,7 @@ func (hdb *HistoryDB) GetCommonAccountAPI(idx common.Idx) (*common.Account, erro
|
||||
defer hdb.apiConnCon.Release()
|
||||
account := &common.Account{}
|
||||
err = meddler.QueryRow(
|
||||
hdb.db, account, `SELECT * FROM account WHERE idx = $1;`, idx,
|
||||
hdb.dbRead, account, `SELECT * FROM account WHERE idx = $1;`, idx,
|
||||
)
|
||||
return account, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -27,30 +27,35 @@ const (
|
||||
|
||||
// HistoryDB persist the historic of the rollup
|
||||
type HistoryDB struct {
|
||||
db *sqlx.DB
|
||||
dbRead *sqlx.DB
|
||||
dbWrite *sqlx.DB
|
||||
apiConnCon *db.APIConnectionController
|
||||
}
|
||||
|
||||
// NewHistoryDB initialize the DB
|
||||
func NewHistoryDB(db *sqlx.DB, apiConnCon *db.APIConnectionController) *HistoryDB {
|
||||
return &HistoryDB{db: db, apiConnCon: apiConnCon}
|
||||
func NewHistoryDB(dbRead, dbWrite *sqlx.DB, apiConnCon *db.APIConnectionController) *HistoryDB {
|
||||
return &HistoryDB{
|
||||
dbRead: dbRead,
|
||||
dbWrite: dbWrite,
|
||||
apiConnCon: apiConnCon,
|
||||
}
|
||||
}
|
||||
|
||||
// DB returns a pointer to the L2DB.db. This method should be used only for
|
||||
// internal testing purposes.
|
||||
func (hdb *HistoryDB) DB() *sqlx.DB {
|
||||
return hdb.db
|
||||
return hdb.dbWrite
|
||||
}
|
||||
|
||||
// AddBlock insert a block into the DB
|
||||
func (hdb *HistoryDB) AddBlock(block *common.Block) error { return hdb.addBlock(hdb.db, block) }
|
||||
func (hdb *HistoryDB) AddBlock(block *common.Block) error { return hdb.addBlock(hdb.dbWrite, block) }
|
||||
func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
|
||||
return tracerr.Wrap(meddler.Insert(d, "block", block))
|
||||
}
|
||||
|
||||
// AddBlocks inserts blocks into the DB
|
||||
func (hdb *HistoryDB) AddBlocks(blocks []common.Block) error {
|
||||
return tracerr.Wrap(hdb.addBlocks(hdb.db, blocks))
|
||||
return tracerr.Wrap(hdb.addBlocks(hdb.dbWrite, blocks))
|
||||
}
|
||||
|
||||
func (hdb *HistoryDB) addBlocks(d meddler.DB, blocks []common.Block) error {
|
||||
@@ -69,7 +74,7 @@ func (hdb *HistoryDB) addBlocks(d meddler.DB, blocks []common.Block) error {
|
||||
func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
|
||||
block := &common.Block{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, block,
|
||||
hdb.dbRead, block,
|
||||
"SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
|
||||
)
|
||||
return block, tracerr.Wrap(err)
|
||||
@@ -79,7 +84,7 @@ func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
|
||||
func (hdb *HistoryDB) GetAllBlocks() ([]common.Block, error) {
|
||||
var blocks []*common.Block
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &blocks,
|
||||
hdb.dbRead, &blocks,
|
||||
"SELECT * FROM block ORDER BY eth_block_num;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(blocks).([]common.Block), tracerr.Wrap(err)
|
||||
@@ -89,7 +94,7 @@ func (hdb *HistoryDB) GetAllBlocks() ([]common.Block, error) {
|
||||
func (hdb *HistoryDB) getBlocks(from, to int64) ([]common.Block, error) {
|
||||
var blocks []*common.Block
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &blocks,
|
||||
hdb.dbRead, &blocks,
|
||||
"SELECT * FROM block WHERE $1 <= eth_block_num AND eth_block_num < $2 ORDER BY eth_block_num;",
|
||||
from, to,
|
||||
)
|
||||
@@ -100,13 +105,13 @@ func (hdb *HistoryDB) getBlocks(from, to int64) ([]common.Block, error) {
|
||||
func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
|
||||
block := &common.Block{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
|
||||
hdb.dbRead, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
|
||||
)
|
||||
return block, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddBatch insert a Batch into the DB
|
||||
func (hdb *HistoryDB) AddBatch(batch *common.Batch) error { return hdb.addBatch(hdb.db, batch) }
|
||||
func (hdb *HistoryDB) AddBatch(batch *common.Batch) error { return hdb.addBatch(hdb.dbWrite, batch) }
|
||||
func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
|
||||
// Calculate total collected fees in USD
|
||||
// Get IDs of collected tokens for fees
|
||||
@@ -129,9 +134,9 @@ func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
query = hdb.dbWrite.Rebind(query)
|
||||
if err := meddler.QueryAll(
|
||||
hdb.db, &tokenPrices, query, args...,
|
||||
hdb.dbWrite, &tokenPrices, query, args...,
|
||||
); err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
@@ -153,7 +158,7 @@ func (hdb *HistoryDB) addBatch(d meddler.DB, batch *common.Batch) error {
|
||||
|
||||
// AddBatches insert Bids into the DB
|
||||
func (hdb *HistoryDB) AddBatches(batches []common.Batch) error {
|
||||
return tracerr.Wrap(hdb.addBatches(hdb.db, batches))
|
||||
return tracerr.Wrap(hdb.addBatches(hdb.dbWrite, batches))
|
||||
}
|
||||
func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
|
||||
for i := 0; i < len(batches); i++ {
|
||||
@@ -168,7 +173,7 @@ func (hdb *HistoryDB) addBatches(d meddler.DB, batches []common.Batch) error {
|
||||
func (hdb *HistoryDB) GetBatch(batchNum common.BatchNum) (*common.Batch, error) {
|
||||
var batch common.Batch
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
|
||||
hdb.dbRead, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
|
||||
batch.fees_collected, 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 WHERE batch_num = $1;`,
|
||||
@@ -181,7 +186,7 @@ func (hdb *HistoryDB) GetBatch(batchNum common.BatchNum) (*common.Batch, error)
|
||||
func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
|
||||
var batches []*common.Batch
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &batches,
|
||||
hdb.dbRead, &batches,
|
||||
`SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr, batch.fees_collected,
|
||||
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
|
||||
@@ -194,7 +199,7 @@ func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
|
||||
func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, error) {
|
||||
var batches []*common.Batch
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &batches,
|
||||
hdb.dbRead, &batches,
|
||||
`SELECT batch_num, eth_block_num, forger_addr, fees_collected, fee_idxs_coordinator,
|
||||
state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd
|
||||
FROM batch WHERE $1 <= batch_num AND batch_num < $2 ORDER BY batch_num;`,
|
||||
@@ -206,7 +211,7 @@ func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, erro
|
||||
// GetFirstBatchBlockNumBySlot returns the ethereum block number of the first
|
||||
// batch within a slot
|
||||
func (hdb *HistoryDB) GetFirstBatchBlockNumBySlot(slotNum int64) (int64, error) {
|
||||
row := hdb.db.QueryRow(
|
||||
row := hdb.dbRead.QueryRow(
|
||||
`SELECT eth_block_num FROM batch
|
||||
WHERE slot_num = $1 ORDER BY batch_num ASC LIMIT 1;`, slotNum,
|
||||
)
|
||||
@@ -216,7 +221,7 @@ func (hdb *HistoryDB) GetFirstBatchBlockNumBySlot(slotNum int64) (int64, error)
|
||||
|
||||
// GetLastBatchNum returns the BatchNum of the latest forged batch
|
||||
func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
|
||||
row := hdb.db.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
|
||||
row := hdb.dbRead.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
|
||||
var batchNum common.BatchNum
|
||||
return batchNum, tracerr.Wrap(row.Scan(&batchNum))
|
||||
}
|
||||
@@ -225,7 +230,7 @@ func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
|
||||
func (hdb *HistoryDB) GetLastBatch() (*common.Batch, error) {
|
||||
var batch common.Batch
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
|
||||
hdb.dbRead, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
|
||||
batch.fees_collected, 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 ORDER BY batch_num DESC LIMIT 1;`,
|
||||
@@ -235,7 +240,7 @@ func (hdb *HistoryDB) GetLastBatch() (*common.Batch, error) {
|
||||
|
||||
// GetLastL1BatchBlockNum returns the blockNum of the latest forged l1Batch
|
||||
func (hdb *HistoryDB) GetLastL1BatchBlockNum() (int64, error) {
|
||||
row := hdb.db.QueryRow(`SELECT eth_block_num FROM batch
|
||||
row := hdb.dbRead.QueryRow(`SELECT eth_block_num FROM batch
|
||||
WHERE forge_l1_txs_num IS NOT NULL
|
||||
ORDER BY batch_num DESC LIMIT 1;`)
|
||||
var blockNum int64
|
||||
@@ -245,7 +250,7 @@ func (hdb *HistoryDB) GetLastL1BatchBlockNum() (int64, error) {
|
||||
// GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB from forged
|
||||
// batches. If there's no batch in the DB (nil, nil) is returned.
|
||||
func (hdb *HistoryDB) GetLastL1TxsNum() (*int64, error) {
|
||||
row := hdb.db.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
|
||||
row := hdb.dbRead.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
|
||||
lastL1TxsNum := new(int64)
|
||||
return lastL1TxsNum, tracerr.Wrap(row.Scan(&lastL1TxsNum))
|
||||
}
|
||||
@@ -256,15 +261,15 @@ func (hdb *HistoryDB) GetLastL1TxsNum() (*int64, error) {
|
||||
func (hdb *HistoryDB) Reorg(lastValidBlock int64) error {
|
||||
var err error
|
||||
if lastValidBlock < 0 {
|
||||
_, err = hdb.db.Exec("DELETE FROM block;")
|
||||
_, err = hdb.dbWrite.Exec("DELETE FROM block;")
|
||||
} else {
|
||||
_, err = hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
|
||||
_, err = hdb.dbWrite.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
|
||||
}
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
// AddBids insert Bids into the DB
|
||||
func (hdb *HistoryDB) AddBids(bids []common.Bid) error { return hdb.addBids(hdb.db, bids) }
|
||||
func (hdb *HistoryDB) AddBids(bids []common.Bid) error { return hdb.addBids(hdb.dbWrite, bids) }
|
||||
func (hdb *HistoryDB) addBids(d meddler.DB, bids []common.Bid) error {
|
||||
if len(bids) == 0 {
|
||||
return nil
|
||||
@@ -281,7 +286,7 @@ func (hdb *HistoryDB) addBids(d meddler.DB, bids []common.Bid) error {
|
||||
func (hdb *HistoryDB) GetAllBids() ([]common.Bid, error) {
|
||||
var bids []*common.Bid
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &bids,
|
||||
hdb.dbRead, &bids,
|
||||
`SELECT bid.slot_num, bid.bid_value, bid.eth_block_num, bid.bidder_addr FROM bid
|
||||
ORDER BY item_id;`,
|
||||
)
|
||||
@@ -292,7 +297,7 @@ func (hdb *HistoryDB) GetAllBids() ([]common.Bid, error) {
|
||||
func (hdb *HistoryDB) GetBestBidCoordinator(slotNum int64) (*common.BidCoordinator, error) {
|
||||
bidCoord := &common.BidCoordinator{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, bidCoord,
|
||||
hdb.dbRead, bidCoord,
|
||||
`SELECT (
|
||||
SELECT default_slot_set_bid
|
||||
FROM auction_vars
|
||||
@@ -315,7 +320,7 @@ func (hdb *HistoryDB) GetBestBidCoordinator(slotNum int64) (*common.BidCoordinat
|
||||
|
||||
// AddCoordinators insert Coordinators into the DB
|
||||
func (hdb *HistoryDB) AddCoordinators(coordinators []common.Coordinator) error {
|
||||
return tracerr.Wrap(hdb.addCoordinators(hdb.db, coordinators))
|
||||
return tracerr.Wrap(hdb.addCoordinators(hdb.dbWrite, coordinators))
|
||||
}
|
||||
func (hdb *HistoryDB) addCoordinators(d meddler.DB, coordinators []common.Coordinator) error {
|
||||
if len(coordinators) == 0 {
|
||||
@@ -330,7 +335,7 @@ func (hdb *HistoryDB) addCoordinators(d meddler.DB, coordinators []common.Coordi
|
||||
|
||||
// AddExitTree insert Exit tree into the DB
|
||||
func (hdb *HistoryDB) AddExitTree(exitTree []common.ExitInfo) error {
|
||||
return tracerr.Wrap(hdb.addExitTree(hdb.db, exitTree))
|
||||
return tracerr.Wrap(hdb.addExitTree(hdb.dbWrite, exitTree))
|
||||
}
|
||||
func (hdb *HistoryDB) addExitTree(d meddler.DB, exitTree []common.ExitInfo) error {
|
||||
if len(exitTree) == 0 {
|
||||
@@ -418,11 +423,13 @@ func (hdb *HistoryDB) updateExitTree(d sqlx.Ext, blockNum int64,
|
||||
|
||||
// AddToken insert a token into the DB
|
||||
func (hdb *HistoryDB) AddToken(token *common.Token) error {
|
||||
return tracerr.Wrap(meddler.Insert(hdb.db, "token", token))
|
||||
return tracerr.Wrap(meddler.Insert(hdb.dbWrite, "token", token))
|
||||
}
|
||||
|
||||
// AddTokens insert tokens into the DB
|
||||
func (hdb *HistoryDB) AddTokens(tokens []common.Token) error { return hdb.addTokens(hdb.db, tokens) }
|
||||
func (hdb *HistoryDB) AddTokens(tokens []common.Token) error {
|
||||
return hdb.addTokens(hdb.dbWrite, tokens)
|
||||
}
|
||||
func (hdb *HistoryDB) addTokens(d meddler.DB, tokens []common.Token) error {
|
||||
if len(tokens) == 0 {
|
||||
return nil
|
||||
@@ -453,7 +460,7 @@ func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error
|
||||
// Sanitize symbol
|
||||
tokenSymbol = strings.ToValidUTF8(tokenSymbol, " ")
|
||||
|
||||
_, err := hdb.db.Exec(
|
||||
_, err := hdb.dbWrite.Exec(
|
||||
"UPDATE token SET usd = $1 WHERE symbol = $2;",
|
||||
value, tokenSymbol,
|
||||
)
|
||||
@@ -464,7 +471,7 @@ func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error
|
||||
func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenWithUSD, error) {
|
||||
token := &TokenWithUSD{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
|
||||
hdb.dbRead, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
|
||||
)
|
||||
return token, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -473,7 +480,7 @@ func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenWithUSD, error) {
|
||||
func (hdb *HistoryDB) GetAllTokens() ([]TokenWithUSD, error) {
|
||||
var tokens []*TokenWithUSD
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &tokens,
|
||||
hdb.dbRead, &tokens,
|
||||
"SELECT * FROM token ORDER BY token_id;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(tokens).([]TokenWithUSD), tracerr.Wrap(err)
|
||||
@@ -482,7 +489,7 @@ func (hdb *HistoryDB) GetAllTokens() ([]TokenWithUSD, error) {
|
||||
// GetTokenSymbols returns all the token symbols from the DB
|
||||
func (hdb *HistoryDB) GetTokenSymbols() ([]string, error) {
|
||||
var tokenSymbols []string
|
||||
rows, err := hdb.db.Query("SELECT symbol FROM token;")
|
||||
rows, err := hdb.dbRead.Query("SELECT symbol FROM token;")
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -500,7 +507,7 @@ func (hdb *HistoryDB) GetTokenSymbols() ([]string, error) {
|
||||
|
||||
// AddAccounts insert accounts into the DB
|
||||
func (hdb *HistoryDB) AddAccounts(accounts []common.Account) error {
|
||||
return tracerr.Wrap(hdb.addAccounts(hdb.db, accounts))
|
||||
return tracerr.Wrap(hdb.addAccounts(hdb.dbWrite, accounts))
|
||||
}
|
||||
func (hdb *HistoryDB) addAccounts(d meddler.DB, accounts []common.Account) error {
|
||||
if len(accounts) == 0 {
|
||||
@@ -523,7 +530,7 @@ func (hdb *HistoryDB) addAccounts(d meddler.DB, accounts []common.Account) error
|
||||
func (hdb *HistoryDB) GetAllAccounts() ([]common.Account, error) {
|
||||
var accs []*common.Account
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &accs,
|
||||
hdb.dbRead, &accs,
|
||||
"SELECT idx, token_id, batch_num, bjj, eth_addr FROM account ORDER BY idx;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(accs).([]common.Account), tracerr.Wrap(err)
|
||||
@@ -531,7 +538,7 @@ func (hdb *HistoryDB) GetAllAccounts() ([]common.Account, error) {
|
||||
|
||||
// AddAccountUpdates inserts accUpdates into the DB
|
||||
func (hdb *HistoryDB) AddAccountUpdates(accUpdates []common.AccountUpdate) error {
|
||||
return tracerr.Wrap(hdb.addAccountUpdates(hdb.db, accUpdates))
|
||||
return tracerr.Wrap(hdb.addAccountUpdates(hdb.dbWrite, accUpdates))
|
||||
}
|
||||
func (hdb *HistoryDB) addAccountUpdates(d meddler.DB, accUpdates []common.AccountUpdate) error {
|
||||
if len(accUpdates) == 0 {
|
||||
@@ -554,7 +561,7 @@ func (hdb *HistoryDB) addAccountUpdates(d meddler.DB, accUpdates []common.Accoun
|
||||
func (hdb *HistoryDB) GetAllAccountUpdates() ([]common.AccountUpdate, error) {
|
||||
var accUpdates []*common.AccountUpdate
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &accUpdates,
|
||||
hdb.dbRead, &accUpdates,
|
||||
"SELECT eth_block_num, batch_num, idx, nonce, balance FROM account_update ORDER BY idx;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(accUpdates).([]common.AccountUpdate), tracerr.Wrap(err)
|
||||
@@ -565,7 +572,7 @@ func (hdb *HistoryDB) GetAllAccountUpdates() ([]common.AccountUpdate, error) {
|
||||
// BatchNum should be null, and the value will be setted by a trigger when a batch forges the tx.
|
||||
// EffectiveAmount and EffectiveDepositAmount are seted with default values by the DB.
|
||||
func (hdb *HistoryDB) AddL1Txs(l1txs []common.L1Tx) error {
|
||||
return tracerr.Wrap(hdb.addL1Txs(hdb.db, l1txs))
|
||||
return tracerr.Wrap(hdb.addL1Txs(hdb.dbWrite, l1txs))
|
||||
}
|
||||
|
||||
// addL1Txs inserts L1 txs to the DB. USD and DepositAmountUSD will be set automatically before storing the tx.
|
||||
@@ -619,7 +626,7 @@ func (hdb *HistoryDB) addL1Txs(d meddler.DB, l1txs []common.L1Tx) error {
|
||||
|
||||
// AddL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
|
||||
func (hdb *HistoryDB) AddL2Txs(l2txs []common.L2Tx) error {
|
||||
return tracerr.Wrap(hdb.addL2Txs(hdb.db, l2txs))
|
||||
return tracerr.Wrap(hdb.addL2Txs(hdb.dbWrite, l2txs))
|
||||
}
|
||||
|
||||
// addL2Txs inserts L2 txs to the DB. TokenID, USD and FeeUSD will be set automatically before storing the tx.
|
||||
@@ -686,7 +693,7 @@ func (hdb *HistoryDB) addTxs(d meddler.DB, txs []txWrite) error {
|
||||
func (hdb *HistoryDB) GetAllExits() ([]common.ExitInfo, error) {
|
||||
var exits []*common.ExitInfo
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &exits,
|
||||
hdb.dbRead, &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 ORDER BY item_id;`,
|
||||
@@ -698,7 +705,7 @@ func (hdb *HistoryDB) GetAllExits() ([]common.ExitInfo, error) {
|
||||
func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
|
||||
var txs []*common.L1Tx
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &txs, // Note that '\x' gets parsed as a big.Int with value = 0
|
||||
hdb.dbRead, &txs, // Note that '\x' gets parsed as a big.Int with value = 0
|
||||
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
|
||||
tx.from_idx, tx.effective_from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id,
|
||||
tx.amount, (CASE WHEN tx.batch_num IS NULL THEN NULL WHEN tx.amount_success THEN tx.amount ELSE '\x' END) AS effective_amount,
|
||||
@@ -715,7 +722,7 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
|
||||
// Since the query specifies that only coordinator txs are returned, it's safe to assume
|
||||
// that returned txs will always have effective amounts
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &txs,
|
||||
hdb.dbRead, &txs,
|
||||
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
|
||||
tx.from_idx, tx.effective_from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id,
|
||||
tx.amount, tx.amount AS effective_amount,
|
||||
@@ -730,7 +737,7 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
|
||||
func (hdb *HistoryDB) GetAllL2Txs() ([]common.L2Tx, error) {
|
||||
var txs []*common.L2Tx
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &txs,
|
||||
hdb.dbRead, &txs,
|
||||
`SELECT tx.id, tx.batch_num, tx.position,
|
||||
tx.from_idx, tx.to_idx, tx.amount, tx.token_id,
|
||||
tx.fee, tx.nonce, tx.type, tx.eth_block_num
|
||||
@@ -743,7 +750,7 @@ func (hdb *HistoryDB) GetAllL2Txs() ([]common.L2Tx, error) {
|
||||
func (hdb *HistoryDB) GetUnforgedL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
|
||||
var txs []*common.L1Tx
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &txs, // only L1 user txs can have batch_num set to null
|
||||
hdb.dbRead, &txs, // only L1 user txs can have batch_num set to null
|
||||
`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, NULL AS effective_amount,
|
||||
@@ -760,7 +767,7 @@ func (hdb *HistoryDB) GetUnforgedL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx
|
||||
|
||||
// GetLastTxsPosition for a given to_forge_l1_txs_num
|
||||
func (hdb *HistoryDB) GetLastTxsPosition(toForgeL1TxsNum int64) (int, error) {
|
||||
row := hdb.db.QueryRow(
|
||||
row := hdb.dbRead.QueryRow(
|
||||
"SELECT position FROM tx WHERE to_forge_l1_txs_num = $1 ORDER BY position DESC;",
|
||||
toForgeL1TxsNum,
|
||||
)
|
||||
@@ -774,15 +781,15 @@ func (hdb *HistoryDB) GetSCVars() (*common.RollupVariables, *common.AuctionVaria
|
||||
var rollup common.RollupVariables
|
||||
var auction common.AuctionVariables
|
||||
var wDelayer common.WDelayerVariables
|
||||
if err := meddler.QueryRow(hdb.db, &rollup,
|
||||
if err := meddler.QueryRow(hdb.dbRead, &rollup,
|
||||
"SELECT * FROM rollup_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &auction,
|
||||
if err := meddler.QueryRow(hdb.dbRead, &auction,
|
||||
"SELECT * FROM auction_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &wDelayer,
|
||||
if err := meddler.QueryRow(hdb.dbRead, &wDelayer,
|
||||
"SELECT * FROM wdelayer_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
@@ -827,7 +834,7 @@ func (hdb *HistoryDB) AddBucketUpdatesTest(d meddler.DB, bucketUpdates []common.
|
||||
func (hdb *HistoryDB) GetAllBucketUpdates() ([]common.BucketUpdate, error) {
|
||||
var bucketUpdates []*common.BucketUpdate
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &bucketUpdates,
|
||||
hdb.dbRead, &bucketUpdates,
|
||||
`SELECT eth_block_num, num_bucket, block_stamp, withdrawals
|
||||
FROM bucket_update ORDER BY item_id;`,
|
||||
)
|
||||
@@ -853,7 +860,7 @@ func (hdb *HistoryDB) addTokenExchanges(d meddler.DB, tokenExchanges []common.To
|
||||
func (hdb *HistoryDB) GetAllTokenExchanges() ([]common.TokenExchange, error) {
|
||||
var tokenExchanges []*common.TokenExchange
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &tokenExchanges,
|
||||
hdb.dbRead, &tokenExchanges,
|
||||
"SELECT eth_block_num, eth_addr, value_usd FROM token_exchange ORDER BY item_id;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(tokenExchanges).([]common.TokenExchange), tracerr.Wrap(err)
|
||||
@@ -881,7 +888,7 @@ func (hdb *HistoryDB) addEscapeHatchWithdrawals(d meddler.DB,
|
||||
func (hdb *HistoryDB) GetAllEscapeHatchWithdrawals() ([]common.WDelayerEscapeHatchWithdrawal, error) {
|
||||
var escapeHatchWithdrawals []*common.WDelayerEscapeHatchWithdrawal
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &escapeHatchWithdrawals,
|
||||
hdb.dbRead, &escapeHatchWithdrawals,
|
||||
"SELECT eth_block_num, who_addr, to_addr, token_addr, amount FROM escape_hatch_withdrawal ORDER BY item_id;",
|
||||
)
|
||||
return db.SlicePtrsToSlice(escapeHatchWithdrawals).([]common.WDelayerEscapeHatchWithdrawal),
|
||||
@@ -894,7 +901,7 @@ func (hdb *HistoryDB) GetAllEscapeHatchWithdrawals() ([]common.WDelayerEscapeHat
|
||||
// exist in the smart contracts.
|
||||
func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
auction *common.AuctionVariables, wDelayer *common.WDelayerVariables) error {
|
||||
txn, err := hdb.db.Beginx()
|
||||
txn, err := hdb.dbWrite.Beginx()
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
@@ -978,7 +985,7 @@ func (hdb *HistoryDB) setExtraInfoForgedL1UserTxs(d sqlx.Ext, txs []common.L1Tx)
|
||||
// the pagination system of the API/DB depends on this. Within blocks, all
|
||||
// items should also be in the correct order (Accounts, Tokens, Txs, etc.)
|
||||
func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
txn, err := hdb.db.Beginx()
|
||||
txn, err := hdb.dbWrite.Beginx()
|
||||
if err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
@@ -1136,7 +1143,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,
|
||||
hdb.dbRead, coordinator,
|
||||
"SELECT * FROM coordinator WHERE bidder_addr = $1 ORDER BY item_id DESC LIMIT 1;",
|
||||
bidderAddr,
|
||||
)
|
||||
@@ -1145,14 +1152,14 @@ func (hdb *HistoryDB) GetCoordinatorAPI(bidderAddr ethCommon.Address) (*Coordina
|
||||
|
||||
// AddAuctionVars insert auction vars into the DB
|
||||
func (hdb *HistoryDB) AddAuctionVars(auctionVars *common.AuctionVariables) error {
|
||||
return tracerr.Wrap(meddler.Insert(hdb.db, "auction_vars", auctionVars))
|
||||
return tracerr.Wrap(meddler.Insert(hdb.dbWrite, "auction_vars", auctionVars))
|
||||
}
|
||||
|
||||
// GetTokensTest used to get tokens in a testing context
|
||||
func (hdb *HistoryDB) GetTokensTest() ([]TokenWithUSD, error) {
|
||||
tokens := []*TokenWithUSD{}
|
||||
if err := meddler.QueryAll(
|
||||
hdb.db, &tokens,
|
||||
hdb.dbRead, &tokens,
|
||||
"SELECT * FROM TOKEN",
|
||||
); err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
|
||||
@@ -39,12 +39,12 @@ func TestMain(m *testing.M) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
historyDB = NewHistoryDB(db, nil)
|
||||
historyDB = NewHistoryDB(db, db, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
apiConnCon := dbUtils.NewAPICnnectionController(1, time.Second)
|
||||
historyDBWithACC = NewHistoryDB(db, apiConnCon)
|
||||
historyDBWithACC = NewHistoryDB(db, db, apiConnCon)
|
||||
// Run tests
|
||||
result := m.Run()
|
||||
// Close DB
|
||||
@@ -817,11 +817,11 @@ func TestSetExtraInfoForgedL1UserTxs(t *testing.T) {
|
||||
}
|
||||
// Add second batch to trigger the update of the batch_num,
|
||||
// while avoiding the implicit call of setExtraInfoForgedL1UserTxs
|
||||
err = historyDB.addBlock(historyDB.db, &blocks[1].Block)
|
||||
err = historyDB.addBlock(historyDB.dbWrite, &blocks[1].Block)
|
||||
require.NoError(t, err)
|
||||
err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch)
|
||||
err = historyDB.addBatch(historyDB.dbWrite, &blocks[1].Rollup.Batches[0].Batch)
|
||||
require.NoError(t, err)
|
||||
err = historyDB.addAccounts(historyDB.db, blocks[1].Rollup.Batches[0].CreatedAccounts)
|
||||
err = historyDB.addAccounts(historyDB.dbWrite, blocks[1].Rollup.Batches[0].CreatedAccounts)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Set the Effective{Amount,DepositAmount} of the L1UserTxs that are forged in the second block
|
||||
@@ -831,7 +831,7 @@ func TestSetExtraInfoForgedL1UserTxs(t *testing.T) {
|
||||
l1Txs[1].EffectiveAmount = big.NewInt(0)
|
||||
l1Txs[2].EffectiveDepositAmount = big.NewInt(0)
|
||||
l1Txs[2].EffectiveAmount = big.NewInt(0)
|
||||
err = historyDB.setExtraInfoForgedL1UserTxs(historyDB.db, l1Txs)
|
||||
err = historyDB.setExtraInfoForgedL1UserTxs(historyDB.dbWrite, l1Txs)
|
||||
require.NoError(t, err)
|
||||
|
||||
dbL1Txs, err := historyDB.GetAllL1UserTxs()
|
||||
@@ -918,10 +918,10 @@ func TestUpdateExitTree(t *testing.T) {
|
||||
common.WithdrawInfo{Idx: 259, NumExitRoot: 3, InstantWithdraw: false,
|
||||
Owner: tc.UsersByIdx[259].Addr, Token: tokenAddr},
|
||||
)
|
||||
err = historyDB.addBlock(historyDB.db, &block.Block)
|
||||
err = historyDB.addBlock(historyDB.dbWrite, &block.Block)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
|
||||
err = historyDB.updateExitTree(historyDB.dbWrite, block.Block.Num,
|
||||
block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -951,10 +951,10 @@ func TestUpdateExitTree(t *testing.T) {
|
||||
Token: tokenAddr,
|
||||
Amount: big.NewInt(80),
|
||||
})
|
||||
err = historyDB.addBlock(historyDB.db, &block.Block)
|
||||
err = historyDB.addBlock(historyDB.dbWrite, &block.Block)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
|
||||
err = historyDB.updateExitTree(historyDB.dbWrite, block.Block.Num,
|
||||
block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -997,7 +997,7 @@ func TestGetBestBidCoordinator(t *testing.T) {
|
||||
URL: "bar",
|
||||
},
|
||||
}
|
||||
err = historyDB.addCoordinators(historyDB.db, coords)
|
||||
err = historyDB.addCoordinators(historyDB.dbWrite, coords)
|
||||
require.NoError(t, err)
|
||||
|
||||
bids := []common.Bid{
|
||||
@@ -1015,7 +1015,7 @@ func TestGetBestBidCoordinator(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
err = historyDB.addBids(historyDB.db, bids)
|
||||
err = historyDB.addBids(historyDB.dbWrite, bids)
|
||||
require.NoError(t, err)
|
||||
|
||||
forger10, err := historyDB.GetBestBidCoordinator(10)
|
||||
@@ -1053,7 +1053,7 @@ func TestAddBucketUpdates(t *testing.T) {
|
||||
Withdrawals: big.NewInt(42),
|
||||
},
|
||||
}
|
||||
err := historyDB.addBucketUpdates(historyDB.db, bucketUpdates)
|
||||
err := historyDB.addBucketUpdates(historyDB.dbWrite, bucketUpdates)
|
||||
require.NoError(t, err)
|
||||
dbBucketUpdates, err := historyDB.GetAllBucketUpdates()
|
||||
require.NoError(t, err)
|
||||
@@ -1078,7 +1078,7 @@ func TestAddTokenExchanges(t *testing.T) {
|
||||
ValueUSD: 67890,
|
||||
},
|
||||
}
|
||||
err := historyDB.addTokenExchanges(historyDB.db, tokenExchanges)
|
||||
err := historyDB.addTokenExchanges(historyDB.dbWrite, tokenExchanges)
|
||||
require.NoError(t, err)
|
||||
dbTokenExchanges, err := historyDB.GetAllTokenExchanges()
|
||||
require.NoError(t, err)
|
||||
@@ -1107,7 +1107,7 @@ func TestAddEscapeHatchWithdrawals(t *testing.T) {
|
||||
Amount: big.NewInt(20003),
|
||||
},
|
||||
}
|
||||
err := historyDB.addEscapeHatchWithdrawals(historyDB.db, escapeHatchWithdrawals)
|
||||
err := historyDB.addEscapeHatchWithdrawals(historyDB.dbWrite, escapeHatchWithdrawals)
|
||||
require.NoError(t, err)
|
||||
dbEscapeHatchWithdrawals, err := historyDB.GetAllEscapeHatchWithdrawals()
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user