11//! A Rust client for [`block-dn`](https://github.com/guggero/block-dn#).
22#![ warn( missing_docs) ]
3- use core:: time:: Duration ;
43use std:: { borrow:: Cow , io:: Cursor , net:: SocketAddr } ;
54
65use bitcoin:: { Block , BlockHash , bip158:: BlockFilter , block:: Header , consensus:: Decodable } ;
@@ -42,24 +41,41 @@ impl<'e> Endpoint<'e> {
4241 }
4342}
4443
44+ /// The response timeout permitted.
45+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
46+ pub struct Timeout ( u64 ) ;
47+
48+ impl Timeout {
49+ /// Build a timeout from number of seconds.
50+ pub const fn from_seconds ( seconds : u64 ) -> Self {
51+ Self ( seconds)
52+ }
53+ }
54+
55+ impl Default for Timeout {
56+ fn default ( ) -> Self {
57+ Self ( 1 )
58+ }
59+ }
60+
4561/// Build a new client to query data for.
4662#[ derive( Debug ) ]
4763pub struct Builder < ' e > {
4864 endpoint : Endpoint < ' e > ,
49- timeout : Duration ,
65+ timeout : Timeout ,
5066}
5167
5268impl < ' e > Builder < ' e > {
5369 /// Create a new builder [`ClientBuilder`].
5470 pub fn new ( ) -> Self {
5571 Self {
5672 endpoint : Endpoint :: BLOCK_DN_ORG ,
57- timeout : Duration :: from_secs ( 1 ) ,
73+ timeout : Timeout :: default ( ) ,
5874 }
5975 }
6076
6177 /// Set the timeout the server has to respond.
62- pub fn timeout ( mut self , timeout : Duration ) -> Self {
78+ pub fn timeout ( mut self , timeout : Timeout ) -> Self {
6379 self . timeout = timeout;
6480 self
6581 }
@@ -89,15 +105,15 @@ impl<'e> Default for Builder<'e> {
89105#[ derive( Debug ) ]
90106pub struct Client < ' e > {
91107 endpoint : Endpoint < ' e > ,
92- timeout : Duration ,
108+ timeout : Timeout ,
93109}
94110
95111impl < ' e > Client < ' e > {
96112 const EXPECTED_HEADER_LIST_SIZE : usize = 100_000 ;
97113 /// Return the root HTML of the server.
98114 pub fn index_html ( & self ) -> Result < Html , Error > {
99115 let response = bitreq:: get ( self . endpoint . 0 . to_string ( ) )
100- . with_timeout ( self . timeout . as_secs ( ) )
116+ . with_timeout ( self . timeout . 0 )
101117 . send ( ) ?;
102118 let html = response. as_str ( ) ?;
103119 Ok ( Html ( html. to_string ( ) ) )
@@ -106,7 +122,7 @@ impl<'e> Client<'e> {
106122 /// Get the status of the server. See [`ServerStatus`] for the response structure.
107123 pub fn status ( & self ) -> Result < ServerStatus , Error > {
108124 let status = bitreq:: get ( self . endpoint . append_route ( "status" ) )
109- . with_timeout ( self . timeout . as_secs ( ) )
125+ . with_timeout ( self . timeout . 0 )
110126 . send ( ) ?;
111127 Ok ( status. json :: < ServerStatus > ( ) ?)
112128 }
@@ -116,9 +132,7 @@ impl<'e> Client<'e> {
116132 let route = self
117133 . endpoint
118134 . append_route ( format ! ( "headers/{start_height}" ) ) ;
119- let response = bitreq:: get ( route)
120- . with_timeout ( self . timeout . as_secs ( ) )
121- . send ( ) ?;
135+ let response = bitreq:: get ( route) . with_timeout ( self . timeout . 0 ) . send ( ) ?;
122136 let mut headers = Vec :: with_capacity ( Self :: EXPECTED_HEADER_LIST_SIZE * 80 ) ;
123137 for chunk in response. as_bytes ( ) . chunks_exact ( 80 ) {
124138 headers. push ( bitcoin:: consensus:: deserialize :: < Header > ( chunk) ?) ;
@@ -131,9 +145,7 @@ impl<'e> Client<'e> {
131145 let route = self
132146 . endpoint
133147 . append_route ( format ! ( "filters/{start_height}" ) ) ;
134- let response = bitreq:: get ( route)
135- . with_timeout ( self . timeout . as_secs ( ) )
136- . send ( ) ?;
148+ let response = bitreq:: get ( route) . with_timeout ( self . timeout . 0 ) . send ( ) ?;
137149 let mut cursor = Cursor :: new ( response. into_bytes ( ) ) ;
138150 let mut filters = Vec :: new ( ) ;
139151 while let Ok ( bytes) = Vec :: < u8 > :: consensus_decode_from_finite_reader ( & mut cursor) {
@@ -147,18 +159,14 @@ impl<'e> Client<'e> {
147159 let route = self
148160 . endpoint
149161 . append_route ( format ! ( "sp/tweak-data/{start_height}" ) ) ;
150- let response = bitreq:: get ( route)
151- . with_timeout ( self . timeout . as_secs ( ) )
152- . send ( ) ?;
162+ let response = bitreq:: get ( route) . with_timeout ( self . timeout . 0 ) . send ( ) ?;
153163 Ok ( response. json :: < TapTweaks > ( ) ?)
154164 }
155165
156166 /// Fetch the block by its hash.
157167 pub fn block ( & self , block_hash : BlockHash ) -> Result < Block , Error > {
158168 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 ( ) ?;
169+ let response = bitreq:: get ( route) . with_timeout ( self . timeout . 0 ) . send ( ) ?;
162170 let block = bitcoin:: consensus:: deserialize :: < Block > ( response. as_bytes ( ) ) ?;
163171 Ok ( block)
164172 }
0 commit comments