Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Offence implementations can disable offenders independently from slashing #10201

Merged
merged 10 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frame/offences/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ where
&concurrent_offenders,
&slash_perbill,
offence.session_index(),
offence.disable_strategy(),
);

// Deposit the event.
Expand Down
9 changes: 6 additions & 3 deletions frame/offences/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ use super::{Config, OffenceDetails, Perbill, SessionIndex};
use frame_support::{
generate_storage_alias, pallet_prelude::ValueQuery, traits::Get, weights::Weight,
};
use sp_staking::offence::OnOffenceHandler;
use sp_staking::offence::{DisableStrategy, OnOffenceHandler};
use sp_std::vec::Vec;

/// Type of data stored as a deferred offence
type DeferredOffenceOf<T> = (
Vec<OffenceDetails<<T as frame_system::Config>::AccountId, <T as Config>::IdentificationTuple>>,
Vec<Perbill>,
SessionIndex,
DisableStrategy,
wigy-opensource-developer marked this conversation as resolved.
Show resolved Hide resolved
wigy-opensource-developer marked this conversation as resolved.
Show resolved Hide resolved
);

// Deferred reports that have been rejected by the offence handler and need to be submitted
Expand All @@ -40,8 +41,9 @@ pub fn remove_deferred_storage<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().reads_writes(1, 1);
let deferred = <DeferredOffences<T>>::take();
wigy-opensource-developer marked this conversation as resolved.
Show resolved Hide resolved
log::info!(target: "runtime::offences", "have {} deferred offences, applying.", deferred.len());
for (offences, perbill, session) in deferred.iter() {
let consumed = T::OnOffenceHandler::on_offence(&offences, &perbill, *session);
for (offences, perbill, session, disable_strategy) in deferred.iter() {
let consumed =
T::OnOffenceHandler::on_offence(&offences, &perbill, *session, *disable_strategy);
weight = weight.saturating_add(consumed);
}

Expand Down Expand Up @@ -78,6 +80,7 @@ mod test {
vec![offence_details],
vec![Perbill::from_percent(5 + 1 * 100 / 5)],
1,
DisableStrategy::WhenSlashed,
));

