From 9b20645df80363d922b25b8f2c0a531d060ff36f Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 22:53:32 +0000 Subject: access: what a node of a swarm holding a store takes leaves its events sound Three nodes write at random: events at their IDs, larger than the fold allows or not, again, or at other keys; strings and maps under the events; deletions; their entries or others'; and what is closed. Each now and then catches up with a node that takes their changes as it receives them (`screen::apply_bytes`). What it takes leaves at every key under `_continuum/events` the bytes whose ID it is, the root's among them; its document saves and loads; and another node, given all it took, takes it all. --- src/access/holding.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) (limited to 'src/access') diff --git a/src/access/holding.rs b/src/access/holding.rs index 3ff3e3e..2d083ba 100644 --- a/src/access/holding.rs +++ b/src/access/holding.rs @@ -349,4 +349,147 @@ mod tests { .is_err() ); } + + /// One to three writes `node` makes at random: events at their IDs, larger than the + /// fold allows or not, again, or at other keys; strings and maps under the events; + /// deletions; its entry or another's; and what is closed. `written` holds the events + /// put so far, by any node. + fn written_at_random( + doc: &mut Transaction<'_>, + rng: &mut crate::testrand::Rng, + node: &str, + written: &mut Vec>, + ) -> Result<(), StoreError> { + let largest = crate::federation::event::LARGEST; + for _ in 0..rng.between(1, 3) { + let fresh = |rng: &mut crate::testrand::Rng| { + let mut bytes = rng.bytes32().to_vec(); + bytes.resize( + if rng.chance(0.1) { + largest + 1 + } else { + rng.between(32, 200) + }, + b' ', + ); + bytes + }; + let some = rng.pick(written).clone(); + let key = Id::of(&some).to_string(); + let entry = |node: &str| meta::path(&[meta::MEMBERS, node]); + match rng.below(10) { + 0 | 1 => { + let bytes = fresh(rng); + put(doc, &Id::of(&bytes).to_string(), &bytes)?; + written.push(bytes); + } + 2 => put(doc, &key, &some)?, + 3 => put(doc, &key, &fresh(rng))?, + 4 => store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!("text"))?, + 5 => store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!({"x": 1}))?, + 6 => store::delete_path(doc, &meta::path(&[meta::EVENTS, &key]), false)?, + 7 => store::write_path(doc, &entry(node), &json!({"alias": node}))?, + 8 => store::write_path(doc, &entry("node-elsewhere"), &json!({}))?, + _ => store::write_path( + doc, + rng.pick(&[ + vec!["config".to_owned()], + meta::path(&[meta::SWARM]), + crate::access::policy::path(), + ]), + &json!(1), + )?, + } + } + Ok(()) + } + + /// Changes generated: three nodes write at random (`written_at_random`), each now and + /// then catching up with a node that takes their changes as it receives them + /// (`screen::apply_bytes`). What it takes leaves at every key under + /// `_continuum/events` the bytes whose ID it is, the root's among them; its document + /// saves and loads; and another node, given all it took, takes it all. + #[test] + fn what_a_node_takes_leaves_its_events_sound() { + use crate::access::screen::{Screening, apply_bytes}; + use crate::document::Change; + use std::cell::Cell; + let id = Id::of(ROOT); + let (taken, refused) = (Cell::new(0), Cell::new(0)); + let offer = + |doc: &mut Document, changes: &[Change], screening: &std::sync::Mutex| { + let bytes: Vec = changes + .iter() + .flat_map(|change| change.raw_bytes().to_vec()) + .collect(); + apply_bytes( + doc, + Epoch::GENESIS, + 0, + &bytes, + screening, + None, + &mut crate::document::PatchLog::inactive(), + ) + .unwrap(); + }; + crate::testrand::for_seeds(24, |rng| { + let screening = std::sync::Mutex::new(Screening::default().holding(Some(id))); + let mut ours = Document::new(); + offer( + &mut ours, + &[events::genesis("federation", ROOT)], + &screening, + ); + let nodes = ["node-a", "node-b", "node-c"]; + let mut writers: Vec = nodes.iter().map(|_| ours.fork()).collect(); + let mut written = vec![ROOT.to_vec()]; + for _ in 0..40 { + let at = rng.below(nodes.len()); + let writer = &mut writers[at]; + let before = writer.get_heads(); + let _ = transact( + writer, + UnixMillis(1), + &Writer { + node: nodes[at], + user: None, + }, + |doc| written_at_random(doc, rng, nodes[at], &mut written), + ); + let changes = writer.get_changes(&before); + for change in &changes { + let heads = ours.get_heads(); + offer(&mut ours, std::slice::from_ref(change), &screening); + let counted = if ours.get_heads() == heads { + &refused + } else { + &taken + }; + counted.set(counted.get() + 1); + } + if rng.chance(0.3) { + *writer = ours.fork(); + } + } + assert_eq!(events::bytes(&ours, &id).as_deref(), Some(ROOT)); + for event in events::ids(&ours) { + let bytes = events::bytes(&ours, &event).expect("bytes at an event's key"); + assert_eq!(Id::of(&bytes), event); + assert!(bytes.len() <= crate::federation::event::LARGEST); + } + let loaded = Document::load(&ours.save()).expect("it loads"); + assert_eq!(loaded.get_heads(), ours.get_heads()); + let mut other = Document::new(); + let screening = std::sync::Mutex::new(Screening::default().holding(Some(id))); + offer(&mut other, &ours.get_changes(&[]), &screening); + assert_eq!(other.get_heads(), ours.get_heads()); + }); + assert!( + taken.get() > 0 && refused.get() > 0, + "{} taken, {} refused", + taken.get(), + refused.get() + ); + } } -- cgit v1.3.1