From 22f9ac13bd0089f89cd2dd74932b522ab0c285c0 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 02:08:53 +0000 Subject: federation: nodes of both kinds pass their sets on to each other, too Two nodes of both kinds say an Automerge document in their handshakes, and synced that alone: what only sets hold, the entries of the nodes on sets, stopped at the first node of both it reached, so that in a swarm larger than its neighbour count the nodes on sets never came to read each other's entries. A node of both now says where its set stands beside each STATUS to a peer on a document; one of both that hears it passes its set on in turn, one on the document alone drops it. A simulation of nine nodes, three of each kind, two neighbours each. --- src/swarm/peer/inbound.rs | 15 ++++++++------ src/swarm/peer/keepalive.rs | 3 +++ src/swarm/set.rs | 50 +++++++++++++++++++++++++++++++++++++++++++-- src/swarm/sync.rs | 2 +- 4 files changed, 61 insertions(+), 9 deletions(-) (limited to 'src/swarm') diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index b6c486d..e16eb3f 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -124,12 +124,14 @@ pub(crate) async fn handle_message( }; // 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); + let peer_state = state.get_or_create_peer_state(&peer).await; + let mut peer_state = peer_state.lock().await; + // A handshake of another kind than before: of a node started anew. + #[cfg(feature = "federation")] + if peer_state.kind != Some(held) { + peer_state.set_beside = false; + } + peer_state.kind = Some(held); } // The root its document starts from: another than ours, of our epoch, is another @@ -236,6 +238,7 @@ pub(crate) async fn handle_message( if let transport::SyncBody::Kind(body) = envelope.body && state.holding.has_a_set() { + crate::swarm::set::holds_one_beside(state, &peer).await; crate::swarm::set::received(state, &peer, body).await; } return Ok(()); diff --git a/src/swarm/peer/keepalive.rs b/src/swarm/peer/keepalive.rs index bbcc6e3..5072f01 100644 --- a/src/swarm/peer/keepalive.rs +++ b/src/swarm/peer/keepalive.rs @@ -22,6 +22,9 @@ use crate::transport::{self, ParsedMessage}; /// Sends `peer` a STATUS with our status. pub(crate) async fn send_status(state: &AppState, peer: &str) -> Result<()> { + // A node of both kinds: where its set stands, beside (`swarm::set`). + #[cfg(feature = "federation")] + crate::swarm::set::say_beside(state, peer).await; let status = local_status(state, peer).await; if let Some(peer_state) = state.get_peer_state(peer).await { peer_state.lock().await.last_status_at = Some(Instant::now()); diff --git a/src/swarm/set.rs b/src/swarm/set.rs index 54a48fe..82158be 100644 --- a/src/swarm/set.rs +++ b/src/swarm/set.rs @@ -22,13 +22,59 @@ use crate::state::AppState; use crate::swarm::topology; use crate::transport::{self, HeadsSummary, ParsedMessage, SyncBody}; -/// Whether `peer` holds a set of events, as its handshakes said: what a node of both +/// Whether `peer` holds a set of events and nothing else, as its handshakes said: with +/// such a peer, a node of both kinds syncs no Automerge document. +pub(crate) async fn holds_a_set_alone(state: &AppState, peer: &str) -> bool { + match state.get_peer_state(peer).await { + Some(peer_state) => { + peer_state.lock().await.kind == Some(crate::federation::store::set::KIND) + } + None => false, + } +} + +/// Notes that `peer`, whose handshakes say an Automerge document, holds a set of +/// events beside it: it sent what a set says. +pub(crate) async fn holds_one_beside(state: &AppState, peer: &str) { + if let Some(peer_state) = state.get_peer_state(peer).await { + let mut peer_state = peer_state.lock().await; + if peer_state.kind == Some(crate::document::KIND) { + peer_state.set_beside = true; + } + } +} + +/// On a node of both kinds, says to `peer`, whose handshakes say an Automerge +/// document, where this node's set stands, beside the STATUS that says where its +/// document does: a node of both kinds takes it, answers what its own set lacks or +/// has more of, and passes its set on to this node from then on; a node on the +/// document alone drops it. So what sets alone hold, the members' entries, crosses +/// between nodes of both kinds too. +pub(crate) async fn say_beside(state: &AppState, peer: &str) { + if !state.holding.has_a_set() || state.holding.of_a_set() { + return; + } + let of_a_document = match state.get_peer_state(peer).await { + Some(peer_state) => peer_state.lock().await.kind == Some(crate::document::KIND), + None => false, + }; + if !of_a_document { + return; + } + if let Some(stamp) = state.holding.with_set(passing::stamp) { + send(state, peer, &Message::Stamp(stamp)).await; + } +} + +/// Whether `peer` holds a set of events, as its handshakes said, or, beside an +/// Automerge document, as what it sent did: what a node of both /// kinds passes its set on to. A node that holds a set alone takes handshakes of no /// other. pub(crate) async fn holds_a_set(state: &AppState, peer: &str) -> bool { match state.get_peer_state(peer).await { Some(peer_state) => { - peer_state.lock().await.kind == Some(crate::federation::store::set::KIND) + let peer_state = peer_state.lock().await; + peer_state.kind == Some(crate::federation::store::set::KIND) || peer_state.set_beside } None => false, } diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs index 3939377..95149f9 100644 --- a/src/swarm/sync.rs +++ b/src/swarm/sync.rs @@ -66,7 +66,7 @@ async fn deliver_sync_to_peer(state: &AppState, peer: &str) -> Result<()> { // A set of events is passed on otherwise (`swarm::set`): with a peer that holds // one, and of the Automerge document a node holds beside one alone, no sync. #[cfg(feature = "federation")] - if state.of_a_set() || crate::swarm::set::holds_a_set(state, peer).await { + if state.of_a_set() || crate::swarm::set::holds_a_set_alone(state, peer).await { return Ok(()); } -- cgit v1.3.1