From 7a5aeb02024ddff71ba60f40913555cd21a633ad Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 13:33:51 +0000 Subject: transport: SPEAKS, which wire versions a node speaks and what it knows Before each HELLO and each HELLO_REPLY, a node sends SPEAKS: the oldest and the newest wire version it speaks, and the ID of the newest revision of the federation it knows. It stands outside the wire versions, its first byte 0, which no version is, laid out as no build changes: that byte, the oldest, the newest, then 32 bytes of the revision's ID where there is one; a reader takes those and leaves whatever follows, which a later build may add. So a peer on another build reads it too, and nodes on different builds can say what they run, which a rollout needs. A build from before it takes it for a message of another wire version and logs that once of the peer (version=0), reading the rest of the peer's messages as before: SPEAKS rolls out node by node, with no change of VERSION. The node keeps what each peer said last (`swarm::speaking`, at most 4096 peers). It counts as `hello` traffic. `OLDEST`, the oldest version this build reads, is `VERSION` for now: a range for a later build to widen. operations.md also says what is so of the other-version warning now: every transport logs it once per peer at warn, not Mycelium alone. --- src/swarm/speaking.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/swarm/speaking.rs (limited to 'src/swarm/speaking.rs') diff --git a/src/swarm/speaking.rs b/src/swarm/speaking.rs new file mode 100644 index 0000000..dcbfac4 --- /dev/null +++ b/src/swarm/speaking.rs @@ -0,0 +1,46 @@ +//! What the peers' builds speak and know, as each says with its handshakes (SPEAKS, +//! `transport::Speaks`): the wire versions it speaks and the newest revision of the +//! federation it knows. A peer that sends none runs a build from before SPEAKS, or isn't +//! heard from yet. + +use std::collections::BTreeMap; + +use crate::locks::MutexExt; +use crate::transport::{Speaks, wire}; + +/// How many peers' SPEAKS are kept: anyone can send one. +const MOST: usize = 4096; + +/// What this build speaks and knows. +pub(crate) fn own() -> Speaks { + Speaks { + oldest: wire::OLDEST, + newest: wire::VERSION, + revision: newest_revision(), + } +} + +#[cfg(feature = "federation")] +fn newest_revision() -> Option<[u8; 32]> { + Some(crate::federation::revision::Revision::newest().id().0) +} + +#[cfg(not(feature = "federation"))] +fn newest_revision() -> Option<[u8; 32]> { + None +} + +/// The SPEAKS each peer sent last, by node ID. +#[derive(Default)] +pub(crate) struct Speaking(std::sync::Mutex>); + +impl Speaking { + /// `peer` said `speaks`. + pub(crate) fn heard(&self, peer: &str, speaks: Speaks) { + let mut heard = self.0.lock_or_recover(); + if heard.len() >= MOST && !heard.contains_key(peer) { + heard.clear(); + } + heard.insert(peer.to_owned(), speaks); + } +} -- cgit v1.3.1