aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 04:16:59 +0000
committertv <tv@krebsco.de>2026-11-17 05:33:18 +0100
commit5439b2e6b27a55a1da252cf745d0557686e9c815 (patch)
tree2b26c192e22e1b130a2ca7674fc1713e94fa59f7 /src/federation/held.rs
parent44a61c46160843d917a1da24bc8e364c5109960d (diff)
remake: holding a store, a node follows an epoch only from a checkpoint it builds
A node of a swarm holding a store rejoining an epoch (`docs/store.md`, Remakes) follows it only where its snapshot has the swarm's policy and store, and holds the node's document's checkpoint, one it builds from that document, or one its operator pinned; with an empty document, the root of its store too (`Holding::follows`). A made-up epoch holding another, the root after the store moved on, say, would leave it holding events whose history nobody puts again: it stays on its own, as when a rejoin's check fails, and tries again later.
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 9ea26ee..b7625e0 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -467,6 +467,77 @@ impl Holding {
lacking
}
+ /// Whether the node follows, rejoining from `doc` (empty for one joining), an epoch
+ /// whose snapshot is `snapshot` (its first change alone), in a swarm holding the store
+ /// `store` (`docs/store.md`, Remakes): only one with its swarm's policy and store, and
+ /// only where the snapshot's checkpoint is its document's, one it builds from that
+ /// document, or one its operator pinned; with an empty document, the root its operator
+ /// names too. Else a made-up epoch could leave it holding events whose history nobody
+ /// puts again. Any epoch, in a swarm holding none.
+ pub(crate) fn follows(
+ &self,
+ snapshot: &impl ReadDoc,
+ doc: &impl ReadDoc,
+ store: Option<Id>,
+ ) -> Result<(), String> {
+ use crate::access::policy::{self, Policy};
+ let ours = match policy::of(doc) {
+ Policy::Store(fold) => Policy::Store(fold),
+ _ if store.is_some() => Policy::Store(Federation::NAME),
+ _ => return Ok(()),
+ };
+ if policy::of(snapshot) != ours {
+ return Err("its snapshot's policy isn't this swarm's: not followed".to_owned());
+ }
+ let swarm = events::swarm(snapshot).ok_or("its snapshot names no store")?;
+ if store.or_else(|| events::swarm(doc)) != Some(swarm) {
+ return Err(format!(
+ "its snapshot names another store, {swarm}: not followed"
+ ));
+ }
+ let root = events::bytes(snapshot, &swarm).ok_or("its snapshot holds no root")?;
+ let checkpoint = events::checkpoint(snapshot).unwrap_or(root);
+ let id = Id::of(&checkpoint);
+ let empty = events::swarm(doc).is_none();
+ let its_own = events::checkpoint(doc)
+ .or_else(|| events::bytes(doc, &swarm))
+ .is_some_and(|own| Id::of(&own) == id);
+ if its_own || self.pin == Some(id) || (empty && id == swarm) {
+ return Ok(());
+ }
+ // One it builds from its document.
+ let reading = Holding {
+ pin: self.pin,
+ inner: Mutex::new(Inner {
+ kept: self.inner.lock_or_recover().kept.clone(),
+ ..Inner::default()
+ }),
+ ..Holding::default()
+ };
+ if let Some(unread) = reading.unread(doc, 0) {
+ reading.read(unread);
+ }
+ // A root, at no heads, is its document's or named.
+ let heads = Federation::parts(&checkpoint)
+ .map(|(heads, _)| heads)
+ .filter(|heads| !heads.is_empty());
+ let builds = reading
+ .with(|store| {
+ Id::of(store.checkpoint()) == id
+ || heads
+ .and_then(|heads| store.checkpoint_at(&heads))
+ .is_some_and(|(built, _)| built == checkpoint)
+ })
+ .unwrap_or(false);
+ if builds {
+ Ok(())
+ } else {
+ Err(format!(
+ "its snapshot holds the checkpoint {id}, which this node neither builds from its document nor has pinned: not followed"
+ ))
+ }
+ }
+
/// What `left`, the document the node left at a switch or a rejoin, holds that a store
/// starting at `doc`'s checkpoint takes, by the node's own refusals and its operator's
/// drops, and `doc` lacks (`docs/store.md`, Remakes): kept, to put again (`lacking`).
@@ -1110,6 +1181,57 @@ mod tests {
.doc
}
+ /// A node follows an epoch whose snapshot holds its document's checkpoint, one it
+ /// builds from its document, or one its operator pinned; with an empty document, the
+ /// root of its store too. Not one holding a later checkpoint it neither builds nor has
+ /// pinned (row 15), one holding a checkpoint before its document's (row 31), nor one
+ /// of another store.
+ #[test]
+ fn a_node_follows_an_epoch_from_a_checkpoint_it_builds_or_pinned() {
+ let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2));
+ let first = Spec::first(&[&tv, &lass], &[]);
+ let root = first.root();
+ let on = first.hash();
+ let head = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
+ let mut next = first.after(on);
+ next.checkpoint = Some(vec![Id::of(&head)]);
+ let proposal = tv.propose(&[Id::of(&head)], &next.json());
+ let votes = [
+ tv.vote(&[Id::of(&proposal)], on, next.hash(), true),
+ lass.vote(&[Id::of(&proposal)], on, next.hash(), true),
+ ];
+ let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], next.hash());
+ let mut before = events::started(Federation::NAME, &root, &[]).unwrap();
+ for event in [&head, &proposal, &votes[0], &votes[1]] {
+ put(&mut before, event);
+ }
+ let mut moved = before.fork();
+ put(&mut moved, &written);
+ let (at_root, at_checkpoint) = (
+ snapshot(&before, &Holding::default()),
+ snapshot(&moved, &Holding::default()),
+ );
+ let pin = Id::of(&events::checkpoint(&at_checkpoint).unwrap());
+ let store = Some(Id::of(&root));
+ let empty = Document::new();
+ let follows = |holding: &Holding, snapshot: &Document, doc: &Document| {
+ holding.follows(snapshot, doc, store).is_ok()
+ };
+ let node = Holding::default();
+ let pinned = Holding::open(None, Arc::new(Files::default()), Some(pin), None).unwrap();
+ assert!(follows(&node, &at_root, &before));
+ assert!(follows(&node, &at_checkpoint, &moved));
+ assert!(follows(&node, &at_checkpoint, &before));
+ assert!(follows(&node, &at_root, &empty));
+ assert!(!follows(&node, &at_checkpoint, &empty));
+ assert!(follows(&pinned, &at_checkpoint, &empty));
+ assert!(!follows(&node, &at_root, &at_checkpoint));
+ assert!(
+ node.follows(&at_root, &empty, Some(Id::of(b"else")))
+ .is_err()
+ );
+ }
+
/// After a switch, what the document left holds beyond the base that a store starting
/// at the new document's checkpoint takes goes again; what nobody takes doesn't.
#[test]