aboutsummaryrefslogtreecommitdiffstats
path: root/src/simtest/holding.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 23:40:50 +0000
committertv <tv@krebsco.de>2026-11-17 01:02:38 +0100
commitfe0feb200097a674bfe2f439cda3096588d87cc3 (patch)
tree555c5dabad4da1db35e3252e6d7b4dbab0c44e7b /src/simtest/holding.rs
parent4bebeca3056739c88ea22d9c076d69861f2f717b (diff)
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.
Diffstat (limited to 'src/simtest/holding.rs')
-rw-r--r--src/simtest/holding.rs110
1 files changed, 110 insertions, 0 deletions
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<Id> {
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<Id> {
+ 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]));
+}