protocol/service: Record relayers of duplicates

Only the first peer to send us a version of an announcement was
recorded as its relayer, as the `FIXME` in `handle_announcement`
notes. Inventory announcements are relayed with a delay, so peers that
sent the same version in the meantime were sent it back, which kept
`announcement_message_amplification` close to the worst case. Node and
refs announcements are relayed as soon as they arrive, so this does
not affect them.

The gossip store does not report which row a duplicate belongs to, so
key the relayers by announcer, type and repository instead, together
with the version they relayed. Having `Store::announced` report the
row of a duplicate would also work, but would change what its result
means for its other callers. As before, entries are never pruned, but
there is now at most one entry per announcer, type and repository,
rather than one per row ever created.

With this, 1000 runs of `announcement_message_amplification` measured
between 2.525 and 2.975, with a mean of 2.72, down from a mean of 3.98,
against a best case of 2.5 for five fully connected nodes. Require it
to stay below 3.25, which leaves room for the variation between runs.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-27 11:05:19 +01:00
committed by Lorenz Leutgeb
parent 108140ac1a
commit 9e80d2d4b9
3 changed files with 60 additions and 20 deletions
+2
View File
@@ -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
+4 -8
View File
@@ -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");
}
+54 -12
View File
@@ -339,9 +339,9 @@ pub struct Service<D, S> {
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<gossip::AnnouncementId, Vec<NodeId>>,
/// 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<AnnouncementSubject, AnnouncementRelayers>,
/// 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<NodeId>,
}
/// The latest version of an announcement and the peers that relayed it.
#[derive(Debug)]
struct AnnouncementRelayers {
timestamp: Timestamp,
relayers: Vec<NodeId>,
}
/// 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)]