diff options
| author | tv <tv@krebsco.de> | 2026-11-16 20:02:12 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 21:26:25 +0100 |
| commit | d8cec1cd61c63659d583168244f8f51ec550b3be (patch) | |
| tree | 75d36422a835c6bcf50c14b149aca4c148b410cb /src/federation/store/memory.rs | |
| parent | ff97d657d76508af290a29ed11e478575317c7df (diff) | |
store: the checkpoint at heads a store holds, as it builds one moving there
`checkpoint_at` builds the checkpoint at heads the store took, its bytes and the events it
keeps, as the store does moving to a checkpoint, which now goes through it: so a node
can build one at heads it reads, to tell whether it is the one its operator pins.
Diffstat (limited to 'src/federation/store/memory.rs')
| -rw-r--r-- | src/federation/store/memory.rs | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index ec25b14..c8b5897 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -36,6 +36,9 @@ pub(crate) enum Offered { Refused(Refusal), } +/// A checkpoint built: its bytes, and the events it keeps, by ID. +pub(crate) type Built = (Vec<u8>, BTreeMap<Id, Vec<u8>>); + struct Event { bytes: Vec<u8>, dependencies: Vec<Id>, @@ -314,16 +317,7 @@ impl<F: Fold> Store<F> { return false; } let ids: Vec<Id> = heads.iter().copied().collect(); - let at = self.state_at(&ids); - let (checkpoint, keeps) = F::checkpoint(&at, &ids); - let Some(kept) = keeps - .iter() - .map(|id| self.get(id).map(<[u8]>::to_vec)) - .collect::<Option<Vec<_>>>() - else { - return false; - }; - let Ok(kept) = matched(&keeps, kept) else { + let Some((checkpoint, kept)) = self.checkpoint_at(&ids) else { return false; }; let Ok(start) = F::start(&checkpoint, &kept.clone().into_iter().collect::<Vec<_>>()) else { @@ -395,6 +389,20 @@ impl<F: Fold> Store<F> { true } + /// The checkpoint at `heads` (in ascending order), events the store took, as it builds + /// one it moves to. + pub(crate) fn checkpoint_at(&self, heads: &[Id]) -> Option<Built> { + if !heads.iter().all(|head| self.events.contains_key(head)) { + return None; + } + let (checkpoint, keeps) = F::checkpoint(&self.state_at(heads), heads); + let kept = keeps + .iter() + .map(|id| Some((*id, self.get(id)?.to_vec()))) + .collect::<Option<BTreeMap<_, _>>>()?; + Some((checkpoint, kept)) + } + /// The events that waited for `id` alone, now to be considered again. fn woken(&mut self, id: &Id) -> Vec<(Id, Event)> { let Some(waiters) = self.waits_for.remove(id) else { @@ -831,6 +839,21 @@ mod tests { assert_eq!(store.offer(testfold::put(&[root], "a", "1")), Offered::Held); } + /// The checkpoint a store builds at heads it holds is the one it moves to there. + #[test] + fn a_checkpoint_built_at_heads_is_the_one_moved_to() { + let mut store = store(); + let root = store.id(); + let first = taken(store.offer(testfold::put(&[root], "a", "1"))); + let second = taken(store.offer(testfold::put(&[first], "a", "2"))); + let lock = taken(store.offer(testfold::lock(&[second], "b"))); + assert_eq!(store.checkpoint_at(&[Id::of(b"not held")]), None); + let (checkpoint, kept) = store.checkpoint_at(&[lock]).expect("held"); + taken(store.offer(testfold::cut(&[lock], 1, &[lock]))); + assert_eq!(store.checkpoint(), checkpoint.as_slice()); + assert_eq!(store.kept(), kept.into_values().collect::<Vec<_>>()); + } + #[test] fn what_was_taken_beside_the_heads_is_refused_by_the_move() { let mut store = store(); |
