aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-25 13:03:52 +0000
committertv <tv@krebsco.de>2026-11-25 13:03:52 +0000
commit1656f73b41a3528048c33774457b0d68ed430934 (patch)
tree56fa9550232e9831f1f2d6ed324d9b16b9d713cf /src
parent1820e326115f803ffa84280aad4e367f20d91e9f (diff)
store: a new event builds on a head holding what it is judged by
A new event builds on the heads taken earliest, 64 at most. Where a store holds more, and those all come from before the current version was written, an event on them is written under the version before: a merge event, under one without merge events, was refused, so the backlog of heads a revision was passed to merge stayed, and with more heads than a checkpoint may name, no node proposed one. The fold now says which events a new one is to have in its history (Fold::judged_by; the federation: those writing the current version), and building_on names the earliest head holding one, in place of the last of the others.
Diffstat (limited to 'src')
-rw-r--r--src/federation/fold.rs10
-rw-r--r--src/federation/held.rs45
-rw-r--r--src/federation/store/memory.rs19
-rw-r--r--src/federation/store/mod.rs7
4 files changed, 80 insertions, 1 deletions
diff --git a/src/federation/fold.rs b/src/federation/fold.rs
index b590f07..a527a74 100644
--- a/src/federation/fold.rs
+++ b/src/federation/fold.rs
@@ -877,6 +877,16 @@ impl Fold for Federation {
(heads.iter().copied().collect::<BTreeSet<Id>>() != now.at).then_some(heads)
}
+ /// The events writing the current version: an event without one in its history is
+ /// written under a version before, by its keys, lists and revision. None where the
+ /// checkpoint's version is current, which every history holds.
+ fn judged_by(now: &State) -> Vec<Id> {
+ now.writing
+ .get(&now.current().hash)
+ .map(|events| events.iter().copied().collect())
+ .unwrap_or_default()
+ }
+
fn members(state: &State) -> Vec<(String, String)> {
state.members()
}
diff --git a/src/federation/held.rs b/src/federation/held.rs
index cf3cf04..72633eb 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -2210,6 +2210,51 @@ mod tests {
assert!(holding.merge(&listed).is_empty());
}
+ /// Heads from before the version naming the revision with merge events, more than an
+ /// event may name: a merge event on the earliest of them alone would be written under
+ /// the version before, which has none, and refused. It builds on one holding the
+ /// current version too, so the backlog a revision was passed for gets merged.
+ #[test]
+ fn a_node_merges_heads_older_than_the_revision() {
+ let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2));
+ let first = Spec::first(&[&tv, &lass], &[]);
+ let root = first.root();
+ let on = first.hash();
+ let mut doc = events::started(Federation::NAME, &root, &[]).unwrap();
+ for n in 0..=event::DEPENDENCIES {
+ put(
+ &mut doc,
+ &tv.write(
+ &[Id::of(&root)],
+ &format!("home/tv/k{n}"),
+ harness::str("1"),
+ ),
+ );
+ }
+ let holding =
+ Holding::open(None, Arc::new(Files::default()), None, Some("tv".into())).unwrap();
+ read(&holding, &doc, 0);
+ // The revision passes: its version event is the head taken last.
+ let mut next = first.after(on);
+ next.revision = Feature::MergeEvents.revision();
+ let proposal = tv.propose(&[Id::of(&root)], &next.json());
+ let votes =
+ [&tv, &lass].map(|party| party.vote(&[Id::of(&proposal)], on, next.hash(), true));
+ let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], next.hash());
+ for event in [&proposal, &votes[0], &votes[1], &written] {
+ put(&mut doc, event);
+ }
+ read(&holding, &doc, 0);
+ assert_eq!(
+ holding.with(|store| store.state().current().hash),
+ Some(next.hash())
+ );
+ let listed = crate::identity::NodeKey::from_seed([128; 32]);
+ assert!(holding.apart(&listed));
+ assert!(!holding.merge(&listed).is_empty());
+ assert!(!holding.apart(&listed));
+ }
+
/// Row 70 (`docs/federation.md`): a checkpoint's version proposed again, by an event
/// beside its heads. A node backs it all the same, as a proposal lapses with the move,
/// and votes on the event proposing it above the heads alone: a version event written
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs
index 092a606..73ee1c3 100644
--- a/src/federation/store/memory.rs
+++ b/src/federation/store/memory.rs
@@ -286,10 +286,27 @@ 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.
+ /// 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.
pub(crate) fn building_on(&self) -> 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);
+ }
heads
}
diff --git a/src/federation/store/mod.rs b/src/federation/store/mod.rs
index d2028da..06f7a58 100644
--- a/src/federation/store/mod.rs
+++ b/src/federation/store/mod.rs
@@ -128,6 +128,13 @@ pub(crate) trait Fold {
/// The heads of a later checkpoint, where a store reading as `now` moves to one.
fn moves_to(now: &Self::State) -> Option<Vec<Id>>;
+ /// The events one of which a new event is to have in its history, where what an event
+ /// is judged by depends on its history beyond what the checkpoint says: of a store
+ /// reading as `now`, none unless the fold names some (`Store::building_on`).
+ fn judged_by(_now: &Self::State) -> Vec<Id> {
+ Vec::new()
+ }
+
/// The nodes that carry the store, each with its group.
fn members(state: &Self::State) -> Vec<(String, String)>;
}