Skip to content

Commit

Permalink
Implement GetNumTransactions (#1629)
Browse files Browse the repository at this point in the history
- Addresses #1541
  • Loading branch information
maxconway authored Oct 14, 2024
1 parent 409328e commit 4791fce
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
7 changes: 7 additions & 0 deletions zilliqa/src/api/zil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub fn rpc_module(node: Arc<Mutex<Node>>) -> RpcModule<Arc<Mutex<Node>>> {
("GetTxnBodiesForTxBlockEx", get_txn_bodies_for_tx_block_ex),
("GetNumDSBlocks", get_num_ds_blocks),
("GetRecentTransactions", get_recent_transactions),
("GetNumTransactions", get_num_transactions),
],
)
}
Expand Down Expand Up @@ -1033,3 +1034,9 @@ fn get_recent_transactions(
txn_hashes: txns,
})
}

fn get_num_transactions(_params: Params, node: &Arc<Mutex<Node>>) -> Result<String> {
let node = node.lock().unwrap();
let num_transactions = node.consensus.block_store.get_num_transactions()?;
Ok(num_transactions.to_string())
}
5 changes: 5 additions & 0 deletions zilliqa/src/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,9 @@ impl BlockStore {
.entry(peer)
.or_insert_with(|| PeerInfo::new(capacity))
}

pub fn get_num_transactions(&self) -> Result<usize> {
let count = self.db.get_total_transaction_count()?;
Ok(count)
}
}
9 changes: 9 additions & 0 deletions zilliqa/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,15 @@ impl Db {
.execute("DELETE FROM receipts WHERE block_hash = ?1", [block_hash])?;
Ok(())
}

pub fn get_total_transaction_count(&self) -> Result<usize> {
let count: usize =
self.db
.lock()
.unwrap()
.query_row("SELECT COUNT(*) FROM transactions", [], |row| row.get(0))?;
Ok(count)
}
}

pub fn checkpoint_block_with_state<P: AsRef<Path> + Debug>(
Expand Down
85 changes: 85 additions & 0 deletions zilliqa/tests/it/zil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,88 @@ async fn get_recent_transactions_1(mut network: Network) {
);
assert_eq!(recent_transactions.number, 4);
}

#[zilliqa_macros::test]
async fn get_num_transactions_0(mut network: Network) {
let wallet = network.genesis_wallet().await;

let response: Value = wallet
.provider()
.request("GetNumTransactions", [""])
.await
.expect("Failed to call GetNumTransactions API");

assert!(
response.is_string(),
"Expected response to be a string, got: {:?}",
response
);
response
.as_str()
.expect("Expected response to be a string")
.parse::<u64>()
.expect("Failed to parse response as u64");
}

#[zilliqa_macros::test]
async fn get_num_transactions_1(mut network: Network) {
let wallet = network.random_wallet().await;

let (secret_key, _address) = zilliqa_account(&mut network).await;

let to_addr: H160 = "0x00000000000000000000000000000000deadbeef"
.parse()
.unwrap();
send_transaction(
&mut network,
&secret_key,
1,
to_addr,
200u128 * 10u128.pow(12),
50_000,
None,
None,
)
.await;

network.run_until_block(&wallet, 1.into(), 50).await;

let (secret_key, _address) = zilliqa_account(&mut network).await;

let to_addr: H160 = "0x00000000000000000000000000000000deadbeef"
.parse()
.unwrap();
send_transaction(
&mut network,
&secret_key,
1,
to_addr,
200u128 * 10u128.pow(12),
50_000,
None,
None,
)
.await;

network.run_until_block(&wallet, 2.into(), 50).await;

let response: Value = wallet
.provider()
.request("GetNumTransactions", [""])
.await
.expect("Failed to call GetNumTransactions API");

assert!(
response.is_string(),
"Expected response to be a string, got: {:?}",
response
);

let response_num = response
.as_str()
.expect("Expected response to be a string")
.parse::<u64>()
.expect("Failed to parse response as u64");

assert_eq!(response_num, 4);
}

0 comments on commit 4791fce

Please sign in to comment.