aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 22:56:25 +0000
committertv <tv@krebsco.de>2026-11-17 00:09:47 +0100
commitce8ccdc68f5b0cc52b380177665ee07505c5bfb5 (patch)
tree339772ea691185c0d9d30c2927d83ab0e07623c3
parent9b20645df80363d922b25b8f2c0a531d060ff36f (diff)
federation: the tests' harness builds a federation's root
`Spec::root`: the root whose first version a spec spells out, built in one place for the tests of the attacks, of the store read from a document, and the properties.
-rw-r--r--src/federation/attacks.rs23
-rw-r--r--src/federation/harness.rs10
-rw-r--r--src/federation/held.rs27
-rw-r--r--src/federation/properties.rs11
4 files changed, 26 insertions, 45 deletions
diff --git a/src/federation/attacks.rs b/src/federation/attacks.rs
index 80c9f9c..a7efd50 100644
--- a/src/federation/attacks.rs
+++ b/src/federation/attacks.rs
@@ -34,19 +34,10 @@ struct Fed {
first: Spec,
}
-fn root_of(spec: &Spec) -> Vec<u8> {
- Checkpoint {
- at: Vec::new(),
- keeps: Vec::new(),
- version: Version::read(&spec.json()).expect("a version"),
- }
- .bytes()
-}
-
fn fed(parties: &[&Party], rules: &[(&str, Json)]) -> Fed {
let first = Spec::first(parties, rules);
Fed {
- store: Store::new(root_of(&first), Vec::new()).expect("a root"),
+ store: Store::new(first.root(), Vec::new()).expect("a root"),
first,
}
}
@@ -381,7 +372,7 @@ fn federation_attack_12() {
for flip in [false, true] {
for backwards in [false, true] {
let mut store: Store<Federation> =
- Store::new(root_of(&first), Vec::new()).expect("a root");
+ Store::new(first.root(), Vec::new()).expect("a root");
let mut order: Vec<Vec<u8>> = before.clone();
let mut written = versions.clone();
if flip {
@@ -430,7 +421,7 @@ fn federation_attack_13() {
permutations(&mut (0..4).collect(), 0, &mut orders);
for order in orders {
let mut store: Store<Federation> =
- Store::new(root_of(&fed.first), Vec::new()).expect("a root");
+ Store::new(fed.first.root(), Vec::new()).expect("a root");
store.offer(proposal.clone());
for i in order {
store.offer(votes[i].clone());
@@ -738,8 +729,8 @@ fn federation_attack_37() {
assert_eq!(one, two);
assert!(fed.store.checks_fork(&[here], &spec, &one.0));
// One first version, one federation.
- assert_eq!(root_of(&fed.first), root_of(&fed.first.clone()));
- assert_eq!(Id::of(&root_of(&fed.first)), root);
+ assert_eq!(fed.first.root(), fed.first.clone().root());
+ assert_eq!(Id::of(&fed.first.root()), root);
}
#[test]
@@ -805,7 +796,7 @@ fn federation_attack_41() {
// A first version listing no node: a store no node is a member of.
let mut nobody = first.clone();
nobody.parties[0].nodes.clear();
- let store: Store<Federation> = Store::new(root_of(&nobody), Vec::new()).expect("a root");
+ let store: Store<Federation> = Store::new(nobody.root(), Vec::new()).expect("a root");
assert!(store.state().members().is_empty());
}
@@ -1045,7 +1036,7 @@ fn federation_attack_56() {
let v2 = harness::version_event(&[Id::of(&votes[2]), Id::of(&votes[3])], two.hash());
for (first, heads) in [(&v1, ia), (&v2, ib)] {
let mut store: Store<Federation> =
- Store::new(root_of(&fed.first), Vec::new()).expect("a root");
+ Store::new(fed.first.root(), Vec::new()).expect("a root");
for bytes in [&a, &b, &p1, &p2] {
store.offer(bytes.clone());
}
diff --git a/src/federation/harness.rs b/src/federation/harness.rs
index 80328dd..42ee870 100644
--- a/src/federation/harness.rs
+++ b/src/federation/harness.rs
@@ -248,4 +248,14 @@ impl Spec {
pub(crate) fn hash(&self) -> Id {
Id::of(&self.json().bytes())
}
+
+ /// The root of a federation whose first version this is.
+ pub(crate) fn root(&self) -> Vec<u8> {
+ event::Checkpoint {
+ at: Vec::new(),
+ keeps: Vec::new(),
+ version: super::version::Version::read(&self.json()).expect("a version"),
+ }
+ .bytes()
+ }
}
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 3c8bafb..ae2264b 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -791,19 +791,8 @@ async fn put_again(state: &crate::state::AppState) {
mod tests {
use super::*;
use crate::document::Document;
- use crate::federation::event::Checkpoint;
use crate::federation::harness::{self, Party, Spec};
use crate::federation::store::memory::Refusal;
- use crate::federation::version::Version;
-
- fn root_of(spec: &Spec) -> Vec<u8> {
- Checkpoint {
- at: Vec::new(),
- keeps: Vec::new(),
- version: Version::read(&spec.json()).expect("a version"),
- }
- .bytes()
- }
fn put(doc: &mut Document, event: &[u8]) {
events::put(doc, &[event]);
@@ -823,7 +812,7 @@ mod tests {
Party::new("makefu", 3),
);
let first = Spec::first(&[&tv, &lass, &makefu], &[]);
- let root = root_of(&first);
+ let root = first.root();
let on = first.hash();
let gone = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
let kept = tv.write(&[Id::of(&gone)], "home/tv/a", harness::str("2"));
@@ -875,7 +864,7 @@ mod tests {
fn an_operator_drops_what_its_node_took_and_allows_it_again() {
let tv = Party::new("tv", 1);
let first = Spec::first(&[&tv], &[]);
- let root = root_of(&first);
+ let root = first.root();
let write = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1"));
let on = tv.write(&[Id::of(&write)], "home/tv/x", harness::str("2"));
let holding = Holding::default();
@@ -905,7 +894,7 @@ mod tests {
fn a_node_puts_again_what_its_document_lacks() {
let tv = Party::new("tv", 1);
let first = Spec::first(&[&tv], &[]);
- let root = root_of(&first);
+ let root = first.root();
let write = tv.write(&[Id::of(&root)], "home/tv/x", harness::str("1"));
let on = tv.write(&[Id::of(&write)], "home/tv/x", harness::str("2"));
let mut left = events::started(Federation::NAME, &root).unwrap();
@@ -930,7 +919,7 @@ mod tests {
fn the_store_takes_the_events_the_document_holds() {
let tv = Party::new("tv", 1);
let first = Spec::first(&[&tv], &[]);
- let root = root_of(&first);
+ let root = first.root();
let mut doc = events::started(Federation::NAME, &root).unwrap();
let holding = Holding::default();
assert!(holding.unread(&Document::new(), 0).is_none());
@@ -972,7 +961,7 @@ mod tests {
Party::new("makefu", 3),
);
let first = Spec::first(&[&tv, &lass, &makefu], &[]);
- let root = root_of(&first);
+ let root = first.root();
let on = first.hash();
let one = first.after(on);
let mut two = first.after(on);
@@ -1032,7 +1021,7 @@ mod tests {
/// tv's federation, its root, and a document started from it.
fn tv_alone() -> (Party, Vec<u8>, Document) {
let tv = Party::new("tv", 1);
- let root = root_of(&Spec::first(&[&tv], &[]));
+ let root = Spec::first(&[&tv], &[]).root();
let doc = events::started(Federation::NAME, &root).unwrap();
(tv, root, doc)
}
@@ -1097,7 +1086,7 @@ mod tests {
fn store_attack_8() {
let (_, _, mut doc) = tv_alone();
let junk = b"{\"not\":\"an event\"}".to_vec();
- let other = root_of(&Spec::first(&[&Party::new("lass", 2)], &[]));
+ let other = Spec::first(&[&Party::new("lass", 2)], &[]).root();
put(&mut doc, &junk);
put(&mut doc, &other);
let holding = Holding::default();
@@ -1142,7 +1131,7 @@ mod tests {
let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2));
let makefu = Party::new("makefu", 3);
let first = Spec::first(&[&tv, &lass, &makefu], &[]);
- let root = root_of(&first);
+ let root = first.root();
let on = first.hash();
let head = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
let mut next = first.after(on);
diff --git a/src/federation/properties.rs b/src/federation/properties.rs
index 082fcac..36724ff 100644
--- a/src/federation/properties.rs
+++ b/src/federation/properties.rs
@@ -99,15 +99,6 @@ fn pick(rng: &mut Rng, ids: &[Id]) -> Id {
}
}
-fn root_of(spec: &Spec) -> Vec<u8> {
- Checkpoint {
- at: Vec::new(),
- keeps: Vec::new(),
- version: Version::read(&spec.json()).expect("a version"),
- }
- .bytes()
-}
-
fn generate(rng: &mut Rng, kind: Kind) -> History {
let count = rng.between(3, 5);
let parties: Vec<Party> = (0..count)
@@ -116,7 +107,7 @@ fn generate(rng: &mut Rng, kind: Kind) -> History {
let adversary = count - 1;
let refs: Vec<&Party> = parties.iter().collect();
let first = Spec::first(&refs, &[("names/{party}", harness::str("{party}"))]);
- let root = root_of(&first);
+ let root = first.root();
let root_id = Id::of(&root);
let mut world: Store<Federation> = Store::new(root.clone(), Vec::new()).expect("a root");
let mut specs: BTreeMap<Id, Spec> = BTreeMap::from([(first.hash(), first)]);