Skip to content

Allow to retrieve a block header by hash #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ 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<BlockHeader, Error> {
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<BlockHeader, Error> {
let resp = self
.client
.get(&format!("{}/block/{}/header", self.url, block_hash))
Expand Down
8 changes: 8 additions & 0 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,17 @@ 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<BlockHeader, Error> {
Copy link
Contributor Author

@tnull tnull Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should rename it to get_header_by_height. Also, it's rather unfortunate that this is reliant on two consecutive calls, which might be race-y and take longer than the user expects. Maybe we could just scrap this and rather align the interface more with the actual Esplora API, i.e., letting the user implement this on its own?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on removing it. If anything, an API for getting a header by block_height should be implemented by Esplora, the library should just reflect what the Esplora API offers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree get_header should be removed since it doesn't reflect an existing Esplora API. But since we have at least one user (bdk) can you just deprecate it for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecated with 68b9d2b.

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<BlockHeader, Error> {
let resp = self
.agent
.get(&format!("{}/block/{}/header", self.url, block_hash))
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down