Skip to content

Commit 544a564

Browse files
authored
Merge pull request #1 from rustaceanrob/master
Add `block` route
2 parents 7d035ac + 9f76548 commit 544a564

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This is a client implementation for a subset of the [`block-dn`](https://github.
1414

1515
`sp/tweak-data/<start_block>`: Returns up to 2_000 blocks of BIP-352 partial secrets.
1616

17+
`block/<block_hash>`: Fetch a block by its hash.
18+
1719
# Getting Started
1820

1921
Download all filters from height 700_000 up to the current height.

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use core::time::Duration;
44
use std::{borrow::Cow, io::Cursor, net::SocketAddr};
55

6-
use bitcoin::{bip158::BlockFilter, block::Header, consensus::Decodable};
6+
use bitcoin::{Block, BlockHash, bip158::BlockFilter, block::Header, consensus::Decodable};
77
use models::{Html, ServerStatus, TapTweaks};
88

99
/// Errors that may occur when querying.
@@ -152,6 +152,16 @@ impl<'e> Client<'e> {
152152
.send()?;
153153
Ok(response.json::<TapTweaks>()?)
154154
}
155+
156+
/// Fetch the block by its hash.
157+
pub fn block(&self, block_hash: BlockHash) -> Result<Block, Error> {
158+
let route = self.endpoint.append_route(format!("block/{block_hash}"));
159+
let response = bitreq::get(route)
160+
.with_timeout(self.timeout.as_secs())
161+
.send()?;
162+
let block = bitcoin::consensus::deserialize::<Block>(response.as_bytes())?;
163+
Ok(block)
164+
}
155165
}
156166

157167
#[cfg(test)]

tests/client.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ fn test_tap_tweaks() {
3838
assert!(!tweaks.blocks.is_empty());
3939
let _ = tweaks.fallible_into_iterator();
4040
}
41+
42+
#[test]
43+
fn test_block() {
44+
let client = default_client();
45+
assert!(
46+
client
47+
.block(
48+
"0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5"
49+
.parse()
50+
.unwrap()
51+
)
52+
.is_ok()
53+
)
54+
}

0 commit comments

Comments
 (0)