Skip to content

Commit 2e3e8b2

Browse files
committed
Merge #164: feat(client): add get_block_infos and deprecate get_blocks
3500b24 feat(client): add `get_block_infos` (Luis Schwab) c61d680 chore(client): deprecate `get_blocks` (Luis Schwab) ed802d4 fix(docs): update CI badge link (Luis Schwab) 8b0d195 fix(ci): pin `socket2@0.6.2` to v0.5.10 (Luis Schwab) Pull request description: ## Changelog - Add `get_block_infos` - Deprecate `get_blocks` Also pins `socket2@0.6.2` to v0.5.10 and fixes the CI badge link. ACKs for top commit: oleonardolima: ACK 3500b24 notmandatory: utACK 3500b24 Tree-SHA512: 1875027eb732ac3ae79d4861bed96bbddc54c6e801368bcf0940229a911eb52be22a1a8c43e632994c8630416d4ff98749f63da479f604553a0306a0d46361a0
2 parents f3054bc + 3500b24 commit 2e3e8b2

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

.github/workflows/cont_integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
cargo update -p parking_lot --precise "0.12.3"
6666
cargo update -p parking_lot_core --precise "0.9.10"
6767
cargo update -p lock_api --precise "0.4.12"
68-
cargo update -p socket2@0.6.1 --precise "0.5.10"
68+
cargo update -p socket2@0.6.2 --precise "0.5.10"
6969
cargo update -p webpki-roots@1.0.5 --precise "1.0.1"
7070
cargo update -p openssl --precise "0.10.73"
7171
cargo update -p openssl-sys --precise "0.9.109"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bitcoin Esplora API client library. Supports plaintext, TLS and Onion servers. B
55
<p>
66
<a href="https://crates.io/crates/esplora-client"><img alt="Crate Info" src="https://img.shields.io/crates/v/esplora-client.svg"/></a>
77
<a href="https://github.com/bitcoindevkit/rust-esplora-client/blob/master/LICENSE"><img alt="MIT Licensed" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
8-
<a href="https://github.com/bitcoindevkit/rust-esplora-client/actions/workflows/cont_integration.yml"><img alt="CI Status" src="https://github.com/bitcoindevkit/rust-esplora-client/workflows/Rust/badge.svg"></a>
8+
<a href="https://github.com/bitcoindevkit/rust-esplora-client/actions/workflows/cont_integration.yml"><img alt="CI Status" src="https://github.com/bitcoindevkit/rust-esplora-client/actions/workflows/cont_integration.yml/badge.svg?branch=release/0.12.x"></a>
99
<a href='https://coveralls.io/github/bitcoindevkit/rust-esplora-client?branch=master'><img src='https://coveralls.io/repos/github/bitcoindevkit/rust-esplora-client/badge.svg?branch=master' alt='Coverage Status' /></a>
1010
<a href="https://docs.rs/esplora-client"><img alt="API Docs" src="https://img.shields.io/badge/docs.rs-esplora--client-green"/></a>
1111
<a href="https://blog.rust-lang.org/2022/08/11/Rust-1.63.0.html"><img alt="Rustc Version 1.63.0+" src="https://img.shields.io/badge/rustc-1.63.0%2B-lightgrey.svg"/></a>
@@ -34,7 +34,7 @@ cargo update -p tracing-core --precise "0.1.33"
3434
cargo update -p parking_lot --precise "0.12.3"
3535
cargo update -p parking_lot_core --precise "0.9.10"
3636
cargo update -p lock_api --precise "0.4.12"
37-
cargo update -p socket2@0.6.1 --precise "0.5.10"
37+
cargo update -p socket2@0.6.2 --precise "0.5.10"
3838
cargo update -p webpki-roots@1.0.5 --precise "1.0.1"
3939
cargo update -p openssl --precise "0.10.73"
4040
cargo update -p openssl-sys --precise "0.9.109"

