From 1656f73b41a3528048c33774457b0d68ed430934 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 13:03:52 +0000 Subject: 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. --- CHANGELOG.md | 6 ++++++ docs/federation.md | 3 ++- docs/store.md | 4 +++- src/federation/fold.rs | 10 ++++++++++ src/federation/held.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ src/federation/store/memory.rs | 19 +++++++++++++++++- src/federation/store/mod.rs | 7 +++++++ 7 files changed, 91 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b123947..7b82eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ it from the release before ([`docs/operations.md`](docs/operations.md), ## Unreleased +* A node merges heads from before the version naming the revision with merge events: a + new event builds on a head holding the current version too. On the earliest heads + alone, its merge event was written under the version before and refused, so the + backlog a revision was passed for stayed, and with it no checkpoint was proposed + ([`docs/store.md`](docs/store.md); [`src/federation/store/memory.rs`](src/federation/store/memory.rs)). + * A party's second `true` builds on just the nays that end its first: one of each party, of as many parties as it takes. It built on every vote held, so one party signing its `false` again and again, or 64 parties voting, left a freed party unable to vote diff --git a/docs/federation.md b/docs/federation.md index d0af71f..79921d6 100644 --- a/docs/federation.md +++ b/docs/federation.md @@ -648,7 +648,8 @@ signs merge events with its own key ([Events](#events)): they count for nothing, gather what they build on into the history of what builds on them. It does so by itself where its store holds more heads than an event may name, a random wait after it first did, so that of the members few do at once: each on as many as an event may name, the -earliest taken first, until no more are left. +earliest taken first, with one holding the current version among them, as an event on +older heads alone is written under the version before, until no more are left. ### Forks diff --git a/docs/store.md b/docs/store.md index d5159f6..f1e7586 100644 --- a/docs/store.md +++ b/docs/store.md @@ -102,7 +102,9 @@ A fold says, of its events: * **how large an event may be**, which the document judges a change by ([On Automerge](#on-automerge)), and how many events it may build on: a new event builds on the heads of what a node holds, as many as that, the earliest taken first, so - that what is written merges what stayed apart the longest; + that what is written merges what stayed apart the longest, one of them holding what + the fold says a new event is judged by, where a head does (the federation: an event + writing the current version); * **whether an event is taken where its history reads as a given state**: judging. A root is taken only as its own store's, never by judging; * **the state a checkpoint and the events after it read as**, built by adding the events 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::>() != 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 { + 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 Store { /// 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 { 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>; + /// 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 { + Vec::new() + } + /// The nodes that carry the store, each with its group. fn members(state: &Self::State) -> Vec<(String, String)>; } -- cgit v1.3.1