Skip to content

Commit

Permalink
RPC method to count the number of TX in the host (#1919)
Browse files Browse the repository at this point in the history
* new rpc method for count tx query
  • Loading branch information
badgersrus authored May 16, 2024
1 parent 11b91f9 commit a0d168c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions go/host/rpc/clientapi/client_api_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (s *ScanAPI) GetTotalTransactionCount() (*big.Int, error) {
return s.host.Storage().FetchTotalTxCount()
}

// GetTotalTransactionsQuery returns the number of recorded transactions on the network.
func (s *ScanAPI) GetTotalTransactionsQuery() (*big.Int, error) {
return s.host.Storage().FetchTotalTxsQuery()
}

// GetBatchListingNew returns a paginated list of batches
func (s *ScanAPI) GetBatchListingNew(pagination *common.QueryPagination) (*common.BatchListingResponse, error) {
return s.host.Storage().FetchBatchListing(pagination)
Expand Down
13 changes: 12 additions & 1 deletion go/host/storage/hostdb/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
selectTxCount = "SELECT total FROM transaction_count WHERE id = 1"
selectTx = "SELECT full_hash, b_sequence FROM transaction_host WHERE hash = "
selectTxs = "SELECT t.full_hash, b.ext_batch FROM transaction_host t JOIN batch_host b ON t.b_sequence = b.sequence ORDER BY b.height DESC "
countTxs = "SELECT COUNT(b_sequence) AS row_count FROM transaction_host"
)

// GetTransactionListing returns a paginated list of transactions in descending order
Expand Down Expand Up @@ -82,7 +83,7 @@ func GetTransaction(db HostDB, hash gethcommon.Hash) (*common.PublicTransaction,
return tx, nil
}

// GetTotalTxCount returns the total number of batched transactions
// GetTotalTxCount returns value from the transaction count table
func GetTotalTxCount(db HostDB) (*big.Int, error) {
var totalCount int
err := db.GetSQLDB().QueryRow(selectTxCount).Scan(&totalCount)
Expand All @@ -91,3 +92,13 @@ func GetTotalTxCount(db HostDB) (*big.Int, error) {
}
return big.NewInt(int64(totalCount)), nil
}

// GetTotalTxsQuery returns the count of the transactions table
func GetTotalTxsQuery(db HostDB) (*big.Int, error) {
var totalCount int
err := db.GetSQLDB().QueryRow(countTxs).Scan(&totalCount)
if err != nil {
return nil, fmt.Errorf("failed to count number of transactions: %w", err)
}
return big.NewInt(int64(totalCount)), nil
}
26 changes: 26 additions & 0 deletions go/host/storage/hostdb/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hostdb

import (
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -143,3 +144,28 @@ func TestCanRetrieveTotalNumberOfTransactions(t *testing.T) {
t.Errorf("total number of batch transactions was not stored correctly")
}
}

func TestTotalTxsQuery(t *testing.T) {
db, _ := createSQLiteDB(t)
var txHashes []common.L2TxHash
for i := 0; i < 100; i++ {
txHash := gethcommon.BytesToHash([]byte(fmt.Sprintf("magicString%d", i+1)))
txHashes = append(txHashes, txHash)
}
batchOne := createBatch(batchNumber, txHashes)
dbtx, _ := db.NewDBTransaction()
err := AddBatch(dbtx, db.GetSQLStatement(), &batchOne)
if err != nil {
t.Errorf("could not store batch. Cause: %s", err)
}
dbtx.Write()

totalTxs, err := GetTotalTxsQuery(db)
if err != nil {
t.Errorf("was not able to count total number of transactions. Cause: %s", err)
}

if totalTxs.Cmp(big.NewInt(100)) != 0 {
t.Errorf("total number of transactions was not counted correction")
}
}
4 changes: 3 additions & 1 deletion go/host/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type BatchResolver interface {
FetchBatchHeaderByHeight(height *big.Int) (*common.BatchHeader, error)
// FetchBatchByHeight returns the `PublicBatch` with the given height
FetchBatchByHeight(height *big.Int) (*common.PublicBatch, error)
// FetchTotalTxCount returns the number of transactions in the DB
// FetchTotalTxCount returns the value of the transactions_count table. Fast but inaccurate for Tenscan
FetchTotalTxCount() (*big.Int, error)
// FetchTotalTxsQuery returns the number of transactions in the DB. Required for e2e tests
FetchTotalTxsQuery() (*big.Int, error)
// FetchTransaction returns the transaction given its hash
FetchTransaction(hash gethcommon.Hash) (*common.PublicTransaction, error)
// FetchBatchTransactions returns a list of public transaction data within a given batch hash
Expand Down
4 changes: 4 additions & 0 deletions go/host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (s *storageImpl) FetchTotalTxCount() (*big.Int, error) {
return hostdb.GetTotalTxCount(s.db)
}

func (s *storageImpl) FetchTotalTxsQuery() (*big.Int, error) {
return hostdb.GetTotalTxsQuery(s.db)
}

func (s *storageImpl) FetchTransaction(hash gethcommon.Hash) (*common.PublicTransaction, error) {
return hostdb.GetTransaction(s.db, hash)
}
Expand Down
1 change: 1 addition & 0 deletions go/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
GetBatchByTx = "scan_getBatchByTx"
GetLatestRollupHeader = "scan_getLatestRollupHeader"
GetTotalTxCount = "scan_getTotalTransactionCount"
GetTotalTxsQuery = "scan_getTotalTransactionsQuery"
GetTotalContractCount = "scan_getTotalContractCount"
GetPublicTransactionData = "scan_getPublicTransactionData"
GetBatchListing = "scan_getBatchListing"
Expand Down

0 comments on commit a0d168c

Please sign in to comment.