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/swarm/bootstrap.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'src/swarm') diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs index 24da958..9d29c5b 100644 --- a/src/swarm/bootstrap.rs +++ b/src/swarm/bootstrap.rs @@ -322,6 +322,53 @@ impl StoreStart { })) } + /// The store's own document, a set of events (`--store-document`), kept in the file + /// at `path`: as the file has it, where there is one, which must be this store's; + /// else begun from the root, which the node must be given then. What a checkpoint + /// forgot is kept for `keeps`. + pub(crate) fn set( + &self, + path: &Path, + files: &Files, + keeps: std::time::Duration, + ) -> Result> { + use crate::federation::fold::Federation; + use crate::federation::store::{memory::Store, set::Set}; + let now = std::time::Instant::now(); + let held = files + .read(FileKind::Store, path) + .with_context(|| format!("Reading the store's document {}", path.display()))?; + if let Some(bytes) = held { + let set = Set::::load(&bytes, keeps, now).map_err(|refused| { + anyhow::anyhow!( + "The store's document {} doesn't read ({}): move it away only knowing the node then starts from the root, and may take what it refused", + path.display(), + refused.0 + ) + })?; + if set.store().id() != self.id { + bail!( + "The store's document {} is of store {}, not {}", + path.display(), + set.store().id(), + self.id + ); + } + info!(swarm_id = %self.id, path = %path.display(), "Going on with the store's document"); + return Ok(set); + } + let Some((root, kept)) = &self.root else { + bail!( + "store-document {} doesn't exist, and store-id names no root to begin it from: give the store's root (store-root)", + path.display() + ); + }; + let store = Store::::new(root.clone(), kept.clone()) + .map_err(|refused| anyhow::anyhow!("The store's root is none: {}", refused.0))?; + info!(swarm_id = %self.id, "Starting the swarm holding the store from its root, as a set of events (--store-document)"); + Ok(Set::new(store, keeps)) + } + /// The swarm's ID: the root's, in hex. pub(crate) fn swarm_id(&self) -> String { self.id.to_string() @@ -651,6 +698,50 @@ mod tests { assert!(joining.starting(None, None).unwrap().is_none()); } + #[cfg(feature = "federation")] + #[test] + fn a_stores_document_is_begun_from_the_root_or_gone_on_with() { + use crate::federation::harness::{self, Party, Spec}; + use crate::federation::store::memory::Offered; + use std::time::{Duration, Instant}; + + let tv = Party::new("tv", 1); + let root = Spec::first(&[&tv], &[]).root(); + let id = Id::of(&root); + let store = StoreStart { + id, + root: Some((root.clone(), Vec::new())), + }; + let joining = StoreStart { id, root: None }; + let dir = crate::files::scratch_dir("store-document"); + let path = dir.join("store.events"); + let files = Files::default(); + let keeps = Duration::from_secs(60); + // No file: from the root, which joining by the ID alone doesn't give. + assert!(joining.set(&path, &files, keeps).is_err()); + let mut set = store.set(&path, &files, keeps).unwrap(); + let write = tv.write(&[id], "home/tv/a", harness::str("1")); + assert!(matches!( + set.offer(write.clone(), Instant::now()), + Offered::Taken(_) + )); + std::fs::write(&path, set.save(Instant::now())).unwrap(); + // A file: gone on with, the root given or not. + for start in [&store, &joining] { + let read = start.set(&path, &files, keeps).unwrap(); + assert_eq!(read.store().taken(), &[Id::of(&write)]); + } + // Of another store, or no document: refused. + let other_root = Spec::first(&[&Party::new("lass", 2)], &[]).root(); + let other = StoreStart { + id: Id::of(&other_root), + root: Some((other_root, Vec::new())), + }; + assert!(other.set(&path, &files, keeps).is_err()); + std::fs::write(&path, b"something else").unwrap(); + assert!(store.set(&path, &files, keeps).is_err()); + } + /// A fork's root, and the event it keeps on a line of its own: the swarm starts from /// the changes founding it. Without that event: no store's root. #[test] -- cgit v1.3.1