Skip to content

Commit 31f9e3e

Browse files
committed
chore(docs): add minor improvements on docs
- standardize dependencies on `Cargo.toml`, and sort it. - add a couple of documentation improvements, on missing docstring fields.
1 parent dbf3aa8 commit 31f9e3e

File tree

6 files changed

+90
-66
lines changed

6 files changed

+90
-66
lines changed

.rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wrap_comments = true
2+
format_code_in_doc_comments = true
3+
edition = "2021"

Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,28 @@ name = "esplora_client"
1717
path = "src/lib.rs"
1818

1919
[dependencies]
20-
serde = { version = "1.0", features = ["derive"] }
2120
bitcoin = { version = "0.32", features = ["serde", "std"], default-features = false }
22-
hex = { package = "hex-conservative", version = "0.2" }
23-
log = "^0.4"
21+
hex = { version = "0.2", package = "hex-conservative" }
22+
log = { version = "^0.4" }
2423
minreq = { version = "2.11.0", features = ["json-using-serde"], optional = true }
25-
reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
24+
reqwest = { version = "0.11", features = ["json"], default-features = false, optional = true }
25+
serde = { version = "1.0", features = ["derive"] }
2626

2727
[dev-dependencies]
28-
serde_json = "1.0"
29-
tokio = { version = "1.20.1", features = ["full"] }
3028
electrsd = { version = "0.28.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_25_0"] }
31-
lazy_static = "1.4.0"
29+
lazy_static = { version = "1.4.0"}
30+
serde_json = { version = "1.0"}
31+
tokio = { version = "1.20.1", features = ["full"] }
3232

3333
[features]
3434
default = ["blocking", "async", "async-https"]
35+
3536
blocking = ["minreq", "minreq/proxy"]
3637
blocking-https = ["blocking", "minreq/https"]
3738
blocking-https-rustls = ["blocking", "minreq/https-rustls"]
3839
blocking-https-native = ["blocking", "minreq/https-native"]
3940
blocking-https-bundled = ["blocking", "minreq/https-bundled"]
41+
4042
async = ["reqwest", "reqwest/socks"]
4143
async-https = ["async", "reqwest/default-tls"]
4244
async-https-native = ["async", "reqwest/native-tls"]

src/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! structs from the esplora API
1+
//! Structs from the Esplora API
22
//!
3-
//! see: <https://github.com/Blockstream/esplora/blob/master/API.md>
3+
//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
44
55
pub use bitcoin::consensus::{deserialize, serialize};
66
pub use bitcoin::hex::FromHex;

