aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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]