diff options
| -rw-r--r-- | docs/federation-tests.md | 10 | ||||
| -rw-r--r-- | src/federation/corpus.rs | 105 | ||||
| -rw-r--r-- | src/federation/properties.rs | 13 |
3 files changed, 109 insertions, 19 deletions
diff --git a/docs/federation-tests.md b/docs/federation-tests.md index a39e749..c22f300 100644 --- a/docs/federation-tests.md +++ b/docs/federation-tests.md @@ -128,9 +128,13 @@ every node must agree on ([`federation.md`](federation.md), What each revision takes, refuses and reads, as fixed bytes: for each revision this continuum knows, a file in `src/federation/corpus/` of histories generated at that -revision (of every kind the properties generate, a few seeds each), each event's bytes -with what became of it, by kind (taken, refused and why, by kind, waiting, held back, gone -with a move), and what the store reads after. A revision's file is written once, as the +revision (of every kind the properties generate, those of the first two seeds, and of the +next few hundred those showing what the ones kept don't: of each kind of event, what +became of one, a version written, a move, a stop; eight at most), each event's bytes with +what became of it, by kind (taken, refused and why, by kind, waiting, held back, gone with +a move), and what the store reads after. Together they show a version written, a move to +a checkpoint, a stop, and a merge event taken where the revision has merge events, else +refused. A revision's file is written once, as the revision is made (`write_the_corpus`, run by hand), and never edited after: every later continuum judges it alike (`every_revision_judges_as_its_corpus_says`), so that a change to judging that alters an earlier revision's comes as a later revision. Its first line diff --git a/src/federation/corpus.rs b/src/federation/corpus.rs index 27c1ab7..f19e2b1 100644 --- a/src/federation/corpus.rs +++ b/src/federation/corpus.rs @@ -6,19 +6,23 @@ //! change to judging that alters what an earlier revision's corpus says comes as a later //! revision. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Write as _; use std::path::PathBuf; +use super::event::{Does, Event}; use super::fold::Federation; -use super::properties::{self, Kind}; -use super::revision::Revision; +use super::properties::{self, History, Kind}; +use super::revision::{Feature, Revision}; use super::store::Id; use super::store::memory::{Refusal, Store}; use crate::testrand::Rng; -/// The seeds each kind of history is generated from. -const SEEDS: u64 = 4; +/// Of each kind, the seeds histories are generated from, the first so many kept +/// whatever they show, and at most how many kept. +const SCANNED: u64 = 400; +const FIRST: u64 = 2; +const MOST: usize = 8; /// The kinds of history generated: with an adversary or not, versions naming checkpoints /// or not, and versions this continuum doesn't read or not. @@ -105,13 +109,51 @@ fn reads(store: &Store<Federation>) -> String { .to_string() } -/// A case: a root, and its events offered in the order written; what each became, and -/// what the store reads after, as the corpus spells them. -fn case(label: &str, root: &[u8], events: &[Vec<u8>]) -> String { +/// A store from `root`, offered `events` in their order. +fn offered(root: &[u8], events: &[Vec<u8>]) -> Store<Federation> { let mut store = Store::new(root.to_vec(), Vec::new()).expect("a root"); for event in events { store.offer(event.clone()); } + store +} + +/// What a history shows of what a corpus is to hold: of each kind of event, what became +/// of one; and whether a version was written, the store moved to a checkpoint, or stopped. +fn shows(history: &History) -> BTreeSet<String> { + let store = offered(&history.root, &history.events); + let mut shown = BTreeSet::new(); + if store.state().current().number > 0 { + shown.insert("a version written".to_owned()); + } + if Id::of(store.checkpoint()) != store.id() { + shown.insert("a move to a checkpoint".to_owned()); + } + if store.stopped().is_some() { + shown.insert("a stop".to_owned()); + } + for event in &history.events { + let kind = match Event::read_taken(event) { + Some(Event::Signed(signed)) => match signed.does { + Does::Write { .. } => "a write", + Does::Keys(_) => "a key set", + Does::Nodes(_) => "a node list", + Does::Proposal { .. } => "a proposal", + Does::Vote { .. } => "a vote", + Does::Merge => "a merge event", + }, + Some(Event::Version { .. }) => "a version event", + None => "no event", + }; + shown.insert(format!("{kind}: {}", became(&store, &Id::of(event)))); + } + shown +} + +/// A case: a root, and its events offered in the order written; what each became, and +/// what the store reads after, as the corpus spells them. +fn case(label: &str, root: &[u8], events: &[Vec<u8>]) -> String { + let store = offered(root, events); let mut text = format!("case {label}\nroot {}\n", String::from_utf8_lossy(root)); for event in events { let _ = writeln!(text, "event {}", String::from_utf8_lossy(event)); @@ -121,14 +163,25 @@ fn case(label: &str, root: &[u8], events: &[Vec<u8>]) -> String { text } -/// `revision`'s corpus, generated: the histories of every kind and seed, the first -/// version following `revision`. +/// `revision`'s corpus, generated: of each kind, histories whose first version follows +/// `revision`, those of the first seeds, and of the rest those showing what the ones kept +/// don't (`shows`), up to `MOST`. fn generated(revision: Revision) -> String { let mut text = header(revision) + "\n"; for kind in kinds() { - for seed in 0..SEEDS { - let mut rng = Rng::new(seed); - let history = properties::generate_at(&mut rng, kind, Some(revision)); + let mut held = BTreeSet::new(); + let mut kept = 0; + for seed in 0..SCANNED { + if kept == MOST { + break; + } + let history = properties::generate_at(&mut Rng::new(seed), kind, Some(revision)); + let shown = shows(&history); + if seed >= FIRST && shown.is_subset(&held) { + continue; + } + held.extend(shown); + kept += 1; let label = format!("seed {seed}, {kind:?}"); text += &case(&label, &history.root, &history.events); } @@ -190,7 +243,9 @@ fn write_the_corpus() { } /// Every revision this continuum knows has its corpus, which it judges and reads as the -/// corpus says. +/// corpus says; and which shows a version written, a move to a checkpoint and a stop, and +/// what became of a merge event at that revision: taken where it has merge events, else +/// refused. #[test] fn every_revision_judges_as_its_corpus_says() { for revision in Revision::known() { @@ -211,9 +266,14 @@ fn every_revision_judges_as_its_corpus_says() { path.display() ); assert!(!cases.is_empty(), "{}: no case", path.display()); + let mut shown = BTreeSet::new(); for case in cases { let events: Vec<Vec<u8>> = case.events.iter().map(|(bytes, _)| bytes.clone()).collect(); let judged = self::case(&case.label, &case.root, &events); + shown.extend(shows(&History { + root: case.root.clone(), + events, + })); let (_, again) = parse(&format!("{named}\n{judged}")).expect("as written"); let again = &again[0]; for ((bytes, was), (_, is)) in case.events.iter().zip(&again.events) { @@ -234,5 +294,22 @@ fn every_revision_judges_as_its_corpus_says() { case.label ); } + let merged = if revision.has(Feature::MergeEvents) { + "a merge event: taken" + } else { + "a merge event: refused: judged" + }; + for wanted in [ + "a version written", + "a move to a checkpoint", + "a stop", + merged, + ] { + assert!( + shown.contains(wanted), + "{}: no case shows {wanted}", + path.display() + ); + } } } diff --git a/src/federation/properties.rs b/src/federation/properties.rs index b45b664..91e038e 100644 --- a/src/federation/properties.rs +++ b/src/federation/properties.rs @@ -161,8 +161,17 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>) .collect(); let roll = rng.below(100); let event = if roll < 4 { - // A node merges, its party's or not. - harness::merge(&party.name, rng.below(10) as u8, &deps) + // A node merges: one its party lists there, more often than not, else any. + let listed = world.state_at(&deps).nodes_of(&party.name, ¤t); + let ours: Vec<u8> = (0..10) + .filter(|seed| listed.contains(&harness::node_id(*seed))) + .collect(); + let seed = if !ours.is_empty() && rng.chance(0.75) { + *rng.pick(&ours) + } else { + rng.below(10) as u8 + }; + harness::merge(&party.name, seed, &deps) } else if roll < 35 || (roll >= 60 && mine.is_empty()) { let path = format!("home/{}/k{}", party.name, rng.below(3)); writer.write(&deps, &path, harness::str(&step.to_string())) |
