From 8017b0e69f0350c2a35629ae71d511ddf7a3aa9c Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 14:55:03 +0000 Subject: A node of a swarm holding a store, on a set of events (--store-document) With store-root or store-id, store-document names the file of the store's own document, a set of events: the node holds its store there and nothing in the Automerge document, syncs with nodes holding a set and no others, and goes on from the file when started again. Without a file yet it begins from the root (StoreStart::set); joining by the ID alone needs a file. What a checkpoint forgot is kept for store-keeps-forgotten (30 days). The file stands for store-file, and no checkpoint is pinned. The set is written to its file where it changed, every five seconds with the store's worker and as the node stops (Holding::save), what a checkpoint forgot longer ago let go of first. Simulations: a node behind catches up; a node goes on from its file. --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index da106b9..8b56399 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -221,11 +221,29 @@ pub async fn run() -> Result<()> { #[cfg(feature = "federation")] let store = bootstrap::StoreStart::of(args.store_root.as_deref(), args.store_id.as_deref(), &files)?; + // Its store's own document, a set of events (`--store-document`): from its file, + // or from the root. + #[cfg(feature = "federation")] + let set = match (&store, &args.store_document) { + (Some(store), Some(path)) => Some(store.set(path, &files, args.store_keeps_forgotten)?), + _ => None, + }; #[cfg(feature = "federation")] let starting = match &store { - Some(store) => { + Some(store) if set.is_none() => { store.starting(starting, remembered.as_ref().map(|r| r.swarm_id.as_str()))? } + // A set is the store's document: the one a swarm syncs otherwise stays empty. + Some(store) => { + let swarm_id = store.swarm_id(); + if let Some(remembered) = remembered.as_ref().filter(|r| r.swarm_id != swarm_id) { + anyhow::bail!( + "The state file remembers swarm {}, not the store's ({swarm_id}): give the store's state file, or another", + remembered.swarm_id + ); + } + None + } None => starting, }; let ( @@ -372,7 +390,8 @@ pub async fn run() -> Result<()> { args.store_party.clone(), )? .near(args.store_neighbour_parties.clone()) - .voting(voter(args)?), + .voting(voter(args)?) + .carrying_if(set, args.store_document.clone(), Arc::clone(&files)), strict_keys: args .strict_keys .iter() @@ -394,7 +413,13 @@ pub async fn run() -> Result<()> { // Join the remembered swarm and no other; HELLOs claim it. A document to start on // names its swarm itself (checked against the remembered one above). + #[cfg(feature = "federation")] + let of_a_set = state.of_a_set(); + #[cfg(not(feature = "federation"))] + let of_a_set = false; let start = match starting_swarm { + // A set of events is what the node starts on: its store's swarm (`bootstrap`). + _ if of_a_set => bootstrap::Start::FromDocument(bootstrap::Origin::Root), Some((swarm_id, origin)) => { state.set_expected_swarm_id(swarm_id).await?; bootstrap::Start::FromDocument(origin) @@ -486,6 +511,10 @@ pub async fn run() -> Result<()> { swarm::state_file::update(&state, true).await; swarm::document_file::flush(&state).await; swarm::document_file::close(&state).await; + #[cfg(feature = "federation")] + if let Err(error) = state.holding.save() { + tracing::error!(error = %error, "Couldn't write the store's document as the node stops"); + } Ok(()) } -- cgit v1.3.1