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
+44 -4
View File
@@ -34,7 +34,7 @@ use sp_runtime::{
Perbill, Percent,
};
use sp_staking::{
offence::{OffenceDetails, OnOffenceHandler},
offence::{DisableStrategy, OffenceDetails, OnOffenceHandler},
SessionIndex,
};
use sp_std::prelude::*;
@@ -2250,6 +2250,7 @@ fn slash_in_old_span_does_not_deselect() {
}],
&[Perbill::from_percent(0)],
1,
DisableStrategy::WhenSlashed,
);
// the validator doesn't get chilled again
@@ -2266,6 +2267,7 @@ fn slash_in_old_span_does_not_deselect() {
// NOTE: A 100% slash here would clean up the account, causing de-registration.
&[Perbill::from_percent(95)],
1,
DisableStrategy::WhenSlashed,
);
// the validator doesn't get chilled again
@@ -2562,6 +2564,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(10)],
2,
DisableStrategy::WhenSlashed,
);
assert_eq!(Balances::free_balance(11), 900);
@@ -2588,6 +2591,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(30)],
3,
DisableStrategy::WhenSlashed,
);
// 11 was not further slashed, but 21 and 101 were.
@@ -2609,6 +2613,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(20)],
2,
DisableStrategy::WhenSlashed,
);
// 11 was further slashed, but 21 and 101 were not.
@@ -2744,6 +2749,7 @@ fn remove_deferred() {
&[OffenceDetails { offender: (11, exposure.clone()), reporters: vec![] }],
&[Perbill::from_percent(15)],
1,
DisableStrategy::WhenSlashed,
);
// fails if empty
@@ -2933,6 +2939,40 @@ fn non_slashable_offence_doesnt_disable_validator() {
});
}
#[test]
fn slashing_independent_of_disabling_validator() {
ExtBuilder::default().build_and_execute(|| {
mock::start_active_era(1);
assert_eq_uvec!(Session::validators(), vec![11, 21]);
let exposure_11 = Staking::eras_stakers(Staking::active_era().unwrap().index, &11);
let exposure_21 = Staking::eras_stakers(Staking::active_era().unwrap().index, &21);
let now = Staking::active_era().unwrap().index;
// offence with no slash associated, BUT disabling
on_offence_in_era(
&[OffenceDetails { offender: (11, exposure_11.clone()), reporters: vec![] }],
&[Perbill::zero()],
now,
DisableStrategy::Always,
);
// offence that slashes 25% of the bond, BUT not disabling
on_offence_in_era(
&[OffenceDetails { offender: (21, exposure_21.clone()), reporters: vec![] }],
&[Perbill::from_percent(25)],
now,
DisableStrategy::Never,
);
// the offence for validator 10 was explicitly disabled
assert!(is_disabled(10));
// whereas validator 20 is explicitly not disabled
assert!(!is_disabled(20));
});
}
#[test]
fn offence_threshold_triggers_new_era() {
ExtBuilder::default()
@@ -3595,7 +3635,7 @@ fn offences_weight_calculated_correctly() {
ExtBuilder::default().nominate(true).build_and_execute(|| {
// On offence with zero offenders: 4 Reads, 1 Write
let zero_offence_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(4, 1);
assert_eq!(Staking::on_offence(&[], &[Perbill::from_percent(50)], 0), zero_offence_weight);
assert_eq!(Staking::on_offence(&[], &[Perbill::from_percent(50)], 0, DisableStrategy::WhenSlashed), zero_offence_weight);
// On Offence with N offenders, Unapplied: 4 Reads, 1 Write + 4 Reads, 5 Writes
let n_offence_unapplied_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(4, 1)
@@ -3608,7 +3648,7 @@ fn offences_weight_calculated_correctly() {
reporters: vec![],
}
).collect();
assert_eq!(Staking::on_offence(&offenders, &[Perbill::from_percent(50)], 0), n_offence_unapplied_weight);
assert_eq!(Staking::on_offence(&offenders, &[Perbill::from_percent(50)], 0, DisableStrategy::WhenSlashed), n_offence_unapplied_weight);
// On Offence with one offenders, Applied
let one_offender = [
@@ -3629,7 +3669,7 @@ fn offences_weight_calculated_correctly() {
// `reward_cost` * reporters (1)
+ <Test as frame_system::Config>::DbWeight::get().reads_writes(2, 2);
assert_eq!(Staking::on_offence(&one_offender, &[Perbill::from_percent(50)], 0), one_offence_unapplied_weight);
assert_eq!(Staking::on_offence(&one_offender, &[Perbill::from_percent(50)], 0, DisableStrategy::WhenSlashed), one_offence_unapplied_weight);
});
}