Skip to content

Commit

Permalink
feat(client): request a block and get a Recevier to await the result
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Dec 16, 2024
1 parent 7b4a322 commit 6dcdebd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<IndexedBlock, FetchBlockError> {
Expand All @@ -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<BlockReceiver, FetchBlockError> {
let (tx, rx) =
tokio::sync::oneshot::channel::<Result<IndexedBlock, FetchBlockError>>();
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
Expand Down
8 changes: 8 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<IndexedBlock, FetchBlockError>>;

const THIRTY_MINS: u64 = 60 * 30;

Expand Down

0 comments on commit 6dcdebd

Please sign in to comment.