Offence implementations can disable offenders independently from slashing (#10201)

* Offence implementations can disable offenders independently from slashing

* Fix build on CI

* Run cargo fmt

* Fixes based on review comments

* Make parameter naming consistent

* Fix migration and some English

* Fix migration - again

* cargo fmt

* Cover 2 new cases with a test
This commit is contained in:
wigy
2021-11-17 15:11:02 +01:00
committed by GitHub
parent 186efbc2a5
commit 3f0f1aa6f7
8 changed files with 129 additions and 46 deletions
@@ -37,6 +37,29 @@ pub type Kind = [u8; 16];
/// so that we can slash it accordingly.
pub type OffenceCount = u32;
/// In case of an offence, which conditions get an offending validator disabled.
#[derive(
Clone,
Copy,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Encode,
Decode,
sp_runtime::RuntimeDebug,
scale_info::TypeInfo,
)]
pub enum DisableStrategy {
/// Independently of slashing, this offence will not disable the offender.
Never,
/// Only disable the offender if it is also slashed.
WhenSlashed,
/// Independently of slashing, this offence will always disable the offender.
Always,
}
/// A trait implemented by an offence report.
///
/// This trait assumes that the offence is legitimate and was validated already.
@@ -79,6 +102,11 @@ pub trait Offence<Offender> {
/// number. Note that for GRANDPA the round number is reset each epoch.
fn time_slot(&self) -> Self::TimeSlot;
/// In which cases this offence needs to disable offenders until the next era starts.
fn disable_strategy(&self) -> DisableStrategy {
DisableStrategy::WhenSlashed
}
/// A slash fraction of the total exposure that should be slashed for this
/// particular offence kind for the given parameters that happened at a singular `TimeSlot`.
///
@@ -150,12 +178,15 @@ pub trait OnOffenceHandler<Reporter, Offender, Res> {
///
/// The `session` parameter is the session index of the offence.
///
/// The `disable_strategy` parameter decides if the offenders need to be disabled immediately.
///
/// The receiver might decide to not accept this offence. In this case, the call site is
/// responsible for queuing the report and re-submitting again.
fn on_offence(
offenders: &[OffenceDetails<Reporter, Offender>],
slash_fraction: &[Perbill],
session: SessionIndex,
disable_strategy: DisableStrategy,
) -> Res;
}
@@ -164,6 +195,7 @@ impl<Reporter, Offender, Res: Default> OnOffenceHandler<Reporter, Offender, Res>
_offenders: &[OffenceDetails<Reporter, Offender>],
_slash_fraction: &[Perbill],
_session: SessionIndex,
_disable_strategy: DisableStrategy,
) -> Res {
Default::default()
}