diff options
| author | tv <tv@krebsco.de> | 2026-11-25 15:34:56 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-25 15:34:56 +0000 |
| commit | 2c9be74e184e0d942f03a8f1b549bdd7317e2dbd (patch) | |
| tree | 52170b9d4d3ba7dd1a9cb4db01830b84b38bf40b /src/federation/store/memory.rs | |
| parent | e22f2e5c38baa1866793338fc7abde8c8e53b635 (diff) | |
store: a party's next event builds on its last, past 64 heads
Where a store holds more heads than an event names, a new event builds
on the earliest. A party's write may then lack its last one in its
history: of two neither in the other's the lower ID counts, not the
later, and a key set written so is written under an older version, and
ranks below one written under the latest.
building_on_with names a head holding each event it is asked to hold,
beside the one holding what the fold judges by; GET /events?party=NAME
asks it for that party's last write, key set or node list (latest_of),
and continuum-party signs on the heads for its party.
Diffstat (limited to 'src/federation/store/memory.rs')
| -rw-r--r-- | src/federation/store/memory.rs | 75 |
1 files changed, 61 insertions, 14 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index 73ee1c3..d49fca0 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -287,26 +287,46 @@ impl<F: Fold> Store<F> { /// What a new event builds on: its heads, as many as an event may name /// (`Fold::DEPENDENCIES`), the earliest taken first, so that what is written merges /// what stayed apart the longest; among them always one holding what the fold judges - /// a new event by (`Fold::judged_by`), where a head does: the earliest such, in place - /// of the last of the others. Without it, an event on old heads alone is judged as at - /// their histories: under the federation's version before, say. + /// a new event by (`Fold::judged_by`), where a head does. Without it, an event on old + /// heads alone is judged as at their histories: under the federation's version before, + /// say. pub(crate) fn building_on(&self) -> Vec<Id> { + self.building_on_with(&[]) + } + + /// As `building_on`, among them a head holding each of `also` too, where one does: a + /// writer's own last event, so that its next has it in its history. Each takes the + /// place of the last of the others. + pub(crate) fn building_on_with(&self, also: &[Id]) -> Vec<Id> { let mut heads = self.heads(); if heads.len() <= F::DEPENDENCIES { return heads; } - let judged_by = F::judged_by(&self.state); - let holding = heads.iter().copied().find(|head| { - let history = self.history(std::slice::from_ref(head)); - judged_by.iter().any(|id| history.contains(id)) - }); - heads.truncate(F::DEPENDENCIES); - if let Some(holding) = holding - && !heads.contains(&holding) - { - heads.pop(); - heads.push(holding); + let holding = |wanted: &[Id]| { + heads.iter().copied().find(|head| { + let history = self.history(std::slice::from_ref(head)); + wanted.iter().any(|id| history.contains(id)) + }) + }; + let mut keep: Vec<Id> = holding(&F::judged_by(&self.state)).into_iter().collect(); + for id in also { + if let Some(head) = holding(std::slice::from_ref(id)) + && !keep.contains(&head) + && keep.len() < F::DEPENDENCIES + { + keep.push(head); + } } + let others = F::DEPENDENCIES - keep.len(); + let mut left = others; + heads.retain(|head| { + if keep.contains(head) { + return true; + } + let taken = left > 0; + left = left.saturating_sub(1); + taken + }); heads } @@ -1203,6 +1223,33 @@ mod tests { ); } + /// What a new event builds on holds the events it is asked to, a writer's own last + /// one say: of more heads than an event may name, the earliest alone may lack it, and + /// of two events of one writer neither in the other's history, the lower ID counts. + #[test] + fn a_new_event_builds_on_what_it_is_asked_to_hold() { + let mut store = store(); + let root = store.id(); + let apart: Vec<Id> = (0..=TestFold::DEPENDENCIES + 1) + .map(|n| taken(store.offer(testfold::put(&[root], &format!("k{n}"), "1")))) + .collect(); + let (last, before) = (apart[apart.len() - 1], apart[apart.len() - 2]); + assert!(!store.building_on().contains(&last)); + let on = store.building_on_with(&[last]); + assert_eq!(on.len(), TestFold::DEPENDENCIES); + assert!(on.contains(&last) && !on.contains(&before)); + assert_eq!(on[..on.len() - 1], apart[..TestFold::DEPENDENCIES - 1]); + // One held already by what it builds on changes nothing; nor does one not held. + assert_eq!(store.building_on_with(&[apart[0]]), store.building_on()); + assert_eq!( + store.building_on_with(&[Id::of(b"none")]), + store.building_on() + ); + // In the history of a head, that head. + let later = taken(store.offer(testfold::put(&[last], "later", "1"))); + assert!(store.building_on_with(&[last]).contains(&later)); + } + /// Heads leave out what is neither among them, in their history, nor above them. #[test] fn heads_leave_out_what_is_beside_them() { |
