aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 00:21:23 +0000
committertv <tv@krebsco.de>2026-11-17 01:47:41 +0100
commitc0dac8179bb402ac5f3c6bcff44dfef26450fbfe (patch)
tree3c283cc402c423a8b8eae9232923880270fd232c /src/federation/held.rs
parent27e5436b5cc5b94831aec272cd7b350be808e3f1 (diff)
federation: a fork's store read from its document, with the events its root keeps
A node reads its store from a fork's root and the events the root keeps, which follow it in the document, once they are all there; until then it holds no store. Its store takes what builds on the fork's root, and nothing the store it came from took after the heads it forked at.
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 29464ea..e1c930d 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -231,7 +231,18 @@ impl Holding {
let id = Id::of(&root);
let (own, dropped) = inner.kept.ids().unwrap_or_default();
let started = match self.pin {
- None => Store::new(root, Vec::new()),
+ // A fork's root keeps events, which follow it in the document.
+ None => {
+ let keeps = Federation::parts(&root).map_or_else(Vec::new, |(_, keeps)| keeps);
+ let kept: Option<Vec<Vec<u8>>> = keeps
+ .iter()
+ .map(|keep| unread.events.get(keep).cloned())
+ .collect();
+ let Some(kept) = kept else {
+ return;
+ };
+ Store::new(root, kept)
+ }
Some(pin) => match found(&pin, &root, &unread.events) {
Some((checkpoint, kept)) => {
info!(store = %id, checkpoint = %pin, "Found the pinned checkpoint in the document");
@@ -1201,6 +1212,43 @@ mod tests {
assert!(events::ids(&doc).contains(&Id::of(&beside)));
}
+ /// A fork's document: its store starts from the fork's root and the events it keeps,
+ /// which follow it there, and takes what builds on it, not what the store it came from
+ /// took after its heads.
+ #[test]
+ fn a_node_reads_a_forks_store_from_its_document() {
+ let tv = Party::new("tv", 1);
+ let first = Spec::first(&[&tv], &[]);
+ let mut came_from = Store::<Federation>::new(first.root(), Vec::new()).unwrap();
+ let write = tv.write(&[came_from.id()], "home/tv/x", harness::str("1"));
+ let head = Id::of(&write);
+ came_from.offer(write);
+ let mut spec = first.after(first.hash());
+ spec.parties[0].nodes_since = 1;
+ let (root, kept) = came_from.fork(&[head], &spec.json().bytes()).unwrap();
+ let stayed = tv.write(&[head], "home/tv/x", harness::str("2"));
+ let went = tv.write(&[Id::of(&root)], "home/tv/y", harness::str("3"));
+ let mut doc = events::started(Federation::NAME, &root, &kept).unwrap();
+ put(&mut doc, &stayed);
+ put(&mut doc, &went);
+ let holding = Holding::default();
+ read(&holding, &doc, 0);
+ assert_eq!(holding.with(|store| store.id()), Some(Id::of(&root)));
+ assert_eq!(
+ holding.with(|store| store.state().value("home/tv/x").cloned()),
+ Some(Some(harness::str("1")))
+ );
+ assert_eq!(
+ holding.with(|store| store.taken().to_vec()),
+ Some(vec![Id::of(&went)])
+ );
+ // Its root alone, the events it keeps not there yet: no store yet.
+ let alone = events::started(Federation::NAME, &root, &[]).unwrap();
+ let waiting = Holding::default();
+ read(&waiting, &alone, 0);
+ assert!(waiting.with(|_| ()).is_none());
+ }
+
/// Two proposals on the first version passing at once, tv backing both: a node writes
/// the one it takes, and refuses the other, of its own.
#[test]