mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 20:31:13 +00:00
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:
@@ -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.
|
||||
@@ -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
|
||||
@@ -224,15 +227,12 @@ 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);
|
||||
@@ -240,13 +240,17 @@ pub(crate) fn compute_slash<T: Config>(
|
||||
}
|
||||
|
||||
let (prior_slash_p, _era_slash) =
|
||||
<Pallet<T> as Store>::ValidatorSlashInEra::get(&slash_era, stash)
|
||||
<Pallet<T> as Store>::ValidatorSlashInEra::get(¶ms.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(
|
||||
¶ms.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.
|
||||
@@ -261,14 +265,14 @@ 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
|
||||
@@ -276,20 +280,19 @@ pub(crate) fn compute_slash<T: Config>(
|
||||
|
||||
// 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(),
|
||||
@@ -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.
|
||||
@@ -371,13 +373,10 @@ 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 ¶ms.exposure.others {
|
||||
let stash = &nominator.who;
|
||||
let mut nom_slashed = Zero::zero();
|
||||
|
||||
@@ -385,15 +384,16 @@ fn slash_nominators<T: Config>(
|
||||
// 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(¶ms.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(¶ms.slash_era, stash, &era_slash);
|
||||
|
||||
era_slash
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user