extern crate alloc;
use core::num::NonZeroUsize;
use crate::kairos::Kairos;
use crate::metis::{
AbandonRefusal, Cut, Departed, Departure, DepartureRefusal, Dot, DotSet, EpochRefusal, Epochs,
SealedEpoch, Stability, VersionVector, Vouched,
};
#[track_caller]
fn d(station: u32, counter: u64) -> Dot {
Dot::from_parts(station, counter).expect("test literal names the non-dot counter zero")
}
const ROSTER: [u32; 3] = [1, 2, 3];
const SURVIVORS: [u32; 2] = [1, 2];
const GONE: u32 = 3;
const DECLARATION: Dot = match Dot::from_parts(1, 2) {
Ok(dot) => dot,
Err(_) => unreachable!(),
};
fn horizon() -> NonZeroUsize {
NonZeroUsize::new(2).expect("positive")
}
fn vector(pairs: &[(u32, u64)]) -> VersionVector {
let mut vector = VersionVector::new();
for &(station, counter) in pairs {
vector.observe(station, counter);
}
vector
}
fn holding(station: u32, dots: &[u64]) -> DotSet {
let mut held = DotSet::new();
for &dot in dots {
let _ = held.insert(d(station, dot));
}
held
}
fn prefix(station: u32, counter: u64) -> DotSet {
holding(station, &(1..=counter).collect::<alloc::vec::Vec<_>>())
}
fn attested(proposals: &[(u32, u64)]) -> Departed {
let mut round = Departure::opened(
GONE,
proposals[0].0,
proposals.iter().map(|&(station, _)| station),
)
.expect("a surviving family");
let (_, own_prefix) = proposals[0];
let _ = round
.propose(&prefix(GONE, own_prefix), &DotSet::new())
.expect("a gap-free holding");
for &(station, counter) in &proposals[1..] {
round
.fold(&Vouched::trust(station, counter))
.expect("a survivor's proposal");
}
round.try_seal().expect("every survivor proposed").clone()
}
#[test]
fn the_round_seals_at_the_join_of_the_survivors_proposals() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(round.propose(&prefix(GONE, 4), &DotSet::new()), Ok(4));
assert!(
round.try_seal().is_none(),
"one survivor's word is not the family's"
);
round
.fold(&Vouched::trust(2, 7))
.expect("a survivor's proposal");
let departed = round.try_seal().expect("every survivor proposed").clone();
assert_eq!(departed.station(), GONE);
assert_eq!(departed.bound(), 7, "the join, not this replica's own 4");
assert_eq!(round.bound(), Some(7));
}
#[test]
fn the_fence_holds_at_the_proposal_and_becomes_a_verdict_at_the_bound() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(round.admits(d(GONE, 9)), Ok(()), "nothing promised yet");
let _ = round
.propose(&prefix(GONE, 4), &DotSet::new())
.expect("gap-free");
let held = round
.admits(d(GONE, 5))
.expect_err("above this replica's own promise");
assert_eq!(held.fence, 4);
assert!(
!held.is_resurgence(),
"a provisional refusal is a hold: the round may still seal above it"
);
round.fold(&Vouched::trust(2, 7)).expect("a proposal");
let _ = round.try_seal().expect("complete");
assert_eq!(
round.admits(d(GONE, 5)),
Ok(()),
"the fence rises to the agreed bound, so the held dot is admitted"
);
let refused = round
.admits(d(GONE, 8))
.expect_err("above the agreed bound");
assert_eq!(refused.fence, 7);
assert!(
refused.is_resurgence(),
"above the sealed bound the refusal is the resurgence verdict"
);
}
#[test]
fn a_survivor_holding_successor_traffic_cannot_propose() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(
round.propose(&prefix(GONE, 4), &holding(GONE, &[1])),
Err(DepartureRefusal::SuccessorHeld {
station: GONE,
held: 1,
})
);
assert_eq!(round.bound(), None, "and the round is untouched");
assert_eq!(round.admits(d(GONE, 9)), Ok(()), "having fenced nothing");
assert_eq!(
round.propose(&prefix(GONE, 4), &holding(1, &[1, 2])),
Ok(4),
"another station's successor traffic is not this station's"
);
}
#[test]
fn the_successor_planes_fence_is_bottom_from_the_open() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
let _ = round
.propose(&prefix(GONE, 4), &DotSet::new())
.expect("gap-free");
assert_eq!(
round.admits(d(GONE, 3)),
Ok(()),
"the old plane admits below the promise"
);
let held = round
.admits_successor(d(GONE, 3))
.expect_err("and the successor plane admits nothing at all");
assert_eq!(held.fence, 0);
assert!(
!held.is_resurgence(),
"a hold while the round is open: abandoning the eviction admits it"
);
assert_eq!(
round.admits_successor(d(1, u64::MAX)),
Ok(()),
"and it speaks for one station, like its old-plane twin"
);
round.fold(&Vouched::trust(2, 7)).expect("a proposal");
let _ = round.try_seal().expect("complete");
assert!(
round.admits(d(GONE, 7)).is_ok(),
"the old plane still admits up to the agreed bound"
);
let refused = round
.admits_successor(d(GONE, 1))
.expect_err("the successor plane never does");
assert!(
refused.is_resurgence(),
"and once sealed the refusal is a verdict"
);
}
#[test]
fn the_fence_speaks_only_for_the_departing_station() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
let _ = round
.propose(&prefix(GONE, 1), &DotSet::new())
.expect("gap-free");
for station in SURVIVORS {
assert_eq!(round.admits(d(station, u64::MAX)), Ok(()));
}
assert!(round.admits(d(GONE, 2)).is_err());
}
#[test]
fn a_ragged_holding_cannot_be_proposed_and_redelivery_cures_it() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
let ragged = holding(GONE, &[1, 2, 4]);
assert_eq!(
round.propose(&ragged, &DotSet::new()),
Err(DepartureRefusal::Ragged {
station: GONE,
floor: 2,
held: 4,
})
);
assert_eq!(
round.proposals().collect::<alloc::vec::Vec<_>>(),
[(1, None), (2, None)],
"a refused proposal leaves the round untouched"
);
assert_eq!(round.admits(d(GONE, 4)), Ok(()), "and fences nothing");
let mut cured = ragged;
let _ = cured.insert(d(GONE, 3));
assert_eq!(round.propose(&cured, &DotSet::new()), Ok(4));
}
#[test]
fn raising_a_promised_fence_is_refused() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(round.propose(&prefix(GONE, 4), &DotSet::new()), Ok(4));
assert_eq!(
round.propose(&prefix(GONE, 4), &DotSet::new()),
Ok(4),
"a repeat of the same proposal absorbs"
);
assert_eq!(
round.propose(&prefix(GONE, 6), &DotSet::new()),
Err(DepartureRefusal::FenceBroken {
station: GONE,
promised: 4,
held: 6,
})
);
assert_eq!(round.bound(), None);
}
#[test]
fn the_departing_station_has_no_voice_in_its_own_departure() {
assert_eq!(
Departure::opened(GONE, 1, ROSTER),
Err(DepartureRefusal::SelfDeparture { station: GONE })
);
assert_eq!(
Departure::opened(GONE, 1, []),
Err(DepartureRefusal::EmptyFamily { station: GONE })
);
assert_eq!(
Departure::opened(GONE, GONE, SURVIVORS),
Err(DepartureRefusal::UnknownStation { station: GONE }),
"and it cannot run the round either"
);
assert_eq!(
Departure::opened(GONE, 4, SURVIVORS),
Err(DepartureRefusal::UnknownStation { station: 4 }),
"nor can anyone outside the family"
);
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(
round.fold(&Vouched::trust(GONE, u64::MAX)),
Err(DepartureRefusal::UnknownStation { station: GONE })
);
assert_eq!(
round.fold(&Vouched::trust(4, 1)),
Err(DepartureRefusal::UnknownStation { station: 4 })
);
}
#[test]
fn proposals_absorb_and_the_bound_never_regresses() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
let _ = round
.propose(&prefix(GONE, 4), &DotSet::new())
.expect("gap-free");
for claim in [7, 2, 7] {
round.fold(&Vouched::trust(2, claim)).expect("a proposal");
}
assert_eq!(
round.try_seal().map(Departed::bound),
Some(7),
"the stale restatement of 2 cannot lower the slot"
);
round
.fold(&Vouched::trust(2, 9))
.expect("a proposal after the seal absorbs");
assert_eq!(
round.bound(),
Some(7),
"and the sealed attestation is latched"
);
}
#[test]
fn a_sealed_round_proves_this_replica_fenced() {
let mut round = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
assert_eq!(
round.fold(&Vouched::trust(1, 4)),
Err(DepartureRefusal::Impersonated { station: 1 }),
"the local slot is the one no report may fill"
);
round
.fold(&Vouched::trust(2, 3))
.expect("a peer's proposal");
assert!(
round.try_seal().is_none(),
"and every other slot being full is still not a promise"
);
assert_eq!(
round.admits(d(GONE, u64::MAX)),
Ok(()),
"nothing is fenced yet"
);
let _ = round
.propose(&prefix(GONE, 2), &DotSet::new())
.expect("gap-free");
assert_eq!(
round.try_seal().map(Departed::bound),
Some(3),
"the bound is the family's, with this replica's own promise in it"
);
}
#[test]
fn a_restart_reinstates_the_promise_it_recorded() {
let mut restarted = Departure::opened(GONE, 1, SURVIVORS).expect("a surviving family");
restarted.reinstate(4).expect("a recorded proposal");
assert!(
restarted.admits(d(GONE, 5)).is_err(),
"the fence is back where it was promised, not where the rebuilt \
holding happens to sit"
);
restarted
.reinstate(4)
.expect("idempotent at the same value");
assert_eq!(
restarted.reinstate(2),
Err(DepartureRefusal::FenceBroken {
station: GONE,
promised: 4,
held: 2,
}),
"and a record below the live fence is refused, never absorbed"
);
assert_eq!(
restarted.reinstate(6),
Err(DepartureRefusal::FenceBroken {
station: GONE,
promised: 4,
held: 6,
}),
"and so is one above it: the local proposal is latched, and a replay \
that could raise a fence is a replay that could unfence traffic"
);
restarted
.fold(&Vouched::trust(2, 6))
.expect("a peer's proposal");
assert_eq!(
restarted.try_seal().map(Departed::bound),
Some(6),
"a reinstated promise completes the round exactly as a fresh one does"
);
restarted
.reinstate(4)
.expect("the recorded proposal still sits inside the agreed bound");
assert!(
restarted.admits(d(GONE, 5)).is_ok() && restarted.admits(d(GONE, 7)).is_err(),
"and replaying it does not move the sealed fence in either direction"
);
assert_eq!(
restarted.reinstate(7),
Err(DepartureRefusal::FenceBroken {
station: GONE,
promised: 6,
held: 7,
}),
"a record above the agreed bound loses to the family's agreement"
);
}
#[test]
fn an_attestation_must_cover_the_family_it_narrows() {
let narrow = attested(&[(1, 4)]);
assert_eq!(
narrow.family().collect::<alloc::vec::Vec<_>>(),
[1],
"the attestation says whose agreement it is"
);
let mut short = settled(&ROSTER, &base());
assert_eq!(
short.abandon_attested(&narrow),
Err(AbandonRefusal::FamilyMismatch { station: GONE }),
"station 2 never proposed, and this tracker still meets over it"
);
assert!(!short.is_abandoned(GONE) && short.attested(GONE).is_none());
let wide = attested(&[(1, 1), (2, 1)]);
let mut second = Departure::opened(2, 1, [1]).expect("a surviving family");
let _ = second
.propose(&prefix(2, 1), &DotSet::new())
.expect("gap-free");
let second = second.try_seal().expect("complete").clone();
let mut tracker = settled(&ROSTER, &base());
for departed in [&wide, &second] {
let _ = tracker
.abandon_attested(departed)
.expect("each covers the family it narrows when it is agreed");
}
assert_eq!(tracker.attested(GONE), Some(1));
assert_eq!(tracker.attested(2), Some(1));
assert!(
tracker.abandon_attested(&wide).is_ok(),
"and re-declaring the earlier one absorbs, though this tracker has \
since narrowed past the family that agreed it: coverage survives a \
later departure, where equality would not"
);
let mut backwards = settled(&ROSTER, &base());
assert_eq!(
backwards.abandon_attested(&second),
Err(AbandonRefusal::FamilyMismatch { station: 2 }),
"the later departure applied first is refused, and rightly: station 3 \
is still in this tracker's family and promised nothing about it"
);
}
fn settled(roster: &[u32], cut: &Cut) -> Stability {
let mut stability = Stability::new(roster.iter().copied());
for &station in roster {
stability.report_cut(station, cut).expect("on roster");
}
stability
}
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 rank() -> Kairos {
Kairos::new(2, 0, 1, 0u16)
}
#[test]
fn only_the_attested_door_supplies_a_coordinate() {
let mut bare = settled(&ROSTER, &base());
let mut attesting = bare.clone();
let departed = attested(&[(1, 1), (2, 1)]);
let by_hand = bare.abandon(GONE).expect("a family remains");
let by_round = attesting
.abandon_attested(&departed)
.expect("a family remains");
assert_eq!(
by_hand, by_round,
"the operator's local write-off read is the same either way"
);
assert_eq!(bare.watermark(), attesting.watermark());
assert!(bare.is_abandoned(GONE) && attesting.is_abandoned(GONE));
assert_eq!(
bare.attested(GONE),
None,
"a bare abandonment supplies none"
);
assert_eq!(attesting.attested(GONE), Some(1));
assert_eq!(attesting.attested(1), None, "and speaks for nobody else");
assert_eq!(attesting.attested(404), None);
}
#[test]
fn re_attesting_at_another_bound_is_refused() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 4), (2, 4)]))
.expect("a family remains");
assert!(
stability
.abandon_attested(&attested(&[(1, 4), (2, 4)]))
.is_ok(),
"the same bound absorbs"
);
assert_eq!(
stability.abandon_attested(&attested(&[(1, 4), (2, 9)])),
Err(AbandonRefusal::Reattested {
station: GONE,
held: 4,
offered: 9,
})
);
assert_eq!(stability.attested(GONE), Some(4));
}
#[test]
fn the_attested_door_still_guards_the_family() {
let mut stability = Stability::new(SURVIVORS);
assert_eq!(
stability.abandon_attested(&attested(&[(1, 1)])),
Err(AbandonRefusal::UnknownStation { station: GONE }),
"off the roster, and the narrow family it names does not change that"
);
let mut pair = settled(&SURVIVORS, &base());
let _ = pair.abandon(2).expect("a family remains");
let mut round = Departure::opened(1, 2, [2]).expect("a surviving family");
let _ = round
.propose(&prefix(1, 3), &DotSet::new())
.expect("gap-free");
let last = round.try_seal().expect("complete").clone();
assert_eq!(
pair.abandon_attested(&last),
Err(AbandonRefusal::LastSurvivor { station: 1 }),
"an attestation does not license emptying the family"
);
assert_eq!(pair.attested(1), None, "and records nothing when refused");
assert!(!pair.is_abandoned(1), "nor narrows anything");
}
#[test]
fn a_departure_that_would_strand_an_attested_bound_is_refused() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 5)]))
.expect("a family remains");
assert_eq!(stability.attested(GONE), Some(5));
assert_eq!(
stability.abandon(2),
Err(AbandonRefusal::StrandsAttestation {
station: 2,
stranded: GONE,
}),
"station 2 is the only member that can still serve the agreed bound"
);
assert!(!stability.is_abandoned(2), "and the tracker is unchanged");
stability
.report_cut(
1,
&Cut::from_witnessed(vector(&[(1, 1), (2, 1), (GONE, 5)])),
)
.expect("on roster");
let _ = stability
.abandon(2)
.expect("station 1 can serve what station 2 was holding alone");
let mut late = settled(&ROSTER, &base());
let _ = late.abandon(2).expect("a family remains");
assert_eq!(
late.abandon_attested(&attested(&[(1, 1), (2, 9)])),
Err(AbandonRefusal::FamilyMismatch { station: GONE }),
"station 2 proposed the 9 and station 2 has already left, so the \
family that remains would have agreed 1"
);
assert!(late.attested(GONE).is_none() && !late.is_abandoned(GONE));
let mut again = settled(&ROSTER, &base());
let _ = again
.abandon_attested(&attested(&[(1, 1), (2, 5)]))
.expect("a family remains");
let _ = again
.abandon_attested(&attested(&[(1, 1), (2, 5)]))
.expect("idempotent");
let _ = again.abandon(GONE).expect("and so is the bare door");
}
#[test]
fn concurrent_departures_that_strand_each_other_refuse_rather_than_wedge() {
let mut round_for_two = Departure::opened(2, 1, [1, GONE]).expect("a surviving family");
let _ = round_for_two
.propose(&DotSet::new(), &DotSet::new())
.expect("holding nothing of 2");
round_for_two
.fold(&Vouched::trust(GONE, 5))
.expect("the other survivor holds it to 5");
let two = round_for_two.try_seal().expect("complete").clone();
let three = attested(&[(1, 0), (2, 5)]);
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&three)
.expect("the first one applies");
assert_eq!(
stability.abandon_attested(&two),
Err(AbandonRefusal::FamilyMismatch { station: 2 }),
"and the second rested on a member that has now left, so the family \
that remains never agreed its bound"
);
assert!(!stability.is_abandoned(2), "recoverably: nothing moved");
stability
.report_cut(
1,
&Cut::from_witnessed(vector(&[(1, 1), (2, 1), (GONE, 5)])),
)
.expect("on roster");
let mut again = Departure::opened(2, 1, [1]).expect("a surviving family");
let _ = again
.propose(&prefix(2, 5), &DotSet::new())
.expect("gap-free after repair");
let two_again = again.try_seal().expect("complete").clone();
let _ = stability
.abandon_attested(&two_again)
.expect("a round over the family that remains binds");
assert_eq!(stability.attested(2), Some(5));
}
#[test]
fn a_wider_rounds_higher_bound_is_not_the_remaining_familys_agreement() {
const WIDE: [u32; 4] = [1, 2, 3, 4];
let narrow = attested(&[(1, 1), (2, 1)]);
let mut wider = Departure::opened(GONE, 1, [1, 2, 4]).expect("a surviving family");
let _ = wider
.propose(&prefix(GONE, 1), &DotSet::new())
.expect("gap-free");
wider.fold(&Vouched::trust(2, 1)).expect("a proposal");
wider
.fold(&Vouched::trust(4, 5))
.expect("station 4 holds more of it");
let wider = wider.try_seal().expect("complete").clone();
assert_eq!(wider.bound(), 5, "the wider round agrees the higher bound");
assert_eq!(narrow.bound(), 1);
let mut holding_narrow = Stability::new(WIDE);
let mut holding_wider = Stability::new(WIDE);
for tracker in [&mut holding_narrow, &mut holding_wider] {
for station in WIDE {
tracker.report_cut(station, &base()).expect("on roster");
}
let mut leaving = Departure::opened(4, 1, [1, 2, GONE]).expect("a surviving family");
let _ = leaving
.propose(&DotSet::new(), &DotSet::new())
.expect("holding nothing of station 4");
for station in [2, GONE] {
leaving
.fold(&Vouched::trust(station, 0))
.expect("a proposal");
}
let leaving = leaving.try_seal().expect("complete").clone();
let _ = tracker
.abandon_attested(&leaving)
.expect("nothing of station 4's is written off");
}
let _ = holding_narrow
.abandon_attested(&narrow)
.expect("the round over exactly this family binds");
assert_eq!(
holding_wider.abandon_attested(&wider),
Err(AbandonRefusal::FamilyMismatch { station: GONE }),
"and the wider round does not, because its bound rested on station 4"
);
let _ = holding_wider
.abandon_attested(&narrow)
.expect("its only route to this departure is the round both families agree");
assert_eq!(
holding_narrow.attested(GONE),
holding_wider.attested(GONE),
"so both replicas retain the same bound over the same family"
);
}
fn drive(stability: &mut Stability, family: &[u32]) -> (Epochs, Option<SealedEpoch>) {
let (mut epochs, epoch) = confirmed(stability, family);
adopt_all(&mut epochs, epoch, stability, family);
let sealed = epochs.try_seal(stability).cloned();
(epochs, sealed)
}
fn confirmed(stability: &mut Stability, family: &[u32]) -> (Epochs, crate::metis::EpochAddress) {
let mut epochs = Epochs::new(ROSTER, horizon());
let declaration = epochs
.declare(DECLARATION, rank(), stability, &Cut::bottom())
.expect("the settled watermark licenses the declaration");
let epoch = declaration.address();
for &station in family {
stability
.report_cut(station, &delivered())
.expect("on roster");
epochs
.confirm(epoch, &Vouched::trust(station, delivered()))
.expect("a confirmation for the delivered declaration");
}
(epochs, epoch)
}
fn adopt_all(
epochs: &mut Epochs,
epoch: crate::metis::EpochAddress,
stability: &Stability,
family: &[u32],
) {
if epochs.adopt(1, 2, stability).is_err() {
return;
}
for &station in family {
if station == 1 {
continue;
}
epochs
.adopt_report(
epoch,
&Vouched::trust(station, delivered().as_vector().get(station)),
)
.expect("an adoption report for a delivered candidate");
}
}
#[test]
fn an_attested_departure_completes_the_rounds_a_bare_one_freezes() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 1)]))
.expect("a family remains");
let (epochs, sealed) = drive(&mut stability, &SURVIVORS);
let sealed = sealed.expect("the rounds complete over the surviving family");
assert_eq!(sealed.declaration().declaration(), DECLARATION);
assert_eq!(
epochs.sealed().count(),
1,
"and the lineage advanced exactly once"
);
}
#[test]
fn the_sealed_join_carries_the_agreed_bound_at_the_departed_coordinate() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 1)]))
.expect("a family remains");
let (_, sealed) = drive(&mut stability, &SURVIVORS);
let sealed = sealed.expect("the rounds complete");
assert_eq!(
sealed.sealed_join().get(GONE),
1,
"the bound the round agreed, not the departed member's own claim"
);
for station in SURVIVORS {
assert_eq!(
sealed.sealed_join().get(station),
delivered().as_vector().get(station),
"and every survivor's own coordinate is still its own report"
);
}
}
#[test]
fn the_attested_bound_waits_for_the_survivors_to_hold_it() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 5), (2, 3)]))
.expect("a family remains");
let (mut epochs, epoch) = confirmed(&mut stability, &SURVIVORS);
adopt_all(&mut epochs, epoch, &stability, &SURVIVORS);
assert!(
epochs.try_seal(&stability).is_none(),
"the survivors have delivered the departed member only to 1, and the \
agreed bound is 5"
);
let repaired = Cut::from_witnessed(vector(&[(1, 2), (2, 1), (GONE, 5)]));
for station in SURVIVORS {
stability.report_cut(station, &repaired).expect("on roster");
}
adopt_all(&mut epochs, epoch, &stability, &SURVIVORS);
let sealed = epochs
.try_seal(&stability)
.expect("the watermark has covered the agreed bound");
assert_eq!(
sealed.sealed_join().get(GONE),
5,
"and the record carries the agreed bound, which no survivor's own \
report could have supplied"
);
}
#[test]
fn the_substitution_discards_the_departed_members_own_testimony() {
let heard = {
let mut stability = settled(&ROSTER, &base());
let mut epochs = Epochs::new(ROSTER, horizon());
let declaration = epochs
.declare(DECLARATION, rank(), &stability, &Cut::bottom())
.expect("licensed");
let epoch = declaration.address();
let ahead = Cut::from_witnessed(vector(&[(1, 2), (2, 1), (GONE, 9)]));
epochs
.confirm(epoch, &Vouched::trust(GONE, ahead))
.expect("a confirmation for the delivered declaration");
epochs
.adopt_report(epoch, &Vouched::trust(GONE, 9))
.expect("an adoption report");
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 1)]))
.expect("a family remains");
for &station in &SURVIVORS {
stability
.report_cut(station, &delivered())
.expect("on roster");
epochs
.confirm(epoch, &Vouched::trust(station, delivered()))
.expect("a confirmation");
}
let _ = epochs.adopt(1, 2, &stability).expect("confirmed");
epochs
.adopt_report(epoch, &Vouched::trust(2, 1))
.expect("an adoption report");
epochs
.try_seal(&stability)
.cloned()
.expect("the seal fires")
};
let unheard = {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 1)]))
.expect("a family remains");
drive(&mut stability, &SURVIVORS).1.expect("the seal fires")
};
assert_eq!(
heard, unheard,
"the record is a function of the family and the attestation alone"
);
}
#[test]
fn eviction_testimony_after_a_peer_has_sealed_cannot_move_the_record() {
let honest = {
let mut stability = settled(&ROSTER, &base());
drive(&mut stability, &ROSTER).1.expect("the honest seal")
};
let evicting = {
let mut stability = settled(&ROSTER, &base());
let forced = honest.sealed_join().get(GONE);
let _ = stability
.abandon_attested(&attested(&[(1, forced), (2, forced)]))
.expect("a family remains");
drive(&mut stability, &SURVIVORS)
.1
.expect("the evicting seal")
};
assert_eq!(
honest, evicting,
"a peer that sealed honestly and one that evicted agree on the record"
);
}
#[test]
fn attesting_one_departure_leaves_every_other_slot_asking() {
let mut stability = settled(&ROSTER, &base());
let _ = stability
.abandon_attested(&attested(&[(1, 1), (2, 1)]))
.expect("a family remains");
let mut epochs = Epochs::new(ROSTER, horizon());
let declaration = epochs
.declare(DECLARATION, rank(), &stability, &Cut::bottom())
.expect("licensed");
let epoch = declaration.address();
stability.report_cut(1, &delivered()).expect("on roster");
epochs
.confirm(epoch, &Vouched::trust(1, delivered()))
.expect("a confirmation");
assert_eq!(
epochs.adopt(1, 2, &stability),
Err(EpochRefusal::Unconfirmed),
"survivor 2 is still owed, and no attestation speaks for it"
);
assert!(epochs.try_seal(&stability).is_none());
}
#[test]
fn a_refounded_attestation_binds_the_family_the_new_plane_opens_with() {
let departed = attested(&[(1, 2), (2, 2)]);
let mut widened = Stability::new([1, 2, GONE, 4]);
let narrow = departed.refounded([1, 2], &DotSet::new());
assert_eq!(
widened.abandon_attested(&narrow),
Err(AbandonRefusal::FamilyMismatch { station: GONE }),
"an attestation re-keyed over the old family refuses the widened tracker"
);
let wide = departed.refounded([1, 2, GONE, 4], &prefix(GONE, 1));
assert!(wide.family().eq([1u32, 2, 4]));
assert_eq!(
wide.bound(),
1,
"the door reads the base's gap-free prefix, nothing else"
);
let mut ready = Stability::new([1, 2, GONE, 4]);
ready
.report_cut(1, &Cut::from_witnessed(vector(&[(GONE, 1)])))
.expect("on roster");
let _ = ready
.abandon_attested(&wide)
.expect("the re-keyed attestation binds the widened family");
assert_eq!(
ready.attested(GONE),
Some(1),
"the carried coordinate is the base prefix, not bottom"
);
let ragged = departed.refounded([1, 2, GONE, 4], &holding(GONE, &[1, 3]));
assert_eq!(ragged.bound(), 1, "the prefix stops at the first hole");
let mut stationary = Stability::new(ROSTER);
stationary
.report_cut(1, &Cut::from_witnessed(vector(&[(GONE, 1)])))
.expect("on roster");
let _ = stationary
.abandon_attested(&departed.refounded(ROSTER, &prefix(GONE, 1)))
.expect("the stationary carry still binds");
assert_eq!(stationary.attested(GONE), Some(1));
}
#[test]
fn a_widened_carry_claims_no_possession_for_the_joiner() {
let departed = attested(&[(1, 2), (2, 2)]);
let wide = departed.refounded([1, 2, GONE, 4], &prefix(GONE, 2));
assert_eq!(wide.bound(), 2);
assert_eq!(
wide.proposal_of(4),
Some(0),
"the joiner's entry is coverage, not a claim of holding"
);
let mut tracker = Stability::new([1, 2, GONE, 4]);
for holder in [1, 2] {
tracker
.report_cut(holder, &Cut::from_witnessed(vector(&[(GONE, 2)])))
.expect("on roster");
}
let _ = tracker
.abandon_attested(&wide)
.expect("the re-keyed attestation binds the widened family");
let _ = tracker
.abandon(1)
.expect("station 2 still serves the bound");
assert_eq!(
tracker.abandon(2),
Err(AbandonRefusal::StrandsAttestation {
station: 2,
stranded: GONE,
}),
"abandoning the last real holder strands the attestation; the joiner's \
coverage entry is not possession"
);
tracker
.report_cut(4, &Cut::from_witnessed(vector(&[(GONE, 2)])))
.expect("on roster");
let _ = tracker
.abandon(2)
.expect("the joiner's report now serves the bound");
}