From f8d01b5998f212ee16861930812ba84b11d5de88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Tue, 23 Feb 2021 11:02:03 +0100 Subject: [PATCH] Change how TX per Batch is calculated in state API endpoint --- api/state_test.go | 4 ++-- db/historydb/apiqueries.go | 21 +++++++++++++++------ db/historydb/historydb_test.go | 12 ++++-------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/api/state_test.go b/api/state_test.go index d720859..44c5ebc 100644 --- a/api/state_test.go +++ b/api/state_test.go @@ -131,7 +131,7 @@ func TestUpdateNetworkInfo(t *testing.T) { func TestUpdateMetrics(t *testing.T) { // Update Metrics needs api.status.Network.LastBatch.BatchNum to be updated lastBlock := tc.blocks[3] - lastBatchNum := common.BatchNum(3) + lastBatchNum := common.BatchNum(12) currentSlotNum := int64(1) err := api.UpdateNetworkInfo(lastBlock, lastBlock, lastBatchNum, currentSlotNum) assert.NoError(t, err) @@ -162,7 +162,7 @@ func TestUpdateRecommendedFee(t *testing.T) { func TestGetState(t *testing.T) { lastBlock := tc.blocks[3] - lastBatchNum := common.BatchNum(3) + lastBatchNum := common.BatchNum(12) currentSlotNum := int64(1) api.SetRollupVariables(tc.rollupVars) api.SetWDelayerVariables(tc.wdelayerVars) diff --git a/db/historydb/apiqueries.go b/db/historydb/apiqueries.go index bde5094..188b8b7 100644 --- a/db/historydb/apiqueries.go +++ b/db/historydb/apiqueries.go @@ -952,11 +952,19 @@ func (hdb *HistoryDB) GetMetricsAPI(lastBatchNum common.BatchNum) (*Metrics, err metricsTotals := &MetricsTotals{} metrics := &Metrics{} err = meddler.QueryRow( - 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 - WHERE block.timestamp >= NOW() - INTERVAL '24 HOURS';`) + hdb.dbRead, metricsTotals, `SELECT + COALESCE (MIN(batch.batch_num), 0) as batch_num, + COALESCE (MIN(block.timestamp), NOW()) AS min_timestamp, + COALESCE (MAX(block.timestamp), NOW()) AS max_timestamp + FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num + WHERE block.timestamp >= NOW() - INTERVAL '24 HOURS' and batch.batch_num <= $1;`, lastBatchNum) + if err != nil { + return nil, tracerr.Wrap(err) + } + + err = meddler.QueryRow( + hdb.dbRead, metricsTotals, `SELECT COUNT(*) as total_txs + FROM tx WHERE tx.batch_num between $1 AND $2;`, metricsTotals.FirstBatchNum, lastBatchNum) if err != nil { return nil, tracerr.Wrap(err) } @@ -979,10 +987,11 @@ func (hdb *HistoryDB) GetMetricsAPI(lastBatchNum common.BatchNum) (*Metrics, err err = meddler.QueryRow( 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) + WHERE batch_num between $1 and $2;`, metricsTotals.FirstBatchNum, lastBatchNum) if err != nil { return nil, tracerr.Wrap(err) } + if metricsTotals.TotalBatches > 0 { metrics.BatchFrequency = seconds / float64(metricsTotals.TotalBatches) } else { diff --git a/db/historydb/historydb_test.go b/db/historydb/historydb_test.go index 09e21c0..3aa7282 100644 --- a/db/historydb/historydb_test.go +++ b/db/historydb/historydb_test.go @@ -1175,16 +1175,12 @@ func TestGetMetricsAPI(t *testing.T) { res, err := historyDBWithACC.GetMetricsAPI(common.BatchNum(numBatches)) assert.NoError(t, err) - assert.Equal(t, float64(numTx)/float64(numBatches-1), res.TransactionsPerBatch) + assert.Equal(t, float64(numTx)/float64(numBatches), res.TransactionsPerBatch) // Frequency is not exactly the desired one, some decimals may appear - assert.GreaterOrEqual(t, res.BatchFrequency, float64(frequency)) - assert.Less(t, res.BatchFrequency, float64(frequency+1)) - // Truncate frecuency into an int to do an exact check - assert.Equal(t, frequency, int(res.BatchFrequency)) - // This may also be different in some decimals - // Truncate it to the third decimal to compare - assert.Equal(t, math.Trunc((float64(numTx)/float64(frequency*blockNum-frequency))/0.001)*0.001, math.Trunc(res.TransactionsPerSecond/0.001)*0.001) + // There is a -2 as time for first and last batch is not taken into account + assert.InEpsilon(t, float64(frequency)*float64(numBatches-2)/float64(numBatches), res.BatchFrequency, 0.01) + assert.InEpsilon(t, float64(numTx)/float64(frequency*blockNum-frequency), res.TransactionsPerSecond, 0.01) assert.Equal(t, int64(3), res.TotalAccounts) assert.Equal(t, int64(3), res.TotalBJJs) // Til does not set fees