diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/attacks.rs | 16 | ||||
| -rw-r--r-- | src/federation/client.rs | 32 | ||||
| -rw-r--r-- | src/federation/held.rs | 70 | ||||
| -rw-r--r-- | src/federation/vote.rs | 48 |
4 files changed, 161 insertions, 5 deletions
diff --git a/src/federation/attacks.rs b/src/federation/attacks.rs index 0d90461..d3dc660 100644 --- a/src/federation/attacks.rs +++ b/src/federation/attacks.rs @@ -1,7 +1,8 @@ //! The attacks of `docs/federation.md` marked unit, each a test named by its row -//! (`docs/federation-tests.md`), on the federation's fold and the store in memory; those +//! (`docs/federation-tests.md`), on the federation's fold and the store in memory; of what +//! a node or a party's client does, beside that code (`held`, `client`, `vote`); those //! marked sim are simulations (`simtest::holding`). A test checks that every row has its -//! test, or waits for a later stage. +//! test, or waits. use super::event::Checkpoint; use super::fold::Federation; @@ -12,8 +13,8 @@ use super::store::memory::{Offered, Refusal, Store}; use super::store::{Fold, Id}; use super::version::Version; -/// Rows whose tests wait: what a node, a party's client or a tool does, and flooding. -const LATER: &[u32] = &[30, 39, 45, 51, 58, 63, 65, 68]; +/// Rows whose tests wait: what a node or a party's client doesn't do yet, and flooding. +const LATER: &[u32] = &[30, 39, 51, 63, 68]; fn tv() -> Party { Party::new("tv", 1) @@ -103,7 +104,12 @@ fn every_row_has_its_test() { harness::every_row_has_its_test( include_str!("../../docs/federation.md"), "federation_attack_", - &[include_str!("attacks.rs")], + &[ + include_str!("attacks.rs"), + include_str!("held.rs"), + include_str!("client.rs"), + include_str!("vote.rs"), + ], &[include_str!("../simtest/holding.rs")], LATER, ); diff --git a/src/federation/client.rs b/src/federation/client.rs index 88e370f..c5d3651 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -719,6 +719,38 @@ mod tests { assert!(again.contains(&Id::of(&later)) && again.contains(&Id::of(&keys))); } + /// Row 65 (`docs/federation.md`): after a checkpoint, a party backs a second proposal + /// once its first is dead, and those who killed the first withhold their nays: its + /// client kept them with its `true`, so its record doesn't name it as voting twice. + #[test] + fn federation_attack_65() { + let (tv, lass, makefu, first, current, one, two) = three(); + let root = Id::of(b"root"); + let mut record = Record::begin(root, "tv", ¤t); + record.began = Id::of(b"earlier").to_string(); + record.cast( + ¤t, + &tv.vote(&[root], first.hash(), one.hash(), true), + Vec::new(), + ); + let nays = [ + lass.vote(&[root], first.hash(), one.hash(), false), + makefu.vote(&[root], first.hash(), one.hash(), false), + ]; + let kept = record + .may_vote(¤t, two.hash(), &two.json(), true, &nays) + .unwrap(); + record.cast( + ¤t, + &tv.vote(&[root], first.hash(), two.hash(), true), + kept, + ); + assert!(voted_twice(std::slice::from_ref(&record)).is_empty()); + // Without the nays it kept, its two `true` are voting twice. + record.nays.clear(); + assert_eq!(voted_twice(&[record]).len(), 1); + } + /// A party backs, and proposes, only a version changing the current one whose /// checkpoint, where it names one, names none of the current checkpoint's heads. #[test] diff --git a/src/federation/held.rs b/src/federation/held.rs index abb3d30..c29dd0a 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -1539,6 +1539,76 @@ mod tests { assert!(Holding::open(Some(path), files, None, None).is_err()); } + /// `event` put into `doc` by the node `node`, in a change of its own, judged as every + /// node judges a change. + fn put_by(doc: &mut Document, node: &str, event: &[u8]) -> Result<(), String> { + use crate::document::replica::{Epoch, Writer, transact}; + use crate::document::typed::{TimestampFormat, Val}; + doc.set_actor(crate::document::ActorId::random()); + transact( + doc, + crate::clock::UnixMillis(1), + &Writer { node, user: None }, + |tx| { + store::write_val_path( + tx, + &meta::path(&[meta::EVENTS, &Id::of(event).to_string()]), + &Val::Scalar(ScalarValue::Bytes(event.to_vec())), + store::Writing::sticky(TimestampFormat::default()), + ) + .map(|_| ()) + }, + ) + .unwrap(); + let heads = doc.get_heads(); + crate::access::rules::judge_held(doc, Epoch::GENESIS, &heads[0]).unwrap() + } + + /// Row 45 (`docs/federation.md`): an honest node writes a version valid where it is + /// written, on the side this node doesn't read, of two changing one: refused by what + /// this node took before, reported apart (`own`); the change that put it is taken as + /// every node takes a change, so nothing is held against the node that put it. + #[test] + fn federation_attack_45() { + let (tv, lass, makefu) = ( + Party::new("tv", 1), + Party::new("lass", 2), + Party::new("makefu", 3), + ); + let first = Spec::first(&[&tv, &lass, &makefu], &[]); + let root = first.root(); + let on = first.hash(); + let one = first.after(on); + let mut two = first.after(on); + two.rules.push(("names/x".to_owned(), harness::str("tv"))); + let p1 = tv.propose(&[Id::of(&root)], &one.json()); + let p2 = lass.propose(&[Id::of(&root)], &two.json()); + let votes = [ + tv.vote(&[Id::of(&p1)], on, one.hash(), true), + lass.vote(&[Id::of(&p1)], on, one.hash(), true), + tv.vote(&[Id::of(&p2)], on, two.hash(), true), + makefu.vote(&[Id::of(&p2)], on, two.hash(), true), + ]; + let v1 = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], one.hash()); + let v2 = harness::version_event(&[Id::of(&votes[2]), Id::of(&votes[3])], two.hash()); + let holding = Holding::default(); + let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); + for event in [&p1, &p2].into_iter().chain(&votes).chain([&v1]) { + put(&mut doc, event); + } + read(&holding, &doc, 0); + assert_eq!(put_by(&mut doc, "node-b", &v2), Ok(())); + read(&holding, &doc, 0); + assert_eq!( + holding.with(|store| store.own()), + Some((vec![Id::of(&v2)], Vec::new())) + ); + assert_eq!( + holding.with(|store| store.state().current().hash), + Some(one.hash()) + ); + } + // The store's attack rows (`docs/store.md`, Attacks) read from a document. /// tv's federation, its root, and a document started from it. diff --git a/src/federation/vote.rs b/src/federation/vote.rs index 9bf7a1c..d4968be 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -512,6 +512,54 @@ mod tests { assert!(decide(&seen(&store, &held), &rules, &record).is_empty()); } + /// Row 58 (`docs/federation.md`): a party the node follows backs, or votes `false` on, + /// a version that changes more than moving to a checkpoint: by default the node follows + /// neither; one following on any version, as its operator chose, follows both. + #[test] + fn federation_attack_58() { + let (tv, _, _, first, mut store) = three(); + let root = store.id(); + let mut backed = first.after(first.hash()); + backed + .rules + .push(("names/x".to_owned(), harness::str("tv"))); + let mut opposed = first.after(first.hash()); + opposed + .rules + .push(("names/y".to_owned(), harness::str("tv"))); + let one = taken(&mut store, tv.propose(&[root], &backed.json())); + let two = taken(&mut store, tv.propose(&[root], &opposed.json())); + taken( + &mut store, + tv.vote(&[one], first.hash(), backed.hash(), true), + ); + taken( + &mut store, + tv.vote(&[two], first.hash(), opposed.hash(), false), + ); + let rules = Rules { + follow: vec!["tv".to_owned()], + ..Rules::default() + }; + let long = |_: &Id| Some(Duration::from_secs(3600)); + let record = record(&store, "makefu"); + assert!(decide(&seen(&store, &long), &rules, &record).is_empty()); + let any = Rules { + follow_any: true, + ..rules + }; + let acts = decide(&seen(&store, &long), &any, &record); + assert_eq!(acts.len(), 2); + assert!(acts.iter().any(|act| matches!( + act, + Act::Vote { proposal, yes: true, .. } if *proposal == backed.hash() + ))); + assert!(acts.iter().any(|act| matches!( + act, + Act::Vote { proposal, yes: false, .. } if *proposal == opposed.hash() + ))); + } + #[test] fn a_vote_by_hand_keeps_to_the_record() { let (tv, _, _, first, mut store) = three(); |
