aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport/identity.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/identity.rs')
-rw-r--r--src/transport/identity.rs154
1 files changed, 147 insertions, 7 deletions
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<HashSet<String>>,
+ /// 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<String, Instant>,
/// Seeds being resolved: who waits for a ME from where.
waiting: HashMap<String, Vec<oneshot::Sender<String>>>,
+ /// 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<String, u8>,
/// 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<String> {
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<Shared>| {
let gate: std::sync::Weak<dyn Gate> = shared.clone();
+ let versions: std::sync::Weak<dyn Versions> = 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<EncodedMessage> {
+ /// In the wire version the address speaks, whatever `_version` asks (`Paths::encode`).
+ fn encode_in(
+ &self,
+ peer: &str,
+ message: &ParsedMessage,
+ _version: u8,
+ ) -> Result<EncodedMessage> {
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();