From 14af46c42a1b894e66aba0bb0ba705099b4caea1 Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Wed, 6 Jul 2022 19:30:51 -0300 Subject: [PATCH] chore: update docs and readme examples, use u32 instead of u64 --- README.md | 31 +++++++++++++++++-------------- src/bin.rs | 1 - src/http.rs | 4 ++-- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3399c1f..b8a67c7 100644 --- a/README.md +++ b/README.md @@ -41,40 +41,43 @@ cargo install --path . cargo run -- data-stream --blocks # to use regtest, you need to pass it as a parameter -cargo run -- --network testnet data-stream --blocks +cargo run -- --base-url localhost:8999/testnet/api/v1 data-stream --blocks ``` ### Subscribing and consuming new block events through the lib: ``` rust use anyhow::{self, Ok}; -use block_events::api::BlockEvent; use futures_util::{pin_mut, StreamExt}; #[tokio::main] async fn main() -> anyhow::Result<()> { env_logger::init(); - // for regtest network - let url = url::Url::parse("ws://localhost:8999/api/v1/ws").unwrap(); + // for mempool.space regtest network + let base_url = "localhost:8999/testnet/api/v1"; - // async fetch the data stream through the lib - let block_events = block_events::websocket::subscribe_to_blocks(&url).await?; + // no checkpoint for this example, check the other one if interested. + let checkpoint = None; - // consume and execute the code (current matching and printing) in async manner for each new block-event + // async fetch the block-events stream through the lib + let block_events = block_events::subscribe_to_blocks(base_url, checkpoint).await?; + + // consume and execute your code (current only matching and printing) in async manner for each new block-event pin_mut!(block_events); while let Some(block_event) = block_events.next().await { match block_event { - BlockEvent::Connected(block) => { - println!("Connected BlockEvent: {:#?}", block); + block_events::api::BlockEvent::Connected(block) => { + println!( + "[connected block][block_hash {:#?}][block_prev_hash {:#?}]", + block.block_hash(), + block.prev_blockhash + ); } - BlockEvent::Disconnected((height, block_hash)) => { + block_events::api::BlockEvent::Disconnected((height, block_hash)) => { println!( - "Disconnected BlockEvent: [height {:#?}] [block_hash: {:#?}]", + "[disconnected block][height {:#?}][block_hash: {:#?}]", height, block_hash ); } - BlockEvent::Error() => { - eprint!("ERROR BlockEvent: received an error from the block-events stream"); - } } } Ok(()) diff --git a/src/bin.rs b/src/bin.rs index 187e596..9825061 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -1,5 +1,4 @@ use anyhow::Ok; -use block_events::api::BlockEvent; use clap::{ArgGroup, Parser, Subcommand}; use futures_util::{pin_mut, StreamExt}; use serde::{Deserialize, Serialize}; diff --git a/src/http.rs b/src/http.rs index 223085a..3bd9346 100644 --- a/src/http.rs +++ b/src/http.rs @@ -37,7 +37,7 @@ impl HttpClient { } /// Get current blockchain block height (the current tip height) - pub async fn _get_height(&self) -> anyhow::Result { + pub async fn _get_height(&self) -> anyhow::Result { let req = self .client .get(&format!("{}/blocks/tip/height", self.url)) @@ -48,7 +48,7 @@ impl HttpClient { } /// Get [`BlockHash`] from mempool.space, for given block height - pub async fn _get_block_height(&self, height: u64) -> anyhow::Result { + pub async fn _get_block_height(&self, height: u32) -> anyhow::Result { let req = self .client .get(&format!("{}/block-height/{}", self.url, height)) diff --git a/src/lib.rs b/src/lib.rs index 6c0e117..4583f8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,48 @@ //! please check out the repository, project proposal or reach out. //! //! # Examples +//! ## Subscribe to all new block events for mempool.space +//! ``` no_run +//! use anyhow::{self, Ok}; +//! use futures_util::{pin_mut, StreamExt}; +//! +//! #[tokio::main] +//! async fn main() -> anyhow::Result<()> { +//! env_logger::init(); +//! +//! // for mempool.space regtest network +//! let base_url = "localhost:8999/testnet/api/v1"; +//! +//! // no checkpoint for this example, check the commented other one if interested. +//! // checkpoint for first BDK Taproot transaction on mainnet (base_url update needed) +//! // let checkpoint = (709635, bitcoin::BlockHash::from("00000000000000000001f9ee4f69cbc75ce61db5178175c2ad021fe1df5bad8f")); +//! let checkpoint = None; +//! +//! // async fetch the block-events stream through the lib +//! let block_events = block_events::subscribe_to_blocks(base_url, checkpoint).await?; +//! +//! // consume and execute your code (current only matching and printing) in async manner for each new block-event +//! pin_mut!(block_events); +//! while let Some(block_event) = block_events.next().await { +//! match block_event { +//! block_events::api::BlockEvent::Connected(block) => { +//! println!( +//! "[connected block][block_hash {:#?}][block_prev_hash {:#?}]", +//! block.block_hash(), +//! block.prev_blockhash +//! ); +//! } +//! block_events::api::BlockEvent::Disconnected((height, block_hash)) => { +//! println!( +//! "[disconnected block][height {:#?}][block_hash: {:#?}]", +//! height, block_hash +//! ); +//! } +//! } +//! } +//! Ok(()) +//! } +//! ``` pub mod api; pub mod http; @@ -133,7 +175,7 @@ impl BlockHeadersCache { /// Subscribe to a real-time stream of [`BlockEvent`], for all new blocks or starting from an optional checkpoint pub async fn subscribe_to_blocks( base_url: &str, - checkpoint: Option<(u64, BlockHash)>, + checkpoint: Option<(u32, BlockHash)>, ) -> anyhow::Result>>> { let http_client = http::HttpClient::new(base_url, DEFAULT_CONCURRENT_REQUESTS); @@ -216,7 +258,7 @@ async fn process_candidates( // FIXME: this fails when checkpoint is genesis block as it does not have a previousblockhash field pub async fn fetch_blocks( http_client: HttpClient, - checkpoint: (u64, BlockHash), + checkpoint: (u32, BlockHash), ) -> anyhow::Result> { let (ckpt_height, ckpt_hash) = checkpoint;