Skip to content

Commit b1930eb

Browse files
committed
feat(client): add get_block_info method
1 parent d623d46 commit b1930eb

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-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_info(&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_info(&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: 23 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,28 @@ 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_info() {
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_info(&blockhash_genesis).unwrap();
937+
let block_info_async = async_client
938+
.get_block_info(&blockhash_genesis)
939+
.await
940+
.unwrap();
941+
942+
assert_eq!(block_info_async, block_info_blocking);
943+
assert_eq!(block_info_async.id, blockhash_genesis);
944+
assert_eq!(block_info_async.height, 0);
945+
assert_eq!(block_info_async.previousblockhash, None);
946+
}
947+
925948
#[cfg(all(feature = "blocking", feature = "async"))]
926949
#[tokio::test]
927950
async fn test_get_blocks() {

0 commit comments

Comments
 (0)