Skip to content

Commit edbacdc

Browse files
committed
feat(client): add get_block_txids method
1 parent 53430a5 commit edbacdc

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
@@ -483,6 +483,13 @@ impl<S: Sleeper> AsyncClient<S> {
483483
self.get_response_json(&path).await
484484
}
485485

486+
/// Get all [`Txid`]s that belong to a [`Block`] identified by it's [`BlockHash`].
487+
pub async fn get_block_txids(&self, blockhash: &BlockHash) -> Result<Vec<Txid>, Error> {
488+
let path = format!("/block/{blockhash}/txids");
489+
490+
self.get_response_json(&path).await
491+
}
492+
486493
/// Gets some recent block summaries starting at the tip or at `height` if
487494
/// provided.
488495
///

src/blocking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ impl BlockingClient {
411411
self.get_response_json(&path)
412412
}
413413

414+
/// Get all [`Txid`]s that belong to a [`Block`] identified by it's [`BlockHash`].
415+
pub fn get_block_txids(&self, blockhash: &BlockHash) -> Result<Vec<Txid>, Error> {
416+
let path = format!("/block/{blockhash}/txids");
417+
418+
self.get_response_json(&path)
419+
}
420+
414421
/// Gets some recent block summaries starting at the tip or at `height` if
415422
/// provided.
416423
///

src/lib.rs

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

945+
#[cfg(all(feature = "blocking", feature = "async"))]
946+
#[tokio::test]
947+
async fn test_get_block_txids() {
948+
let (blocking_client, async_client) = setup_clients().await;
949+
950+
let address = BITCOIND
951+
.client
952+
.new_address_with_type(AddressType::Legacy)
953+
.unwrap();
954+
955+
// Create 5 transactions and mine a block.
956+
let txids: Vec<_> = (0..5)
957+
.map(|_| {
958+
BITCOIND
959+
.client
960+
.send_to_address(&address, Amount::from_sat(1000))
961+
.unwrap()
962+
.txid()
963+
.unwrap()
964+
})
965+
.collect();
966+
967+
let _miner = MINER.lock().await;
968+
generate_blocks_and_wait(1);
969+
970+
// Get the block hash at height 1.
971+
let blockhash = blocking_client.get_block_hash(1).unwrap();
972+
973+
let txids_async = async_client.get_block_txids(&blockhash).await.unwrap();
974+
let txids_blocking = blocking_client.get_block_txids(&blockhash).unwrap();
975+
976+
assert_eq!(txids_async, txids_blocking);
977+
978+
// Compare expected and received (skipping the coinbase TXID).
979+
txids
980+
.iter()
981+
.zip(txids_async.iter().skip(1))
982+
.for_each(|(expected_txid, received_txid)| {
983+
assert_eq!(expected_txid, received_txid);
984+
});
985+
}
986+
945987
#[cfg(all(feature = "blocking", feature = "async"))]
946988
#[tokio::test]
947989
async fn test_get_blocks() {

0 commit comments

Comments
 (0)