use crate::slot::{SlotIndex, SlotInfo, SlotStatus};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Winner {
Empty,
Confirmed(SlotIndex),
Unconfirmed(SlotIndex),
Fork,
Refused,
Lost,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PairReport {
pub a: SlotStatus,
pub b: SlotStatus,
pub winner: Winner,
pub generation_gap: bool,
pub chain_break: bool,
}
impl PairReport {
#[must_use]
pub fn winner_info(&self) -> Option<(SlotIndex, SlotInfo)> {
let index = match self.winner {
Winner::Confirmed(i) | Winner::Unconfirmed(i) => i,
_ => return None,
};
match self.status(index) {
SlotStatus::Valid(info) => Some((index, info)),
_ => None,
}
}
#[must_use]
pub fn status(&self, index: SlotIndex) -> SlotStatus {
match index {
SlotIndex::A => self.a,
SlotIndex::B => self.b,
}
}
#[must_use]
pub fn next_write(&self) -> Option<SlotIndex> {
match self.winner {
Winner::Empty => Some(SlotIndex::A),
Winner::Confirmed(i) | Winner::Unconfirmed(i) => Some(i.other()),
Winner::Fork | Winner::Refused | Winner::Lost => None,
}
}
}
fn explains_survivor(other: &SlotStatus) -> bool {
matches!(
other,
SlotStatus::Blank | SlotStatus::BadHeaderCrc | SlotStatus::BadPayloadCrc
)
}
fn refused(s: &SlotStatus) -> bool {
matches!(
s,
SlotStatus::UnsupportedVersion { .. } | SlotStatus::Incompatible { .. }
)
}
#[must_use]
pub fn decide(a: SlotStatus, b: SlotStatus) -> PairReport {
let mut report = PairReport {
a,
b,
winner: Winner::Lost,
generation_gap: false,
chain_break: false,
};
if refused(&a) || refused(&b) {
report.winner = Winner::Refused;
return report;
}
report.winner = match (a, b) {
(SlotStatus::Blank, SlotStatus::Blank) => Winner::Empty,
(SlotStatus::Valid(ia), SlotStatus::Valid(ib)) => {
if ia.generation == ib.generation {
Winner::Fork
} else {
let (index, newer, older) = if ia.generation > ib.generation {
(SlotIndex::A, ia, ib)
} else {
(SlotIndex::B, ib, ia)
};
let consecutive = newer.generation - older.generation == 1;
report.generation_gap = !consecutive;
report.chain_break = consecutive && newer.prev_header_crc32c != older.header_crc32c;
Winner::Confirmed(index)
}
}
(SlotStatus::Valid(_), other) => survivor(SlotIndex::A, &other),
(other, SlotStatus::Valid(_)) => survivor(SlotIndex::B, &other),
_ => Winner::Lost,
};
report
}
fn survivor(index: SlotIndex, other: &SlotStatus) -> Winner {
if explains_survivor(other) {
Winner::Confirmed(index)
} else {
Winner::Unconfirmed(index)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::slot::{Expect, MAGIC, SlotFields, encode, validate};
fn info(generation: u64, prev: u32) -> SlotStatus {
let fields = SlotFields {
volume_uuid: [1; 16],
object_id: 9,
generation,
owner_generation: 1,
slot_offset: 0,
slot_index: SlotIndex::A,
log2_slot_size: 12,
prev_header_crc32c: prev,
};
let mut buf = vec![0u8; 4096];
assert!(encode(&mut buf, &fields, b"p").is_ok());
validate(
&buf,
&Expect {
volume_uuid: [1; 16],
object_id: 9,
slot_offset: 0,
slot_index: SlotIndex::A,
log2_slot_size: 12,
},
)
}
fn crc_of(s: SlotStatus) -> u32 {
match s {
SlotStatus::Valid(i) => i.header_crc32c,
_ => 0,
}
}
#[test]
fn test_decide_both_blank_is_empty_and_writes_a() {
let r = decide(SlotStatus::Blank, SlotStatus::Blank);
assert_eq!(r.winner, Winner::Empty);
assert_eq!(r.next_write(), Some(SlotIndex::A));
}
#[test]
fn test_decide_newer_generation_wins_and_next_write_targets_loser() {
let old = info(4, 0);
let new = info(5, crc_of(old));
let r = decide(old, new);
assert_eq!(r.winner, Winner::Confirmed(SlotIndex::B));
assert_eq!(r.next_write(), Some(SlotIndex::A));
assert!(!r.generation_gap && !r.chain_break);
assert_eq!(r.winner_info().map(|(_, i)| i.generation), Some(5));
}
#[test]
fn test_decide_flags_gap_and_chain_break() {
let r = decide(info(4, 0), info(7, 0));
assert!(r.generation_gap);
let r = decide(info(4, 0), info(5, 0xDEAD));
assert!(r.chain_break);
}
#[test]
fn test_decide_equal_generations_are_a_fork_with_no_write() {
let r = decide(info(3, 0), info(3, 1));
assert_eq!(r.winner, Winner::Fork);
assert_eq!(r.next_write(), None);
}
#[test]
fn test_decide_torn_other_slot_confirms_survivor() {
for torn in [
SlotStatus::Blank,
SlotStatus::BadHeaderCrc,
SlotStatus::BadPayloadCrc,
] {
assert_eq!(
decide(info(2, 0), torn).winner,
Winner::Confirmed(SlotIndex::A)
);
assert_eq!(
decide(torn, info(2, 0)).winner,
Winner::Confirmed(SlotIndex::B)
);
}
}
#[test]
fn test_decide_unexplained_other_slot_leaves_survivor_unconfirmed() {
for odd in [
SlotStatus::ReadError,
SlotStatus::Misplaced,
SlotStatus::Foreign,
SlotStatus::WrongObject,
SlotStatus::BadMagic,
SlotStatus::Malformed,
SlotStatus::BadLength,
] {
assert_eq!(
decide(info(2, 0), odd).winner,
Winner::Unconfirmed(SlotIndex::A),
"{odd:?}"
);
}
}
#[test]
fn test_decide_future_format_refuses_the_pair() {
let r = decide(info(2, 0), SlotStatus::UnsupportedVersion { major: 2 });
assert_eq!(r.winner, Winner::Refused);
assert_eq!(r.next_write(), None);
let r = decide(SlotStatus::Incompatible { flags: 4 }, SlotStatus::Blank);
assert_eq!(r.winner, Winner::Refused);
}
#[test]
fn test_decide_nothing_valid_is_lost() {
let r = decide(SlotStatus::BadHeaderCrc, SlotStatus::ReadError);
assert_eq!(r.winner, Winner::Lost);
assert_eq!(r.next_write(), None);
let _ = MAGIC;
}
}