diff options
| author | tv <tv@krebsco.de> | 2026-11-19 04:24:52 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-19 05:27:32 +0100 |
| commit | 86e510f96659b3eb8c21de88107fd7b86cdde902 (patch) | |
| tree | 64052975767ed8a612c6338046cf2ae8413e619b /src/federation/corpus.rs | |
| parent | 2c9d42ed955705a0207b047918f58ef258dcfa4f (diff) | |
corpus: cases chosen by what they show, merges by nodes their party lists
The corpora written from four seeds of each kind showed little: of 32 histories, 4
versions written, no move and no stop, and every merge event refused, as the generator
signed them with any of ten node keys, so the corpus of revision 1 judged as the base
revision's did.
A generated merge event is signed by a node its party lists there, more often than not.
Of each kind, the corpus keeps the histories of the first two seeds, and of the next 400
those showing what the ones kept don't (`shows`: of each kind of event, what became of
one; a version written, a move, a stop), eight at most. The check asks that together a
revision's cases show a version written, a move to a checkpoint, a stop, and a merge event
taken where the revision has merge events, else refused.
Diffstat (limited to 'src/federation/corpus.rs')
| -rw-r--r-- | src/federation/corpus.rs | 105 |
1 files changed, 91 insertions, 14 deletions
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() + ); + } } } |
