diff options
Diffstat (limited to 'src/federation/held.rs')
| -rw-r--r-- | src/federation/held.rs | 102 |
1 files changed, 95 insertions, 7 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index 5155573..66f20e9 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -156,7 +156,8 @@ struct Inner { pub(crate) struct AtBase { /// The checkpoint the store reaches there, where it isn't the root. pub(crate) checkpoint: Option<Vec<u8>>, - /// The root, the events that checkpoint keeps, and those after it, by ID. + /// The root, the events that checkpoint keeps, and those after it, by ID: those it + /// holds back too, where it stops there. pub(crate) events: BTreeMap<Id, Vec<u8>>, /// The members there, by node ID. pub(crate) members: BTreeSet<String>, @@ -473,7 +474,8 @@ impl Holding { /// The events the node has that `doc` lacks, each after its dependencies: of the /// document it left, what a store starting at `doc`'s checkpoint takes (`left`); then - /// what its store's checkpoint keeps, then what it took after it. + /// what its store's checkpoint keeps, then what it took after it, then what it holds + /// back, stopped (`Store::held_back`). pub(crate) fn lacking(&self, doc: &impl ReadDoc) -> Vec<(Id, Vec<u8>)> { let mut held: HashSet<Id> = events::ids(doc).into_iter().collect(); let mut inner = self.inner.lock_or_recover(); @@ -490,6 +492,12 @@ impl Holding { .taken() .iter() .filter_map(|id| Some((*id, store.get(id)?.to_vec()))), + ) + .chain( + store + .held_back() + .into_iter() + .map(|(id, bytes)| (id, bytes.to_vec())), ), ); } @@ -570,9 +578,10 @@ impl Holding { /// 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`). - /// So what the node held beyond the base goes again, and, where it moved to a later - /// checkpoint since, what a store starting at the snapshot's takes to reach it. + /// drops, or holds back, stopped, and `doc` lacks (`docs/store.md`, Remakes): kept, to + /// put again (`lacking`). So what the node held beyond the base goes again, and, where + /// it moved to a later checkpoint since, what a store starting at the snapshot's takes + /// to reach it. pub(crate) fn left(&self, doc: &impl ReadDoc, left: &impl ReadDoc) { let reading = Holding { pin: self.pin, @@ -602,6 +611,13 @@ impl Holding { ); } } + put.extend( + store + .held_back() + .into_iter() + .filter(|(id, _)| events.contains_key(id)) + .map(|(id, bytes)| (id, bytes.to_vec())), + ); } if !put.is_empty() { info!( @@ -854,8 +870,9 @@ impl Holding { /// What a remake's snapshot holds of the store: as this node reads it anew from /// `base`, the document at the remake's base, by its own refusals and its operator's - /// drops, as it reads its document whenever it starts. None where `base` holds no - /// store, or none this node reads there. + /// drops, as it reads its document whenever it starts; and what it holds back there, + /// where it stops (`Store::held_back`). None where `base` holds no store, or none this + /// node reads there. pub(crate) fn at_base(&self, base: &impl ReadDoc) -> Option<AtBase> { let reading = Holding { pin: self.pin, @@ -880,6 +897,10 @@ impl Holding { events.insert(*id, bytes.to_vec()); } } + // Stopped, what it can't judge it doesn't leave out. + for (id, bytes) in store.held_back() { + events.insert(id, bytes.to_vec()); + } events.insert(swarm, root); AtBase { checkpoint: (Id::of(checkpoint) != swarm).then(|| checkpoint.to_vec()), @@ -1804,6 +1825,73 @@ mod tests { assert!(holding.with(|_| ()).is_none()); } + /// A node whose store stopped leaves out none of what it holds back, as it can't tell + /// which will be taken: what waited when it stopped among it, it puts again where its + /// document lacks it, each after what it builds on, and a remake's snapshot holds it, so + /// that a store read anew from that stops there again. + #[test] + fn a_node_that_stopped_keeps_what_it_holds_back_through_a_remake() { + let (tv, lass, makefu) = ( + Party::new("tv", 1), + Party::new("lass", 2), + Party::new("makefu", 3), + ); + let first = Spec::first(&[&tv, &lass, &makefu], &[]); + let root = first.root(); + let on = first.hash(); + let mut none = first.after(on); + none.rules + .push(("_continuum/x".to_owned(), harness::str("tv"))); + let proposal = tv.propose(&[Id::of(&root)], &none.json()); + let votes = + [&tv, &lass].map(|party| party.vote(&[Id::of(&proposal)], on, none.hash(), true)); + let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], none.hash()); + let after = tv.write(&[Id::of(&written)], "home/tv/a", harness::str("1")); + let beside = lass.write(&[Id::of(&votes[1])], "home/lass/b", harness::str("2")); + let on_beside = makefu.write(&[Id::of(&beside)], "home/makefu/c", harness::str("3")); + let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); + for event in [&proposal].into_iter().chain(&votes) { + put(&mut doc, event); + } + let holding = Holding::default(); + read(&holding, &doc, 0); + // Waiting for what it builds on when the store stops; that one comes after. + assert_eq!(holding.offer(on_beside.clone()), Some(Offered::Waiting)); + put(&mut doc, &written); + put(&mut doc, &after); + read(&holding, &doc, 0); + assert_eq!(holding.offer(beside.clone()), Some(Offered::Stopped)); + let lacking: Vec<Id> = holding + .lacking(&doc) + .into_iter() + .map(|(id, _)| id) + .collect(); + assert_eq!(lacking, vec![Id::of(&beside), Id::of(&on_beside)]); + + put(&mut doc, &on_beside); + put(&mut doc, &beside); + let snapshot = snapshot(&doc, &holding); + let held = events::ids(&snapshot); + for kept in [&written, &after, &beside, &on_beside] { + assert!(held.contains(&Id::of(kept))); + } + let anew = Holding::default(); + read(&anew, &snapshot, 1); + assert_eq!( + anew.with(|store| store.stopped().map(|(id, ..)| *id)), + Some(Some(Id::of(&written))) + ); + anew.drop_taken(&[Id::of(&written)]).unwrap().unwrap(); + assert_eq!( + anew.with(|store| { + store.holds(&Id::of(&beside)) + && store.holds(&Id::of(&on_beside)) + && !store.holds(&Id::of(&after)) + }), + Some(true) + ); + } + /// A node voting by itself whose store stopped casts nothing, by itself or by hand: /// what it reads is behind what passed. #[test] |
