diff --git a/.github/workflows/cont_integration.yml b/.github/workflows/cont_integration.yml index 3f33321..b8e09c8 100644 --- a/.github/workflows/cont_integration.yml +++ b/.github/workflows/cont_integration.yml @@ -65,7 +65,7 @@ jobs: cargo update -p parking_lot --precise "0.12.3" cargo update -p parking_lot_core --precise "0.9.10" cargo update -p lock_api --precise "0.4.12" - cargo update -p socket2@0.6.1 --precise "0.5.10" + cargo update -p socket2@0.6.2 --precise "0.5.10" cargo update -p webpki-roots@1.0.5 --precise "1.0.1" cargo update -p openssl --precise "0.10.73" cargo update -p openssl-sys --precise "0.9.109" diff --git a/README.md b/README.md index d13f1b5..1f15d0b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Bitcoin Esplora API client library. Supports plaintext, TLS and Onion servers. B

Crate Info MIT Licensed - CI Status + CI Status Coverage Status API Docs Rustc Version 1.63.0+ @@ -34,7 +34,7 @@ cargo update -p tracing-core --precise "0.1.33" cargo update -p parking_lot --precise "0.12.3" cargo update -p parking_lot_core --precise "0.9.10" cargo update -p lock_api --precise "0.4.12" -cargo update -p socket2@0.6.1 --precise "0.5.10" +cargo update -p socket2@0.6.2 --precise "0.5.10" cargo update -p webpki-roots@1.0.5 --precise "1.0.1" cargo update -p openssl --precise "0.10.73" cargo update -p openssl-sys --precise "0.9.109" diff --git a/src/async.rs b/src/async.rs index 9f8e854..795e8f4 100644 --- a/src/async.rs +++ b/src/async.rs @@ -564,6 +564,7 @@ impl AsyncClient { /// /// The maximum number of summaries returned depends on the backend itself: /// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`. + #[deprecated(since = "0.12.3", note = "use `get_block_infos` instead")] pub async fn get_blocks(&self, height: Option) -> Result, Error> { let path = match height { Some(height) => format!("/blocks/{height}"), @@ -576,6 +577,24 @@ impl AsyncClient { Ok(blocks) } + /// Get summaries about recent blocks as [`BlockInfo`]s, + /// starting at the tip, or at `height`, if provided. + /// + /// The maximum number of elements returned depends on the backend itself: + /// - Esplora returns `10` + /// - [mempool.space](https://mempool.space/docs/api) returns `15` + pub async fn get_block_infos(&self, height: Option) -> Result, Error> { + let path = match height { + Some(height) => format!("/blocks/{height}"), + None => "/blocks".to_string(), + }; + let blocks: Vec = self.get_response_json(&path).await?; + if blocks.is_empty() { + return Err(Error::InvalidResponse); + } + Ok(blocks) + } + /// Get all UTXOs locked to an address. pub async fn get_address_utxos(&self, address: &Address) -> Result, Error> { let path = format!("/address/{address}/utxo"); diff --git a/src/blocking.rs b/src/blocking.rs index 3e68e6c..022feaf 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -498,6 +498,7 @@ impl BlockingClient { /// /// The maximum number of summaries returned depends on the backend itself: /// esplora returns `10` while [mempool.space](https://mempool.space/docs/api) returns `15`. + #[deprecated(since = "0.12.3", note = "use `get_block_infos` instead")] pub fn get_blocks(&self, height: Option) -> Result, Error> { let path = match height { Some(height) => format!("/blocks/{height}"), @@ -510,6 +511,24 @@ impl BlockingClient { Ok(blocks) } + /// Get summaries about recent blocks as [`BlockInfo`]s, + /// starting at the tip, or at `height`, if provided. + /// + /// The maximum number of elements returned depends on the backend itself: + /// - Esplora returns `10` + /// - [mempool.space](https://mempool.space/docs/api) returns `15` + pub fn get_block_infos(&self, height: Option) -> Result, Error> { + let path = match height { + Some(height) => format!("/blocks/{height}"), + None => "/blocks".to_string(), + }; + let blocks: Vec = self.get_response_json(&path)?; + if blocks.is_empty() { + return Err(Error::InvalidResponse); + } + Ok(blocks) + } + /// Get all UTXOs locked to an address. pub fn get_address_utxos(&self, address: &Address) -> Result, Error> { let path = format!("/address/{address}/utxo"); diff --git a/src/lib.rs b/src/lib.rs index d5bb38c..cc9ffcb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1006,6 +1006,7 @@ mod test { assert_eq!(txs_blocking.len(), txs_async.len()); } + #[allow(deprecated)] #[cfg(all(feature = "blocking", feature = "async"))] #[tokio::test] async fn test_get_blocks() { @@ -1035,6 +1036,41 @@ mod test { assert_eq!(blocks_genesis, blocks_genesis_async); } + #[cfg(all(feature = "blocking", feature = "async"))] + #[tokio::test] + async fn test_get_block_infos() { + let (blocking_client, async_client) = setup_clients().await; + + let start_height = BITCOIND.client.get_block_count().unwrap().0; + + let blocks_blocking_0 = blocking_client.get_block_infos(None).unwrap(); + let blocks_async_0 = async_client.get_block_infos(None).await.unwrap(); + assert_eq!(blocks_blocking_0[0].height, start_height as u32); + assert_eq!(blocks_blocking_0, blocks_async_0); + + generate_blocks_and_wait(10); + + let blocks_blocking_1 = blocking_client.get_block_infos(None).unwrap(); + let blocks_async_1 = async_client.get_block_infos(None).await.unwrap(); + assert_eq!(blocks_blocking_1, blocks_async_1); + assert_ne!(blocks_blocking_0, blocks_blocking_1); + + let blocks_blocking_2 = blocking_client + .get_block_infos(Some(start_height as u32)) + .unwrap(); + let blocks_async_3 = async_client + .get_block_infos(Some(start_height as u32)) + .await + .unwrap(); + assert_eq!(blocks_blocking_2, blocks_async_3); + assert_eq!(blocks_blocking_2[0].height, start_height as u32); + assert_eq!(blocks_blocking_2, blocks_blocking_0); + + let blocks_blocking_genesis = blocking_client.get_block_infos(Some(0)).unwrap(); + let blocks_async_genesis = async_client.get_block_infos(Some(0)).await.unwrap(); + assert_eq!(blocks_blocking_genesis, blocks_async_genesis); + } + #[cfg(all(feature = "blocking", feature = "async"))] #[tokio::test] async fn test_get_tx_with_http_header() {