use std::fmt::Debug;
use crate::components::consensus::{
protocols::zug::{Fault, RoundId, Zug},
traits::Context,
utils::ValidatorIndex,
};
#[derive(Debug)]
#[allow(dead_code)]
pub(super) struct Participation<C>
where
C: Context,
{
pub(super) instance_id: C::InstanceId,
pub(super) faulty_stake_percent: u8,
pub(super) inactive_stake_percent: u8,
pub(super) inactive_validators: Vec<(ValidatorIndex, C::ValidatorId, ParticipationStatus)>,
pub(super) faulty_validators: Vec<(ValidatorIndex, C::ValidatorId, ParticipationStatus)>,
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub(super) enum ParticipationStatus {
LastSeenInRound(RoundId),
Inactive,
EquivocatedInOtherEra,
Equivocated,
}
impl ParticipationStatus {
pub(super) fn for_index<C: Context + 'static>(
idx: ValidatorIndex,
zug: &Zug<C>,
) -> Option<ParticipationStatus> {
if let Some(fault) = zug.faults.get(&idx) {
return Some(match fault {
Fault::Banned | Fault::Indirect => ParticipationStatus::EquivocatedInOtherEra,
Fault::Direct(..) => ParticipationStatus::Equivocated,
});
}
let last_seen_round = zug
.active
.get(idx)
.and_then(Option::as_ref)
.map(|signed_msg| signed_msg.round_id);
match last_seen_round {
None => Some(ParticipationStatus::Inactive),
Some(r_id) if r_id.saturating_add(2) < zug.current_round => {
Some(ParticipationStatus::LastSeenInRound(r_id))
}
_ => None,
}
}
}