diff options
| author | tv <tv@krebsco.de> | 2026-11-16 23:37:55 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 01:02:34 +0100 |
| commit | 53212ecad04b3426dd2c694d527497d7c5148322 (patch) | |
| tree | e040ddd7a4921a14c16b6c0e3f9fcdcedc80f2f2 | |
| parent | 10f44bab6be521d7cebf23901b0502dd55246f12 (diff) | |
simtest: different events to different peers, read alike once together
`federation_attack_28`: tv, lass and makefu, a node each, apart while each is given its
own: two writes of tv's at one place, a write of lass's, and a proposal and the votes
that pass it, each on another node. Apart, each reads its own; together, they read the
same, the version that passed current, written by whichever node held it passed. The
simulations of swarms holding a store share their federation, their nodes started from
its root, and what they read of a node's store.
| -rw-r--r-- | src/simtest/holding.rs | 242 |
1 files changed, 205 insertions, 37 deletions
diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index cbf1942..657f019 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -1,7 +1,9 @@ -//! A swarm holding a store (`docs/store.md`, Members): nodes started from its root, or -//! joining by its ID, sync with the members its store names; the events put on any of -//! them reach all of them, and none reaches a node its party doesn't list. +//! Swarms holding a store (`docs/store.md`, Members), the federation its fold: nodes +//! started from its root, or joining by its ID, sync with the members its store names; +//! the events put on any of them reach all of them, and none reaches a node its party +//! doesn't list; and the attack rows marked sim, each by its test. +use std::collections::{BTreeMap, BTreeSet}; use std::sync::Arc; use std::time::Duration; @@ -10,33 +12,135 @@ use crate::document::events; use crate::federation::fold::Federation; use crate::federation::harness::{self, Party, Spec}; use crate::federation::held::{self, Holding}; +use crate::federation::jcs::Json; +use crate::federation::store::memory::Store; use crate::federation::store::{Fold, Id}; use crate::files::Files; use crate::node::NodeConfig; use crate::state::AppState; use crate::swarm::bootstrap::{Origin, Start}; -/// `config`, of a node of the swarm holding the store `id`, of `party`: started from the -/// store's `root` where given (`--store-root`), else joining by its ID (`--store-id`). -fn of_the_store(config: &mut NodeConfig, id: Id, party: &str, root: Option<&[u8]>) { - config.store = Some(id); - config.holding = Holding::open( +/// How long a test waits for what takes a few rounds of syncs, writes and reads. +const LONG: Duration = Duration::from_secs(30); + +/// A federation of `parties`, each listing the simulated nodes named with it: its first +/// version, its root, and its ID. +fn federation(parties: &[(&Party, &[&str])]) -> (Spec, Vec<u8>, Id) { + let named: Vec<&Party> = parties.iter().map(|(party, _)| *party).collect(); + let mut first = Spec::first(&named, &[]); + for (spec, (_, nodes)) in first.parties.iter_mut().zip(parties) { + spec.nodes = nodes.iter().map(|node| sim_key(node).id()).collect(); + } + let root = first.root(); + let id = Id::of(&root); + (first, root, id) +} + +/// tv, lass and makefu. +fn three() -> (Party, Party, Party) { + ( + Party::new("tv", 1), + Party::new("lass", 2), + Party::new("makefu", 3), + ) +} + +fn s(text: &str) -> Json { + harness::str(text) +} + +/// What a node of `party` keeps of its store, without a file (`--store-party`). +fn holding(party: &str) -> Holding { + Holding::open( None, Arc::new(Files::default()), None, Some(party.to_owned()), ) - .expect("no file to read"); + .expect("no file to read") +} + +/// `config`, of a node of the swarm holding the store `id`, keeping `holding`: started +/// from the store's `root` where given (`--store-root`), else joining by its ID +/// (`--store-id`). +fn of_the_store(config: &mut NodeConfig, id: Id, root: Option<&[u8]>, holding: Holding) { + config.store = Some(id); + config.holding = holding; if let Some(root) = root { config.initial_doc = events::started(Federation::NAME, root).expect("a root"); } } +/// Starts `nodes`, each by its name with what it keeps of its store, from the store's +/// `root`, the first one the others' seed, and waits until each holds the store. +async fn started(sim: &Sim, root: &[u8], nodes: Vec<(&str, Holding)>) -> Vec<AppState> { + let id = Id::of(root); + let first = nodes[0].0.to_owned(); + let states: Vec<AppState> = nodes + .into_iter() + .map(|(name, holding)| { + let seeds: Vec<&str> = if name == first { + Vec::new() + } else { + vec![first.as_str()] + }; + let root = root.to_vec(); + sim.start_with( + name, + &seeds, + Start::FromDocument(Origin::Root), + move |config| of_the_store(config, id, Some(&root), holding), + ) + }) + .collect(); + let all: Vec<&AppState> = states.iter().collect(); + sim.wait_ready(&all, SOON).await; + let all = &all; + sim.wait_until("every node holds the store", SOON, move || async move { + all.iter().all(|node| node.holding.tells_members()) + }) + .await; + states +} + +/// Puts `events` on `node`, each in a change of its own. +async fn put(node: &AppState, events: &[&[u8]]) { + for event in events { + held::write(node, None, event.to_vec()) + .await + .expect("an event put"); + } +} + +/// What `f` reads of `node`'s store, while it holds one. +fn read<T>(node: &AppState, f: impl FnOnce(&Store<Federation>) -> T) -> Option<T> { + node.holding.with(f) +} + /// Whether `node` holds every event of `ids`. fn holds(node: &AppState, ids: &[Id]) -> bool { - node.holding - .with(|store| ids.iter().all(|id| store.holds(id))) - == Some(true) + read(node, |store| ids.iter().all(|id| store.holds(id))) == Some(true) +} + +/// What counts, as `node`'s store reads: the current version, the values at places, and +/// the members; and the events it took after its checkpoint. +type Counts = ( + Id, + BTreeMap<String, Json>, + Vec<(String, String)>, + BTreeSet<Id>, +); + +fn counts(node: &AppState) -> Option<Counts> { + read(node, |store| { + let state = store.state(); + ( + state.current().hash, + state.values(), + state.members(), + store.taken().iter().copied().collect(), + ) + }) } /// tv lists a and b, which start from the root; lass lists c, which joins by the store's @@ -45,22 +149,19 @@ fn holds(node: &AppState, ids: &[Id]) -> bool { #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn the_events_put_on_a_member_reach_every_member_alone() { let sim = Sim::with_identity(81); - let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); - let mut first = Spec::first(&[&tv, &lass], &[]); - first.parties[0].nodes = vec![sim_key("a").id(), sim_key("b").id()]; - first.parties[1].nodes = vec![sim_key("c").id()]; - let root = first.root(); - let id = Id::of(&root); - let a = sim.start_with("a", &[], Start::FromDocument(Origin::Root), |config| { - of_the_store(config, id, "tv", Some(&root)) - }); - let b = sim.start_with("b", &["a"], Start::FromDocument(Origin::Root), |config| { - of_the_store(config, id, "tv", Some(&root)) - }); - let c = sim.start_with("c", &["a"], Start::Join, |config| { - of_the_store(config, id, "lass", None) - }); - let members = [&a, &b, &c]; + let (tv, lass, _) = three(); + let (_, root, id) = federation(&[(&tv, &["a", "b"]), (&lass, &["c"])]); + let mut nodes = started( + &sim, + &root, + vec![("a", holding("tv")), ("b", holding("tv"))], + ) + .await; + nodes.push(sim.start_with("c", &["a"], Start::Join, |config| { + of_the_store(config, id, None, holding("lass")) + })); + let [a, b, c] = [&nodes[0], &nodes[1], &nodes[2]]; + let members = [a, b, c]; sim.wait_ready(&members, SOON).await; sim.wait_until("a, b and c hold the store", SOON, move || async move { members.iter().all(|node| node.holding.tells_members()) @@ -68,23 +169,20 @@ async fn the_events_put_on_a_member_reach_every_member_alone() { .await; let d = sim.start_with("d", &["a"], Start::Join, |config| { - of_the_store(config, id, "lass", None) + of_the_store(config, id, None, holding("lass")) }); - let on_a = tv.write(&[id], "home/tv/x", harness::str("1")); - let on_c = lass.write(&[id], "home/lass/y", harness::str("2")); + let on_a = tv.write(&[id], "home/tv/x", s("1")); + let on_c = lass.write(&[id], "home/lass/y", s("2")); let ids = [Id::of(&on_a), Id::of(&on_c)]; - held::write(&a, None, on_a).await.expect("put on a"); - held::write(&c, None, on_c).await.expect("put on c"); + put(a, &[&on_a]).await; + put(c, &[&on_c]).await; sim.wait_until("a, b and c hold both events", SOON, move || async move { members.iter().all(|node| holds(node, &ids)) }) .await; sim.wait_converged(&members, SOON).await; for node in members { - assert_eq!( - node.holding.with(|store| store.state().values().len()), - Some(2) - ); + assert_eq!(read(node, |store| store.state().values().len()), Some(2)); } // Long enough for d to have joined, were it let in. tokio::time::sleep(Duration::from_secs(3)).await; @@ -92,3 +190,73 @@ async fn the_events_put_on_a_member_reach_every_member_alone() { assert!(!d.holding.tells_members()); assert!(events::ids(&d.doc.read().await.doc).is_empty()); } + +/// Different events to different peers: tv, lass and makefu, a node each, apart while +/// each is given its own: two writes of tv's at one place, a write of lass's, and a +/// proposal and the votes that pass it, each on another node. Together, they read the +/// same, the version that passed current, written by whichever held it passed. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn federation_attack_28() { + let sim = Sim::with_identity(82); + let (tv, lass, makefu) = three(); + let (first, root, id) = federation(&[(&tv, &["a"]), (&lass, &["b"]), (&makefu, &["c"])]); + let nodes = started( + &sim, + &root, + vec![ + ("a", holding("tv")), + ("b", holding("lass")), + ("c", holding("makefu")), + ], + ) + .await; + for name in ["a", "b", "c"] { + sim.net.take_down(name); + } + let on = first.hash(); + let mut next = first.after(on); + next.rules.push(("names/x".to_owned(), s("tv"))); + let proposal = makefu.propose(&[id], &next.json()); + let given = [ + [tv.write(&[id], "home/tv/x", s("a")), proposal.clone()], + [ + tv.write(&[id], "home/tv/x", s("b")), + tv.vote(&[Id::of(&proposal)], on, next.hash(), true), + ], + [ + lass.write(&[id], "home/lass/y", s("c")), + lass.vote(&[Id::of(&proposal)], on, next.hash(), true), + ], + ]; + for (node, events) in nodes.iter().zip(&given) { + put(node, &[&events[0], &events[1]]).await; + } + let (all, writes) = (&nodes, [0, 1, 2].map(|i| Id::of(&given[i][0]))); + sim.wait_until( + "each holds the write it was given", + SOON, + move || async move { + all.iter() + .zip(writes) + .all(|(node, write)| holds(node, &[write])) + }, + ) + .await; + // Apart, a and b read their own write at one place. + let at = |node: &AppState| read(node, |store| store.state().value("home/tv/x").cloned()); + assert_eq!(at(&nodes[0]), Some(Some(s("a")))); + assert_eq!(at(&nodes[1]), Some(Some(s("b")))); + + for name in ["a", "b", "c"] { + sim.net.bring_up(name); + } + let passed = next.hash(); + sim.wait_until("every node reads the same", LONG, move || async move { + let first = counts(&all[0]); + first.as_ref().is_some_and(|counts| counts.0 == passed) + && all.iter().all(|node| counts(node) == first) + }) + .await; + let counts = counts(&nodes[0]).expect("a store"); + assert!(writes.iter().all(|write| counts.3.contains(write))); +} |