src/async.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ use crate::{BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus
3030

3131
#[derive(Debug, Clone)]
3232
pub struct AsyncClient {
33+
/// The URL of the Esplora Server.
3334
url: String,
35+
/// The inner [`reqwest::Client`] to make HTTP requests.
3436
client: Client,
3537
}
3638

3739
impl AsyncClient {
38-
/// build an async client from a builder
40+
/// Build an async client from a builder
3941
pub fn from_builder(builder: Builder) -> Result<Self, Error> {
4042
let mut client_builder = Client::builder();
4143

@@ -64,7 +66,7 @@ impl AsyncClient {
6466
Ok(Self::from_client(builder.base_url, client_builder.build()?))
6567
}
6668

67-
/// build an async client from the base url and [`Client`]
69+
/// Build an async client from the base url and [`Client`]
6870
pub fn from_client(url: String, client: Client) -> Self {
6971
AsyncClient { url, client }
7072
}
@@ -100,7 +102,8 @@ impl AsyncClient {
100102
}
101103
}
102104

103-
/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
105+
/// Get a [`Txid`] of a transaction given its index in a block with a given
106+
/// hash.
104107
pub async fn get_txid_at_block_index(
105108
&self,
106109
block_hash: &BlockHash,
@@ -222,7 +225,8 @@ impl AsyncClient {
222225
}
223226
}
224227

225-
/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
228+
/// Get a merkle inclusion proof for a [`Transaction`] with the given
229+
/// [`Txid`].
226230
pub async fn get_merkle_proof(&self, tx_hash: &Txid) -> Result<Option<MerkleProof>, Error> {
227231
let resp = self
228232
.client
@@ -244,7 +248,8 @@ impl AsyncClient {
244248
}
245249
}
246250

247-
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
251+
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the
252+
/// given [`Txid`].
248253
pub async fn get_merkle_block(&self, tx_hash: &Txid) -> Result<Option<MerkleBlock>, Error> {
249254
let resp = self
250255
.client
@@ -267,7 +272,8 @@ impl AsyncClient {
267272
}
268273
}
269274

270-
/// Get the spending status of an output given a [`Txid`] and the output index.
275+
/// Get the spending status of an output given a [`Txid`] and the output
276+
/// index.
271277
pub async fn get_output_status(
272278
&self,
273279
txid: &Txid,
@@ -372,7 +378,8 @@ impl AsyncClient {
372378

373379
/// Get confirmed transaction history for the specified address/scripthash,
374380
/// sorted with newest first. Returns 25 transactions per page.
375-
/// More can be requested by specifying the last txid seen by the previous query.
381+
/// More can be requested by specifying the last txid seen by the previous
382+
/// query.
376383
pub async fn scripthash_txs(
377384
&self,
378385
script: &Script,
@@ -399,8 +406,8 @@ impl AsyncClient {
399406
}
400407
}
401408

402-
/// Get an map where the key is the confirmation target (in number of blocks)
403-
/// and the value is the estimated feerate (in sat/vB).
409+
/// Get an map where the key is the confirmation target (in number of
410+
/// blocks) and the value is the estimated feerate (in sat/vB).
404411
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
405412
let resp = self
406413
.client
@@ -418,10 +425,11 @@ impl AsyncClient {
418425
}
419426
}
420427

421-
/// Gets some recent block summaries starting at the tip or at `height` if provided.
428+
/// Gets some recent block summaries starting at the tip or at `height` if
429+
/// provided.
422430
///
423-
/// The maximum number of summaries returned depends on the backend itself: esplora returns `10`
424-
/// while [mempool.space](https://mempool.space/docs/api) returns `15`.
431+
/// The maximum number of summaries returned depends on the backend itself:
432+
/// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`.
425433
pub async fn get_blocks(&self, height: Option<u32>) -> Result<Vec<BlockSummary>, Error> {
426434
let url = match height {
427435
Some(height) => format!("{}/blocks/{}", self.url, height),

src/blocking.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{BlockStatus, BlockSummary, Builder, Error, MerkleProof, OutputStatus
3131

3232
#[derive(Debug, Clone)]
3333
pub struct BlockingClient {
34+
/// The URL of the Esplora server.
3435
url: String,
3536
/// The proxy is ignored when targeting `wasm32`.
3637
pub proxy: Option<String>,
@@ -41,7 +42,7 @@ pub struct BlockingClient {
4142
}
4243

4344
impl BlockingClient {
44-
/// build a blocking client from a [`Builder`]
45+
/// Build a blocking client from a [`Builder`]
4546
pub fn from_builder(builder: Builder) -> Self {
4647
Self {
4748
url: builder.base_url,
@@ -199,7 +200,8 @@ impl BlockingClient {
199200
}
200201
}
201202

202-
/// Get a [`Txid`] of a transaction given its index in a block with a given hash.
203+
/// Get a [`Txid`] of a transaction given its index in a block with a given
204+
/// hash.
203205
pub fn get_txid_at_block_index(
204206
&self,
205207
block_hash: &BlockHash,
@@ -233,17 +235,20 @@ impl BlockingClient {
233235
self.get_opt_response(&format!("/block/{}/raw", block_hash))
234236
}
235237

236-
/// Get a merkle inclusion proof for a [`Transaction`] with the given [`Txid`].
238+
/// Get a merkle inclusion proof for a [`Transaction`] with the given
239+
/// [`Txid`].
237240
pub fn get_merkle_proof(&self, txid: &Txid) -> Result<Option<MerkleProof>, Error> {
238241
self.get_opt_response_json(&format!("/tx/{}/merkle-proof", txid))
239242
}
240243

241-
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
244+
/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the
245+
/// given [`Txid`].
242246
pub fn get_merkle_block(&self, txid: &Txid) -> Result<Option<MerkleBlock>, Error> {
243247
self.get_opt_response_hex(&format!("/tx/{}/merkleblock-proof", txid))
244248
}
245249

246-
/// Get the spending status of an output given a [`Txid`] and the output index.
250+
/// Get the spending status of an output given a [`Txid`] and the output
251+
/// index.
247252
pub fn get_output_status(
248253
&self,
249254
txid: &Txid,
@@ -299,15 +304,16 @@ impl BlockingClient {
299304
.map(|s| BlockHash::from_str(s.as_str()).map_err(Error::HexToArray))?
300305
}
301306

302-
/// Get an map where the key is the confirmation target (in number of blocks)
303-
/// and the value is the estimated feerate (in sat/vB).
307+
/// Get an map where the key is the confirmation target (in number of
308+
/// blocks) and the value is the estimated feerate (in sat/vB).
304309
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {
305310
self.get_response_json("/fee-estimates")
306311
}
307312

308313
/// Get confirmed transaction history for the specified address/scripthash,
309314
/// sorted with newest first. Returns 25 transactions per page.
310-
/// More can be requested by specifying the last txid seen by the previous query.
315+
/// More can be requested by specifying the last txid seen by the previous
316+
/// query.
311317
pub fn scripthash_txs(
312318
&self,
313319
script: &Script,
@@ -321,10 +327,11 @@ impl BlockingClient {
321327
self.get_response_json(&path)
322328
}
323329

324-
/// Gets some recent block summaries starting at the tip or at `height` if provided.
330+
/// Gets some recent block summaries starting at the tip or at `height` if
331+
/// provided.
325332
///
326-
/// The maximum number of summaries returned depends on the backend itself: esplora returns `10`
327-
/// while [mempool.space](https://mempool.space/docs/api) returns `15`.
333+
/// The maximum number of summaries returned depends on the backend itself:
334+
/// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`.
328335
pub fn get_blocks(&self, height: Option<u32>) -> Result<Vec<BlockSummary>, Error> {
329336
let path = match height {
330337
Some(height) => format!("/blocks/{}", height),

src/lib.rs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,31 @@
4141
//! specific features, set `default-features` to `false` in your `Cargo.toml`
4242
//! and specify the features you want. This will look like this:
4343
//!
44-
//! `esplora-client = { version = "*", default-features = false, features = ["blocking"] }`
44+
//! `esplora-client = { version = "*", default-features = false, features =
45+
//! ["blocking"] }`
4546
//!
4647
//! * `blocking` enables [`minreq`], the blocking client with proxy.
47-
//! * `blocking-https` enables [`minreq`], the blocking client with proxy and TLS (SSL)
48-
//! capabilities using the default [`minreq`] backend.
49-
//! * `blocking-https-rustls` enables [`minreq`], the blocking client with proxy and TLS (SSL)
50-
//! capabilities using the `rustls` backend.
51-
//! * `blocking-https-native` enables [`minreq`], the blocking client with proxy and TLS (SSL)
52-
//! capabilities using the platform's native TLS backend (likely OpenSSL).
53-
//! * `blocking-https-bundled` enables [`minreq`], the blocking client with proxy and TLS (SSL)
54-
//! capabilities using a bundled OpenSSL library backend.
55-
//! * `async` enables [`reqwest`], the async client with proxy capabilities.
56-
//! * `async-https` enables [`reqwest`], the async client with support for proxying and TLS (SSL)
57-
//! using the default [`reqwest`] TLS backend.
58-
//! * `async-https-native` enables [`reqwest`], the async client with support for proxying and TLS
59-
//! (SSL) using the platform's native TLS backend (likely OpenSSL).
60-
//! * `async-https-rustls` enables [`reqwest`], the async client with support for proxying and TLS
61-
//! (SSL) using the `rustls` TLS backend.
62-
//! * `async-https-rustls-manual-roots` enables [`reqwest`], the async client with support for
63-
//! proxying and TLS (SSL) using the `rustls` TLS backend without using its the default root
64-
//! certificates.
48+
//! * `blocking-https` enables [`minreq`], the blocking client with proxy and
49+
//! TLS (SSL) capabilities using the default [`minreq`] backend.
50+
//! * `blocking-https-rustls` enables [`minreq`], the blocking client with proxy
51+
//! and TLS (SSL) capabilities using the `rustls` backend.
52+
//! * `blocking-https-native` enables [`minreq`], the blocking client with proxy
53+
//! and TLS (SSL) capabilities using the platform's native TLS backend (likely
54+
//! OpenSSL).
55+
//! * `blocking-https-bundled` enables [`minreq`], the blocking client with
56+
//! proxy and TLS (SSL) capabilities using a bundled OpenSSL library backend.
6557
//!
58+
//! * `async` enables [`reqwest`], the async client with proxy capabilities.
59+
//! * `async-https` enables [`reqwest`], the async client with support for
60+
//! proxying and TLS (SSL) using the default [`reqwest`] TLS backend.
61+
//! * `async-https-native` enables [`reqwest`], the async client with support
62+
//! for proxying and TLS (SSL) using the platform's native TLS backend (likely
63+
//! OpenSSL).
64+
//! * `async-https-rustls` enables [`reqwest`], the async client with support
65+
//! for proxying and TLS (SSL) using the `rustls` TLS backend.
66+
//! * `async-https-rustls-manual-roots` enables [`reqwest`], the async client
67+
//! with support for proxying and TLS (SSL) using the `rustls` TLS backend
68+
//! without using its the default root certificates.
6669
//!
6770
6871
#![allow(clippy::result_large_err)]
@@ -71,8 +74,6 @@ use std::collections::HashMap;
7174
use std::fmt;
7275
use std::num::TryFromIntError;
7376

74-
use bitcoin::consensus;
75-
7677
pub mod api;
7778

7879
#[cfg(feature = "async")]
@@ -103,21 +104,24 @@ pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Result<f
103104

104105
#[derive(Debug, Clone)]
105106
pub struct Builder {
107+
/// The URL of the Esplora server.
106108
pub base_url: String,
107109
/// Optional URL of the proxy to use to make requests to the Esplora server
108110
///
109-
/// The string should be formatted as: `<protocol>://<user>:<password>@host:<port>`.
111+
/// The string should be formatted as:
112+
/// `<protocol>://<user>:<password>@host:<port>`.
110113
///
111-
/// Note that the format of this value and the supported protocols change slightly between the
112-
/// blocking version of the client (using `minreq`) and the async version (using `reqwest`). For more
113-
/// details check with the documentation of the two crates. Both of them are compiled with
114+
/// Note that the format of this value and the supported protocols change
115+
/// slightly between the blocking version of the client (using `minreq`)
116+
/// and the async version (using `reqwest`). For more details check with
117+
/// the documentation of the two crates. Both of them are compiled with
114118
/// the `socks` feature enabled.
115119
///
116120
/// The proxy is ignored when targeting `wasm32`.
117121
pub proxy: Option<String>,
118122
/// Socket timeout.
119123
pub timeout: Option<u64>,
120-
/// HTTP headers to set on every request made to Esplora server
124+
/// HTTP headers to set on every request made to Esplora server.
121125
pub headers: HashMap<String, String>,
122126
}
123127

@@ -150,20 +154,20 @@ impl Builder {
150154
self
151155
}
152156

153-
/// build a blocking client from builder
157+
/// Build a blocking client from builder
154158
#[cfg(feature = "blocking")]
155159
pub fn build_blocking(self) -> BlockingClient {
156160
BlockingClient::from_builder(self)
157161
}
158162

159-
// build an asynchronous client from builder
163+
// Build an asynchronous client from builder
160164
#[cfg(feature = "async")]
161165
pub fn build_async(self) -> Result<AsyncClient, Error> {
162166
AsyncClient::from_builder(self)
163167
}
164168
}
165169

166-
/// Errors that can happen during a sync with `Esplora`
170+
/// Errors that can happen during a request to `Esplora` servers.
167171
#[derive(Debug)]
168172
pub enum Error {
169173
/// Error during `minreq` HTTP request
@@ -186,9 +190,9 @@ pub enum Error {
186190
HexToBytes(bitcoin::hex::HexToBytesError),
187191
/// Transaction not found
188192
TransactionNotFound(Txid),
189-
/// Header height not found
193+
/// Block Header height not found
190194
HeaderHeightNotFound(u32),
191-
/// Header hash not found
195+
/// Block Header hash not found
192196
HeaderHashNotFound(BlockHash),
193197
/// Invalid HTTP Header name specified
194198
InvalidHttpHeaderName(String),
@@ -221,7 +225,7 @@ impl_error!(::minreq::Error, Minreq, Error);
221225
#[cfg(feature = "async")]
222226
impl_error!(::reqwest::Error, Reqwest, Error);
223227
impl_error!(std::num::ParseIntError, Parsing, Error);
224-
impl_error!(consensus::encode::Error, BitcoinEncoding, Error);
228+
impl_error!(bitcoin::consensus::encode::Error, BitcoinEncoding, Error);
225229
impl_error!(bitcoin::hex::HexToArrayError, HexToArray, Error);
226230
impl_error!(bitcoin::hex::HexToBytesError, HexToBytes, Error);
227231

@@ -592,8 +596,8 @@ mod test {
592596
#[cfg(all(feature = "blocking", feature = "async"))]
593597
#[tokio::test]
594598
async fn test_get_non_existing_block_status() {
595-
// Esplora returns the same status for orphaned blocks as for non-existing blocks:
596-
// non-existing: https://blockstream.info/api/block/0000000000000000000000000000000000000000000000000000000000000000/status
599+
// Esplora returns the same status for orphaned blocks as for non-existing
600+
// blocks: non-existing: https://blockstream.info/api/block/0000000000000000000000000000000000000000000000000000000000000000/status
597601
// orphaned: https://blockstream.info/api/block/000000000000000000181b1a2354620f66868a723c0c4d5b24e4be8bdfc35a7f/status
598602
// (Here the block is cited as orphaned: https://bitcoinchain.com/block_explorer/block/000000000000000000181b1a2354620f66868a723c0c4d5b24e4be8bdfc35a7f/ )
599603
// For this reason, we only test for the non-existing case here.

0 commit comments

Comments
 (0)