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. --- docs/document.md | 15 ++++++- docs/operations.md | 4 +- src/simtest/holding.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++ src/state/peer.rs | 6 +++ src/swarm/peer/inbound.rs | 15 ++++--- src/swarm/peer/keepalive.rs | 3 ++ src/swarm/set.rs | 50 +++++++++++++++++++++- src/swarm/sync.rs | 2 +- 8 files changed, 185 insertions(+), 12 deletions(-) diff --git a/docs/document.md b/docs/document.md index 641fa92..2a8b015 100644 --- a/docs/document.md +++ b/docs/document.md @@ -787,8 +787,19 @@ alone and nodes on a set alone take none of each other's handshakes: what goes b them goes through the nodes of both ([`operations.md`](operations.md), [Moving to sets of events](operations.md#moving-to-sets-of-events)). -Not built: that no swarm holding a store is started on Automerge any more. One that -runs there runs on, until it has moved. +A member's entry is in the document or in the set its node holds, and a node of both +writes its own in both; no node writes another's. So a node on the document alone reads +the entries of the nodes on the document and of both, a node on a set alone those of +the nodes on sets and of both, and each has its neighbours among those; a node of both +reads them all. Two nodes of both kinds say an Automerge document in their handshakes, +and sync it; beside each STATUS, a node of both says where its set stands to every peer +that said so (`swarm::set::say_beside`). A node of both that hears it knows the peer +holds a set too, and passes its own on to it from then on; a node on the document alone +drops it. So the entries that sets alone hold cross from one node of both to the next, +and every node on a set comes to read every other's. + +A swarm holding a store may still be started on Automerge, so that the move can be +tried: one that runs there runs on, until it has moved. ## What others do diff --git a/docs/operations.md b/docs/operations.md index b12af28..a643c41 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -179,7 +179,9 @@ of both kinds do with either, and carry between them, so move every node through Between the steps, the swarm works as ever: an event put on any node reaches every node. Within a step, it works as long as the nodes of both kinds reach each other and every other node reaches one of them; `GET /events` on each node shows whether its -store holds what the others' hold (`heads`). +store holds what the others' hold (`heads`). A node on the document alone doesn't see +the nodes on sets alone among the members it reaches (`GET /peers`), nor those it: each +has its neighbours among the nodes of its own kind and of both, until all are of one. A node that joins later is given `--store-id`, `--store-document` and seeds: with nothing of the store, it asks the members it reaches for the root by its name, or, with diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index 5d5cecf..0a40657 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -2270,6 +2270,108 @@ async fn a_node_of_both_kinds_carries_events_between_them() { assert!(e.doc.read().await.heads().is_empty()); } +/// A larger swarm on its way to sets of events, each node keeping two neighbours: a, b +/// and c hold the store in the Automerge document, m, n and o in both, e, f and g in a +/// set. The nodes of the one kind and of the other read different members' entries, and +/// so different neighbours; those of both read them all. An event put on a node of +/// either kind reaches all nine all the same, and so does one put while one of the +/// nodes of both kinds is down. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_larger_swarm_on_its_way_to_sets_passes_events_on_between_the_kinds() { + let sim = Sim::with_identity(100); + let (tv, lass, makefu) = three(); + let (_, root, id) = federation(&[ + (&tv, &["a", "b", "c"]), + (&lass, &["m", "n", "o"]), + (&makefu, &["e", "f", "g"]), + ]); + let start = |name: &'static str, seeds: &'static [&'static str], holding: Holding| { + let root = root.clone(); + sim.start_with( + name, + seeds, + Start::FromDocument(Origin::Root), + move |config| { + config.neighbours_k = 2; + of_the_store(config, id, Some(&root), holding); + }, + ) + }; + let on_a_set = |name: &'static str, seeds: &'static [&'static str]| { + let root = root.clone(); + sim.start_with( + name, + seeds, + Start::FromDocument(Origin::Root), + move |config| { + config.neighbours_k = 2; + config.store = Some(id); + config.holding = holding("makefu").carrying_if( + Some(held::Begun::Set(Box::new(a_set(&root)))), + None, + Arc::new(Files::default()), + ); + }, + ) + }; + let both = || holding("lass").of_both(Duration::from_secs(600)); + let nodes = [ + start("a", &[], holding("tv")), + start("b", &["a"], holding("tv")), + start("c", &["a"], holding("tv")), + start("m", &["a"], both()), + start("n", &["a"], both()), + start("o", &["a"], both()), + on_a_set("e", &["m"]), + on_a_set("f", &["n"]), + on_a_set("g", &["o"]), + ]; + let all: Vec<&AppState> = nodes.iter().collect(); + let all = &all; + sim.wait_ready(all, LONG).await; + sim.wait_until( + "the nodes holding sets hold each other's entries", + LONG, + move || async move { all[3..].iter().all(|node| entries(node) == 6) }, + ) + .await; + + let on_a = tv.write(&[id], "home/tv/x", s("1")); + let on_g = makefu.write(&[id], "home/makefu/y", s("2")); + let ids = [Id::of(&on_a), Id::of(&on_g)]; + put(all[0], &[&on_a]).await; + put(all[8], &[&on_g]).await; + let first = &ids; + sim.wait_until("all nine hold both events", LONG, move || async move { + all.iter().all(|node| holds(node, first)) + }) + .await; + + // One of the nodes of both kinds down: the others carry between the kinds. + sim.net.take_down("m"); + let on_c = tv.write(&[ids[0], ids[1]], "home/tv/z", s("3")); + let on_e = makefu.write(&[ids[0], ids[1]], "home/makefu/w", s("4")); + let later = [Id::of(&on_c), Id::of(&on_e)]; + put(all[2], &[&on_c]).await; + put(all[6], &[&on_e]).await; + let later = &later; + sim.wait_until( + "the eight that are up hold those too", + LONG, + move || async move { + all.iter() + .enumerate() + .all(|(at, node)| at == 3 || holds(node, later)) + }, + ) + .await; + sim.net.bring_up("m"); + sim.wait_until("and m, back, as well", LONG, move || async move { + holds(all[3], later) + }) + .await; +} + /// A swarm moves from Automerge documents to sets of events, node by node, as its /// operators are told to: a and b on the document; then each of both kinds, started /// again on its document with a file for its set; then each on its set alone, started diff --git a/src/state/peer.rs b/src/state/peer.rs index 356736a..768fead 100644 --- a/src/state/peer.rs +++ b/src/state/peer.rs @@ -95,6 +95,10 @@ pub(crate) struct PeerState { /// The kind of document its handshakes say it holds, of those this node holds: what /// this node says to it in turn, and passes on to it (`swarm::peer::inbound`). pub(crate) kind: Option<&'static str>, + /// Whether it holds a set of events beside the Automerge document its handshakes + /// say, a node of both kinds as this one: it sent what a set says (`swarm::set`). + #[cfg(feature = "federation")] + pub(crate) set_beside: bool, pub(crate) hello_state: HelloState, /// Wait between unanswered HELLOs; reset whenever a new handshake starts. pub(crate) hello_backoff: Backoff, @@ -343,6 +347,8 @@ impl PeerState { left: false, hello_attempts: 0, kind: None, + #[cfg(feature = "federation")] + set_beside: false, last_hello_at: None, unreachable_since: None, incidents: Incidents::default(), 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