summaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/properties.rs124
1 files changed, 85 insertions, 39 deletions
diff --git a/src/federation/properties.rs b/src/federation/properties.rs
index 91e038e..2b824f0 100644
--- a/src/federation/properties.rs
+++ b/src/federation/properties.rs
@@ -159,6 +159,16 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>)
.iter()
.filter(|(id, ..)| known[w].contains(id))
.collect();
+ // Of the proposals it knows, more often than not the earliest on the version current
+ // where it writes, else any: so that parties vote alike and versions pass, of those
+ // this continuum doesn't read too.
+ let earliest = mine.iter().position(|(_, _, on, _)| *on == current.hash);
+ let chosen = |rng: &mut Rng| -> (Id, Id, Id, bool) {
+ match earliest {
+ Some(at) if rng.chance(0.6) => *mine[at],
+ _ => **rng.pick(&mine),
+ }
+ };
let roll = rng.below(100);
let event = if roll < 4 {
// A node merges: one its party lists there, more often than not, else any.
@@ -222,7 +232,7 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>)
}
}
} else if roll < 85 {
- let &&(proposal, hash, on, named) = rng.pick(&mine);
+ let (proposal, hash, on, named) = chosen(rng);
let yes = if kind.adversary && w == adversary {
rng.chance(0.7)
} else {
@@ -237,7 +247,7 @@ pub(super) fn generate_at(rng: &mut Rng, kind: Kind, revision: Option<Revision>)
votes.push((Id::of(&bytes), proposal));
bytes
} else if roll < 96 || !kind.adversary {
- let &&(proposal, hash, ..) = rng.pick(&mine);
+ let (proposal, hash, ..) = chosen(rng);
let on_it: Vec<Id> = votes
.iter()
.filter(|(vote, voted)| *voted == proposal && known[w].contains(vote))
@@ -347,54 +357,90 @@ fn verdict(offered: &Offered) -> Verdict {
}
}
+/// Whether the store judges and reads `history` as the model does, its events offered in
+/// the order written; and whether it stopped.
+fn judged_as_the_model(history: &History) -> Result<bool, String> {
+ let mut model = Model::new(&history.root);
+ let mut store: Store<Federation> =
+ Store::new(history.root.clone(), Vec::new()).expect("a root");
+ for event in &history.events {
+ let id = Id::of(event);
+ let stored = verdict(&store.offer(event.clone()));
+ let modelled = model.offer(event);
+ if stored != modelled {
+ return Err(format!(
+ "{id}: the store says {stored:?}, the model {modelled:?}"
+ ));
+ }
+ }
+ if reads(&store) != model_reads(&model) {
+ return Err(format!(
+ "the store reads {:?}, the model {:?}",
+ reads(&store),
+ model_reads(&model)
+ ));
+ }
+ Ok(store.stopped().is_some())
+}
+
+/// Seeds of the kinds with versions this continuum doesn't read scanned beyond the cases
+/// where none of those stopped a store, till one does.
+const SCANNED_FOR_A_STOP: u64 = 1000;
+
/// Where parties propose versions this continuum doesn't read too, the store stops where
/// the model does: written in order, events come after what they build on, and both stop
-/// at the same event, taking the same before it.
+/// at the same event, taking the same before it. Where none of the cases stops a store,
+/// later seeds are scanned for one that does, which is checked too: a stop is checked
+/// whatever the generator's luck.
#[test]
fn the_store_judges_and_reads_as_the_model() {
let stopped = std::cell::Cell::new(0);
- for (adversary, unread, cases) in [
+ let kinds = [
(false, false, 48),
(true, false, 48),
(false, true, 32),
(true, true, 32),
- ] {
- check(
- cases,
- Kind {
- adversary,
- checkpoints: false,
- unread,
- },
- |history| {
- let mut model = Model::new(&history.root);
- let mut store: Store<Federation> =
- Store::new(history.root.clone(), Vec::new()).expect("a root");
- for event in &history.events {
- let id = Id::of(event);
- let stored = verdict(&store.offer(event.clone()));
- let modelled = model.offer(event);
- if stored != modelled {
- return Err(format!(
- "{id}: the store says {stored:?}, the model {modelled:?}"
- ));
- }
- }
- if store.stopped().is_some() {
- stopped.set(stopped.get() + 1);
- }
- if reads(&store) != model_reads(&model) {
- return Err(format!(
- "the store reads {:?}, the model {:?}",
- reads(&store),
- model_reads(&model)
- ));
+ ];
+ let kind = |adversary, unread| Kind {
+ adversary,
+ checkpoints: false,
+ unread,
+ };
+ for (adversary, unread, cases) in kinds {
+ check(cases, kind(adversary, unread), |history| {
+ if judged_as_the_model(history)? {
+ stopped.set(stopped.get() + 1);
+ }
+ Ok(())
+ });
+ }
+ // Past the seeds of the cases of the kinds with versions this continuum doesn't read.
+ let mut seed = kinds
+ .iter()
+ .filter(|(_, unread, _)| *unread)
+ .map(|(_, _, cases)| *cases)
+ .max()
+ .unwrap_or(0);
+ while stopped.get() == 0 && seed < SCANNED_FOR_A_STOP {
+ for adversary in [false, true] {
+ let history = generate(&mut Rng::new(seed), kind(adversary, true));
+ match judged_as_the_model(&history) {
+ Ok(true) => stopped.set(stopped.get() + 1),
+ Ok(false) => {}
+ Err(why) => {
+ let (history, why) = cut_down(history, why, &|history: &History| {
+ judged_as_the_model(history).map(|_| ())
+ });
+ panic!("{why}, for seed {seed}, in the history\n{}", history.show());
}
- Ok(())
- },
- );
+ }
+ }
+ seed += 1;
}
- assert!(stopped.get() > 0, "no history stopped a store");
+ assert!(
+ stopped.get() > 0,
+ "no history of the first {SCANNED_FOR_A_STOP} seeds stopped a store"
+ );
}
#[test]