Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: extract client logic to macro #122

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 69 additions & 118 deletions src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use bitcoin::ScriptBuf;
use tokio::sync::broadcast;
pub use tokio::sync::broadcast::Receiver;
pub use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::Sender;

use crate::{IndexedBlock, TxBroadcast};

Expand Down Expand Up @@ -47,67 +47,6 @@ impl Client {
self.nrx.subscribe()
}

/// Tell the node to stop running.
///
/// # Errors
///
/// Errors if the node has already stopped.
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Shutdown)
.await
.map_err(|_| ClientError::SendError)
}

/// Broadcast a new transaction to the network.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn broadcast_tx(&self, tx: TxBroadcast) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Broadcast(tx))
.await
.map_err(|_| ClientError::SendError)
}

/// Add more Bitcoin [`ScriptBuf`] to watch for. Does not rescan the filters.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn add_scripts(&self, scripts: HashSet<ScriptBuf>) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::AddScripts(scripts))
.await
.map_err(|_| ClientError::SendError)
}

/// Starting at the configured anchor checkpoint, look for block inclusions with newly added scripts.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn rescan(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Rescan)
.await
.map_err(|_| ClientError::SendError)
}

/// Explicitly start the block filter syncing process. Note that the node will automatically download and check
/// filters unless the policy is to expilictly halt.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn continue_download(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::ContinueDownload)
.await
.map_err(|_| ClientError::SendError)
}

/// Collect the blocks received from the node into an in-memory cache,
/// returning once the node is synced to its peers.
/// Only recommended for machines that can tolerate such a memory allocation,
Expand Down Expand Up @@ -189,69 +128,81 @@ impl ClientSender {
fn new(ntx: Sender<ClientMessage>) -> Self {
Self { ntx }
}
}

/// Tell the node to shut down.
///
/// # Errors
///
/// If the node has already stopped running.
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Shutdown)
.await
.map_err(|_| ClientError::SendError)
}
macro_rules! impl_core_client {
($client:ident) => {
impl $client {
/// Tell the node to shut down.
///
/// # Errors
///
/// If the node has already stopped running.
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Shutdown)
.await
.map_err(|_| ClientError::SendError)
}

/// Broadcast a new transaction to the network.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn broadcast_tx(&self, tx: TxBroadcast) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Broadcast(tx))
.await
.map_err(|_| ClientError::SendError)
}
/// Broadcast a new transaction to the network.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn broadcast_tx(&self, tx: TxBroadcast) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Broadcast(tx))
.await
.map_err(|_| ClientError::SendError)
}

/// Add more Bitcoin [`ScriptBuf`] to watch for. Does not rescan the filters.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn add_scripts(&self, scripts: HashSet<ScriptBuf>) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::AddScripts(scripts))
.await
.map_err(|_| ClientError::SendError)
}
/// Add more Bitcoin [`ScriptBuf`] to watch for. Does not rescan the filters.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn add_scripts(
&self,
scripts: HashSet<ScriptBuf>,
) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::AddScripts(scripts))
.await
.map_err(|_| ClientError::SendError)
}

/// Starting at the configured anchor checkpoint, look for block inclusions with newly added scripts.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn rescan(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Rescan)
.await
.map_err(|_| ClientError::SendError)
}
/// Starting at the configured anchor checkpoint, look for block inclusions with newly added scripts.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn rescan(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Rescan)
.await
.map_err(|_| ClientError::SendError)
}

/// Explicitly start the block filter syncing process. Note that the node will automatically download and check
/// filters unless the policy is to expilictly halt.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn continue_download(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::ContinueDownload)
.await
.map_err(|_| ClientError::SendError)
}
/// Explicitly start the block filter syncing process. Note that the node will automatically download and check
/// filters unless the policy is to explicitly halt.
///
/// # Errors
///
/// If the node has stopped running.
pub async fn continue_download(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::ContinueDownload)
.await
.map_err(|_| ClientError::SendError)
}
}
};
}

impl_core_client!(Client);
impl_core_client!(ClientSender);

#[cfg(test)]
mod tests {
use bitcoin::{consensus::deserialize, Transaction};
Expand Down
Loading