From 829d7033f5de1f1358584b81f7080124268e95c6 Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Fri, 29 Aug 2025 13:16:14 +0100 Subject: [PATCH] Track inventory annoucements of transactions A user may want to know if they are receiving an absurd amount of transaction for DoS protection. They may also want to gauge the connected-ness of a peer and disconnect if they decide the peer is insufficiently connected. --- src/lib.rs | 5 ++++- src/net.rs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index dff038b..44ede77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,8 +181,10 @@ pub enum TimedMessage { CFilters, /// Bitcoin blocks Block, - /// Potential peers on the network + /// Potential peers on the network, either `addr` or `addrv2`. Addr, + /// Transaction announcements by `Tx`, `WTx`, or `WitnessTransaction`. + TransactionAnnouncement, } #[derive(Debug, Clone)] @@ -196,6 +198,7 @@ impl TimedMessages { TimedMessage::CFilters, TimedMessage::Block, TimedMessage::Addr, + TimedMessage::TransactionAnnouncement, ] { map.insert(key, MessageRate::new()); } diff --git a/src/net.rs b/src/net.rs index db830d3..ffde394 100644 --- a/src/net.rs +++ b/src/net.rs @@ -16,6 +16,7 @@ use bitcoin::{ }; use p2p::{ message::{NetworkMessage, RawNetworkMessage, V1MessageHeader}, + message_blockdata::Inventory, Magic, NetworkExt, }; @@ -336,6 +337,26 @@ impl ConnectionReader { } } } + NetworkMessage::Inv(payload) => { + let payload = &payload.0; + let now = Instant::now(); + if let Ok(mut lock) = self.timed_messages.lock() { + for inv in payload { + match inv { + Inventory::WTx(_) => { + lock.add_single(TimedMessage::TransactionAnnouncement, now); + } + Inventory::Transaction(_) => { + lock.add_single(TimedMessage::TransactionAnnouncement, now); + } + Inventory::WitnessTransaction(_) => { + lock.add_single(TimedMessage::TransactionAnnouncement, now); + } + _ => (), + } + } + } + } _ => (), } }