Skip to content

Add get raw block endpoint #25

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 1 commit into from
Nov 1, 2022
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
16 changes: 15 additions & 1 deletion src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::str::FromStr;
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid};
use bitcoin::{Block, BlockHash, BlockHeader, Script, Transaction, Txid};

#[allow(unused_imports)]
use log::{debug, error, info, trace};
Expand Down Expand Up @@ -147,6 +147,20 @@ impl AsyncClient {
Ok(resp.error_for_status()?.json().await?)
}

/// Get a [`Block`] given a particular [`BlockHash`].
pub async fn get_block_by_hash(&self, block_hash: &BlockHash) -> Result<Option<Block>, Error> {
let resp = self
.client
.get(&format!("{}/block/{}/raw", self.url, block_hash))
.send()
.await?;

if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}
Ok(Some(deserialize(&resp.error_for_status()?.bytes().await?)?))
}

/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
pub async fn get_merkle_proof(&self, tx_hash: &Txid) -> Result<Option<MerkleProof>, Error> {
let resp = self
Expand Down
21 changes: 20 additions & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ureq::{Agent, Proxy, Response};
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid};
use bitcoin::{Block, BlockHash, BlockHeader, Script, Transaction, Txid};

use crate::{BlockStatus, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus};

Expand Down Expand Up @@ -164,6 +164,25 @@ impl BlockingClient {
}
}

/// Get a [`Block`] given a particular [`BlockHash`].
pub fn get_block_by_hash(&self, block_hash: &BlockHash) -> Result<Option<Block>, Error> {
let resp = self
.agent
.get(&format!("{}/block/{}/raw", self.url, block_hash))
.call();

match resp {
Ok(resp) => Ok(Some(deserialize(&into_bytes(resp)?)?)),
Err(ureq::Error::Status(code, _)) => {
if is_status_not_found(code) {
return Ok(None);
}
Err(Error::HttpResponse(code))
}
Err(e) => Err(Error::Ureq(e)),
}
}

/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
pub fn get_merkle_proof(&self, txid: &Txid) -> Result<Option<MerkleProof>, Error> {
let resp = self
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,37 @@ mod test {
assert_eq!(expected, block_status_async);
}

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

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

let expected = Some(BITCOIND.client.get_block(&block_hash).unwrap());

let block = blocking_client.get_block_by_hash(&block_hash).unwrap();
let block_async = async_client.get_block_by_hash(&block_hash).await.unwrap();
assert_eq!(expected, block);
assert_eq!(expected, block_async);
}

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

let block = blocking_client
.get_block_by_hash(&BlockHash::all_zeros())
.unwrap();
let block_async = async_client
.get_block_by_hash(&BlockHash::all_zeros())
.await
.unwrap();
assert!(block.is_none());
assert!(block_async.is_none());
}

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