Skip to content

Commit

Permalink
refactor!: track broadcast fee minimum and remove FeeFilter event
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Jan 10, 2025
1 parent 4523bfe commit e59e378
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
1 change: 0 additions & 1 deletion example/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ async fn main() {
Event::BlocksDisconnected(_) => {
tracing::warn!("Some blocks were reorganized")
},
Event::FeeFilter(fee_rate) => tracing::info!("Minimum broadcast fee: {fee_rate}"),
_ => (),
}
}
Expand Down
1 change: 0 additions & 1 deletion example/testnet4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ async fn main() {
Event::BlocksDisconnected(_) => {
tracing::warn!("Some blocks were reorganized")
},
Event::FeeFilter(fee_rate) => tracing::info!("Minimum broadcast fee: {fee_rate}"),
_ => (),
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/core/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, ops::Range, time::Duration};

#[cfg(feature = "filter-control")]
use bitcoin::BlockHash;
use bitcoin::{block::Header, p2p::message_network::RejectReason, FeeRate, ScriptBuf, Txid};
use bitcoin::{block::Header, p2p::message_network::RejectReason, ScriptBuf, Txid};

#[cfg(feature = "filter-control")]
use crate::IndexedFilter;
Expand Down Expand Up @@ -64,11 +64,6 @@ pub enum Event {
BlocksDisconnected(Vec<DisconnectedHeader>),
/// A problem occured sending a transaction. Either the remote node disconnected or the transaction was rejected.
TxBroadcastFailure(FailurePayload),
/// A connection has a minimum transaction fee requirement to enter its mempool. For proper transaction propagation,
/// transactions should have a fee rate at least as high as the maximum fee filter received.
///
/// For more information, refer to BIP133.
FeeFilter(FeeRate),
/// A compact block filter with associated height and block hash.
#[cfg(feature = "filter-control")]
IndexedFilter(IndexedFilter),
Expand Down
4 changes: 4 additions & 0 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
.send_warning(Warning::TransactionRejected).await;
self.dialog.send_event(Event::TxBroadcastFailure(payload)).await;
}
PeerMessage::FeeFilter(feerate) => {
let mut peer_map = self.peer_map.lock().await;
peer_map.set_broadcast_min(peer_thread.nonce, feerate);
}
_ => continue,
}
},
Expand Down
20 changes: 19 additions & 1 deletion src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use bitcoin::{
key::rand,
p2p::{address::AddrV2, ServiceFlags},
Network,
FeeRate, Network,
};
use rand::{rngs::StdRng, seq::IteratorRandom, SeedableRng};
use tokio::{
Expand Down Expand Up @@ -50,6 +50,7 @@ pub(crate) struct ManagedPeer {
address: AddrV2,
port: u16,
service_flags: Option<ServiceFlags>,
broadcast_min: FeeRate,
ptx: Sender<MainThreadMessage>,
handle: JoinHandle<Result<(), PeerError>>,
}
Expand Down Expand Up @@ -198,6 +199,7 @@ impl<P: PeerStore> PeerMap<P> {
service_flags: None,
address: loaded_peer.addr,
port: loaded_peer.port,
broadcast_min: FeeRate::BROADCAST_MIN,
net_time: 0,
ptx,
handle,
Expand All @@ -206,6 +208,13 @@ impl<P: PeerStore> PeerMap<P> {
Ok(())
}

// Set the minimum fee rate this peer will accept
pub fn set_broadcast_min(&mut self, nonce: u32, fee_rate: FeeRate) {
if let Some(peer) = self.map.get_mut(&nonce) {
peer.broadcast_min = fee_rate;
}
}

// Set the services of a peer
pub fn set_services(&mut self, nonce: u32, flags: ServiceFlags) {
if let Some(peer) = self.map.get_mut(&nonce) {
Expand All @@ -230,6 +239,15 @@ impl<P: PeerStore> PeerMap<P> {
self.heights.values().max()
}

// The minimum fee rate to successfully broadcast a transaction to all peers
pub fn broadcast_min(&self) -> FeeRate {
self.map
.values()
.map(|peer| peer.broadcast_min)
.max()
.unwrap_or(FeeRate::BROADCAST_MIN)
}

// Send a message to the specified peer
pub async fn send_message(&mut self, nonce: u32, message: MainThreadMessage) {
if let Some(peer) = self.map.get(&nonce) {
Expand Down
9 changes: 7 additions & 2 deletions src/network/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::{
PeerTimeoutConfig,
},
network::outbound_messages::V1OutboundMessage,
Event,
};

use super::{
Expand Down Expand Up @@ -297,7 +296,13 @@ impl Peer {
}
PeerMessage::Pong(_) => Ok(()),
PeerMessage::FeeFilter(fee) => {
self.dialog.send_event(Event::FeeFilter(fee)).await;
self.main_thread_sender
.send(PeerThreadMessage {
nonce: self.nonce,
message: PeerMessage::FeeFilter(fee),
})
.await
.map_err(|_| PeerError::ThreadChannel)?;
Ok(())
}
PeerMessage::Reject(payload) => {
Expand Down

0 comments on commit e59e378

Please sign in to comment.