Skip to content

Commit b9dadcb

Browse files
committed
feat: add get_block_txids
1 parent 9442c36 commit b9dadcb

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
@@ -472,6 +472,13 @@ impl<S: Sleeper> AsyncClient<S> {
472472
self.get_response_json(&path).await
473473
}
474474

475+
/// Get all [`Txid`]s that belong to a block identified by it's [`BlockHash`].
476+
pub async fn get_block_txids(&self, blockhash: &BlockHash) -> Result<Vec<Txid>, Error> {
477+
let path = format!("/block/{blockhash}/txids");
478+
479+
self.get_response_json(&path).await
480+
}
481+
475482
/// Gets some recent block summaries starting at the tip or at `height` if
476483
/// provided.
477484
///

src/blocking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ impl BlockingClient {
397397
self.get_response_json(&path)
398398
}
399399

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

src/lib.rs

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

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

0 commit comments

Comments
 (0)