use crate::metis::dot::RawDot;
extern crate alloc;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::num::NonZeroUsize;
use crate::kairos::Kairos;
use crate::metis::tests::support::dot as d;
use crate::metis::{
Cut, Dot, EpochAddress, EpochRefusal, Epochs, SealedEpoch, Stability, VersionVector, Vouched,
};
use super::fabric::{
Equivocator, Fabric, ForkedAdoption, ForkedCut, Note, Replaying, Then, Withholding,
};
use super::replica::Replica;
use super::{act, fleet_of};
const ROSTER: [u32; 3] = [1, 2, 3];
const DECLARATION: (u32, u64) = (1, 2);
const LIAR: u32 = 3;
const SURVIVING: [u32; 2] = [1, 2];
const LIAR_TRUTH: u64 = 1;
fn declaration_dot() -> Dot {
d(DECLARATION.0, DECLARATION.1)
}
fn liar_dot(counter: u64) -> Dot {
d(LIAR, counter)
}
fn vector(pairs: &[(u32, u64)]) -> VersionVector {
let mut vector = VersionVector::new();
for &(station, counter) in pairs {
vector.observe(station, counter);
}
vector
}
fn base() -> Cut {
Cut::from_witnessed(vector(&[(1, 1), (2, 1), (3, 1)]))
}
fn delivered() -> Cut {
Cut::from_witnessed(vector(&[(1, 2), (2, 1), (3, 1)]))
}
fn address() -> EpochAddress {
EpochAddress::try_from_parts(1, DECLARATION).expect("a well-formed address")
}
fn round(own: u32, own_counter: u64, liar_says: u64) -> (Epochs, Option<SealedEpoch>) {
let mut stability = Stability::new(ROSTER);
for &station in &ROSTER {
stability.report_cut(station, &base()).expect("on roster");
}
let mut epochs = Epochs::new(ROSTER, NonZeroUsize::new(2).expect("positive"));
let declaration = epochs
.declare(
declaration_dot(),
Kairos::new(2, 0, 1, 0u16),
&stability,
&Cut::bottom(),
)
.expect("the settled watermark licenses the declaration");
let epoch = declaration.address();
for &station in &ROSTER {
stability
.report_cut(station, &delivered())
.expect("on roster");
epochs
.confirm(epoch, &Vouched::trust(station, delivered()))
.expect("a confirmation for the delivered declaration");
}
let _ = epochs
.adopt(own, own_counter, &stability)
.expect("the confirmation round is complete under the watermark");
for &station in &ROSTER {
if station == own {
continue;
}
let counter = if station == LIAR {
liar_says
} else {
delivered().as_vector().get(station)
};
epochs
.adopt_report(epoch, &Vouched::trust(station, counter))
.expect("an adoption report for a delivered candidate");
}
let sealed = epochs.try_seal(&stability).cloned();
(epochs, sealed)
}
#[test]
fn an_honest_round_seals_one_record() {
let (first, sealed_first) = round(1, 2, LIAR_TRUTH);
let (second, sealed_second) = round(2, 1, LIAR_TRUTH);
assert_eq!(
sealed_first, sealed_second,
"honest members seal the same record"
);
let sealed = sealed_first.expect("the round seals");
assert_eq!(
sealed.sealed_join().get(LIAR),
LIAR_TRUTH,
"the join carries the liar's honest counter"
);
for machine in [&first, &second] {
assert_eq!(
machine.recognize(address(), liar_dot(LIAR_TRUTH)),
Ok(()),
"both grant the duplicate verdict for a covered dot"
);
}
}
#[test]
fn an_overstated_adoption_wedges_the_window() {
let (_, honest) = round(1, 2, LIAR_TRUTH);
let (victim, overstated) = round(2, 1, LIAR_TRUTH + 4);
assert!(honest.is_some(), "the undeceived member seals");
assert!(
overstated.is_none(),
"an overstated join cannot clear the watermark, so nothing seals"
);
assert_eq!(
victim.recognize(address(), liar_dot(LIAR_TRUTH)),
Err(EpochRefusal::WindowOpen { open: address() }),
"the victim's window is still open: wedged, not diverged"
);
}
#[test]
fn an_understated_adoption_diverges_the_seal_record() {
let (honest, sealed_honest) = round(1, 2, LIAR_TRUTH);
let (victim, sealed_victim) = round(2, 1, LIAR_TRUTH - 1);
let sealed_honest = sealed_honest.expect("the undeceived member seals");
let sealed_victim = sealed_victim.expect("the understated join seals too: nothing refuses it");
assert_ne!(
sealed_honest, sealed_victim,
"one voice saying two things splits the sealed record"
);
assert_eq!(sealed_honest.sealed_join().get(LIAR), LIAR_TRUTH);
assert_eq!(
sealed_victim.sealed_join().get(LIAR),
LIAR_TRUTH - 1,
"the victim's join carries the counter it was told"
);
assert_eq!(
sealed_honest.declaration(),
sealed_victim.declaration(),
"the winner still agrees: the split is in the join, not the choice"
);
assert_eq!(honest.recognize(address(), liar_dot(LIAR_TRUTH)), Ok(()));
assert_eq!(
victim.recognize(address(), liar_dot(LIAR_TRUTH)),
Err(EpochRefusal::AddressMiss {
epoch: address(),
dot: RawDot::new(LIAR, LIAR_TRUTH),
}),
"the victim refuses what its peer grants"
);
}
#[test]
fn an_understated_adoption_halts_the_consumer_at_the_consignment_door() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0002, &ROSTER, 0);
fabric.corrupt(
Equivocator::within(1).speaking(LIAR, Box::new(ForkedAdoption { to: 2, delta: -1 })),
);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
let halted: Vec<u32> = fleet
.values()
.filter(|replica| replica.halted().is_some())
.map(super::replica::Replica::id)
.collect();
assert!(
!halted.is_empty(),
"the consignment door must refuse a divergent sealed join, not fold it"
);
assert!(
halted.contains(&2),
"the deceived member is the one that halts, got {halted:?}"
);
}
#[test]
#[should_panic(expected = "may not exceed its declared budget")]
fn the_fault_set_refuses_to_outgrow_its_budget() {
let _ = Equivocator::within(1)
.speaking(2, Box::new(ForkedAdoption { to: 1, delta: -1 }))
.speaking(3, Box::new(ForkedAdoption { to: 1, delta: -1 }));
}
fn wedged_fleet() -> (
alloc::collections::BTreeMap<u32, super::replica::Replica>,
Fabric,
) {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0002, &ROSTER, 0);
fabric.corrupt(
Equivocator::within(1).speaking(LIAR, Box::new(ForkedAdoption { to: 2, delta: -1 })),
);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
(fleet, fabric)
}
#[test]
fn the_wedge_is_durable_and_no_shipped_door_recovers_it() {
let (mut fleet, mut fabric) = wedged_fleet();
let wedged = fleet.get(&2).expect("roster member");
assert!(wedged.halted().is_some(), "station 2 is wedged");
assert_eq!(
(wedged.generation(), wedged.epochs().generation()),
(1, 2),
"the ledger stands exactly one generation ahead of the plane"
);
assert!(
wedged.joiner_checkpoint().is_none(),
"a wedged replica cannot offer the sealed checkpoint the bootstrap door needs"
);
for &station in &[1u32, 3] {
assert!(
fleet[&station].halted().is_none(),
"station {station} was told the truth and crossed the seal"
);
}
let journal = fleet[&2].journal().clone();
let _ = fleet.insert(
2,
super::replica::Replica::rehydrate_tolerating_refusals(
2,
&ROSTER,
NonZeroUsize::new(2).expect("positive"),
&journal,
),
);
super::resync(&mut fabric, &fleet, 2);
fabric.drain(&mut fleet);
let restarted = fleet.get(&2).expect("roster member");
assert!(
restarted.halted().is_some(),
"restart re-derives the divergent seal: the wedge is durable, not volatile"
);
assert_ne!(
fleet[&1].epochs(),
fleet[&2].epochs(),
"the ledgers stay split across the restart"
);
}
#[test]
fn a_divergent_seal_record_never_reaches_the_certifiable_grade() {
let (fleet, _) = wedged_fleet();
let wedged = fleet.get(&2).expect("roster member");
assert!(wedged.halted().is_some(), "station 2 is wedged");
let retired = wedged
.epochs()
.sealed()
.next()
.expect("the wedged member retired the divergent record");
assert_eq!(
retired.sealed_join().get(LIAR),
LIAR_TRUTH - 1,
"the retired record carries the counter the liar forked"
);
assert_eq!(
wedged.certified().count(),
0,
"the divergent record reached the certifiable grade"
);
for &station in &[1u32, 3] {
let certified: Vec<&SealedEpoch> = fleet[&station].certified().collect();
assert_eq!(
certified.len(),
1,
"station {station} accepted exactly one consignment"
);
assert_eq!(
certified[0].sealed_join().get(LIAR),
LIAR_TRUTH,
"station {station} certified a record carrying the honest counter"
);
}
assert_eq!(
fleet[&1].certified().collect::<Vec<_>>(),
fleet[&3].certified().collect::<Vec<_>>(),
"the certifiable grade must agree across every member that holds it"
);
}
#[test]
fn a_composed_adversary_still_cannot_fold_divergent_state() {
fn is_confirm(note: &Note) -> bool {
matches!(note, Note::Confirm { .. })
}
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0003, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(Then(
Replaying::of(is_confirm),
ForkedAdoption { to: 2, delta: -1 },
)),
));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
let crossed: Vec<u32> = fleet
.values()
.filter(|replica| replica.halted().is_none() && replica.generation() > 1)
.map(super::replica::Replica::id)
.collect();
let halted: Vec<u32> = fleet
.values()
.filter(|replica| replica.halted().is_some())
.map(super::replica::Replica::id)
.collect();
assert_eq!(
crossed,
[1, 3],
"the truthfully-told members must really cross, or the law below is vacuous"
);
assert_eq!(
halted,
[2],
"the deceived member must really halt, or the composition reached nothing"
);
for &station in &crossed {
assert_eq!(
fleet[&station].text().store().to_bytes(),
fleet[&crossed[0]].text().store().to_bytes(),
"station {station} folded a document plane its fellow crossers do not hold"
);
assert_eq!(
fleet[&station].epochs(),
fleet[&crossed[0]].epochs(),
"station {station} crossed on a ledger its fellow crossers do not hold"
);
}
}
#[test]
fn withholding_from_one_peer_freezes_the_whole_fleet() {
fn is_report(note: &Note) -> bool {
matches!(note, Note::Report { .. })
}
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0004, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(Withholding {
from: 1,
kind: is_report,
}),
));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the watermark it had before the withholding bit");
fabric.drain(&mut fleet);
for &station in &ROSTER {
let replica = &fleet[&station];
assert_eq!(
replica.generation(),
1,
"station {station} sealed under a withheld watermark"
);
assert!(
replica.halted().is_none(),
"station {station} halted: the freeze must be a stall, never a refusal"
);
assert!(
replica.epochs().sealed().next().is_none(),
"station {station} retired a seal the frozen round cannot license"
);
}
}
#[test]
fn abandoning_the_withholder_releases_the_meet_and_cures_this_freeze() {
fn is_report(note: &Note) -> bool {
matches!(note, Note::Report { .. })
}
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0005, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(Withholding {
from: 1,
kind: is_report,
}),
));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the watermark it had before the withholding bit");
fabric.drain(&mut fleet);
let pinned = fleet[&1].watermark();
for &station in &SURVIVING {
let abandoned = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.abandon(LIAR, out)
});
assert_eq!(abandoned.station(), LIAR);
assert_eq!(
abandoned.counter(),
1,
"the bound is what the survivors vouched for of the withholder's minting"
);
}
fabric.drain(&mut fleet);
assert!(
fleet[&1].watermark() > pinned,
"the meet is released: every watermark consumer moves again"
);
let survivors: Vec<&Replica> = SURVIVING.iter().map(|station| &fleet[station]).collect();
for replica in &survivors {
assert_eq!(
replica.generation(),
2,
"and the rounds, which were never short, complete and seal"
);
assert!(
replica.halted().is_none(),
"with no replica wedged at the consignment door"
);
}
assert_eq!(
fleet[&2].resurgent().collect::<Vec<_>>(),
[LIAR],
"the survivor still being reported to observes the departed member speaking"
);
assert!(
fleet[&1].resurgent().next().is_none(),
"the survivor the lie targeted observes nothing, on the same fleet at the same moment"
);
let first: Vec<&SealedEpoch> = survivors[0].certified().collect();
assert!(!first.is_empty(), "a record reached the certifiable grade");
for replica in &survivors[1..] {
assert_eq!(
replica.certified().collect::<Vec<_>>(),
first,
"and every survivor accepted the identical record"
);
}
assert_eq!(
first[0].sealed_join().get(LIAR),
1,
"carrying the withholder's own coordinate, because it reported on this channel"
);
}
#[test]
fn attesting_the_silent_members_departure_carries_the_fleet_through_the_seal() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0006, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
fabric.sever(&[LIAR]);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let replica = &fleet[&station];
assert_eq!(
replica.generation(),
1,
"station {station} sealed a window the silent member never confirmed"
);
assert!(replica.epochs().sealed().next().is_none());
}
for &station in &SURVIVING {
let prefix = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding of the silent member's traffic");
assert_eq!(
prefix, LIAR_TRUTH,
"station {station} proposes exactly the prefix it holds"
);
}
fabric.drain(&mut fleet);
let survivors: Vec<&Replica> = SURVIVING.iter().map(|station| &fleet[station]).collect();
for replica in &survivors {
assert_eq!(
replica.attested(LIAR),
Some(1),
"the departure crossed the boundary at the compacted prefix the \
new base carries for the evictee (its surviving element), never \
at the old plane's number"
);
assert_eq!(
replica.generation(),
2,
"and the rounds the silence froze complete and seal"
);
assert!(
replica.halted().is_none(),
"with no replica wedged at the consignment door: the bound the \
record names is one the survivors hold"
);
}
let first: Vec<&SealedEpoch> = survivors[0].certified().collect();
assert!(!first.is_empty(), "a record reached the certifiable grade");
for replica in &survivors[1..] {
assert_eq!(
replica.certified().collect::<Vec<_>>(),
first,
"and every survivor accepted the identical record"
);
}
assert_eq!(
first[0].sealed_join().get(LIAR),
LIAR_TRUTH,
"carrying the agreed bound, which no self-report of the silent \
member's supplied"
);
for replica in &survivors {
assert!(
replica.resurgences().next().is_none(),
"and nothing arrived above the fence, so no verdict is claimed"
);
}
}
#[test]
fn a_departed_member_that_speaks_again_is_refused_at_the_fence() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0007, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
fabric.sever(&[LIAR]);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
}
fabric.drain(&mut fleet);
let sealed: Vec<&SealedEpoch> = fleet[&1].certified().collect();
let record = sealed[0].clone();
assert_eq!(record.sealed_join().get(LIAR), LIAR_TRUTH);
let _ = act(&mut fabric, &mut fleet, LIAR, |replica, out| {
replica.insert_visible(9, out)
});
fabric.heal();
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let replica = &fleet[&station];
let verdicts: Vec<&crate::metis::Fenced> = replica.resurgences().collect();
assert_eq!(
verdicts.len(),
1,
"station {station} recorded exactly one resurgence verdict"
);
assert_eq!(verdicts[0].station, LIAR);
assert!(
verdicts[0].counter > record.sealed_join().get(LIAR),
"and the dot it names is one the sealed record says does not exist"
);
assert!(
verdicts[0].is_resurgence(),
"a sealed fence refused it, so it is evidence rather than a prompt"
);
assert!(
replica.halted().is_none(),
"station {station} halted: the fence is what keeps this a refusal \
at the door rather than a wedge one door later"
);
assert_eq!(
replica.certified().collect::<Vec<_>>(),
[&record],
"and the record it accepted is untouched"
);
}
}
#[test]
fn a_crash_between_the_promise_and_the_seal_does_not_lower_the_fence() {
let horizon = NonZeroUsize::new(2).expect("positive");
let mut fleet = fleet_of(&ROSTER, horizon);
let mut fabric = Fabric::new(0xB17E_0008, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
fabric.sever(&[LIAR]);
let promised = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
assert_eq!(promised, LIAR_TRUTH);
super::crash(&mut fleet, &ROSTER, horizon, 1);
let restarted = &fleet[&1];
assert_eq!(
restarted.departure_fence(LIAR),
Some(promised),
"the promise came back at the value it was emitted at"
);
assert!(
restarted.attested(LIAR).is_none(),
"and the round is still open: a restart re-enters, it does not seal"
);
let _ = act(&mut fabric, &mut fleet, 2, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
for &station in &SURVIVING {
assert_eq!(
fleet[&station].attested(LIAR),
Some(1),
"station {station} sealed and carried the departure across, at \
the compacted prefix the new base holds of the evictee"
);
assert!(fleet[&station].certified().next().is_some());
}
}
#[test]
fn two_departures_survive_a_restart_in_the_order_they_were_agreed() {
let horizon = NonZeroUsize::new(2).expect("positive");
let mut fleet = fleet_of(&ROSTER, horizon);
let mut fabric = Fabric::new(0xB17E_000A, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.open_departure(2, out)
})
.expect("a gap-free holding");
fabric.drain(&mut fleet);
assert_eq!(fleet[&1].attested(LIAR), Some(1));
assert_eq!(fleet[&1].attested(2), Some(1));
super::crash(&mut fleet, &ROSTER, horizon, 1);
let restarted = &fleet[&1];
assert_eq!(
restarted.attested(LIAR),
Some(1),
"the earlier departure came back"
);
assert_eq!(
restarted.attested(2),
Some(1),
"and so did the one agreed over the family the first one left"
);
assert_eq!(restarted.departure_fence(LIAR), Some(1));
assert_eq!(restarted.departure_fence(2), Some(1));
}
#[test]
fn a_departing_members_native_traffic_does_not_ride_the_seal() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0009, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
for &station in &SURVIVING {
assert_eq!(
fleet[&station].generation(),
2,
"station {station} should have sealed over the substituted bound"
);
}
assert!(
fleet[&LIAR].adopted() || fleet[&LIAR].generation() == 2,
"the evicted member adopted the epoch it was never told it left"
);
let native = act(&mut fabric, &mut fleet, LIAR, |replica, out| {
replica.insert_visible(0, out)
});
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let replica = &fleet[&station];
assert!(
!replica.effective_order().contains(&native),
"station {station} folded the evicted member's successor-plane \
dot {native:?}"
);
assert!(
replica.halted().is_none(),
"station {station} halted: the refusal must be at the fence, not \
at the consignment door"
);
assert!(
replica.resurgences().any(|verdict| verdict.station == LIAR),
"station {station} refused it silently rather than as a verdict"
);
}
}
#[test]
fn successor_traffic_minted_before_the_round_seals_is_still_refused() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_000B, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
let mut without_the_targets_adoption =
|_: u32, note: &Note| !matches!(note, Note::Adoption { station: LIAR, .. });
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
for &station in &SURVIVING {
assert!(
fleet[&station].adopted(),
"station {station} must be holding the window open"
);
}
assert_eq!(
fleet[&LIAR].generation(),
2,
"and the target, which folds its own report, has already crossed: \
everything it mints now belongs to the successor plane"
);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
let native = act(&mut fabric, &mut fleet, LIAR, |replica, out| {
replica.insert_visible(0, out)
});
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
assert!(
fleet[&1].attested(LIAR).is_none(),
"the round is still open when that dot arrives, which is the point"
);
assert!(
!fleet[&1].effective_order().contains(&native),
"and it is held out anyway: the successor fence does not wait for the \
seal"
);
let _ = act(&mut fabric, &mut fleet, 2, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("a gap-free holding");
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
for &station in &SURVIVING {
let replica = &fleet[&station];
assert!(
!replica.effective_order().contains(&native),
"station {station} carried the departing member's successor-plane \
dot {native:?} across the boundary"
);
assert!(
replica.halted().is_none(),
"station {station} wedged at the consignment door, which is exactly \
what the fence exists to prevent"
);
assert_eq!(
replica.attested(LIAR),
Some(1),
"and the departure crossed the boundary at the base's compacted \
prefix for the evictee"
);
assert_eq!(
replica.generation(),
2,
"the attestation supplied the report the target never sent"
);
}
}
#[test]
fn successor_traffic_folded_before_the_eviction_blocks_it_rather_than_wedging() {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_000C, &ROSTER, 0);
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
let mut without_the_targets_adoption =
|_: u32, note: &Note| !matches!(note, Note::Adoption { station: LIAR, .. });
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
let native = act(&mut fabric, &mut fleet, LIAR, |replica, out| {
replica.insert_visible(0, out)
});
fabric.drain_selected(&mut fleet, &mut without_the_targets_adoption);
assert!(
fleet[&1].effective_order().contains(&native),
"the survivor folded it, lawfully: there was no departure to fence it"
);
let refusal = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect_err("a survivor already holding successor traffic cannot propose");
assert!(
matches!(
refusal,
crate::metis::DepartureRefusal::SuccessorHeld { station: LIAR, .. }
),
"and it says why: {refusal:?}"
);
assert!(
fleet[&1].departure_fence(LIAR).is_none(),
"nothing was fenced, so nothing has to be unfenced"
);
fabric.drain(&mut fleet);
for &station in &SURVIVING {
assert_eq!(fleet[&station].generation(), 2);
let prefix = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.open_departure(LIAR, out)
})
.expect("the plane that held the dot is now the old one");
assert!(
prefix >= 1,
"station {station} proposes a prefix that covers what it holds"
);
}
fabric.drain(&mut fleet);
for &station in &SURVIVING {
let replica = &fleet[&station];
assert!(
replica.attested(LIAR).is_some(),
"station {station} completed the eviction one boundary later"
);
assert!(
replica.halted().is_none(),
"and no replica was wedged getting there"
);
}
}
#[test]
fn a_forked_confirmation_cannot_hide_its_own_declaration() {
let mut stability = Stability::new(ROSTER);
for &station in &ROSTER {
stability.report_cut(station, &base()).expect("on roster");
}
let mut epochs = Epochs::new(ROSTER, NonZeroUsize::new(2).expect("positive"));
let declaration = epochs
.declare(
declaration_dot(),
Kairos::new(2, 0, 1, 0u16),
&stability,
&Cut::bottom(),
)
.expect("the settled watermark licenses the declaration");
let epoch = declaration.address();
let understated = Cut::from_witnessed(vector(&[(1, DECLARATION.1 - 1), (2, 1), (3, 1)]));
assert_eq!(
epochs.confirm(epoch, &Vouched::trust(LIAR, understated)),
Err(EpochRefusal::ConfirmationDoesNotCover {
epoch,
station: LIAR,
covered: DECLARATION.1 - 1,
}),
"a confirmation below the declaration it names must be refused, or the \
candidate set stops being complete at the latch"
);
assert_eq!(
epochs.confirm(epoch, &Vouched::trust(LIAR, delivered())),
Ok(()),
"the honest cut confirms"
);
}
#[test]
fn a_forked_confirmation_either_freezes_the_fleet_or_changes_nothing() {
fn is_confirmation(note: &Note) -> bool {
matches!(note, Note::Confirm { .. })
}
for (at, delta, freezes) in [
(2u32, -1i64, false),
(2, 1, true),
(1, 1, true),
(3, 1, true),
] {
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0x5EED_0C17, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(ForkedCut {
to: 2,
kind: is_confirmation,
at,
delta,
}),
));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
for &station in &[1u32, LIAR] {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.try_declare(out)
})
.expect("the settled watermark licenses the declaration");
}
fabric.drain(&mut fleet);
for &station in &ROSTER {
let replica = &fleet[&station];
assert!(
replica.halted().is_none(),
"at {at} delta {delta}: station {station} halted; \
a confirmation fork must freeze or pass, never diverge"
);
if freezes {
assert!(
station != 2 || replica.epochs().fixed().is_none(),
"at {at} delta {delta}: the deceived member latched over an inflated join"
);
assert_eq!(
replica.generation(),
1,
"at {at} delta {delta}: station {station} latched over an inflated join"
);
assert!(
replica.epochs().sealed().next().is_none(),
"at {at} delta {delta}: station {station} retired a seal \
the frozen round cannot license"
);
} else {
assert_eq!(
replica.generation(),
2,
"at {at} delta {delta}: station {station} did not cross"
);
}
}
if !freezes {
let witness = fleet[&1]
.epochs()
.sealed()
.next()
.expect("the seal")
.clone();
for &station in &ROSTER {
let sealed = fleet[&station]
.epochs()
.sealed()
.next()
.expect("every member sealed");
assert_eq!(
sealed, &witness,
"at {at} delta {delta}: station {station} sealed a record its peers do not hold"
);
}
}
}
}
#[test]
fn a_forked_report_cannot_force_a_seal_the_liar_has_not_licensed() {
fn is_report(note: &Note) -> bool {
matches!(note, Note::Report { .. })
}
const UNHELD_STATION: u32 = 2;
let unheld = d(UNHELD_STATION, 1);
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0x5EED_0C17, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(Then(
ForkedCut {
to: 1,
kind: is_report,
at: UNHELD_STATION,
delta: 1,
},
ForkedCut {
to: 2,
kind: is_report,
at: UNHELD_STATION,
delta: 1,
},
)),
));
let held = |dst: u32, note: &Note| matches!(note, Note::Old { dots, .. } if dst == LIAR && dots.contains(&unheld));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain_selected(&mut fleet, |dst, note| !held(dst, note));
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the watermark the lie inflated");
fabric.drain_selected(&mut fleet, |dst, note| !held(dst, note));
assert_eq!(
fabric.in_flight(),
1,
"exactly the withheld delta waits: the liar is genuinely behind"
);
for &station in &[1u32, 2] {
assert!(
fleet[&station].epochs().fixed().is_some(),
"station {station} did not latch: the forked report reached nothing"
);
}
assert!(
fleet[&LIAR].epochs().fixed().is_none(),
"the liar latched under its own honest meet"
);
for &station in &ROSTER {
let replica = &fleet[&station];
assert_eq!(
replica.generation(),
1,
"station {station} sealed on stability no member licensed"
);
assert!(
replica.epochs().sealed().next().is_none(),
"station {station} retired a seal the incomplete adoption round cannot license"
);
assert!(
replica.halted().is_none(),
"station {station} halted: the block must be a stall, never a refusal"
);
}
}
#[test]
fn withholding_the_covering_delta_freezes_instead_of_hiding_the_lie() {
fn is_old_plane(note: &Note) -> bool {
matches!(note, Note::Old { .. })
}
let mut fleet = fleet_of(&ROSTER, NonZeroUsize::new(2).expect("positive"));
for replica in fleet.values_mut() {
replica.tolerate_refusals();
}
let mut fabric = Fabric::new(0xB17E_0002, &ROSTER, 0);
fabric.corrupt(Equivocator::within(1).speaking(
LIAR,
Box::new(Then(
Withholding {
from: 2,
kind: is_old_plane,
},
ForkedAdoption { to: 2, delta: -1 },
)),
));
for (offset, &station) in ROSTER.iter().enumerate() {
let _ = act(&mut fabric, &mut fleet, station, |replica, out| {
replica.insert_visible(offset, out)
});
}
fabric.drain(&mut fleet);
let _ = act(&mut fabric, &mut fleet, 1, |replica, out| {
replica.try_declare(out)
})
.expect("station 1 declares over the settled watermark");
fabric.drain(&mut fleet);
for &station in &ROSTER {
let replica = &fleet[&station];
assert_eq!(
replica.generation(),
1,
"station {station} sealed under a watermark the withheld delta holds down"
);
assert!(
replica.epochs().sealed().next().is_none(),
"station {station} retired a seal the frozen round cannot license"
);
assert!(
replica.halted().is_none(),
"station {station} halted: the composed lie must stall, never diverge"
);
}
}