diff options
Diffstat (limited to 'src/federation/held.rs')
| -rw-r--r-- | src/federation/held.rs | 70 |
1 files changed, 70 insertions, 0 deletions
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. |
