From fe0feb200097a674bfe2f439cda3096588d87cc3 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 23:40:50 +0000 Subject: simtest: keys voting twice part honest nodes; their operators bring them back `federation_attack_49`: tv backs a proposal of its own with lass, and one of lass's with makefu; b, apart, takes the other version than a and c. Together, they stay parted, each keeping the version it took. b's operator has it drop what it took and take the other side's (`Holding::drop_taken`, `allow`); or, where the versions named checkpoints, starts it again from the checkpoint the others moved to, pinned. No fork. --- src/simtest/holding.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) (limited to 'src') diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index b0e0442..956bd62 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -9,6 +9,7 @@ use std::time::Duration; use super::{SOON, Sim, sim_key}; use crate::document::events; +use crate::federation::event::Event; use crate::federation::fold::Federation; use crate::federation::harness::{self, Party, Spec}; use crate::federation::held::{self, Holding}; @@ -60,6 +61,18 @@ fn holding(party: &str) -> Holding { .expect("no file to read") } +/// What a node of `party` keeps of its store, starting from the checkpoint `pin`, found in +/// the document (`--store-checkpoint`). +fn pinned(party: &str, pin: Id) -> Holding { + Holding::open( + None, + Arc::new(Files::default()), + Some(pin), + Some(party.to_owned()), + ) + .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`). @@ -132,6 +145,19 @@ fn checkpoint(node: &AppState) -> Option { read(node, |store| Id::of(store.checkpoint())) } +/// The version event writing `version` that `node`'s store took after its checkpoint. +fn written(node: &AppState, version: Id) -> Option { + read(node, |store| { + store.taken().iter().copied().find(|id| { + matches!( + store.get(id).and_then(Event::read_taken), + Some(Event::Version { version: writes, .. }) if writes == version + ) + }) + }) + .flatten() +} + /// 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 = ( @@ -348,3 +374,87 @@ async fn store_attack_24() { assert_eq!(counts(&nodes[2]), counts(&nodes[0])); assert!(events::ids(&nodes[2].doc.read().await.doc).contains(&Id::of(&gone))); } + +/// Keys voting twice part honest nodes: tv backs a proposal of its own with lass, and one +/// of lass's with makefu; b, apart, takes the other version than a and c. Together, they +/// stay parted, and b's operator has it drop what it took and take the other side's; or, +/// where the versions named checkpoints, starts it again from the checkpoint the others +/// moved to, pinned. No fork. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn federation_attack_49() { + for (seed, checkpoints) in [(84, false), (85, true)] { + parted(seed, checkpoints).await; + } +} + +async fn parted(seed: u64, checkpoints: bool) { + let sim = Sim::with_identity(seed); + 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; + sim.net.take_down("b"); + let on = first.hash(); + let (mut one, mut two) = (first.after(on), first.after(on)); + two.rules.push(("names/x".to_owned(), s("tv"))); + let heads = [ + tv.write(&[id], "home/tv/a", s("1")), + lass.write(&[id], "home/lass/b", s("2")), + ]; + if checkpoints { + one.checkpoint = Some(vec![Id::of(&heads[0])]); + two.checkpoint = Some(vec![Id::of(&heads[1])]); + } + let p1 = tv.propose(&[Id::of(&heads[0])], &one.json()); + let p2 = lass.propose(&[Id::of(&heads[1])], &two.json()); + let ones = [&tv, &lass].map(|party| party.vote(&[Id::of(&p1)], on, one.hash(), true)); + let twos = [&tv, &makefu].map(|party| party.vote(&[Id::of(&p2)], on, two.hash(), true)); + put(&nodes[0], &[&heads[0], &p1, &ones[0], &ones[1]]).await; + put(&nodes[1], &[&heads[1], &p2, &twos[0], &twos[1]]).await; + let (all, one_hash, two_hash) = (&nodes, one.hash(), two.hash()); + sim.wait_until( + "a and c take one version, b the other", + LONG, + move || async move { + current(&all[0]) == Some(one_hash) + && current(&all[2]) == Some(one_hash) + && current(&all[1]) == Some(two_hash) + }, + ) + .await; + sim.net.bring_up("b"); + let members: Vec<&AppState> = nodes.iter().collect(); + sim.wait_converged(&members, LONG).await; + // Parted: each keeps the version it took. + assert_eq!(current(&nodes[0]), Some(one_hash)); + assert_eq!(current(&nodes[1]), Some(two_hash)); + assert_eq!(current(&nodes[2]), Some(one_hash)); + + let b = if checkpoints { + let pin = checkpoint(&nodes[0]).expect("a store"); + sim.restart_with("b", &["a"], Start::Join, move |config| { + of_the_store(config, id, None, pinned("lass", pin)) + }) + } else { + let took = written(&nodes[1], two_hash).expect("b wrote what it took"); + let other = written(&nodes[0], one_hash).expect("a wrote what it took"); + let dropped = nodes[1].holding.drop_taken(&[took]); + assert!(matches!(dropped, Some(Ok(_))), "{dropped:?}"); + assert!(nodes[1].holding.allow(&[other])); + nodes[1].clone() + }; + let b = &b; + sim.wait_until("b takes the others' version", LONG, move || async move { + current(b) == Some(one_hash) + }) + .await; + assert_eq!(checkpoint(b), checkpoint(&nodes[0])); +} -- cgit v1.3.1