Skip to content

Commit 98137c9

Browse files
Merge #17: Allow to retrieve a block header by hash
92606cc Test `get_header_by_hash` rather than `get_height` (Elias Rohrer) 6673912 Deprecate `get_height` (Elias Rohrer) 9e233ef Allow to retrieve a block header by hash (Elias Rohrer) Pull request description: ~~Based on #13 since it probably land soon.~~ This PR adds a method allowing to retrieve a `BlockHeader` via a given `BlockHash` and reuses it in `get_height`. Top commit has no ACKs. Tree-SHA512: 2be894f7e745e317b02512183e073806e90145cbde9ad89f10e5782d6bc302da6d72adb0cef0fce7cd746e4f7b93fba0844a640bc30457ccafbcf19dd5673254
2 parents 8364d05 + 92606cc commit 98137c9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/async.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ impl AsyncClient {
113113
Ok(Some(resp.error_for_status()?.json().await?))
114114
}
115115

116+
#[deprecated(
117+
since = "0.1.2",
118+
note = "Deprecated to improve alignment with Esplora API. Users should use `get_block_hash` and `get_header_by_hash` methods directly."
119+
)]
116120
/// Get a [`BlockHeader`] given a particular block height.
117121
pub async fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
118122
let block_hash = self.get_block_hash(block_height).await?;
123+
self.get_header_by_hash(&block_hash).await
124+
}
119125

126+
/// Get a [`BlockHeader`] given a particular block hash.
127+
pub async fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result<BlockHeader, Error> {
120128
let resp = self
121129
.client
122130
.get(&format!("{}/block/{}/header", self.url, block_hash))

src/blocking.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,17 @@ impl BlockingClient {
127127
}
128128

129129
/// Get a [`BlockHeader`] given a particular block height.
130+
#[deprecated(
131+
since = "0.1.2",
132+
note = "Deprecated to improve alignment with Esplora API. Users should use `get_block_hash` and `get_header_by_hash` methods directly."
133+
)]
130134
pub fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
131135
let block_hash = self.get_block_hash(block_height)?;
136+
self.get_header_by_hash(&block_hash)
137+
}
132138

139+
/// Get a [`BlockHeader`] given a particular block hash.
140+
pub fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result<BlockHeader, Error> {
133141
let resp = self
134142
.agent
135143
.get(&format!("{}/block/{}/header", self.url, block_hash))

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,13 @@ mod test {
450450

451451
#[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
452452
#[tokio::test]
453-
async fn test_get_header() {
453+
async fn test_get_header_by_hash() {
454454
let (blocking_client, async_client) = setup_clients().await;
455-
let block_header = blocking_client.get_header(53).unwrap();
456-
let block_header_async = async_client.get_header(53).await.unwrap();
455+
456+
let block_hash = BITCOIND.client.get_block_hash(23).unwrap();
457+
458+
let block_header = blocking_client.get_header_by_hash(&block_hash).unwrap();
459+
let block_header_async = async_client.get_header_by_hash(&block_hash).await.unwrap();
457460
assert_eq!(block_header, block_header_async);
458461
}
459462

0 commit comments

Comments
 (0)