Skip to content

Commit ae38615

Browse files
committed
feat(client): add get_block
1 parent 697e6e8 commit ae38615

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/async.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ use log::{debug, error, info, trace};
2727
use reqwest::{header, Client, Response};
2828

2929
use crate::{
30-
AddressStats, BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputSpendStatus,
31-
OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
30+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MerkleProof,
31+
OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS,
32+
RETRYABLE_ERROR_CODES,
3233
};
3334

3435
#[derive(Debug, Clone)]
@@ -458,6 +459,13 @@ impl<S: Sleeper> AsyncClient<S> {
458459
self.get_response_json("/fee-estimates").await
459460
}
460461

462+
/// Get a summary about a [`Block`], given it's [`BlockHash`].
463+
pub async fn get_block(&self, blockhash: &BlockHash) -> Result<BlockInformation, Error> {
464+
let path = format!("/block/{blockhash}");
465+
466+
self.get_response_json(&path).await
467+
}
468+
461469
/// Gets some recent block summaries starting at the tip or at `height` if
462470
/// provided.
463471
///

src/blocking.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ use bitcoin::hex::{DisplayHex, FromHex};
2828
use bitcoin::{Address, Block, BlockHash, MerkleBlock, Script, Transaction, Txid};
2929

3030
use crate::{
31-
AddressStats, BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputSpendStatus,
32-
OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
31+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MerkleProof,
32+
OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS,
33+
RETRYABLE_ERROR_CODES,
3334
};
3435

3536
#[derive(Debug, Clone)]
@@ -386,6 +387,13 @@ impl BlockingClient {
386387
self.get_response_json(&path)
387388
}
388389

390+
/// Get a summary about a [`Block`], given it's [`BlockHash`].
391+
pub fn get_block(&self, blockhash: &BlockHash) -> Result<BlockInformation, Error> {
392+
let path = format!("/block/{blockhash}");
393+
394+
self.get_response_json(&path)
395+
}
396+
389397
/// Gets some recent block summaries starting at the tip or at `height` if
390398
/// provided.
391399
///

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ mod test {
269269
use electrsd::{corepc_node, ElectrsD};
270270
use lazy_static::lazy_static;
271271
use std::env;
272+
use std::str::FromStr;
272273
use tokio::sync::Mutex;
273274
#[cfg(all(feature = "blocking", feature = "async"))]
274275
use {
@@ -968,6 +969,25 @@ mod test {
968969
assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
969970
}
970971

972+
#[cfg(all(feature = "blocking", feature = "async"))]
973+
#[tokio::test]
974+
async fn test_get_block() {
975+
let (blocking_client, async_client) = setup_clients().await;
976+
977+
// Genesis block `BlockHash` on regtest.
978+
let blockhash_genesis =
979+
BlockHash::from_str("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")
980+
.unwrap();
981+
982+
let block_info_blocking = blocking_client.get_block(&blockhash_genesis).unwrap();
983+
let block_info_async = async_client.get_block(&blockhash_genesis).await.unwrap();
984+
985+
assert_eq!(block_info_async, block_info_blocking);
986+
assert_eq!(block_info_async.id, blockhash_genesis);
987+
assert_eq!(block_info_async.height, 0);
988+
assert_eq!(block_info_async.previousblockhash, None);
989+
}
990+
971991
#[cfg(all(feature = "blocking", feature = "async"))]
972992
#[tokio::test]
973993
async fn test_get_blocks() {

0 commit comments

Comments
 (0)