diff options
Diffstat (limited to 'src/swarm')
| -rw-r--r-- | src/swarm/auth.rs | 3 | ||||
| -rw-r--r-- | src/swarm/mod.rs | 1 | ||||
| -rw-r--r-- | src/swarm/peer/handshake.rs | 11 | ||||
| -rw-r--r-- | src/swarm/peer/inbound.rs | 8 | ||||
| -rw-r--r-- | src/swarm/speaking.rs | 123 |
5 files changed, 144 insertions, 2 deletions
diff --git a/src/swarm/auth.rs b/src/swarm/auth.rs index 98a004c..7ac813b 100644 --- a/src/swarm/auth.rs +++ b/src/swarm/auth.rs @@ -166,7 +166,8 @@ mod tests { /// A swarm key file is made only when asked, then read as any other. #[test] fn swarm_key_created_only_when_asked() { - let path = crate::files::scratch_dir("swarm-key-create").join("swarm.key"); + let dir = crate::files::scratch_dir("swarm-key-create"); + let path = dir.join("swarm.key"); assert!(SwarmKey::from_file(&path, false).is_err()); assert!(!path.exists()); let created = SwarmKey::from_file(&path, true).unwrap(); diff --git a/src/swarm/mod.rs b/src/swarm/mod.rs index ad7f83e..6c0a242 100644 --- a/src/swarm/mod.rs +++ b/src/swarm/mod.rs @@ -21,6 +21,7 @@ pub(crate) mod roots; pub(crate) mod rtt; pub(crate) mod skew; pub(crate) mod source; +pub(crate) mod speaking; pub(crate) mod stability; pub(crate) mod state_file; pub(crate) mod stats; diff --git a/src/swarm/peer/handshake.rs b/src/swarm/peer/handshake.rs index 4cc0cf4..ef7d379 100644 --- a/src/swarm/peer/handshake.rs +++ b/src/swarm/peer/handshake.rs @@ -127,6 +127,7 @@ async fn transmit_hello(state: &AppState, peer: &str) -> Result<()> { None => 0, }; state.lock_rtt().hello_sent(peer, Instant::now()); + say_what_we_speak(state, peer).await; let result = state .transport .send_message( @@ -169,6 +170,15 @@ async fn transmit_hello(state: &AppState, peer: &str) -> Result<()> { } } +/// Tells `peer` what this build speaks and knows, with each handshake (SPEAKS, +/// `swarm::speaking`): a build from before SPEAKS ignores it. +async fn say_what_we_speak(state: &AppState, peer: &str) { + let speaks = ParsedMessage::Speaks(crate::swarm::speaking::own()); + if let Err(error) = state.transport.send_message(peer, &speaks).await { + debug!(peer = %peer, error = %format!("{error:#}"), "SPEAKS not sent"); + } +} + pub(super) async fn ensure_handshake(state: &AppState, peer: &str) { if let Err(error) = send_hello_once(state, peer).await { warn!(peer = %peer, error = %format!("{error:#}"), "HELLO failed; will retry"); @@ -247,6 +257,7 @@ pub(super) async fn send_hello_reply( }; debug!(to = %peer, ready, "Sending HELLO_REPLY"); + say_what_we_speak(state, peer).await; state .transport diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index d266e5f..e23ec14 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -39,6 +39,11 @@ pub(crate) async fn handle_message( note_alive(state, &peer, &message).await; return Ok(()); } + // SPEAKS only says what the peer's build speaks and knows (`swarm::speaking`). + if let ParsedMessage::Speaks(speaks) = message { + state.speaking.heard(&peer, speaks); + return Ok(()); + } // WHO and ME end at the identity layer (`transport::identity`). A sync message names // its epoch by a tag (`EpochTag`): ours, or the sender's as we know it; one naming // neither is of an epoch we don't sync on, and says nothing we could record. @@ -317,7 +322,8 @@ pub(crate) async fn handle_message( transport::ParsedMessage::Who(_) | transport::ParsedMessage::Me(_) | transport::ParsedMessage::Ping(_) - | transport::ParsedMessage::Pong { .. } => {} + | transport::ParsedMessage::Pong { .. } + | transport::ParsedMessage::Speaks(_) => {} } Ok(()) diff --git a/src/swarm/speaking.rs b/src/swarm/speaking.rs new file mode 100644 index 0000000..4626fb1 --- /dev/null +++ b/src/swarm/speaking.rs @@ -0,0 +1,123 @@ +//! 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 +} + +/// `speaks` as the API shows it (`knows` in `GET /peers` and `GET /events/counts`): the +/// wire versions, the oldest and the newest, and the newest revision of the federation, +/// by number and ID and as people read it, its number null where this continuum doesn't +/// know it; null where there is none. +pub(crate) fn shown(speaks: Speaks) -> serde_json::Value { + serde_json::json!({ + "wire": [speaks.oldest, speaks.newest], + "revision": speaks.revision.map(revision_shown), + }) +} + +#[cfg(feature = "federation")] +fn revision_shown(id: [u8; 32]) -> serde_json::Value { + use crate::federation::revision::Revision; + let id = crate::federation::store::Id(id); + match Revision::of(&id) { + Some(revision) => serde_json::json!({ + "number": revision.0, + "id": id.to_string(), + "named": revision.to_string(), + }), + None => unknown(&id.to_string()), + } +} + +#[cfg(not(feature = "federation"))] +fn revision_shown(id: [u8; 32]) -> serde_json::Value { + unknown(&crate::transport::Hex(&id).to_string()) +} + +fn unknown(id: &str) -> serde_json::Value { + serde_json::json!({ + "number": null, + "id": id, + "named": format!("{id}, a revision this continuum doesn't know"), + }) +} + +/// The SPEAKS each peer sent last, by node ID. +#[derive(Default)] +pub(crate) struct Speaking(std::sync::Mutex<BTreeMap<String, Speaks>>); + +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); + } + + /// What `peer` said last, if it said anything. + pub(crate) fn of(&self, peer: &str) -> Option<Speaks> { + self.0.lock_or_recover().get(peer).copied() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_peer_is_heard_as_it_spoke_last() { + let speaking = Speaking::default(); + assert_eq!(speaking.of("a"), None); + speaking.heard("a", own()); + let older = Speaks { + oldest: 1, + newest: 1, + revision: None, + }; + speaking.heard("b", older); + speaking.heard("a", older); + assert_eq!(speaking.of("a"), Some(older)); + assert_eq!(own().newest, wire::VERSION); + let unknown = shown(Speaks { + revision: Some([7; 32]), + ..older + }); + assert_eq!(unknown["wire"], serde_json::json!([1, 1])); + assert_eq!(unknown["revision"]["number"], serde_json::Value::Null); + assert_eq!(unknown["revision"]["id"], "07".repeat(32)); + assert_eq!(shown(older)["revision"], serde_json::Value::Null); + #[cfg(feature = "federation")] + assert_eq!( + shown(own())["revision"]["number"], + crate::federation::revision::FEATURES.len() + ); + } +} |