src/async.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ impl<S: Sleeper> AsyncClient<S> {
564564
///
565565
/// The maximum number of summaries returned depends on the backend itself:
566566
/// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`.
567+
#[deprecated(since = "0.12.3", note = "use `get_block_infos` instead")]
567568
pub async fn get_blocks(&self, height: Option<u32>) -> Result<Vec<BlockSummary>, Error> {
568569
let path = match height {
569570
Some(height) => format!("/blocks/{height}"),
@@ -576,6 +577,24 @@ impl<S: Sleeper> AsyncClient<S> {
576577
Ok(blocks)
577578
}
578579

580+
/// Get summaries about recent blocks as [`BlockInfo`]s,
581+
/// starting at the tip, or at `height`, if provided.
582+
///
583+
/// The maximum number of elements returned depends on the backend itself:
584+
/// - Esplora returns `10`
585+
/// - [mempool.space](https://mempool.space/docs/api) returns `15`
586+
pub async fn get_block_infos(&self, height: Option<u32>) -> Result<Vec<BlockInfo>, Error> {
587+
let path = match height {
588+
Some(height) => format!("/blocks/{height}"),
589+
None => "/blocks".to_string(),
590+
};
591+
let blocks: Vec<BlockInfo> = self.get_response_json(&path).await?;
592+
if blocks.is_empty() {
593+
return Err(Error::InvalidResponse);
594+
}
595+
Ok(blocks)
596+
}
597+
579598
/// Get all UTXOs locked to an address.
580599
pub async fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
581600
let path = format!("/address/{address}/utxo");

src/blocking.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ impl BlockingClient {
498498
///
499499
/// The maximum number of summaries returned depends on the backend itself:
500500
/// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`.
501+
#[deprecated(since = "0.12.3", note = "use `get_block_infos` instead")]
501502
pub fn get_blocks(&self, height: Option<u32>) -> Result<Vec<BlockSummary>, Error> {
502503
let path = match height {
503504
Some(height) => format!("/blocks/{height}"),
@@ -510,6 +511,24 @@ impl BlockingClient {
510511
Ok(blocks)
511512
}
512513

514+
/// Get summaries about recent blocks as [`BlockInfo`]s,
515+
/// starting at the tip, or at `height`, if provided.
516+
///
517+
/// The maximum number of elements returned depends on the backend itself:
518+
/// - Esplora returns `10`
519+
/// - [mempool.space](https://mempool.space/docs/api) returns `15`
520+
pub fn get_block_infos(&self, height: Option<u32>) -> Result<Vec<BlockInfo>, Error> {
521+
let path = match height {
522+
Some(height) => format!("/blocks/{height}"),
523+
None => "/blocks".to_string(),
524+
};
525+
let blocks: Vec<BlockInfo> = self.get_response_json(&path)?;
526+
if blocks.is_empty() {
527+
return Err(Error::InvalidResponse);
528+
}
529+
Ok(blocks)
530+
}
531+
513532
/// Get all UTXOs locked to an address.
514533
pub fn get_address_utxos(&self, address: &Address) -> Result<Vec<Utxo>, Error> {
515534
let path = format!("/address/{address}/utxo");

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ mod test {
10061006
assert_eq!(txs_blocking.len(), txs_async.len());
10071007
}
10081008

1009+
#[allow(deprecated)]
10091010
#[cfg(all(feature = "blocking", feature = "async"))]
10101011
#[tokio::test]
10111012
async fn test_get_blocks() {
@@ -1035,6 +1036,41 @@ mod test {
10351036
assert_eq!(blocks_genesis, blocks_genesis_async);
10361037
}
10371038

1039+
#[cfg(all(feature = "blocking", feature = "async"))]
1040+
#[tokio::test]
1041+
async fn test_get_block_infos() {
1042+
let (blocking_client, async_client) = setup_clients().await;
1043+
1044+
let start_height = BITCOIND.client.get_block_count().unwrap().0;
1045+
1046+
let blocks_blocking_0 = blocking_client.get_block_infos(None).unwrap();
1047+
let blocks_async_0 = async_client.get_block_infos(None).await.unwrap();
1048+
assert_eq!(blocks_blocking_0[0].height, start_height as u32);
1049+
assert_eq!(blocks_blocking_0, blocks_async_0);
1050+
1051+
generate_blocks_and_wait(10);
1052+
1053+
let blocks_blocking_1 = blocking_client.get_block_infos(None).unwrap();
1054+
let blocks_async_1 = async_client.get_block_infos(None).await.unwrap();
1055+
assert_eq!(blocks_blocking_1, blocks_async_1);
1056+
assert_ne!(blocks_blocking_0, blocks_blocking_1);
1057+
1058+
let blocks_blocking_2 = blocking_client
1059+
.get_block_infos(Some(start_height as u32))
1060+
.unwrap();
1061+
let blocks_async_3 = async_client
1062+
.get_block_infos(Some(start_height as u32))
1063+
.await
1064+
.unwrap();
1065+
assert_eq!(blocks_blocking_2, blocks_async_3);
1066+
assert_eq!(blocks_blocking_2[0].height, start_height as u32);
1067+
assert_eq!(blocks_blocking_2, blocks_blocking_0);
1068+
1069+
let blocks_blocking_genesis = blocking_client.get_block_infos(Some(0)).unwrap();
1070+
let blocks_async_genesis = async_client.get_block_infos(Some(0)).await.unwrap();
1071+
assert_eq!(blocks_blocking_genesis, blocks_async_genesis);
1072+
}
1073+
10381074
#[cfg(all(feature = "blocking", feature = "async"))]
10391075
#[tokio::test]
10401076
async fn test_get_tx_with_http_header() {

0 commit comments

Comments
 (0)