Skip to content

Commit

Permalink
Any offender is disabled for a whole era - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tdimitrov committed Nov 8, 2023
1 parent b16c8b6 commit bbb6631
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 85 deletions.
1 change: 0 additions & 1 deletion substrate/frame/offences/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ where
&concurrent_offenders,
&slash_perbill,
offence.session_index(),
offence.disable_strategy(),
);

// Deposit the event.
Expand Down
9 changes: 2 additions & 7 deletions substrate/frame/offences/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use frame_support::{
weights::Weight,
Twox64Concat,
};
use sp_staking::offence::{DisableStrategy, OnOffenceHandler};
use sp_staking::offence::OnOffenceHandler;
use sp_std::vec::Vec;

#[cfg(feature = "try-runtime")]
Expand Down Expand Up @@ -106,12 +106,7 @@ pub fn remove_deferred_storage<T: Config>() -> Weight {
let deferred = <DeferredOffences<T>>::take();
log::info!(target: LOG_TARGET, "have {} deferred offences, applying.", deferred.len());
for (offences, perbill, session) in deferred.iter() {
let consumed = T::OnOffenceHandler::on_offence(
offences,
perbill,
*session,
DisableStrategy::WhenSlashed,
);
let consumed = T::OnOffenceHandler::on_offence(offences, perbill, *session);
weight = weight.saturating_add(consumed);
}

Expand Down
1 change: 0 additions & 1 deletion substrate/frame/offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ impl<Reporter, Offender> offence::OnOffenceHandler<Reporter, Offender, Weight>
_offenders: &[OffenceDetails<Reporter, Offender>],
slash_fraction: &[Perbill],
_offence_session: SessionIndex,
_disable_strategy: DisableStrategy,
) -> Weight {
OnOffencePerbill::mutate(|f| {
*f = slash_fraction.to_vec();
Expand Down
8 changes: 3 additions & 5 deletions substrate/frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sp_runtime::{
BuildStorage,
};
use sp_staking::{
offence::{DisableStrategy, OffenceDetails, OnOffenceHandler},
offence::{OffenceDetails, OnOffenceHandler},
OnStakingUpdate,
};

Expand Down Expand Up @@ -723,12 +723,11 @@ 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, disable_strategy);
let _ = Staking::on_offence(offenders, slash_fraction, start_session);
return
} else if bonded_era > era {
break
Expand All @@ -740,7 +739,6 @@ 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 @@ -755,7 +753,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, DisableStrategy::WhenSlashed)
on_offence_in_era(offenders, slash_fraction, now)
}

pub(crate) fn add_slash(who: &AccountId) {
Expand Down
10 changes: 3 additions & 7 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sp_runtime::{
};
use sp_staking::{
currency_to_vote::CurrencyToVote,
offence::{DisableStrategy, OffenceDetails, OnOffenceHandler},
offence::{OffenceDetails, OnOffenceHandler},
EraIndex, Page, SessionIndex, Stake,
StakingAccount::{self, Controller, Stash},
StakingInterface,
Expand Down Expand Up @@ -427,10 +427,8 @@ impl<T: Config> Pallet<T> {
}

// disable all offending validators that have been disabled for the whole era
for (index, disabled) in <OffendingValidators<T>>::get() {
if disabled {
T::SessionInterface::disable_validator(index);
}
for index in <OffendingValidators<T>>::get() {
T::SessionInterface::disable_validator(index);
}
}

Expand Down Expand Up @@ -1354,7 +1352,6 @@ where
>],
slash_fraction: &[Perbill],
slash_session: SessionIndex,
disable_strategy: DisableStrategy,
) -> Weight {
let reward_proportion = SlashRewardFraction::<T>::get();
let mut consumed_weight = Weight::from_parts(0, 0);
Expand Down Expand Up @@ -1419,7 +1416,6 @@ where
window_start,
now: active_era,
reward_proportion,
disable_strategy,
});

