EPM and staking events improvement (#13035)

* EPM and staking events improvement

* Uses RawOrigin in ElectionCompute event

* Refactors new phase events to PhaseTransition event

* PhaseTransitioned and remove RawOrigin from event

* Adds helpers for epm phase transition and staking force new

* addresses review comments

* nit: removes unecessary clone

* fixes benchmarks

Co-authored-by: parity-processbot <>
This commit is contained in:
Gonçalo Pestana
2023-01-09 16:06:47 +00:00
committed by GitHub
parent b929e0282d
commit fcdd8a88ad
7 changed files with 369 additions and 124 deletions
+9 -2
View File
@@ -339,7 +339,7 @@ impl<T: Config> Pallet<T> {
if maybe_new_era_validators.is_some() &&
matches!(ForceEra::<T>::get(), Forcing::ForceNew)
{
ForceEra::<T>::put(Forcing::NotForcing);
Self::set_force_era(Forcing::NotForcing);
}
maybe_new_era_validators
@@ -717,11 +717,18 @@ impl<T: Config> Pallet<T> {
}
}
/// Helper to set a new `ForceEra` mode.
pub(crate) fn set_force_era(mode: Forcing) {
log!(info, "Setting force era mode {:?}.", mode);
ForceEra::<T>::put(mode);
Self::deposit_event(Event::<T>::ForceEra { mode });
}
/// Ensures that at the end of the current session there will be a new era.
pub(crate) fn ensure_new_era() {
match ForceEra::<T>::get() {
Forcing::ForceAlways | Forcing::ForceNew => (),
_ => ForceEra::<T>::put(Forcing::ForceNew),
_ => Self::set_force_era(Forcing::ForceNew),
}
}
+5 -3
View File
@@ -714,6 +714,8 @@ pub mod pallet {
PayoutStarted { era_index: EraIndex, validator_stash: T::AccountId },
/// A validator has set their preferences.
ValidatorPrefsSet { stash: T::AccountId, prefs: ValidatorPrefs },
/// A new force era mode was set.
ForceEra { mode: Forcing },
}
#[pallet::error]
@@ -1377,7 +1379,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force_no_eras())]
pub fn force_no_eras(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
ForceEra::<T>::put(Forcing::ForceNone);
Self::set_force_era(Forcing::ForceNone);
Ok(())
}
@@ -1401,7 +1403,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force_new_era())]
pub fn force_new_era(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
ForceEra::<T>::put(Forcing::ForceNew);
Self::set_force_era(Forcing::ForceNew);
Ok(())
}
@@ -1452,7 +1454,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force_new_era_always())]
pub fn force_new_era_always(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
ForceEra::<T>::put(Forcing::ForceAlways);
Self::set_force_era(Forcing::ForceAlways);
Ok(())
}
+21 -7
View File
@@ -899,7 +899,7 @@ fn forcing_new_era_works() {
assert_eq!(active_era(), 1);
// no era change.
ForceEra::<Test>::put(Forcing::ForceNone);
Staking::set_force_era(Forcing::ForceNone);
start_session(4);
assert_eq!(active_era(), 1);
@@ -915,7 +915,7 @@ fn forcing_new_era_works() {
// back to normal.
// this immediately starts a new session.
ForceEra::<Test>::put(Forcing::NotForcing);
Staking::set_force_era(Forcing::NotForcing);
start_session(8);
assert_eq!(active_era(), 1);
@@ -923,7 +923,7 @@ fn forcing_new_era_works() {
start_session(9);
assert_eq!(active_era(), 2);
// forceful change
ForceEra::<Test>::put(Forcing::ForceAlways);
Staking::set_force_era(Forcing::ForceAlways);
start_session(10);
assert_eq!(active_era(), 2);
@@ -935,7 +935,7 @@ fn forcing_new_era_works() {
assert_eq!(active_era(), 4);
// just one forceful change
ForceEra::<Test>::put(Forcing::ForceNew);
Staking::set_force_era(Forcing::ForceNew);
start_session(13);
assert_eq!(active_era(), 5);
assert_eq!(ForceEra::<Test>::get(), Forcing::NotForcing);
@@ -2303,7 +2303,7 @@ fn era_is_always_same_length() {
);
let session = Session::current_index();
ForceEra::<Test>::put(Forcing::ForceNew);
Staking::set_force_era(Forcing::ForceNew);
advance_session();
advance_session();
assert_eq!(current_era(), 3);
@@ -2914,7 +2914,10 @@ fn deferred_slashes_are_deferred() {
staking_events_since_last_call().as_slice(),
&[
Event::Chilled { stash: 11 },
Event::ForceEra { mode: Forcing::ForceNew },
Event::SlashReported { validator: 11, slash_era: 1, .. },
Event::StakersElected,
Event::ForceEra { mode: Forcing::NotForcing },
..,
Event::Slashed { staker: 11, amount: 100 },
Event::Slashed { staker: 101, amount: 12 }
@@ -2949,6 +2952,7 @@ fn retroactive_deferred_slashes_two_eras_before() {
staking_events_since_last_call().as_slice(),
&[
Event::Chilled { stash: 11 },
Event::ForceEra { mode: Forcing::ForceNew },
Event::SlashReported { validator: 11, slash_era: 1, .. },
..,
Event::Slashed { staker: 11, amount: 100 },
@@ -3251,6 +3255,7 @@ fn slash_kicks_validators_not_nominators_and_disables_nominator_for_kicked_valid
Event::StakersElected,
Event::EraPaid { era_index: 0, validator_payout: 11075, remainder: 33225 },
Event::Chilled { stash: 11 },
Event::ForceEra { mode: Forcing::ForceNew },
Event::SlashReported {
validator: 11,
fraction: Perbill::from_percent(10),
@@ -3318,6 +3323,7 @@ fn non_slashable_offence_doesnt_disable_validator() {
Event::StakersElected,
Event::EraPaid { era_index: 0, validator_payout: 11075, remainder: 33225 },
Event::Chilled { stash: 11 },
Event::ForceEra { mode: Forcing::ForceNew },
Event::SlashReported {
validator: 11,
fraction: Perbill::from_percent(0),
@@ -3380,6 +3386,7 @@ fn slashing_independent_of_disabling_validator() {
Event::StakersElected,
Event::EraPaid { era_index: 0, validator_payout: 11075, remainder: 33225 },
Event::Chilled { stash: 11 },
Event::ForceEra { mode: Forcing::ForceNew },
Event::SlashReported {
validator: 11,
fraction: Perbill::from_percent(0),
@@ -4662,8 +4669,15 @@ mod election_data_provider {
MinimumValidatorCount::<Test>::put(2);
run_to_block(55);
assert_eq!(Staking::next_election_prediction(System::block_number()), 55 + 25);
assert_eq!(staking_events().len(), 6);
assert_eq!(*staking_events().last().unwrap(), Event::StakersElected);
assert_eq!(staking_events().len(), 10);
assert_eq!(
*staking_events().last().unwrap(),
Event::ForceEra { mode: Forcing::NotForcing }
);
assert_eq!(
*staking_events().get(staking_events().len() - 2).unwrap(),
Event::StakersElected
);
// The new era has been planned, forcing is changed from `ForceNew` to `NotForcing`.
assert_eq!(ForceEra::<Test>::get(), Forcing::NotForcing);
})