From 93defc68f9b99da921f4746e91e5cee9e6e3f1b9 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 17:39:23 +0000 Subject: swarm: a node says to each peer the kind that peer holds, of those it holds A node holds one kind of document, or, to come, its store in both (Holding::kinds, of_both). The kind a peer's handshakes say is kept with the peer, where the node holds it too, and is what the node says to it in turn; to a peer that said none yet, a node of more than one kind says each in turn, HELLO by HELLO, until one is answered. A node of one kind says and takes that one, as before. --- src/swarm/peer/handshake.rs | 18 ++++++++++++++++-- src/swarm/peer/inbound.rs | 22 ++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) (limited to 'src/swarm') diff --git a/src/swarm/peer/handshake.rs b/src/swarm/peer/handshake.rs index fc1bc86..fc0a31b 100644 --- a/src/swarm/peer/handshake.rs +++ b/src/swarm/peer/handshake.rs @@ -144,7 +144,7 @@ async fn transmit_hello(state: &AppState, peer: &str) -> Result<()> { .await .map(|root| root.0.to_vec()), party: party(state), - kind: state.kind().to_owned(), + kind: kind_to(state, peer).await.to_owned(), }), ) .await; @@ -180,6 +180,20 @@ async fn say_what_we_speak(state: &AppState, peer: &str) { } } +/// The kind of document this node says to `peer` it holds: the one the peer's +/// handshakes said, of those this node holds; where the peer said none yet, and this +/// node holds more than one, each in turn, HELLO by HELLO, until one is answered. +async fn kind_to(state: &AppState, peer: &str) -> &'static str { + let kinds = state.kinds(); + let Some(peer_state) = state.get_peer_state(peer).await else { + return kinds[0]; + }; + let peer_state = peer_state.lock().await; + peer_state + .kind + .unwrap_or(kinds[peer_state.hello_attempts as usize % kinds.len()]) +} + 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"); @@ -255,7 +269,7 @@ pub(super) async fn send_hello_reply( .await .map(|root| root.0.to_vec()), party: party(state), - kind: state.kind().to_owned(), + kind: kind_to(state, peer).await.to_owned(), }; debug!(to = %peer, ready, "Sending HELLO_REPLY"); diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index 0354356..35d67c8 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -114,12 +114,22 @@ pub(crate) async fn handle_message( ParsedMessage::HelloReply(reply) => Some(&reply.kind), _ => None, }; - if let Some(theirs) = kind.filter(|kind| *kind != state.kind()) { - anyhow::bail!( - "{peer} holds a document of another kind ({theirs}; this node's is {}): they sync \ - nothing; a release that knows the kind does", - state.kind() - ); + if let Some(theirs) = kind { + let Some(held) = state.kinds().iter().find(|held| *held == theirs) else { + anyhow::bail!( + "{peer} holds a document of another kind ({theirs}; this node's is {}): they \ + sync nothing; a release that knows the kind does", + state.kinds().join(" and ") + ); + }; + // Of the kinds this node holds, the one the peer holds: what it is told in + // turn, and passed on. + state + .get_or_create_peer_state(&peer) + .await + .lock() + .await + .kind = Some(held); } // The root its document starts from: another than ours, of our epoch, is another -- cgit v1.3.1