Browse Source

Merge pull request #352 from hermeznetwork/fix/historydb-norows

Fix queries when no rows
feature/sql-semaphore1
Eduard S 3 years ago
committed by GitHub
parent
commit
8e1bfd150d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions
  1. +10
    -5
      db/historydb/historydb.go
  2. +24
    -0
      db/historydb/historydb_test.go

+ 10
- 5
db/historydb/historydb.go

@ -1256,7 +1256,10 @@ 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("SELECT MAX(position) FROM tx WHERE to_forge_l1_txs_num = $1;", toForgeL1TxsNum)
row := hdb.db.QueryRow(
"SELECT position FROM tx WHERE to_forge_l1_txs_num = $1 ORDER BY position DESC;",
toForgeL1TxsNum,
)
var lastL1TxsPosition int
return lastL1TxsPosition, tracerr.Wrap(row.Scan(&lastL1TxsPosition))
}
@ -1766,7 +1769,8 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
metricsTotals := &MetricsTotals{}
metrics := &Metrics{}
err := meddler.QueryRow(
hdb.db, metricsTotals, `SELECT COUNT(tx.*) as total_txs, MIN(tx.batch_num) as batch_num
hdb.db, 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 '24 HOURS';`)
if err != nil {
@ -1783,7 +1787,7 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
err = meddler.QueryRow(
hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches,
SUM(total_fees_usd) AS total_fees FROM batch
COALESCE (SUM(total_fees_usd), 0) AS total_fees FROM batch
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
if err != nil {
return nil, tracerr.Wrap(err)
@ -1812,7 +1816,8 @@ func (hdb *HistoryDB) GetMetrics(lastBatchNum common.BatchNum) (*Metrics, error)
func (hdb *HistoryDB) GetAvgTxFee() (float64, error) {
metricsTotals := &MetricsTotals{}
err := meddler.QueryRow(
hdb.db, metricsTotals, `SELECT COUNT(tx.*) as total_txs, MIN(tx.batch_num) as batch_num
hdb.db, 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';`)
if err != nil {
@ -1820,7 +1825,7 @@ func (hdb *HistoryDB) GetAvgTxFee() (float64, error) {
}
err = meddler.QueryRow(
hdb.db, metricsTotals, `SELECT COUNT(*) AS total_batches,
SUM(total_fees_usd) AS total_fees FROM batch
COALESCE (SUM(total_fees_usd), 0) AS total_fees FROM batch
WHERE batch_num > $1;`, metricsTotals.FirstBatchNum)
if err != nil {
return 0, tracerr.Wrap(err)

+ 24
- 0
db/historydb/historydb_test.go

@ -1019,6 +1019,30 @@ func TestAddEscapeHatchWithdrawals(t *testing.T) {
assert.Equal(t, escapeHatchWithdrawals, dbEscapeHatchWithdrawals)
}
func TestGetMetricsEmpty(t *testing.T) {
test.WipeDB(historyDB.DB())
_, err := historyDB.GetMetrics(0)
assert.NoError(t, err)
}
func TestGetAvgTxFeeEmpty(t *testing.T) {
test.WipeDB(historyDB.DB())
_, err := historyDB.GetAvgTxFee()
assert.NoError(t, err)
}
func TestGetLastL1TxsNum(t *testing.T) {
test.WipeDB(historyDB.DB())
_, err := historyDB.GetLastL1TxsNum()
assert.NoError(t, err)
}
func TestGetLastTxsPosition(t *testing.T) {
test.WipeDB(historyDB.DB())
_, err := historyDB.GetLastTxsPosition(0)
assert.Equal(t, sql.ErrNoRows.Error(), err.Error())
}
// setTestBlocks WARNING: this will delete the blocks and recreate them
func setTestBlocks(from, to int64) []common.Block {
test.WipeDB(historyDB.DB())

Loading…
Cancel
Save