diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf1e14b3..338149b0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A node now relays newer versions of an announcement to peers that relayed an older version of it. Before, a peer could stop receiving a node's announcements after its direct link to that node dropped. +- A node no longer relays an inventory announcement back to peers that + already sent it the same version, which reduces duplicate gossip traffic. ## 1.10.3 - 2026-09-07 diff --git a/crates/radicle-node/src/tests.rs b/crates/radicle-node/src/tests.rs index 190ef9daa..6c7124629 100644 --- a/crates/radicle-node/src/tests.rs +++ b/crates/radicle-node/src/tests.rs @@ -2203,15 +2203,11 @@ fn announcement_message_amplification() { // In the above case of 5 nodes, this is 4 * 4 = 16 messages. This is an amplification of 4.0. // The best case is an amplification of 1.0, ie. each node receives the message once only. // - // Delayed message propagation is meant to bring this down closer to the minimum. But a node - // does not record the sender of a duplicate as a relayer, and so relays the message back to - // it, which keeps the amplification close to the worst case. + // By using delayed message propagation though, we can bring this down closer to the minimum. + // Since every node relays to every peer it did not receive the message from, the best case + // here is 4 direct messages plus 3 + 2 + 1 + 0 relayed ones, an amplification of 2.5. log::debug!(target: "test", "Average message amplification: {amp}"); - // FIXME: This should be lower. - assert!( - amp > 3., - "Amplification factor of {amp} is lower than expected" - ); + assert!(amp < 3.25, "Amplification factor of {amp} is too high"); assert!(amp >= 1., "Amplification can't be lower than 1"); } diff --git a/crates/radicle-protocol/src/service.rs b/crates/radicle-protocol/src/service.rs index 348d1b91b..a544debf5 100644 --- a/crates/radicle-protocol/src/service.rs +++ b/crates/radicle-protocol/src/service.rs @@ -339,9 +339,9 @@ pub struct Service { sessions: Sessions, /// Clock. Tells the time. clock: LocalTime, - /// Who relayed what announcement to us. We keep track of this to ensure that - /// we don't relay messages to nodes that already know about these messages. - relayed_by: HashMap>, + /// The latest version of each announcement we have seen, and who relayed that version to us. + /// We keep track of this to ensure that we do not relay it back to them. + relayed_by: HashMap, /// I/O outbox. outbox: Outbox, /// Cached local node announcement. @@ -1488,6 +1488,23 @@ where } } + // Keep track of who relayed the latest version of the message for later, including + // duplicates of it. + let entry = self + .relayed_by + .entry(AnnouncementSubject::from(announcement)) + .or_insert_with(|| AnnouncementRelayers { + timestamp, + relayers: Vec::new(), + }); + if entry.timestamp < timestamp { + entry.timestamp = timestamp; + entry.relayers.clear(); + } + if entry.timestamp == timestamp && !entry.relayers.contains(relayer) { + entry.relayers.push(*relayer); + } + // Discard announcement messages we've already seen; otherwise, update our last seen time. let relay = match self.db.gossip_mut().announced(announcer, announcement) { Ok(Some(id)) => { @@ -1496,9 +1513,6 @@ where "Stored announcement from {announcer} to be broadcast in {} (t={timestamp})", (self.last_gossip + GOSSIP_INTERVAL) - self.clock ); - // Keep track of who relayed this version of the message for later. - self.relayed_by.insert(id, vec![*relayer]); - // Decide whether or not to relay this message, if it's fresh. // To avoid spamming peers on startup with historical gossip messages, // don't relay messages that are too old. We make an exception for node announcements, @@ -1508,7 +1522,6 @@ where relay.then_some(id) } Ok(None) => { - // FIXME: Still mark as relayed by this peer. // FIXME: Refs announcements should not be delayed, since they are only sent // to subscribers. debug!(target: "service", "Ignoring stale announcement from {announcer} (t={timestamp})"); @@ -1823,7 +1836,7 @@ where return Ok(()); } } else { - self.relay(id, ann); + self.relay(ann); } } } @@ -2326,9 +2339,13 @@ where self.last_timestamp } - fn relay(&mut self, id: gossip::AnnouncementId, ann: Announcement) { + fn relay(&mut self, ann: Announcement) { let announcer = ann.node; - let relayed_by = self.relayed_by.get(&id); + let relayed_by = self + .relayed_by + .get(&AnnouncementSubject::from(&ann)) + .filter(|entry| entry.timestamp == ann.timestamp()) + .map(|entry| &entry.relayers); let rid = if let AnnouncementMessage::Refs(RefsAnnouncement { rid, .. }) = ann.message { Some(rid) } else { @@ -2376,13 +2393,13 @@ where let rows = self.database_mut().gossip_mut().relays(now)?; let local = *self.nid(); - for (id, msg) in rows { + for (_, msg) in rows { let announcer = msg.node; if announcer == local { // Don't relay our own stored gossip messages. continue; } - self.relay(id, msg); + self.relay(msg); } Ok(()) } @@ -2826,6 +2843,31 @@ pub struct Lookup { pub remote: Vec, } +/// The latest version of an announcement and the peers that relayed it. +#[derive(Debug)] +struct AnnouncementRelayers { + timestamp: Timestamp, + relayers: Vec, +} + +/// The subject of an announcement, regardless of its version. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +enum AnnouncementSubject { + Node(NodeId), + Inventory(NodeId), + Refs(NodeId, RepoId), +} + +impl From<&Announcement> for AnnouncementSubject { + fn from(ann: &Announcement) -> Self { + match &ann.message { + AnnouncementMessage::Node(_) => Self::Node(ann.node), + AnnouncementMessage::Inventory(_) => Self::Inventory(ann.node), + AnnouncementMessage::Refs(refs) => Self::Refs(ann.node, refs.rid), + } + } +} + #[derive(thiserror::Error, Debug)] pub enum LookupError { #[error(transparent)]