Skip to content

Commit 3a054b9

Browse files
committed
feat(client): add get_block_txids
1 parent 81885dc commit 3a054b9

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/async.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ impl<S: Sleeper> AsyncClient<S> {
466466
self.get_response_json(&path).await
467467
}
468468

469+
/// Get all [`Txid`]s that belong to a [`Block`] identified by it's [`BlockHash`].
470+
pub async fn get_block_txids(&self, blockhash: &BlockHash) -> Result<Vec<Txid>, Error> {
471+
let path = format!("/block/{blockhash}/txids");
472+
473+
self.get_response_json(&path).await
474+
}
475+
469476
/// Gets some recent block summaries starting at the tip or at `height` if
470477
/// provided.
471478
///

src/blocking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ impl BlockingClient {
394394
self.get_response_json(&path)
395395
}
396396

397+
/// Get all [`Txid`]s that belong to a [`Block`] identified by it's [`BlockHash`].
398+
pub fn get_block_txids(&self, blockhash: &BlockHash) -> Result<Vec<Txid>, Error> {
399+
let path = format!("/block/{blockhash}/txids");
400+
401+
self.get_response_json(&path)
402+
}
403+
397404
/// Gets some recent block summaries starting at the tip or at `height` if
398405
/// provided.
399406
///

src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,48 @@ mod test {
988988
assert_eq!(block_info_async.previousblockhash, None);
989989
}
990990

991+
#[cfg(all(feature = "blocking", feature = "async"))]
992+
#[tokio::test]
993+
async fn test_get_block_txids() {
994+
let (blocking_client, async_client) = setup_clients().await;
995+
996+
let address = BITCOIND
997+
.client
998+
.new_address_with_type(AddressType::Legacy)
999+
.unwrap();
1000+
1001+
// Create 5 transactions and mine a block.
1002+
let txids: Vec<_> = (0..5)
1003+
.map(|_| {
1004+
BITCOIND
1005+
.client
1006+
.send_to_address(&address, Amount::from_sat(1000))
1007+
.unwrap()
1008+
.txid()
1009+
.unwrap()
1010+
})
1011+
.collect();
1012+
1013+
let _miner = MINER.lock().await;
1014+
generate_blocks_and_wait(1);
1015+
1016+
// Get the block hash at height 1.
1017+
let blockhash = blocking_client.get_block_hash(1).unwrap();
1018+
1019+
let txids_async = async_client.get_block_txids(&blockhash).await.unwrap();
1020+
let txids_blocking = blocking_client.get_block_txids(&blockhash).unwrap();
1021+
1022+
assert_eq!(txids_async, txids_blocking);
1023+
1024+
// Compare expected and received (skipping the coinbase TXID).
1025+
txids
1026+
.iter()
1027+
.zip(txids_async.iter().skip(1))
1028+
.for_each(|(expected_txid, received_txid)| {
1029+
assert_eq!(expected_txid, received_txid);
1030+
});
1031+
}
1032+
9911033
#[cfg(all(feature = "blocking", feature = "async"))]
9921034
#[tokio::test]
9931035
async fn test_get_blocks() {

0 commit comments

Comments
 (0)