From d8cec1cd61c63659d583168244f8f51ec550b3be Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 20:02:12 +0000 Subject: 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. --- src/federation/store/memory.rs | 43 ++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'src') 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, BTreeMap>); + struct Event { bytes: Vec, dependencies: Vec, @@ -314,16 +317,7 @@ impl Store { return false; } let ids: Vec = 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::>>() - 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::>()) else { @@ -395,6 +389,20 @@ impl Store { 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 { + 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::>>()?; + 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::>()); + } + #[test] fn what_was_taken_beside_the_heads_is_refused_by_the_move() { let mut store = store(); -- cgit v1.3.1