Skip to content

Commit 9442c36

Browse files
committed
feat: add get_block
1 parent a4b3bcc commit 9442c36

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)]
@@ -464,6 +465,13 @@ impl<S: Sleeper> AsyncClient<S> {
464465
self.get_response_json("/fee-estimates").await
465466
}
466467

468+
/// Get a summary about a [`Block`], given it's [`BlockHash`].
469+
pub async fn get_block(&self, blockhash: &BlockHash) -> Result<BlockInformation, Error> {
470+
let path = format!("/block/{blockhash}");
471+
472+
self.get_response_json(&path).await
473+
}
474+
467475
/// Gets some recent block summaries starting at the tip or at `height` if
468476
/// provided.
469477
///

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)]
@@ -389,6 +390,13 @@ impl BlockingClient {
389390
self.get_response_json(&path)
390391
}
391392

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

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 {
@@ -966,6 +967,25 @@ mod test {
966967
assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
967968
}
968969

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

0 commit comments

Comments
 (0)