Skip to content

Commit 53430a5

Browse files
committed
feat(client): add get_block method
1 parent 4f2630c commit 53430a5

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/async.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ impl<S: Sleeper> AsyncClient<S> {
476476
self.get_response_json("/fee-estimates").await
477477
}
478478

479+
/// Get a summary about a [`Block`], given it's [`BlockHash`].
480+
pub async fn get_block(&self, blockhash: &BlockHash) -> Result<BlockInformation, Error> {
481+
let path = format!("/block/{blockhash}");
482+
483+
self.get_response_json(&path).await
484+
}
485+
479486
/// Gets some recent block summaries starting at the tip or at `height` if
480487
/// provided.
481488
///

src/blocking.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ impl BlockingClient {
403403

404404
self.get_response_json(&path)
405405
}
406+
407+
/// Get a summary about a [`Block`], given it's [`BlockHash`].
408+
pub fn get_block(&self, blockhash: &BlockHash) -> Result<BlockInformation, Error> {
409+
let path = format!("/block/{blockhash}");
410+
411+
self.get_response_json(&path)
412+
}
413+
406414
/// Gets some recent block summaries starting at the tip or at `height` if
407415
/// provided.
408416
///

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ mod test {
267267
use electrsd::{corepc_node, ElectrsD};
268268
use lazy_static::lazy_static;
269269
use std::env;
270+
use std::str::FromStr;
270271
use tokio::sync::Mutex;
271272
#[cfg(all(feature = "blocking", feature = "async"))]
272273
use {
@@ -922,6 +923,25 @@ mod test {
922923
assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
923924
}
924925

926+
#[cfg(all(feature = "blocking", feature = "async"))]
927+
#[tokio::test]
928+
async fn test_get_block() {
929+
let (blocking_client, async_client) = setup_clients().await;
930+
931+
// Genesis block `BlockHash` on regtest.
932+
let blockhash_genesis =
933+
BlockHash::from_str("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")
934+
.unwrap();
935+
936+
let block_info_blocking = blocking_client.get_block(&blockhash_genesis).unwrap();
937+
let block_info_async = async_client.get_block(&blockhash_genesis).await.unwrap();
938+
939+
assert_eq!(block_info_async, block_info_blocking);
940+
assert_eq!(block_info_async.id, blockhash_genesis);
941+
assert_eq!(block_info_async.height, 0);
942+
assert_eq!(block_info_async.previousblockhash, None);
943+
}
944+
925945
#[cfg(all(feature = "blocking", feature = "async"))]
926946
#[tokio::test]
927947
async fn test_get_blocks() {

0 commit comments

Comments
 (0)