diff --git a/src/client.rs b/src/client.rs index 31fa39d0..7eda3bf5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -6,7 +6,7 @@ use tokio::sync::oneshot; use crate::chain::block_subsidy; use crate::messages::ClientRequest; -use crate::{Event, Info, TrustedPeer, TxBroadcast, Warning}; +use crate::{Event, Info, TrustedPeer, Warning}; use super::{error::ClientError, messages::ClientMessage}; use super::{error::FetchBlockError, IndexedBlock}; @@ -76,25 +76,9 @@ impl Requester { /// # Errors /// /// If the node has stopped running. - pub async fn broadcast_tx(&self, tx_broadcast: TxBroadcast) -> Result { + pub async fn broadcast_tx(&self, transaction: Transaction) -> Result { let (tx, rx) = tokio::sync::oneshot::channel::(); - let client_request = ClientRequest::new(tx_broadcast, tx); - self.ntx - .send(ClientMessage::Broadcast(client_request)) - .map_err(|_| ClientError::SendError)?; - rx.await.map_err(|_| ClientError::RecvError) - } - - /// Broadcast a new transaction to the network to a random peer, waiting for the peer to - /// request the data. - /// - /// # Errors - /// - /// If the node has stopped running. - pub async fn broadcast_random(&self, tx: Transaction) -> Result { - let tx_broadcast = TxBroadcast::random_broadcast(tx); - let (tx, rx) = tokio::sync::oneshot::channel::(); - let client_request = ClientRequest::new(tx_broadcast, tx); + let client_request = ClientRequest::new(transaction, tx); self.ntx .send(ClientMessage::Broadcast(client_request)) .map_err(|_| ClientError::SendError)?; diff --git a/src/lib.rs b/src/lib.rs index 2e670f15..de58f6f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,43 +177,6 @@ impl std::cmp::Ord for IndexedFilter { } } -/// Broadcast a [`Transaction`] to a set of connected peers. -#[derive(Debug, Clone)] -pub struct TxBroadcast { - /// The presumably valid Bitcoin transaction. - pub tx: Transaction, - /// The strategy for how this transaction should be shared with the network. - pub broadcast_policy: TxBroadcastPolicy, -} - -impl TxBroadcast { - /// Prepare a transaction for broadcast with associated broadcast strategy. - pub fn new(tx: Transaction, broadcast_policy: TxBroadcastPolicy) -> Self { - Self { - tx, - broadcast_policy, - } - } - - /// Prepare a transaction to be broadcasted to a random connection. - pub fn random_broadcast(tx: Transaction) -> Self { - Self { - tx, - broadcast_policy: TxBroadcastPolicy::RandomPeer, - } - } -} - -/// The strategy for how this transaction should be shared with the network. -#[derive(Debug, Default, Clone)] -pub enum TxBroadcastPolicy { - /// Broadcast the transaction to all peers at the same time. - AllPeers, - /// Broadcast the transaction to a single random peer, optimal for user privacy. - #[default] - RandomPeer, -} - /// A peer on the Bitcoin P2P network /// /// # Building peers diff --git a/src/messages.rs b/src/messages.rs index 0de3e663..36dd3b53 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,11 +1,13 @@ use std::collections::BTreeMap; use std::ops::Div; -use bitcoin::{block::Header, p2p::message_network::RejectReason, BlockHash, FeeRate, Wtxid}; +use bitcoin::{ + block::Header, p2p::message_network::RejectReason, BlockHash, FeeRate, Transaction, Wtxid, +}; use crate::chain::BlockHeaderChanges; use crate::IndexedFilter; -use crate::{chain::checkpoints::HeaderCheckpoint, IndexedBlock, TrustedPeer, TxBroadcast}; +use crate::{chain::checkpoints::HeaderCheckpoint, IndexedBlock, TrustedPeer}; use super::error::FetchBlockError; @@ -138,7 +140,7 @@ pub(crate) enum ClientMessage { /// Stop the node. Shutdown, /// Broadcast a [`crate::Transaction`] with a [`crate::TxBroadcastPolicy`]. - Broadcast(ClientRequest), + Broadcast(ClientRequest), /// Starting at the configured anchor checkpoint, re-emit all filters. Rescan, /// Explicitly request a block from the node. diff --git a/src/node.rs b/src/node.rs index 62cef75c..9258770e 100644 --- a/src/node.rs +++ b/src/node.rs @@ -9,7 +9,7 @@ use bitcoin::{ message_network::VersionMessage, ServiceFlags, }, - Block, BlockHash, Network, Wtxid, + Block, BlockHash, Network, Transaction, Wtxid, }; use tokio::{ select, @@ -37,7 +37,7 @@ use crate::{ peer_map::PeerMap, LastBlockMonitor, MainThreadMessage, PeerId, PeerMessage, PeerThreadMessage, }, - Config, IndexedBlock, NodeState, TxBroadcast, TxBroadcastPolicy, + Config, IndexedBlock, NodeState, }; use super::{ @@ -288,29 +288,15 @@ impl Node { } // Broadcast transactions according to the configured policy - async fn broadcast_transaction(&self, broadcast: ClientRequest) { + async fn broadcast_transaction(&self, broadcast: ClientRequest) { let mut queue = self.peer_map.tx_queue.lock().await; - let (data, oneshot) = broadcast.into_values(); - let policy = data.broadcast_policy; - queue.add_to_queue(data.tx, oneshot); + let (transaction, oneshot) = broadcast.into_values(); + queue.add_to_queue(transaction, oneshot); drop(queue); - match policy { - TxBroadcastPolicy::AllPeers => { - crate::debug!(format!( - "Sending transaction to {} connected peers", - self.peer_map.live() - )); - self.peer_map - .broadcast(MainThreadMessage::BroadcastPending) - .await - } - TxBroadcastPolicy::RandomPeer => { - crate::debug!("Sending transaction to a random peer"); - self.peer_map - .send_random(MainThreadMessage::BroadcastPending) - .await - } - }; + crate::debug!("Sending transaction to a random peer"); + self.peer_map + .send_random(MainThreadMessage::BroadcastPending) + .await; } // Try to continue with the syncing process diff --git a/tests/core.rs b/tests/core.rs index e306ea1c..00dafacf 100644 --- a/tests/core.rs +++ b/tests/core.rs @@ -623,7 +623,7 @@ async fn tx_can_broadcast() { warn_rx: _, event_rx: _, } = client; - tokio::time::timeout(Duration::from_secs(60), requester.broadcast_random(tx)) + tokio::time::timeout(Duration::from_secs(60), requester.broadcast_tx(tx)) .await .unwrap() .unwrap();