From 6dcdebdb484fc810c3d9a057a4816e4fb121f823 Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Sun, 15 Dec 2024 18:29:42 -1000 Subject: [PATCH] feat(client): request a block and get a `Recevier` to await the result --- example/managed.rs | 2 +- src/core/client.rs | 25 +++++++++++++++++++++++-- src/core/mod.rs | 8 ++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/example/managed.rs b/example/managed.rs index 38d7f4c..1b7e469 100644 --- a/example/managed.rs +++ b/example/managed.rs @@ -66,7 +66,7 @@ async fn main() { if filter.contains_any(&addresses).await { let hash = *filter.block_hash(); tracing::info!("Found script at {}!", hash); - let indexed_block = client.request_block(hash).await.unwrap(); + let indexed_block = client.get_block(hash).await.unwrap(); let coinbase = indexed_block.block.txdata.first().unwrap().compute_txid(); tracing::info!("Coinbase transaction ID: {}", coinbase); break; diff --git a/src/core/client.rs b/src/core/client.rs index 7d754fa..75a8353 100644 --- a/src/core/client.rs +++ b/src/core/client.rs @@ -11,7 +11,7 @@ use tokio::sync::mpsc::Sender; use crate::{IndexedBlock, TrustedPeer, TxBroadcast}; #[cfg(feature = "filter-control")] -use super::{error::FetchBlockError, messages::BlockRequest}; +use super::{error::FetchBlockError, messages::BlockRequest, BlockReceiver}; use super::{ error::{ClientError, FetchHeaderError}, messages::{ClientMessage, HeaderRequest, NodeMessage, SyncUpdate}, @@ -241,7 +241,7 @@ macro_rules! impl_core_client { /// /// If the node has stopped running. #[cfg(feature = "filter-control")] - pub async fn request_block( + pub async fn get_block( &self, block_hash: BlockHash, ) -> Result { @@ -255,6 +255,27 @@ macro_rules! impl_core_client { rx.await.map_err(|_| FetchBlockError::RecvError)? } + /// Request a block be fetched and receive a [`tokio::sync::oneshot::Receiver`] + /// to await the resulting block. + /// + /// # Errors + /// + /// If the node has stopped running. + #[cfg(feature = "filter-control")] + pub async fn request_block( + &self, + block_hash: BlockHash, + ) -> Result { + let (tx, rx) = + tokio::sync::oneshot::channel::>(); + let message = BlockRequest::new(tx, block_hash); + self.ntx + .send(ClientMessage::GetBlock(message)) + .await + .map_err(|_| FetchBlockError::SendError)?; + Ok(rx) + } + /// Starting at the configured anchor checkpoint, look for block inclusions with newly added scripts. /// /// # Errors diff --git a/src/core/mod.rs b/src/core/mod.rs index c2ae66a..b0596c2 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -27,6 +27,14 @@ pub mod messages; /// The structure that communicates with the Bitcoin P2P network and collects data. pub mod node; mod peer_map; +#[cfg(feature = "filter-control")] +use crate::IndexedBlock; +#[cfg(feature = "filter-control")] +use error::FetchBlockError; + +/// Receive an [`IndexedBlock`] from a request. +#[cfg(feature = "filter-control")] +pub type BlockReceiver = tokio::sync::oneshot::Receiver>; const THIRTY_MINS: u64 = 60 * 30;