// when
Expand Down
3 changes: 2 additions & 1 deletion frame/offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use sp_runtime::{
Perbill,
};
use sp_staking::{
offence::{self, Kind, OffenceDetails},
offence::{self, DisableStrategy, Kind, OffenceDetails},
SessionIndex,
};
use std::cell::RefCell;
Expand All @@ -55,6 +55,7 @@ impl<Reporter, Offender> offence::OnOffenceHandler<Reporter, Offender, Weight>
_offenders: &[OffenceDetails<Reporter, Offender>],
slash_fraction: &[Perbill],
_offence_session: SessionIndex,
_disable_strategy: DisableStrategy,
) -> Weight {
ON_OFFENCE_PERBILL.with(|f| {
*f.borrow_mut() = slash_fraction.to_vec();
Expand Down
8 changes: 5 additions & 3 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sp_runtime::{
testing::{Header, TestXt, UintAuthorityId},
traits::{IdentityLookup, Zero},
};
use sp_staking::offence::{OffenceDetails, OnOffenceHandler};
use sp_staking::offence::{DisableStrategy, OffenceDetails, OnOffenceHandler};
use std::cell::RefCell;

pub const INIT_TIMESTAMP: u64 = 30_000;
Expand Down Expand Up @@ -764,11 +764,12 @@ pub(crate) fn on_offence_in_era(
>],
slash_fraction: &[Perbill],
era: EraIndex,
disable_strategy: DisableStrategy,
) {
let bonded_eras = crate::BondedEras::<Test>::get();
for &(bonded_era, start_session) in bonded_eras.iter() {
if bonded_era == era {
let _ = Staking::on_offence(offenders, slash_fraction, start_session);
let _ = Staking::on_offence(offenders, slash_fraction, start_session, disable_strategy);
return
} else if bonded_era > era {
break
Expand All @@ -780,6 +781,7 @@ pub(crate) fn on_offence_in_era(
offenders,
slash_fraction,
Staking::eras_start_session_index(era).unwrap(),
disable_strategy,
);
} else {
panic!("cannot slash in era {}", era);
Expand All @@ -794,7 +796,7 @@ pub(crate) fn on_offence_now(
slash_fraction: &[Perbill],
) {
let now = Staking::active_era().unwrap().index;
on_offence_in_era(offenders, slash_fraction, now)
on_offence_in_era(offenders, slash_fraction, now, DisableStrategy::WhenSlashed)
}

pub(crate) fn add_slash(who: &AccountId) {
Expand Down
4 changes: 3 additions & 1 deletion frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use sp_runtime::{
Perbill,
};
use sp_staking::{
offence::{OffenceDetails, OnOffenceHandler},
offence::{DisableStrategy, OffenceDetails, OnOffenceHandler},
SessionIndex,
};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
Expand Down Expand Up @@ -1139,6 +1139,7 @@ where
>],
slash_fraction: &[Perbill],
slash_session: SessionIndex,
disable_strategy: DisableStrategy,
) -> Weight {
let reward_proportion = SlashRewardFraction::<T>::get();
let mut consumed_weight: Weight = 0;
Expand Down Expand Up @@ -1208,6 +1209,7 @@ where
window_start,
now: active_era,
reward_proportion,
disable_strategy,
});

if let Some(mut unapplied) = unapplied {
Expand Down
70 changes: 35 additions & 35 deletions frame/staking/src/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use sp_runtime::{
traits::{Saturating, Zero},
DispatchResult, RuntimeDebug,
};
use sp_staking::offence::DisableStrategy;
use sp_std::vec::Vec;

/// The proportion of the slashing reward to be paid out on the first slashing detection.
Expand Down Expand Up @@ -213,6 +214,8 @@ pub(crate) struct SlashParams<'a, T: 'a + Config> {
/// The maximum percentage of a slash that ever gets paid out.
/// This is f_inf in the paper.
pub(crate) reward_proportion: Perbill,
/// When to disable offenders.
pub(crate) disable_strategy: DisableStrategy,
}

/// Computes a slash of a validator and nominators. It returns an unapplied
Expand All @@ -224,29 +227,30 @@ pub(crate) struct SlashParams<'a, T: 'a + Config> {
pub(crate) fn compute_slash<T: Config>(
params: SlashParams<T>,
) -> Option<UnappliedSlash<T::AccountId, BalanceOf<T>>> {
let SlashParams { stash, slash, exposure, slash_era, window_start, now, reward_proportion } =
params.clone();

let mut reward_payout = Zero::zero();
let mut val_slashed = Zero::zero();

// is the slash amount here a maximum for the era?
let own_slash = slash * exposure.own;
if slash * exposure.total == Zero::zero() {
let own_slash = params.slash * params.exposure.own;
if params.slash * params.exposure.total == Zero::zero() {
// kick out the validator even if they won't be slashed,
// as long as the misbehavior is from their most recent slashing span.
kick_out_if_recent::<T>(params);
return None
}

let (prior_slash_p, _era_slash) =
<Pallet<T> as Store>::ValidatorSlashInEra::get(&slash_era, stash)
<Pallet<T> as Store>::ValidatorSlashInEra::get(&params.slash_era, params.stash)
.unwrap_or((Perbill::zero(), Zero::zero()));

// compare slash proportions rather than slash values to avoid issues due to rounding
// error.
if slash.deconstruct() > prior_slash_p.deconstruct() {
<Pallet<T> as Store>::ValidatorSlashInEra::insert(&slash_era, stash, &(slash, own_slash));
if params.slash.deconstruct() > prior_slash_p.deconstruct() {
<Pallet<T> as Store>::ValidatorSlashInEra::insert(
&params.slash_era,
params.stash,
&(params.slash, own_slash),
);
} else {
// we slash based on the max in era - this new event is not the max,
// so neither the validator or any nominators will need an update.
Expand All @@ -261,35 +265,34 @@ pub(crate) fn compute_slash<T: Config>(
// apply slash to validator.
{
let mut spans = fetch_spans::<T>(
stash,
window_start,
params.stash,
params.window_start,
&mut reward_payout,
&mut val_slashed,
reward_proportion,
params.reward_proportion,
);

let target_span = spans.compare_and_update_span_slash(slash_era, own_slash);
let target_span = spans.compare_and_update_span_slash(params.slash_era, own_slash);

if target_span == Some(spans.span_index()) {
// misbehavior occurred within the current slashing span - take appropriate
// actions.

// chill the validator - it misbehaved in the current span and should
// not continue in the next election. also end the slashing span.
spans.end_span(now);
<Pallet<T>>::chill_stash(stash);
spans.end_span(params.now);
<Pallet<T>>::chill_stash(params.stash);
}
}

// add the validator to the offenders list and make sure it is disabled for
// the duration of the era
add_offending_validator::<T>(params.stash, true);
let disable_when_slashed = params.disable_strategy != DisableStrategy::Never;
add_offending_validator::<T>(params.stash, disable_when_slashed);

let mut nominators_slashed = Vec::new();
reward_payout += slash_nominators::<T>(params, prior_slash_p, &mut nominators_slashed);
reward_payout += slash_nominators::<T>(params.clone(), prior_slash_p, &mut nominators_slashed);

Some(UnappliedSlash {
validator: stash.clone(),
validator: params.stash.clone(),
own: val_slashed,
others: nominators_slashed,
reporters: Vec::new(),
Expand All @@ -316,9 +319,8 @@ fn kick_out_if_recent<T: Config>(params: SlashParams<T>) {
<Pallet<T>>::chill_stash(params.stash);
}

// add the validator to the offenders list but since there's no slash being
// applied there's no need to disable the validator
add_offending_validator::<T>(params.stash, false);
let disable_without_slash = params.disable_strategy == DisableStrategy::Always;
add_offending_validator::<T>(params.stash, disable_without_slash);
}

/// Add the given validator to the offenders list and optionally disable it.
Expand Down Expand Up @@ -371,29 +373,27 @@ fn slash_nominators<T: Config>(
prior_slash_p: Perbill,
nominators_slashed: &mut Vec<(T::AccountId, BalanceOf<T>)>,
) -> BalanceOf<T> {
let SlashParams { stash: _, slash, exposure, slash_era, window_start, now, reward_proportion } =
params;

let mut reward_payout = Zero::zero();

nominators_slashed.reserve(exposure.others.len());
for nominator in &exposure.others {
nominators_slashed.reserve(params.exposure.others.len());
for nominator in &params.exposure.others {
let stash = &nominator.who;
let mut nom_slashed = Zero::zero();

// the era slash of a nominator always grows, if the validator
// had a new max slash for the era.
let era_slash = {
let own_slash_prior = prior_slash_p * nominator.value;
let own_slash_by_validator = slash * nominator.value;
let own_slash_by_validator = params.slash * nominator.value;
let own_slash_difference = own_slash_by_validator.saturating_sub(own_slash_prior);

let mut era_slash = <Pallet<T> as Store>::NominatorSlashInEra::get(&slash_era, stash)
.unwrap_or_else(|| Zero::zero());
let mut era_slash =
<Pallet<T> as Store>::NominatorSlashInEra::get(&params.slash_era, stash)
.unwrap_or_else(|| Zero::zero());

era_slash += own_slash_difference;

<Pallet<T> as Store>::NominatorSlashInEra::insert(&slash_era, stash, &era_slash);
<Pallet<T> as Store>::NominatorSlashInEra::insert(&params.slash_era, stash, &era_slash);

era_slash
};
Expand All @@ -402,18 +402,18 @@ fn slash_nominators<T: Config>(
{
let mut spans = fetch_spans::<T>(
stash,
window_start,
params.window_start,
&mut reward_payout,
&mut nom_slashed,
reward_proportion,
params.reward_proportion,
);

let target_span = spans.compare_and_update_span_slash(slash_era, era_slash);
let target_span = spans.compare_and_update_span_slash(params.slash_era, era_slash);

if target_span == Some(spans.span_index()) {
// End the span, but don't chill the nominator. its nomination
// on this validator will be ignored in the future.
spans.end_span(now);
spans.end_span(params.now);
}
}

Expand Down
14 changes: 10 additions & 4 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use sp_runtime::{
Perbill, Percent,
};
use sp_staking::{
offence::{OffenceDetails, OnOffenceHandler},
offence::{DisableStrategy, OffenceDetails, OnOffenceHandler},
SessionIndex,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -2316,6 +2316,7 @@ fn slash_in_old_span_does_not_deselect() {
}],
&[Perbill::from_percent(0)],
1,
DisableStrategy::WhenSlashed,
);

// the validator doesn't get chilled again
Expand All @@ -2332,6 +2333,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
Expand Down Expand Up @@ -2628,6 +2630,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(10)],
2,
DisableStrategy::WhenSlashed,
);

assert_eq!(Balances::free_balance(11), 900);
Expand All @@ -2654,6 +2657,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(30)],
3,
DisableStrategy::WhenSlashed,
);

// 11 was not further slashed, but 21 and 101 were.
Expand All @@ -2675,6 +2679,7 @@ fn slashing_nominators_by_span_max() {
}],
&[Perbill::from_percent(20)],
2,
DisableStrategy::WhenSlashed,
);

// 11 was further slashed, but 21 and 101 were not.
Expand Down Expand Up @@ -2810,6 +2815,7 @@ fn remove_deferred() {
&[OffenceDetails { offender: (11, exposure.clone()), reporters: vec![] }],
&[Perbill::from_percent(15)],
1,
DisableStrategy::WhenSlashed,
);

// fails if empty
Expand Down Expand Up @@ -3661,7 +3667,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)
Expand All @@ -3674,7 +3680,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 = [
Expand All @@ -3695,7 +3701,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);
});
}

Expand Down
Loading