aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/store/memory.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 17:10:41 +0000
committertv <tv@krebsco.de>2026-11-16 18:28:20 +0100
commit8f6ec848b01fa692b09ae8aae25d383d281eb996 (patch)
treefdfd1a03b55802cb667c55b68b3c70817c413e91 /src/federation/store/memory.rs
parente7f8b368db441dbd2c91b08e7e04fa34bed01a1a (diff)
federation: groups that parted come together as parties come in, not by merges
A merge, a fork at heads of several stores, named every party's keys anew, so that a party wrote its key set again before anything a key of its set signed counted there; and kept each side's values side by side, so that a value a party replaced on one side came back where the other side's write had the lower ID. Letting the other side's parties in, by a version or a fork naming them, who sign again what they keep as a party's client signs again what a move refused, does the same with nothing new. So a fork is at heads of one store: the fold makes its root from that store's state, and the store from its own heads (`Store::fork`). Merges' rows go, those after them move up, and Roads not taken says why, with merging in place.
Diffstat (limited to 'src/federation/store/memory.rs')
-rw-r--r--src/federation/store/memory.rs69
1 files changed, 22 insertions, 47 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs
index 6a67bfa..8f97299 100644
--- a/src/federation/store/memory.rs
+++ b/src/federation/store/memory.rs
@@ -113,31 +113,27 @@ impl<F: Fold> Store<F> {
})
}
- /// The root of a fork at heads of one store or of several, each given with its heads,
- /// made by `spec`: its bytes, and those of the events it keeps. A fork at heads of
- /// several stores merges them.
+ /// The root of a fork at `heads`, which the store holds, made by `spec`: its bytes, and
+ /// those of the events it keeps.
pub(crate) fn fork(
- sides: &[(&Self, &[Id])],
+ &self,
+ heads: &[Id],
spec: &[u8],
) -> Result<(Vec<u8>, Vec<Vec<u8>>), Refused> {
- let mut states = Vec::new();
- let mut heads = BTreeSet::new();
- for (store, at) in sides {
- if let Some(head) = at.iter().find(|head| !store.holds(head)) {
- return Err(Refused::new(format!("{head} isn't held there")));
- }
- states.push(store.state_at(at));
- heads.extend(at.iter().copied());
+ if let Some(head) = heads.iter().find(|head| !self.holds(head)) {
+ return Err(Refused::new(format!("{head} isn't held here")));
}
- let heads: Vec<Id> = heads.into_iter().collect();
- let states: Vec<&F::State> = states.iter().collect();
- let (root, keeps) = F::fork(&states, &heads, spec)?;
+ let heads: Vec<Id> = heads
+ .iter()
+ .copied()
+ .collect::<BTreeSet<_>>()
+ .into_iter()
+ .collect();
+ let (root, keeps) = F::fork(&self.state_at(&heads), &heads, spec)?;
let kept = keeps
.iter()
.map(|id| {
- sides
- .iter()
- .find_map(|(store, _)| store.get(id))
+ self.get(id)
.map(<[u8]>::to_vec)
.ok_or_else(|| Refused::new(format!("{id} isn't held")))
})
@@ -145,10 +141,10 @@ impl<F: Fold> Store<F> {
Ok((root, kept))
}
- /// Whether `root` is the root of the fork at those heads, made by `spec`, as a node
- /// holding them builds it.
- pub(crate) fn checks_fork(sides: &[(&Self, &[Id])], spec: &[u8], root: &[u8]) -> bool {
- Self::fork(sides, spec).is_ok_and(|(built, _)| built == root)
+ /// Whether `root` is the root of the fork at `heads`, made by `spec`, as this store
+ /// builds it.
+ pub(crate) fn checks_fork(&self, heads: &[Id], spec: &[u8], root: &[u8]) -> bool {
+ self.fork(heads, spec).is_ok_and(|(built, _)| built == root)
}
/// Whether the store holds an event: taken after its checkpoint, or one of its heads.
@@ -816,14 +812,11 @@ mod tests {
let root = store.id();
let here = taken(store.offer(testfold::put(&[root], "a", "1")));
let spec = testfold::fork_spec(1, "fork");
- let (fork_root, kept) = Store::fork(&[(&store, &[here])], &spec).expect("a fork");
+ let (fork_root, kept) = store.fork(&[here], &spec).expect("a fork");
let mut fork: Store<TestFold> = Store::new(fork_root.clone(), kept).expect("its root");
- assert!(Store::checks_fork(&[(&store, &[here])], &spec, &fork_root));
- assert!(!Store::checks_fork(
- &[(&store, &[here])],
- &testfold::fork_spec(2, "fork"),
- &fork_root
- ));
+ assert!(store.checks_fork(&[here], &spec, &fork_root));
+ assert!(!store.checks_fork(&[here], &testfold::fork_spec(2, "fork"), &fork_root));
+ assert!(store.fork(&[Id::of(b"nothing held")], &spec).is_err());
assert_eq!(fork.state().counts()["a"], "1");
assert_eq!(fork.state().era(), (1, Some(&"fork".to_owned())));
let stayed = testfold::put(&[here], "a", "2");
@@ -837,24 +830,6 @@ mod tests {
}
#[test]
- fn a_merge_keeps_each_sides_values_the_lower_id_across() {
- let mut left = store();
- let mut right: Store<TestFold> =
- Store::new(testfold::root("other"), Vec::new()).expect("a root");
- let l = taken(left.offer(testfold::put(&[left.id()], "a", "left")));
- let only = taken(left.offer(testfold::put(&[l], "b", "left")));
- let r = taken(right.offer(testfold::put(&[right.id()], "a", "right")));
- let spec = testfold::fork_spec(1, "merge");
- let (merge_root, kept) =
- Store::fork(&[(&left, &[only]), (&right, &[r])], &spec).expect("a merge");
- let merged: Store<TestFold> = Store::new(merge_root, kept).expect("its root");
- let lower = if l < r { "left" } else { "right" };
- assert_eq!(merged.state().counts()["a"], lower);
- assert_eq!(merged.state().counts()["b"], "left");
- assert!(Store::fork(&[(&left, &[r])], &spec).is_err());
- }
-
- #[test]
fn an_event_waits_until_all_it_builds_on_is_held() {
let mut store = store();
let root = store.id();