use tetcore_std::vec::Vec;
use codec::{Encode, Decode};
use tp_runtime::Perbill;
use crate::SessionIndex;
pub type Kind = [u8; 16];
pub type OffenceCount = u32;
pub trait Offence<Offender> {
const ID: Kind;
type TimeSlot: Clone + codec::Codec + Ord;
fn offenders(&self) -> Vec<Offender>;
fn session_index(&self) -> SessionIndex;
fn validator_set_count(&self) -> u32;
fn time_slot(&self) -> Self::TimeSlot;
fn slash_fraction(
offenders_count: u32,
validator_set_count: u32,
) -> Perbill;
}
#[derive(PartialEq, tp_runtime::RuntimeDebug)]
pub enum OffenceError {
DuplicateReport,
Other(u8),
}
impl tp_runtime::traits::Printable for OffenceError {
fn print(&self) {
"OffenceError".print();
match self {
Self::DuplicateReport => "DuplicateReport".print(),
Self::Other(e) => {
"Other".print();
e.print();
}
}
}
}
pub trait ReportOffence<Reporter, Offender, O: Offence<Offender>> {
fn report_offence(reporters: Vec<Reporter>, offence: O) -> Result<(), OffenceError>;
fn is_known_offence(offenders: &[Offender], time_slot: &O::TimeSlot) -> bool;
}
impl<Reporter, Offender, O: Offence<Offender>> ReportOffence<Reporter, Offender, O> for () {
fn report_offence(_reporters: Vec<Reporter>, _offence: O) -> Result<(), OffenceError> {
Ok(())
}
fn is_known_offence(_offenders: &[Offender], _time_slot: &O::TimeSlot) -> bool {
true
}
}
pub trait OnOffenceHandler<Reporter, Offender, Res> {
fn on_offence(
offenders: &[OffenceDetails<Reporter, Offender>],
slash_fraction: &[Perbill],
session: SessionIndex,
) -> Result<Res, ()>;
fn can_report() -> bool;
}
impl<Reporter, Offender, Res: Default> OnOffenceHandler<Reporter, Offender, Res> for () {
fn on_offence(
_offenders: &[OffenceDetails<Reporter, Offender>],
_slash_fraction: &[Perbill],
_session: SessionIndex,
) -> Result<Res, ()> {
Ok(Default::default())
}
fn can_report() -> bool { true }
}
#[derive(Clone, PartialEq, Eq, Encode, Decode, tp_runtime::RuntimeDebug)]
pub struct OffenceDetails<Reporter, Offender> {
pub offender: Offender,
pub reporters: Vec<Reporter>,
}