Self::deposit_event(Event::<T>::SlashReported {
Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,10 +645,11 @@ pub mod pallet {
/// `OffendingValidatorsThreshold` is reached. The vec is always kept sorted so that we can find
/// whether a given validator has previously offended using binary search. It gets cleared when
/// the era ends.
// TODO: Fix the comment above
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn offending_validators)]
pub type OffendingValidators<T: Config> = StorageValue<_, Vec<(u32, bool)>, ValueQuery>;
pub type OffendingValidators<T: Config> = StorageValue<_, Vec<u32>, ValueQuery>;

/// The threshold for when users can start calling `chill_other` for other validators /
/// nominators. The threshold is compared to the actual number of validators / nominators
Expand Down
43 changes: 14 additions & 29 deletions substrate/frame/staking/src/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use sp_runtime::{
traits::{Saturating, Zero},
DispatchResult, RuntimeDebug,
};
use sp_staking::{offence::DisableStrategy, EraIndex};
use sp_staking::EraIndex;
use sp_std::vec::Vec;

/// The proportion of the slashing reward to be paid out on the first slashing detection.
Expand Down Expand Up @@ -215,8 +215,6 @@ 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 Down Expand Up @@ -285,8 +283,7 @@ pub(crate) fn compute_slash<T: Config>(
}
}

let disable_when_slashed = params.disable_strategy != DisableStrategy::Never;
add_offending_validator::<T>(params.stash, disable_when_slashed);
add_offending_validator::<T>(params.stash);

let mut nominators_slashed = Vec::new();
reward_payout += slash_nominators::<T>(params.clone(), prior_slash_p, &mut nominators_slashed);
Expand Down Expand Up @@ -319,14 +316,13 @@ fn kick_out_if_recent<T: Config>(params: SlashParams<T>) {
<Pallet<T>>::chill_stash(params.stash);
}

let disable_without_slash = params.disable_strategy == DisableStrategy::Always;
add_offending_validator::<T>(params.stash, disable_without_slash);
add_offending_validator::<T>(params.stash);
}

/// Add the given validator to the offenders list and optionally disable it.
/// If after adding the validator `OffendingValidatorsThreshold` is reached
/// a new era will be forced.
fn add_offending_validator<T: Config>(stash: &T::AccountId, disable: bool) {
fn add_offending_validator<T: Config>(stash: &T::AccountId) {
OffendingValidators::<T>::mutate(|offending| {
let validators = T::SessionInterface::validators();
let validator_index = match validators.iter().position(|i| i == stash) {
Expand All @@ -336,31 +332,20 @@ fn add_offending_validator<T: Config>(stash: &T::AccountId, disable: bool) {

let validator_index_u32 = validator_index as u32;

match offending.binary_search_by_key(&validator_index_u32, |(index, _)| *index) {
if let Err(index) = offending.binary_search_by_key(&validator_index_u32, |index| *index) {
// this is a new offending validator
Err(index) => {
offending.insert(index, (validator_index_u32, disable));
offending.insert(index, validator_index_u32);

let offending_threshold =
T::OffendingValidatorsThreshold::get() * validators.len() as u32;
let offending_threshold =
T::OffendingValidatorsThreshold::get() * validators.len() as u32;

if offending.len() >= offending_threshold as usize {
// force a new era, to select a new validator set
<Pallet<T>>::ensure_new_era()
}
// TODO - don't do this
if offending.len() >= offending_threshold as usize {
// force a new era, to select a new validator set
<Pallet<T>>::ensure_new_era()
}

if disable {
T::SessionInterface::disable_validator(validator_index_u32);
}
},
Ok(index) => {
if disable && !offending[index].1 {
// the validator had previously offended without being disabled,
// let's make sure we disable it now
offending[index].1 = true;
T::SessionInterface::disable_validator(validator_index_u32);
}
},
T::SessionInterface::disable_validator(validator_index_u32);
}
});
}
Expand Down
Loading

0 comments on commit bbb6631

Please sign in to comment.