aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/store/memory.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-19 01:48:57 +0000
committertv <tv@krebsco.de>2026-11-19 03:18:59 +0100
commit31d41eb1c902c907a5c6e5608510f96c4b1bc864 (patch)
tree5a1a38166c6b22312b4d1eb02adc5dc5f12bce95 /src/federation/store/memory.rs
parent03744437a5ffc36fbb5e973a0ddfd6b91945b212 (diff)
store: a new event builds on as many heads as an event may name
A party's client builds on the heads its node shows (`GET /events`), and a node on its own heads; an event names at most 64 dependencies. With more heads than that, where many write at once and nothing merges, every event built on them was none. `Store::building_on` gives the heads, as many as an event may name (`Fold::DEPENDENCIES`, the federation's 64), the earliest taken first, so that what is written merges what stayed apart the longest; `GET /events` shows those, and a node voting by itself builds on them.
Diffstat (limited to 'src/federation/store/memory.rs')
-rw-r--r--src/federation/store/memory.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs
index 8493735..05479e3 100644
--- a/src/federation/store/memory.rs
+++ b/src/federation/store/memory.rs
@@ -263,8 +263,8 @@ impl<F: Fold> Store<F> {
ordered
}
- /// What a new event builds on: the events nothing else held builds on, or the
- /// checkpoint's heads where nothing came after it.
+ /// The heads of what the store holds: the events nothing else held builds on, in the
+ /// order taken, or the checkpoint's heads where nothing came after it.
pub(crate) fn heads(&self) -> Vec<Id> {
let built_on: HashSet<&Id> = self
.events
@@ -284,6 +284,15 @@ 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.
+ pub(crate) fn building_on(&self) -> Vec<Id> {
+ let mut heads = self.heads();
+ heads.truncate(F::DEPENDENCIES);
+ heads
+ }
+
/// Offers an event: it is taken once the store holds its dependencies, where the fold
/// takes it at its history.
pub(crate) fn offer(&mut self, bytes: Vec<u8>) -> Offered {
@@ -1139,6 +1148,24 @@ mod tests {
assert!(store.drop_taken(&[lock]).is_err());
}
+ /// A new event builds on as many heads as an event may name, the earliest taken first:
+ /// a later one merges the rest.
+ #[test]
+ fn a_new_event_builds_on_the_earliest_heads_an_event_may_name() {
+ let mut store = store();
+ let root = store.id();
+ let apart: Vec<Id> = (0..=TestFold::DEPENDENCIES)
+ .map(|n| taken(store.offer(testfold::put(&[root], &format!("k{n}"), "1"))))
+ .collect();
+ assert_eq!(store.heads(), apart);
+ assert_eq!(store.building_on(), apart[..TestFold::DEPENDENCIES]);
+ let merged = taken(store.offer(testfold::put(&store.building_on(), "m", "1")));
+ assert_eq!(
+ store.building_on(),
+ vec![apart[TestFold::DEPENDENCIES], merged]
+ );
+ }
+
/// Heads leave out what is neither among them, in their history, nor above them.
#[test]
fn heads_leave_out_what_is_beside_them() {