From 59e889254b1fda2dcc8e3e6d596635d1b77f589f Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 17:44:03 +0000 Subject: transport: wire versions spoken side by side A build speaks wire versions OLDEST to VERSION and writes each node the newest both speak (wire::choose), as the node's SPEAKS says; the oldest until it says. The identity layer keeps that per address (Book::speaks): a SPEAKS from an address sets it for the address, and for every address of the node proven there, and a new address of a node takes it over. Paths encodes each message for an address in its version (Transport::encode_in; Transport::encode is the newest). So that two nodes know what the other speaks once they have proven themselves to each other, before any other message goes between them, a SPEAKS goes ahead of every WHO and ME, and WHO and ME are read in every version from 52 on (wire::PROOFS): their tags and layout never change. Without that, a node whose oldest version is past the other's couldn't read its proofs, and the two would never get to the handshake that says what each speaks. A SPEAKS naming no version this build speaks is logged as a message of another version is, naming the node to upgrade, the versions as ranges. A 0.0.1 node takes a SPEAKS from an address not yet proven for any other message from there: it drops it, and asks WHO. This build speaks 52 alone, so nothing it sends changes but the SPEAKS ahead of the proofs. A release adding a version encodes and decodes the older ones from their own, frozen, types. --- src/transport/identity.rs | 154 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 147 insertions(+), 7 deletions(-) (limited to 'src/transport/identity.rs') diff --git a/src/transport/identity.rs b/src/transport/identity.rs index 5d3e2f5..29624db 100644 --- a/src/transport/identity.rs +++ b/src/transport/identity.rs @@ -26,6 +26,12 @@ //! //! Addresses and the transports behind them are the paths layer's //! (`transport::paths`): the books keep addresses as paths keep them. +//! +//! Each address is written the newest wire version its node and this build both speak +//! (`transport::wire`), as a SPEAKS from there, or from another address of its node, +//! said; the oldest this build speaks until one does. A SPEAKS goes ahead of every WHO +//! and ME, and WHO and ME read in every version, so two nodes know what the other speaks +//! once they proved themselves to each other, before anything else goes between them. use std::collections::{HashMap, HashSet}; use std::sync::{Arc, Mutex, MutexGuard}; @@ -40,12 +46,12 @@ use tracing::{debug, info}; use crate::identity::{self, NodeKey}; use crate::locks::MutexExt; use crate::transport::firewall::Firewall; -use crate::transport::paths::{Gate, PathHandler, Paths, scheme_of}; +use crate::transport::paths::{Gate, PathHandler, Paths, Versions, scheme_of}; use crate::transport::stats::{IdentityCounts, IdentityStats, TransportStats}; use crate::transport::traffic::Traffic; use crate::transport::{ Connection, Destination, EncodedMessage, HandlerFn, IdentityProof, MessageHandler, MessageKind, - ParsedMessage, Transport, + ParsedMessage, Transport, wire, }; pub(crate) const WHO_LABEL: &[u8] = b"continuum/who/1"; @@ -116,6 +122,8 @@ struct Shared { /// addresses they proved themselves at, their messages may be large /// (`transport::Admission`). admitted: Mutex>, + /// Addresses whose SPEAKS named no wire version we speak, logged once each. + other_versions: wire::OtherVersions, } /// Who is where, by address. @@ -132,6 +140,9 @@ struct Book { asked: HashMap, /// Seeds being resolved: who waits for a ME from where. waiting: HashMap>>, + /// The wire version to write at an address, where a SPEAKS from there or from its + /// node said what it speaks (`Book::speaks`). + spoken: HashMap, /// Since start (`stats`). counts: IdentityCounts, } @@ -148,11 +159,18 @@ impl Book { if *slot != address { let previous = std::mem::replace(slot, address.to_owned()); self.at.remove(&previous); + self.spoken.remove(&previous); self.counts.address_changes += 1; } } else { addresses.push(address.to_owned()); } + // What the node speaks at its other addresses, it speaks here: one build. + if !self.spoken.contains_key(address) + && let Some(version) = addresses.iter().find_map(|at| self.spoken.get(at).copied()) + { + self.spoken.insert(address.to_owned(), version); + } if let Some((previous, _)) = self.at.insert(address.to_owned(), (node.to_owned(), now)) && previous != node { @@ -166,6 +184,7 @@ impl Book { .map(|(address, _)| address.clone()) && let Some((node, _)) = self.at.remove(&oldest) { + self.spoken.remove(&oldest); self.forget_proof(&node, &oldest); } for waiter in self.waiting.remove(address).into_iter().flatten() { @@ -174,6 +193,39 @@ impl Book { news } + /// A SPEAKS from `address` said its build speaks wire versions `oldest` to `newest`: + /// it, and every address its node proved itself at, are written the newest both + /// speak (`wire::choose`), or the oldest this build speaks where there is none (and + /// the node says it can't read us). Before a proof, for `address` alone: a SPEAKS + /// goes ahead of each WHO and ME. Bounded as the books are: past `MAX_ENTRIES`, + /// addresses no node proved itself at are forgotten first. + fn speaks(&mut self, address: &str, oldest: u8, newest: u8) { + let version = wire::choose(oldest, newest); + let addresses = match self.at.get(address) { + Some((node, _)) => self.proven.get(node).cloned().unwrap_or_default(), + None => vec![address.to_owned()], + }; + for address in addresses { + let Some(version) = version else { + self.spoken.remove(&address); + continue; + }; + if self.spoken.len() >= MAX_ENTRIES && !self.spoken.contains_key(&address) { + let at = &self.at; + self.spoken.retain(|address, _| at.contains_key(address)); + if self.spoken.len() >= MAX_ENTRIES { + continue; + } + } + self.spoken.insert(address, version); + } + } + + /// The wire version to write at `address` (`speaks`). + fn version_at(&self, address: &str) -> u8 { + self.spoken.get(address).copied().unwrap_or(wire::OLDEST) + } + /// The node proven at `address`, which was heard from there `now`. fn heard(&mut self, address: &str, now: Instant) -> Option { self.at.get_mut(address).map(|(node, heard)| { @@ -293,6 +345,7 @@ impl IdentityTransport { let node_id = config.key.id(); let shared = Arc::new_cyclic(|shared: &std::sync::Weak| { let gate: std::sync::Weak = shared.clone(); + let versions: std::sync::Weak = shared.clone(); let admitting = shared.clone(); let admission = crate::transport::Admission::new(move |address| { admitting @@ -310,6 +363,7 @@ impl IdentityTransport { .collect(), Some(Arc::clone(&traffic)), firewall.is_some().then_some(gate), + Some(versions), clock, keepalive, )), @@ -319,6 +373,7 @@ impl IdentityTransport { traffic, book: Mutex::default(), admitted: Mutex::default(), + other_versions: wire::OtherVersions::default(), } }); // The node's messages are noted here, with the address each went to or came @@ -340,6 +395,13 @@ impl Gate for Shared { } } +/// Each address is written what its node speaks (`Book::speaks`). +impl Versions for Shared { + fn version_at(&self, address: &str) -> u8 { + self.lock().version_at(address) + } +} + impl Shared { /// Whether the node proven at `address` is admitted (`Transport::admit`). fn admits(&self, address: &str) -> bool { @@ -442,7 +504,9 @@ impl Shared { tokio::spawn(async move { shared.send_proof(MessageKind::Who, &address).await }); } - /// Sends a WHO or ME (`kind`) to `address`. + /// Sends a WHO or ME (`kind`) to `address`, a SPEAKS ahead of it: so the node there + /// knows what this build speaks once it knows who we are, and the first messages it + /// writes us are in a version we read. async fn send_proof(&self, kind: MessageKind, address: &str) { let Some(address) = self.paths.canonical(address) else { return; @@ -456,10 +520,21 @@ impl Shared { let Some(message) = message else { return; }; - let encoded = match self.paths.encode(address, &message) { + self.send_to( + address, + &ParsedMessage::Speaks(crate::swarm::speaking::own()), + ) + .await; + self.send_to(address, &message).await; + } + + /// Sends `message`, which ends at the identity layer, to `address`, counted under the + /// address (`traffic`). + async fn send_to(&self, address: &str, message: &ParsedMessage) { + let encoded = match self.paths.encode(address, message) { Ok(encoded) => encoded, Err(error) => { - debug!(to = %address, error = %format!("{error:#}"), "Couldn't encode an identity proof"); + debug!(to = %address, kind = ?message.kind(), error = %format!("{error:#}"), "Couldn't encode a message of the identity layer"); return; } }; @@ -470,7 +545,7 @@ impl Shared { Err(error) => { self.traffic .refused(address, encoded.kind, encoded.payload_len); - debug!(to = %address, error = %format!("{error:#}"), "Couldn't send an identity proof"); + debug!(to = %address, kind = ?message.kind(), error = %format!("{error:#}"), "Couldn't send a message of the identity layer"); } } } @@ -499,6 +574,29 @@ impl Shared { self.prove(&address, ME_LABEL, &proof); Ok(()) } + // What the address's build speaks, noted first (`Book::speaks`), and logged + // where we share no version; from a node proven there, it goes on up too + // (`swarm::speaking`). From an address no node proved itself at, it ends here: + // the WHO or ME it goes ahead of follows. + ParsedMessage::Speaks(speaks) => { + self.other_versions + .speaks(&address, speaks.oldest, speaks.newest); + let node = { + let mut book = self.lock(); + book.speaks(&address, speaks.oldest, speaks.newest); + book.heard(&address, Instant::now()) + }; + let Some(node) = node else { + self.traffic + .received(&address, MessageKind::Speaks, payload_len); + return Ok(()); + }; + let message = ParsedMessage::Speaks(speaks); + self.traffic + .log + .note("received", &node, Some(&address), &message, payload_len); + handler(node, message, payload_len).await + } message => { // Heard from: a node that talks isn't the one evicted when strangers' // proofs fill the book (`Book::bind`). @@ -774,7 +872,13 @@ impl Transport for IdentityTransport { /// For the address `destination` picks (a send there is refused until `peer` /// proved itself there, `send_encoded`). Only a look at the choice: the send makes /// it (encoding again for another path if it changes). - fn encode(&self, peer: &str, message: &ParsedMessage) -> Result { + /// In the wire version the address speaks, whatever `_version` asks (`Paths::encode`). + fn encode_in( + &self, + peer: &str, + message: &ParsedMessage, + _version: u8, + ) -> Result { let Some(address) = self.shared.destination(peer, false) else { bail!("no address known for {peer} on this node's transports"); }; @@ -855,6 +959,42 @@ mod tests { assert_eq!(book.at.len(), MAX_ENTRIES); } + /// An address is written what a SPEAKS from there said, and so is every address of + /// the node proven there; the oldest version this build speaks until one says. + #[test] + fn each_address_is_written_what_its_node_speaks() { + let now = Instant::now(); + let (oldest, newest) = (wire::OLDEST, wire::VERSION); + let mut book = Book::default(); + assert_eq!(book.version_at("s:x"), oldest); + // Before a proof, for the address alone. + book.speaks("s:x", oldest, newest + 3); + assert_eq!(book.version_at("s:x"), newest); + book.bind("s:x", "n1", now); + // Another address of the node is written the same: one build. + book.bind("t:x", "n1", now); + assert_eq!(book.version_at("t:x"), newest); + // Moved: the old address is forgotten, the new one written the same. + book.bind("t:y", "n1", now); + assert!(!book.spoken.contains_key("t:x")); + assert_eq!(book.version_at("t:y"), newest); + // Speaking none of this build's versions: the oldest, at each of its addresses. + book.speaks("s:x", newest + 1, newest + 2); + assert!(book.spoken.is_empty()); + assert_eq!(book.version_at("t:y"), oldest); + + // Full: addresses no node proved itself at go first. + let mut book = Book::default(); + book.bind("s:member", "member", now); + for i in 0..MAX_ENTRIES { + book.speaks(&format!("s:{i}"), oldest, newest); + } + assert_eq!(book.spoken.len(), MAX_ENTRIES); + book.speaks("s:member", oldest, newest); + assert_eq!(book.spoken.len(), 1); + assert_eq!(book.version_at("s:member"), newest); + } + #[test] fn one_address_per_node_and_scheme_and_one_node_per_address() { let now = Instant::now(); -- cgit v1.3.1