diff --git a/examples/status.rs b/examples/status.rs index c15ce80..312a87e 100644 --- a/examples/status.rs +++ b/examples/status.rs @@ -1,7 +1,7 @@ -use core::time::Duration; +use block_dn_client::Timeout; const ENDPOINT: block_dn_client::Endpoint<'static> = block_dn_client::Endpoint::BLOCK_DN_ORG; -const TIMEOUT: Duration = Duration::from_secs(2); +const TIMEOUT: Timeout = Timeout::from_seconds(2); fn main() { let mut client_builder = block_dn_client::Builder::new(); diff --git a/examples/taproot.rs b/examples/taproot.rs index 9fb96d6..86333d7 100644 --- a/examples/taproot.rs +++ b/examples/taproot.rs @@ -1,8 +1,9 @@ -use core::time::Duration; use std::time::Instant; +use block_dn_client::Timeout; + const ENDPOINT: block_dn_client::Endpoint<'static> = block_dn_client::Endpoint::BLOCK_DN_ORG; -const TIMEOUT: Duration = Duration::from_secs(5); +const TIMEOUT: Timeout = Timeout::from_seconds(5); const TAPROOT_ACTIVATION_HEIGHT: u32 = 700_000; fn main() { diff --git a/src/lib.rs b/src/lib.rs index 5b49c1f..11392eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ //! A Rust client for [`block-dn`](https://github.com/guggero/block-dn#). #![warn(missing_docs)] -use core::time::Duration; use std::{borrow::Cow, io::Cursor, net::SocketAddr}; use bitcoin::{Block, BlockHash, bip158::BlockFilter, block::Header, consensus::Decodable}; @@ -42,11 +41,28 @@ impl<'e> Endpoint<'e> { } } +/// The response timeout permitted. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct Timeout(u64); + +impl Timeout { + /// Build a timeout from number of seconds. + pub const fn from_seconds(seconds: u64) -> Self { + Self(seconds) + } +} + +impl Default for Timeout { + fn default() -> Self { + Self(1) + } +} + /// Build a new client to query data for. #[derive(Debug)] pub struct Builder<'e> { endpoint: Endpoint<'e>, - timeout: Duration, + timeout: Timeout, } impl<'e> Builder<'e> { @@ -54,12 +70,12 @@ impl<'e> Builder<'e> { pub fn new() -> Self { Self { endpoint: Endpoint::BLOCK_DN_ORG, - timeout: Duration::from_secs(1), + timeout: Timeout::default(), } } /// Set the timeout the server has to respond. - pub fn timeout(mut self, timeout: Duration) -> Self { + pub fn timeout(mut self, timeout: Timeout) -> Self { self.timeout = timeout; self } @@ -89,7 +105,7 @@ impl<'e> Default for Builder<'e> { #[derive(Debug)] pub struct Client<'e> { endpoint: Endpoint<'e>, - timeout: Duration, + timeout: Timeout, } impl<'e> Client<'e> { @@ -97,7 +113,7 @@ impl<'e> Client<'e> { /// Return the root HTML of the server. pub fn index_html(&self) -> Result { let response = bitreq::get(self.endpoint.0.to_string()) - .with_timeout(self.timeout.as_secs()) + .with_timeout(self.timeout.0) .send()?; let html = response.as_str()?; Ok(Html(html.to_string())) @@ -106,7 +122,7 @@ impl<'e> Client<'e> { /// Get the status of the server. See [`ServerStatus`] for the response structure. pub fn status(&self) -> Result { let status = bitreq::get(self.endpoint.append_route("status")) - .with_timeout(self.timeout.as_secs()) + .with_timeout(self.timeout.0) .send()?; Ok(status.json::()?) } @@ -116,9 +132,7 @@ impl<'e> Client<'e> { let route = self .endpoint .append_route(format!("headers/{start_height}")); - let response = bitreq::get(route) - .with_timeout(self.timeout.as_secs()) - .send()?; + let response = bitreq::get(route).with_timeout(self.timeout.0).send()?; let mut headers = Vec::with_capacity(Self::EXPECTED_HEADER_LIST_SIZE * 80); for chunk in response.as_bytes().chunks_exact(80) { headers.push(bitcoin::consensus::deserialize::
(chunk)?); @@ -131,9 +145,7 @@ impl<'e> Client<'e> { let route = self .endpoint .append_route(format!("filters/{start_height}")); - let response = bitreq::get(route) - .with_timeout(self.timeout.as_secs()) - .send()?; + let response = bitreq::get(route).with_timeout(self.timeout.0).send()?; let mut cursor = Cursor::new(response.into_bytes()); let mut filters = Vec::new(); while let Ok(bytes) = Vec::::consensus_decode_from_finite_reader(&mut cursor) { @@ -147,18 +159,14 @@ impl<'e> Client<'e> { let route = self .endpoint .append_route(format!("sp/tweak-data/{start_height}")); - let response = bitreq::get(route) - .with_timeout(self.timeout.as_secs()) - .send()?; + let response = bitreq::get(route).with_timeout(self.timeout.0).send()?; Ok(response.json::()?) } /// Fetch the block by its hash. pub fn block(&self, block_hash: BlockHash) -> Result { let route = self.endpoint.append_route(format!("block/{block_hash}")); - let response = bitreq::get(route) - .with_timeout(self.timeout.as_secs()) - .send()?; + let response = bitreq::get(route).with_timeout(self.timeout.0).send()?; let block = bitcoin::consensus::deserialize::(response.as_bytes())?; Ok(block) } diff --git a/tests/client.rs b/tests/client.rs index 9d636b9..97cef41 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,4 +1,4 @@ -use block_dn_client::{Builder, Client}; +use block_dn_client::{Builder, Client, Timeout}; fn default_client() -> Client<'static> { Builder::default().build() @@ -31,9 +31,7 @@ fn test_filters() { #[test] fn test_tap_tweaks() { - let client = Builder::new() - .timeout(core::time::Duration::from_secs(10)) - .build(); + let client = Builder::new().timeout(Timeout::from_seconds(10)).build(); let tweaks = client.tweaks(900_000).unwrap(); assert!(!tweaks.blocks.is_empty()); let _ = tweaks.fallible_into_iterator();