From 9e233efa152382af66bfa936b1a790175d743911 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Thu, 6 Oct 2022 10:21:11 +0200 Subject: [PATCH 1/3] Allow to retrieve a block header by hash --- src/async.rs | 4 ++++ src/blocking.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/async.rs b/src/async.rs index f2a58e9..3d8f992 100644 --- a/src/async.rs +++ b/src/async.rs @@ -116,7 +116,11 @@ impl AsyncClient { /// Get a [`BlockHeader`] given a particular block height. pub async fn get_header(&self, block_height: u32) -> Result { let block_hash = self.get_block_hash(block_height).await?; + self.get_header_by_hash(&block_hash).await + } + /// Get a [`BlockHeader`] given a particular block hash. + pub async fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result { let resp = self .client .get(&format!("{}/block/{}/header", self.url, block_hash)) diff --git a/src/blocking.rs b/src/blocking.rs index f2c2507..a3c1a4c 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -129,7 +129,11 @@ impl BlockingClient { /// Get a [`BlockHeader`] given a particular block height. pub fn get_header(&self, block_height: u32) -> Result { let block_hash = self.get_block_hash(block_height)?; + self.get_header_by_hash(&block_hash) + } + /// Get a [`BlockHeader`] given a particular block hash. + pub fn get_header_by_hash(&self, block_hash: &BlockHash) -> Result { let resp = self .agent .get(&format!("{}/block/{}/header", self.url, block_hash)) From 6673912c08ac5786c2f99641df6a9b1a7b5dbcac Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 11 Oct 2022 11:02:36 -0400 Subject: [PATCH 2/3] Deprecate `get_height` This deprecates `get_height` to improve alignment with the Esplora API. --- src/async.rs | 4 ++++ src/blocking.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/async.rs b/src/async.rs index 3d8f992..1f2e2ad 100644 --- a/src/async.rs +++ b/src/async.rs @@ -113,6 +113,10 @@ impl AsyncClient { Ok(Some(resp.error_for_status()?.json().await?)) } + #[deprecated( + since = "0.1.2", + note = "Deprecated to improve alignment with Esplora API. Users should use `get_block_hash` and `get_header_by_hash` methods directly." + )] /// Get a [`BlockHeader`] given a particular block height. pub async fn get_header(&self, block_height: u32) -> Result { let block_hash = self.get_block_hash(block_height).await?; diff --git a/src/blocking.rs b/src/blocking.rs index a3c1a4c..08e72b4 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -127,6 +127,10 @@ impl BlockingClient { } /// Get a [`BlockHeader`] given a particular block height. + #[deprecated( + since = "0.1.2", + note = "Deprecated to improve alignment with Esplora API. Users should use `get_block_hash` and `get_header_by_hash` methods directly." + )] pub fn get_header(&self, block_height: u32) -> Result { let block_hash = self.get_block_hash(block_height)?; self.get_header_by_hash(&block_hash) From 92606cc6af93a0a2aae133d8e9d4392b78f1c5e1 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 11 Oct 2022 11:03:36 -0400 Subject: [PATCH 3/3] Test `get_header_by_hash` rather than `get_height` --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6e1d8c1..2af5709 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -450,10 +450,13 @@ mod test { #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))] #[tokio::test] - async fn test_get_header() { + async fn test_get_header_by_hash() { let (blocking_client, async_client) = setup_clients().await; - let block_header = blocking_client.get_header(53).unwrap(); - let block_header_async = async_client.get_header(53).await.unwrap(); + + let block_hash = BITCOIND.client.get_block_hash(23).unwrap(); + + let block_header = blocking_client.get_header_by_hash(&block_hash).unwrap(); + let block_header_async = async_client.get_header_by_hash(&block_hash).await.unwrap(); assert_eq!(block_header, block_header_async); }