Skip to content

Query block hash by by block height #13

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
32 changes: 19 additions & 13 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,11 @@ impl AsyncClient {

/// Get a [`BlockHeader`] given a particular block height.
pub async fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
let resp = self
.client
.get(&format!("{}/block-height/{}", self.url, block_height))
.send()
.await?;

if let StatusCode::NOT_FOUND = resp.status() {
return Err(Error::HeaderHeightNotFound(block_height));
}
let bytes = resp.bytes().await?;
let hash =
std::str::from_utf8(&bytes).map_err(|_| Error::HeaderHeightNotFound(block_height))?;
let block_hash = self.get_block_hash(block_height).await?;

let resp = self
.client
.get(&format!("{}/block/{}/header", self.url, hash))
.get(&format!("{}/block/{}/header", self.url, block_hash))
.send()
.await?;

Expand Down Expand Up @@ -209,6 +198,23 @@ impl AsyncClient {
)?)
}

/// Get the [`BlockHash`] of a specific block height
pub async fn get_block_hash(&self, block_height: u32) -> Result<BlockHash, Error> {
let resp = self
.client
.get(&format!("{}/block-height/{}", self.url, block_height))
.send()
.await?;

if let StatusCode::NOT_FOUND = resp.status() {
return Err(Error::HeaderHeightNotFound(block_height));
}

Ok(BlockHash::from_str(
&resp.error_for_status()?.text().await?,
)?)
}

/// Get confirmed transaction history for the specified address/scripthash,
/// sorted with newest first. Returns 25 transactions per page.
/// More can be requested by specifying the last txid seen by the previous query.
Expand Down
38 changes: 23 additions & 15 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,11 @@ impl BlockingClient {

/// Get a [`BlockHeader`] given a particular block height.
pub fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
let resp = self
.agent
.get(&format!("{}/block-height/{}", self.url, block_height))
.call();

let bytes = match resp {
Ok(resp) => Ok(into_bytes(resp)?),
Err(ureq::Error::Status(code, _)) => Err(Error::HttpResponse(code)),
Err(e) => Err(Error::Ureq(e)),
}?;

let hash =
std::str::from_utf8(&bytes).map_err(|_| Error::HeaderHeightNotFound(block_height))?;
let block_hash = self.get_block_hash(block_height)?;

let resp = self
.agent
.get(&format!("{}/block/{}/header", self.url, hash))
.get(&format!("{}/block/{}/header", self.url, block_hash))
.call();

match resp {
Expand Down Expand Up @@ -231,7 +219,27 @@ impl BlockingClient {
.get(&format!("{}/blocks/tip/hash", self.url))
.call();

match resp {
Self::process_block_result(resp)
}

/// Get the [`BlockHash`] of a specific block height
pub fn get_block_hash(&self, block_height: u32) -> Result<BlockHash, Error> {
let resp = self
.agent
.get(&format!("{}/block-height/{}", self.url, block_height))
.call();

if let Err(ureq::Error::Status(code, _)) = resp {
if is_status_not_found(code) {
return Err(Error::HeaderHeightNotFound(block_height));
}
}

Self::process_block_result(resp)
}

fn process_block_result(response: Result<Response, ureq::Error>) -> Result<BlockHash, Error> {
match response {
Ok(resp) => Ok(BlockHash::from_str(&resp.into_string()?)?),
Err(ureq::Error::Status(code, _)) => Err(Error::HttpResponse(code)),
Err(e) => Err(Error::Ureq(e)),
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,19 @@ mod test {
assert_eq!(tip_hash, tip_hash_async);
}

#[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
#[tokio::test]
async fn test_get_block_hash() {
let (blocking_client, async_client) = setup_clients().await;

let block_hash = BITCOIND.client.get_block_hash(21).unwrap();

let block_hash_blocking = blocking_client.get_block_hash(21).unwrap();
let block_hash_async = async_client.get_block_hash(21).await.unwrap();
assert_eq!(block_hash, block_hash_blocking);
assert_eq!(block_hash, block_hash_async);
Copy link
Contributor

Choose a reason for hiding this comment

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

You may want to add another check comparing the Esplora results to RPC, i.e., BITCOIND.client.get_block_hash(21)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right. Added: 7c66cf1

}

#[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
#[tokio::test]
async fn test_get_txid_at_block_index() {
Expand Down