Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC method to count the number of TX in the host #1919

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
2 changes: 2 additions & 0 deletions go/host/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type BatchResolver interface {
FetchBatchByHeight(height *big.Int) (*common.PublicBatch, error)
// FetchTotalTxCount returns the number of transactions in the DB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe explain in the comment the purpose of these 2 methods.
A fast but inaccurate one to be used for tenscan, and the other for testing

FetchTotalTxCount() (*big.Int, error)
// FetchTotalTxsQuery returns the number of transactions in the DB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to give this a different comment to the one above imo, clarify why these two identical looking methods exist a bit haha.

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
Loading