Skip to content

Commit 60045b7

Browse files
committed
Fix some errors after merge
1 parent 065e64d commit 60045b7

File tree

3 files changed

+14
-141
lines changed

3 files changed

+14
-141
lines changed

substrate/frame/staking/src/migrations.rs

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ type StorageVersion<T: Config> = StorageValue<Pallet<T>, ObsoleteReleases, Value
6666
pub mod v17 {
6767
use super::*;
6868

69+
#[frame_support::storage_alias]
70+
pub type DisabledValidators<T: Config> =
71+
StorageValue<Pallet<T>, BoundedVec<(u32, OffenceSeverity), ConstU32<100>>, ValueQuery>;
72+
6973
pub struct MigrateDisabledToSession<T>(core::marker::PhantomData<T>);
7074
impl<T: Config> pallet_session::migrations::v1::MigrateDisabledValidators
7175
for MigrateDisabledToSession<T>

substrate/frame/staking/src/pallet/impls.rs

+7-33
Original file line numberDiff line numberDiff line change
@@ -1799,11 +1799,8 @@ where
17991799
>,
18001800
{
18011801
fn on_offence(
1802-
offenders: &[OffenceDetails<
1803-
T::AccountId,
1804-
pallet_session::historical::IdentificationTuple<T>,
1805-
>],
1806-
slash_fraction: &[Perbill],
1802+
offenders: &[OffenceDetails<T::AccountId, historical::IdentificationTuple<T>>],
1803+
slash_fractions: &[Perbill],
18071804
slash_session: SessionIndex,
18081805
) -> Weight {
18091806
log!(
@@ -1857,7 +1854,8 @@ impl<T: Config> Pallet<T> {
18571854
let window_start = active_era.saturating_sub(T::BondingDuration::get());
18581855

18591856
// Fast path for active-era report - most likely.
1860-
// `slash_session` cannot be in a future active era. It must be in `active_era` or before.
1857+
// `slash_session` cannot be in a future active era. It must be in `active_era` or
1858+
// before.
18611859
let slash_era = if slash_session >= active_era_start_session_index {
18621860
active_era
18631861
} else {
@@ -1880,43 +1878,19 @@ impl<T: Config> Pallet<T> {
18801878
add_db_reads_writes(1, 0);
18811879

18821880
for (details, slash_fraction) in offenders.zip(slash_fractions) {
1883-
let validator = &details.offender;
1881+
let (stash, exposure) = &details.offender;
1882+
18841883
// Skip if the validator is invulnerable.
1885-
if invulnerables.contains(&validator) {
1886-
log!(debug, "🦹 on_offence: {:?} is invulnerable; ignoring offence", validator);
1884+
if invulnerables.contains(stash) {
18871885
continue
18881886
}
18891887

1890-
add_db_reads_writes(1, 0);
1891-
let Some(exposure_overview) = <ErasStakersOverview<T>>::get(&offence_era, validator)
1892-
else {
1893-
// defensive: this implies offence is for a discarded era, and should already be
1894-
// filtered out.
1895-
log!(
1896-
warn,
1897-
"🦹 on_offence: no exposure found for {:?} in era {}; ignoring offence",
1898-
validator,
1899-
offence_era
1900-
);
1901-
continue;
1902-
};
1903-
19041888
Self::deposit_event(Event::<T>::SlashReported {
19051889
validator: stash.clone(),
19061890
fraction: *slash_fraction,
19071891
slash_era,
19081892
});
19091893

1910-
if offence_era == active_era.index {
1911-
// offence is in the current active era. Report it to session to maybe disable the
1912-
// validator.
1913-
add_db_reads_writes(2, 2);
1914-
T::SessionInterface::report_offence(
1915-
validator.clone(),
1916-
OffenceSeverity(*slash_fraction),
1917-
);
1918-
}
1919-
19201894
let unapplied = slashing::compute_slash::<T>(slashing::SlashParams {
19211895
stash,
19221896
slash: *slash_fraction,

substrate/frame/staking/src/slashing.rs

+3-108
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
//! Based on research at <https://research.web3.foundation/en/latest/polkadot/slashing/npos.html>
5151
5252
use crate::{
53-
asset, log, BalanceOf, Config, EraInfo, Error, NegativeImbalanceOf, NominatorSlashInEra,
54-
OffenceQueue, OffenceQueueEras, PagedExposure, Pallet, Perbill, ProcessingOffence,
55-
SlashRewardFraction, SpanSlash, UnappliedSlash, UnappliedSlashes, ValidatorSlashInEra,
53+
asset, log, BalanceOf, Config, EraInfo, Error, Exposure, NegativeImbalanceOf,
54+
NominatorSlashInEra, PagedExposure, Pallet, Perbill, SlashRewardFraction, SpanSlash,
55+
UnappliedSlash, UnappliedSlashes, ValidatorSlashInEra,
5656
};
5757
use alloc::vec::Vec;
5858
use codec::{Decode, Encode, MaxEncodedLen};
@@ -284,8 +284,6 @@ pub(crate) fn compute_slash<T: Config>(
284284
}
285285
}
286286

287-
add_offending_validator::<T>(&params);
288-
289287
let mut nominators_slashed = Vec::new();
290288
reward_payout += slash_nominators::<T>(params.clone(), prior_slash_p, &mut nominators_slashed);
291289

@@ -316,109 +314,6 @@ fn kick_out_if_recent<T: Config>(params: SlashParams<T>) {
316314
// Check https://github.com/paritytech/polkadot-sdk/issues/2650 for details
317315
spans.end_span(params.now);
318316
}
319-
320-
add_offending_validator::<T>(&params);
321-
}
322-
323-
/// Inform the [`DisablingStrategy`] implementation about the new offender and disable the list of
324-
/// validators provided by [`decision`].
325-
pub(crate) fn add_offending_validator<T: Config>(
326-
stash: &T::AccountId,
327-
slash: Perbill,
328-
offence_era: EraIndex,
329-
) {
330-
DisabledValidators::<T>::mutate(|disabled| {
331-
let new_severity = OffenceSeverity(slash);
332-
let decision = T::DisablingStrategy::decision(stash, new_severity, offence_era, &disabled);
333-
334-
if let Some(offender_idx) = decision.disable {
335-
// Check if the offender is already disabled
336-
match disabled.binary_search_by_key(&offender_idx, |(index, _)| *index) {
337-
// Offender is already disabled, update severity if the new one is higher
338-
Ok(index) => {
339-
let (_, old_severity) = &mut disabled[index];
340-
if new_severity > *old_severity {
341-
*old_severity = new_severity;
342-
}
343-
},
344-
Err(index) => {
345-
// Offender is not disabled, add to `DisabledValidators` and disable it
346-
if disabled.try_insert(index, (offender_idx, new_severity)).defensive().is_ok()
347-
{
348-
// Propagate disablement to session level
349-
T::SessionInterface::disable_validator(offender_idx);
350-
// Emit event that a validator got disabled
351-
<Pallet<T>>::deposit_event(super::Event::<T>::ValidatorDisabled {
352-
stash: stash.clone(),
353-
});
354-
}
355-
},
356-
}
357-
}
358-
359-
if let Some(reenable_idx) = decision.reenable {
360-
// Remove the validator from `DisabledValidators` and re-enable it.
361-
if let Ok(index) = disabled.binary_search_by_key(&reenable_idx, |(index, _)| *index) {
362-
disabled.remove(index);
363-
// Propagate re-enablement to session level
364-
T::SessionInterface::enable_validator(reenable_idx);
365-
// Emit event that a validator got re-enabled
366-
let reenabled_stash =
367-
T::SessionInterface::validators()[reenable_idx as usize].clone();
368-
<Pallet<T>>::deposit_event(super::Event::<T>::ValidatorReenabled {
369-
stash: reenabled_stash,
370-
});
371-
}
372-
}
373-
});
374-
375-
// `DisabledValidators` should be kept sorted
376-
debug_assert!(DisabledValidators::<T>::get().windows(2).all(|pair| pair[0] < pair[1]));
377-
}
378-
379-
/// Compute the slash for a validator. Returns the amount slashed and the reward payout.
380-
fn slash_validator<T: Config>(params: SlashParams<T>) -> (BalanceOf<T>, BalanceOf<T>) {
381-
let own_slash = params.slash * params.exposure.exposure_metadata.own;
382-
log!(
383-
warn,
384-
"🦹 slashing validator {:?} of stake: {:?} with {:?}% for {:?} in era {:?}",
385-
params.stash,
386-
params.exposure.exposure_metadata.own,
387-
params.slash,
388-
own_slash,
389-
params.slash_era,
390-
);
391-
392-
if own_slash == Zero::zero() {
393-
// kick out the validator even if they won't be slashed,
394-
// as long as the misbehavior is from their most recent slashing span.
395-
kick_out_if_recent::<T>(params);
396-
return (Zero::zero(), Zero::zero())
397-
}
398-
399-
// apply slash to validator.
400-
let mut reward_payout = Zero::zero();
401-
let mut val_slashed = Zero::zero();
402-
403-
{
404-
let mut spans = fetch_spans::<T>(
405-
params.stash,
406-
params.window_start,
407-
&mut reward_payout,
408-
&mut val_slashed,
409-
params.reward_proportion,
410-
);
411-
412-
let target_span = spans.compare_and_update_span_slash(params.slash_era, own_slash);
413-
414-
if target_span == Some(spans.span_index()) {
415-
// misbehavior occurred within the current slashing span - end current span.
416-
// Check <https://github.com/paritytech/polkadot-sdk/issues/2650> for details.
417-
spans.end_span(params.now);
418-
}
419-
}
420-
421-
(val_slashed, reward_payout)
422317
}
423318

424319
/// Slash nominators. Accepts general parameters and the prior slash percentage of the validator.

0 commit comments

Comments
 (0)