use crate::slot::{HeaderFacts, SlotIndex, SlotStatus};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlotRead {
pub status: SlotStatus,
pub facts: Option<HeaderFacts>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PairRead {
pub a: SlotRead,
pub b: SlotRead,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotVerdict {
Winner,
Nothing,
OlderDamaged,
InFlight,
Damage,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Standing {
Confirmed,
Unconfirmed,
Lost,
Refused,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PairVerdict {
pub standing: Standing,
pub winner: Option<SlotIndex>,
pub a: SlotVerdict,
pub b: SlotVerdict,
}
impl PairVerdict {
const LOST: Self = Self {
standing: Standing::Lost,
winner: None,
a: SlotVerdict::Damage,
b: SlotVerdict::Damage,
};
const REFUSED: Self = Self {
standing: Standing::Refused,
..Self::LOST
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Judgement {
pub heal: Option<(usize, SlotIndex)>,
pub writable: bool,
pub refused: bool,
pub clean: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JudgeError {
Empty,
OutputLength,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Opposite {
Nothing,
OlderDamaged,
Candidate(Option<u64>),
Damage,
}
fn valid_info(s: &SlotStatus) -> Option<crate::slot::SlotInfo> {
match s {
SlotStatus::Valid(i) => Some(*i),
_ => None,
}
}
fn numbered(s: &SlotRead) -> Option<u64> {
match s.status {
SlotStatus::Valid(i) => Some(i.meta_seq),
SlotStatus::BadPayloadCrc => s.facts.and_then(|f| f.numbered).map(|n| n.meta_seq),
_ => None,
}
}
fn opposite(winner: &crate::slot::SlotInfo, o: &SlotRead, o_is_b: bool) -> Opposite {
let g = winner.generation;
match o.status {
SlotStatus::Blank if g == 1 && o_is_b => Opposite::Nothing,
SlotStatus::BadPayloadCrc => match o.facts.and_then(|f| f.numbered) {
Some(n)
if Some(n.generation) == g.checked_add(1)
&& n.prev_header_crc32c == winner.header_crc32c =>
{
Opposite::Candidate(Some(n.meta_seq))
}
Some(n) if n.generation < g => Opposite::OlderDamaged,
_ => Opposite::Damage,
},
SlotStatus::BadHeaderCrc => match o.facts {
Some(f) if g >= 2 && f.stored_header_crc32c == winner.prev_header_crc32c => {
Opposite::OlderDamaged
}
Some(_) => Opposite::Candidate(None),
None => Opposite::Damage,
},
_ => Opposite::Damage,
}
}
pub fn judge(
pairs: &[PairRead],
clean_close: bool,
out: &mut [PairVerdict],
) -> Result<Judgement, JudgeError> {
if pairs.is_empty() {
return Err(JudgeError::Empty);
}
if out.len() != pairs.len() {
return Err(JudgeError::OutputLength);
}
let (mut max1, mut max1_count, mut max2) = (0u64, 0usize, 0u64);
let mut note = |v: u64| {
if v > max1 {
max2 = max1;
max1 = v;
max1_count = 1;
} else if v == max1 {
max1_count += 1;
} else if v > max2 {
max2 = v;
}
};
for p in pairs {
for s in [&p.a, &p.b] {
if let Some(v) = numbered(s) {
note(v);
}
}
}
let others_max = |v: Option<u64>| match v {
Some(v) if v == max1 && max1_count == 1 => max2,
_ => max1,
};
let mut candidates = 0usize;
let mut page_had_candidate = false;
for (i, (p, o)) in pairs.iter().zip(out.iter_mut()).enumerate() {
*o = PairVerdict::LOST;
let refused = |s: &SlotStatus| {
matches!(
s,
SlotStatus::UnsupportedVersion { .. } | SlotStatus::Incompatible { .. }
)
};
if refused(&p.a.status) || refused(&p.b.status) {
*o = PairVerdict::REFUSED;
continue;
}
match (valid_info(&p.a.status), valid_info(&p.b.status)) {
(None, None) => {}
(Some(a), Some(b)) => {
if a.generation == b.generation {
continue; }
let a_wins = a.generation > b.generation;
*o = PairVerdict {
standing: Standing::Confirmed,
winner: Some(if a_wins { SlotIndex::A } else { SlotIndex::B }),
a: if a_wins {
SlotVerdict::Winner
} else {
SlotVerdict::Nothing
},
b: if a_wins {
SlotVerdict::Nothing
} else {
SlotVerdict::Winner
},
};
}
(Some(w), None) | (None, Some(w)) => {
let (wi, other) = if valid_info(&p.a.status).is_some() {
(SlotIndex::A, &p.b)
} else {
(SlotIndex::B, &p.a)
};
let (verdict, standing) = match opposite(&w, other, wi == SlotIndex::A) {
Opposite::Nothing => (SlotVerdict::Nothing, Standing::Confirmed),
Opposite::OlderDamaged => (SlotVerdict::OlderDamaged, Standing::Confirmed),
Opposite::Damage => (SlotVerdict::Damage, Standing::Unconfirmed),
Opposite::Candidate(m) => {
page_had_candidate |= i > 0;
let qualifies = match m {
None => true,
Some(m) => others_max(Some(m)).checked_add(1) == Some(m),
};
if qualifies {
candidates += 1;
(SlotVerdict::InFlight, Standing::Confirmed)
} else {
(SlotVerdict::Damage, Standing::Unconfirmed)
}
}
};
*o = PairVerdict {
standing,
winner: Some(wi),
a: if wi == SlotIndex::A {
SlotVerdict::Winner
} else {
verdict
},
b: if wi == SlotIndex::B {
SlotVerdict::Winner
} else {
verdict
},
};
}
}
}
let volume_has_candidate = [out[0].a, out[0].b].contains(&SlotVerdict::InFlight);
let clean = out[0].standing == Standing::Confirmed && !volume_has_candidate && clean_close;
let troubled = out.iter().any(|v| v.standing != Standing::Confirmed);
let refused = out.iter().any(|v| v.standing == Standing::Refused);
let volume_lost = matches!(out[0].standing, Standing::Lost | Standing::Refused);
let page_demote = clean || candidates != 1 || troubled;
let volume_demote = candidates != 1 || refused;
let mut heal = None;
for (i, v) in out.iter_mut().enumerate() {
for (slot, idx) in [(&mut v.a, SlotIndex::A), (&mut v.b, SlotIndex::B)] {
if *slot == SlotVerdict::InFlight {
let demote = if i == 0 { volume_demote } else { page_demote };
if demote {
*slot = SlotVerdict::Damage;
v.standing = Standing::Unconfirmed;
} else {
heal = Some((i, idx));
}
}
}
}
let writable = !refused
&& !volume_lost
&& (out[0].standing != Standing::Unconfirmed || !page_had_candidate);
Ok(Judgement {
heal,
writable,
clean,
refused,
})
}