From 45940b23136eaf523264beb6659c3ba9315edf07 Mon Sep 17 00:00:00 2001 From: gpestana Date: Thu, 29 Dec 2022 22:48:22 +0000 Subject: [PATCH 1/8] EPM and staking events improvement --- .../election-provider-multi-phase/src/lib.rs | 123 ++++++++++++++---- .../src/signed.rs | 104 ++++++++++++--- .../src/unsigned.rs | 1 + frame/staking/src/pallet/impls.rs | 6 +- frame/staking/src/pallet/mod.rs | 5 + frame/staking/src/tests.rs | 18 ++- 6 files changed, 211 insertions(+), 46 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 6c4a55800f7e8..d97cdaa7149dd 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -931,6 +931,7 @@ pub mod pallet { >::put(ready); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Unsigned, + account_id: None, prev_ejected: ejected_a_solution, }); @@ -983,6 +984,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Emergency, + account_id: None, prev_ejected: QueuedSolution::::exists(), }); @@ -1060,6 +1062,7 @@ pub mod pallet { signed_submissions.put(); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Signed, + account_id: Some(who), prev_ejected: ejected_a_solution, }); Ok(()) @@ -1102,6 +1105,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Fallback, + account_id: None, prev_ejected: QueuedSolution::::exists(), }); @@ -1118,8 +1122,14 @@ pub mod pallet { /// If the solution is signed, this means that it hasn't yet been processed. If the /// solution is unsigned, this means that it has also been processed. /// - /// The `bool` is `true` when a previous solution was ejected to make room for this one. - SolutionStored { compute: ElectionCompute, prev_ejected: bool }, + /// If `account_id` is `None` if the solution is stored during the unsigned phase or by + /// `T::ForceOrigin`. The `bool` is `true` when a previous solution was ejected to make + /// room for this one. + SolutionStored { + compute: ElectionCompute, + account_id: Option, + prev_ejected: bool, + }, /// The election has been finalized, with the given computation and score. ElectionFinalized { compute: ElectionCompute, score: ElectionScore }, /// An election failed. @@ -1134,6 +1144,10 @@ pub mod pallet { SignedPhaseStarted { round: u32 }, /// The unsigned phase of the given round has started. UnsignedPhaseStarted { round: u32 }, + /// The `Phase::Off` of the given round has started. + OffPhaseStarted { round: u32 }, + /// The emergency phase has started in the given round. + EmergencyPhaseStarted { round: u32 }, } /// Error of the pallet that can be returned in response to dispatches. @@ -1573,6 +1587,8 @@ impl Pallet { // Phase is off now. >::put(Phase::Off); + Self::deposit_event(Event::OffPhaseStarted { round: Self::round() }); + // Kill snapshots. Self::kill_snapshot(); } @@ -1653,6 +1669,7 @@ impl ElectionProvider for Pallet { Err(why) => { log!(error, "Entering emergency mode: {:?}", why); >::put(Phase::Emergency); + Self::deposit_event(Event::EmergencyPhaseStarted { round: Self::round() }); Err(why) }, } @@ -1959,6 +1976,7 @@ mod tests { sum_stake_squared: 0 } }, + Event::OffPhaseStarted { round: 2 }, Event::SignedPhaseStarted { round: 2 }, Event::UnsignedPhaseStarted { round: 2 } ] @@ -1998,7 +2016,8 @@ mod tests { sum_stake: 0, sum_stake_squared: 0 } - } + }, + Event::OffPhaseStarted { round: 2 }, ] ); }); @@ -2036,7 +2055,8 @@ mod tests { sum_stake: 0, sum_stake_squared: 0 } - } + }, + Event::OffPhaseStarted { round: 2 }, ] ) }); @@ -2064,10 +2084,17 @@ mod tests { assert_eq!( multi_phase_events(), - vec![Event::ElectionFinalized { - compute: ElectionCompute::Fallback, - score: ElectionScore { minimal_stake: 0, sum_stake: 0, sum_stake_squared: 0 } - }] + vec![ + Event::ElectionFinalized { + compute: ElectionCompute::Fallback, + score: ElectionScore { + minimal_stake: 0, + sum_stake: 0, + sum_stake_squared: 0 + } + }, + Event::OffPhaseStarted { round: 2 }, + ] ); }); } @@ -2094,7 +2121,8 @@ mod tests { Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: Default::default() - } + }, + Event::OffPhaseStarted { round: 2 }, ], ); // All storage items must be cleared. @@ -2145,11 +2173,31 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, Event::Slashed { account: 99, value: 5 }, Event::Slashed { account: 99, value: 5 }, Event::Slashed { account: 99, value: 5 }, @@ -2162,7 +2210,8 @@ mod tests { sum_stake: 0, sum_stake_squared: 0 } - } + }, + Event::OffPhaseStarted { round: 2 }, ] ); }) @@ -2187,7 +2236,11 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, Event::Rewarded { account: 99, value: 7 }, Event::UnsignedPhaseStarted { round: 1 }, Event::ElectionFinalized { @@ -2197,7 +2250,8 @@ mod tests { sum_stake: 100, sum_stake_squared: 5200 } - } + }, + Event::OffPhaseStarted { round: 2 }, ], ); }) @@ -2234,6 +2288,7 @@ mod tests { Event::UnsignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, + account_id: None, prev_ejected: false }, Event::ElectionFinalized { @@ -2243,7 +2298,8 @@ mod tests { sum_stake: 100, sum_stake_squared: 5200 } - } + }, + Event::OffPhaseStarted { round: 2 }, ], ); }) @@ -2279,7 +2335,8 @@ mod tests { sum_stake: 0, sum_stake_squared: 0 } - } + }, + Event::OffPhaseStarted { round: 2 }, ] ); }); @@ -2301,7 +2358,8 @@ mod tests { vec![ Event::SignedPhaseStarted { round: 1 }, Event::UnsignedPhaseStarted { round: 1 }, - Event::ElectionFailed + Event::ElectionFailed, + Event::EmergencyPhaseStarted { round: 1 }, ] ); }) @@ -2342,14 +2400,17 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::UnsignedPhaseStarted { round: 1 }, Event::ElectionFailed, + Event::EmergencyPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Fallback, + account_id: None, prev_ejected: false }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: Default::default() - } + }, + Event::OffPhaseStarted { round: 2 }, ] ); }) @@ -2375,10 +2436,17 @@ mod tests { assert_eq!( multi_phase_events(), - vec![Event::ElectionFinalized { - compute: ElectionCompute::Fallback, - score: ElectionScore { minimal_stake: 0, sum_stake: 0, sum_stake_squared: 0 } - }] + vec![ + Event::ElectionFinalized { + compute: ElectionCompute::Fallback, + score: ElectionScore { + minimal_stake: 0, + sum_stake: 0, + sum_stake_squared: 0 + } + }, + Event::OffPhaseStarted { round: 2 }, + ] ); }); } @@ -2404,7 +2472,10 @@ mod tests { assert_eq!(err, ElectionError::Fallback("NoFallback.")); assert_eq!(MultiPhase::current_phase(), Phase::Emergency); - assert_eq!(multi_phase_events(), vec![Event::ElectionFailed]); + assert_eq!( + multi_phase_events(), + vec![Event::ElectionFailed, Event::EmergencyPhaseStarted { round: 1 }] + ); }); } diff --git a/frame/election-provider-multi-phase/src/signed.rs b/frame/election-provider-multi-phase/src/signed.rs index 12d39e83b6c09..b230f40ffc25c 100644 --- a/frame/election-provider-multi-phase/src/signed.rs +++ b/frame/election-provider-multi-phase/src/signed.rs @@ -621,7 +621,11 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false } + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + } ] ); }) @@ -646,7 +650,11 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, Event::Rewarded { account: 99, value: 7 } ] ); @@ -677,7 +685,11 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, Event::Slashed { account: 99, value: 5 } ] ); @@ -714,8 +726,16 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(999), + prev_ejected: false + }, Event::Rewarded { account: 99, value: 7 } ] ); @@ -789,11 +809,31 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(100), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(101), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(102), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(103), + prev_ejected: false + }, Event::Rewarded { account: 99, value: 7 }, Event::ElectionFinalized { compute: ElectionCompute::Signed, @@ -859,10 +899,12 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, + account_id: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, + account_id: Some(99), prev_ejected: true } ] @@ -1113,9 +1155,21 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(100), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(101), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(102), + prev_ejected: false + }, Event::Rewarded { account: 100, value: 7 }, Event::UnsignedPhaseStarted { round: 1 } ] @@ -1171,9 +1225,21 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(999), + prev_ejected: false + }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(9999), + prev_ejected: false + }, Event::Slashed { account: 999, value: 5 }, Event::Rewarded { account: 99, value: 7 } ] @@ -1305,7 +1371,11 @@ mod tests { multi_phase_events(), vec![ Event::SignedPhaseStarted { round: 1 }, - Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }, + Event::SolutionStored { + compute: ElectionCompute::Signed, + account_id: Some(99), + prev_ejected: false + }, Event::Rewarded { account: 99, value: 7 } ] ); diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index 7340605dfe621..71fcc2e25286e 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -1325,6 +1325,7 @@ mod tests { Event::UnsignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, + account_id: None, prev_ejected: false } ] diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index db9aeba6fb58e..2f0db230feb12 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -340,6 +340,7 @@ impl Pallet { matches!(ForceEra::::get(), Forcing::ForceNew) { ForceEra::::put(Forcing::NotForcing); + Self::deposit_event(Event::::ForceEra { mode: Forcing::NotForcing }); } maybe_new_era_validators @@ -721,7 +722,10 @@ impl Pallet { pub(crate) fn ensure_new_era() { match ForceEra::::get() { Forcing::ForceAlways | Forcing::ForceNew => (), - _ => ForceEra::::put(Forcing::ForceNew), + _ => { + ForceEra::::put(Forcing::ForceNew); + Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNew }); + }, } } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 8e8a8d9c7f600..f0d6fe83cd29a 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -710,6 +710,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] @@ -1374,6 +1376,7 @@ pub mod pallet { pub fn force_no_eras(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceNone); + Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNone }); Ok(()) } @@ -1398,6 +1401,7 @@ pub mod pallet { pub fn force_new_era(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceNew); + Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNew }); Ok(()) } @@ -1449,6 +1453,7 @@ pub mod pallet { pub fn force_new_era_always(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceAlways); + Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceAlways }); Ok(()) } diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 46c3c97441938..9f955b29ae620 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -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::::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::::get(), Forcing::NotForcing); }) From 9d1a25fd1df22d7adbbe944f4b0918c7b1de4b56 Mon Sep 17 00:00:00 2001 From: gpestana Date: Fri, 30 Dec 2022 14:59:44 +0000 Subject: [PATCH 2/8] Uses RawOrigin in ElectionCompute event --- .../election-provider-multi-phase/src/lib.rs | 37 +++++++++--------- .../src/signed.rs | 38 +++++++++---------- .../src/unsigned.rs | 2 +- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index d97cdaa7149dd..abe976eb22f20 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -241,7 +241,7 @@ use frame_support::{ weights::Weight, DefaultNoBound, EqNoBound, PartialEqNoBound, }; -use frame_system::{ensure_none, offchain::SendTransactionTypes}; +use frame_system::{ensure_none, offchain::SendTransactionTypes, RawOrigin}; use scale_info::TypeInfo; use sp_arithmetic::{ traits::{CheckedAdd, Zero}, @@ -931,7 +931,7 @@ pub mod pallet { >::put(ready); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Unsigned, - account_id: None, + origin: RawOrigin::None, prev_ejected: ejected_a_solution, }); @@ -968,7 +968,7 @@ pub mod pallet { origin: OriginFor, supports: Supports, ) -> DispatchResult { - T::ForceOrigin::ensure_origin(origin)?; + T::ForceOrigin::ensure_origin(origin.clone())?; ensure!(Self::current_phase().is_emergency(), >::CallNotAllowed); // bound supports with T::MaxWinners @@ -984,7 +984,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Emergency, - account_id: None, + origin: RawOrigin::Root, prev_ejected: QueuedSolution::::exists(), }); @@ -1062,7 +1062,7 @@ pub mod pallet { signed_submissions.put(); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(who), + origin: RawOrigin::Signed(who), prev_ejected: ejected_a_solution, }); Ok(()) @@ -1105,7 +1105,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Fallback, - account_id: None, + origin: RawOrigin::Root, prev_ejected: QueuedSolution::::exists(), }); @@ -1119,15 +1119,14 @@ pub mod pallet { pub enum Event { /// A solution was stored with the given compute. /// - /// If the solution is signed, this means that it hasn't yet been processed. If the - /// solution is unsigned, this means that it has also been processed. - /// - /// If `account_id` is `None` if the solution is stored during the unsigned phase or by + /// The `origin` indicates the origin of the solution. If the origin is `Signed`, the + /// solution was stored during `Phase::Signed`. If the origin is `Unsigned`, the solution + /// was stored during the `Phase::Unsigned`. `Root` signals that the solution was stored by /// `T::ForceOrigin`. The `bool` is `true` when a previous solution was ejected to make /// room for this one. SolutionStored { compute: ElectionCompute, - account_id: Option, + origin: RawOrigin, prev_ejected: bool, }, /// The election has been finalized, with the given computation and score. @@ -2175,27 +2174,27 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Slashed { account: 99, value: 5 }, @@ -2238,7 +2237,7 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 }, @@ -2288,7 +2287,7 @@ mod tests { Event::UnsignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, - account_id: None, + origin: RawOrigin::None, prev_ejected: false }, Event::ElectionFinalized { @@ -2403,7 +2402,7 @@ mod tests { Event::EmergencyPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Fallback, - account_id: None, + origin: RawOrigin::Root, prev_ejected: false }, Event::ElectionFinalized { diff --git a/frame/election-provider-multi-phase/src/signed.rs b/frame/election-provider-multi-phase/src/signed.rs index b230f40ffc25c..c73cb902a3c00 100644 --- a/frame/election-provider-multi-phase/src/signed.rs +++ b/frame/election-provider-multi-phase/src/signed.rs @@ -623,7 +623,7 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false } ] @@ -652,7 +652,7 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } @@ -687,7 +687,7 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Slashed { account: 99, value: 5 } @@ -728,12 +728,12 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(999), + origin: RawOrigin::Signed(999), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } @@ -811,27 +811,27 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(100), + origin: RawOrigin::Signed(100), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(101), + origin: RawOrigin::Signed(101), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(102), + origin: RawOrigin::Signed(102), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(103), + origin: RawOrigin::Signed(103), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 }, @@ -899,12 +899,12 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: true } ] @@ -1157,17 +1157,17 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(100), + origin: RawOrigin::Signed(100), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(101), + origin: RawOrigin::Signed(101), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(102), + origin: RawOrigin::Signed(102), prev_ejected: false }, Event::Rewarded { account: 100, value: 7 }, @@ -1227,17 +1227,17 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(999), + origin: RawOrigin::Signed(999), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(9999), + origin: RawOrigin::Signed(9999), prev_ejected: false }, Event::Slashed { account: 999, value: 5 }, @@ -1373,7 +1373,7 @@ mod tests { Event::SignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - account_id: Some(99), + origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index 71fcc2e25286e..e8b06de103a55 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -1325,7 +1325,7 @@ mod tests { Event::UnsignedPhaseStarted { round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, - account_id: None, + origin: RawOrigin::None, prev_ejected: false } ] From 73dcbd65890639221ec987b70f95c4fe39316f24 Mon Sep 17 00:00:00 2001 From: gpestana Date: Mon, 2 Jan 2023 11:43:26 +0100 Subject: [PATCH 3/8] Refactors new phase events to PhaseTransition event --- .../election-provider-multi-phase/src/lib.rs | 185 +++++++++++++----- .../src/signed.rs | 25 ++- .../src/unsigned.rs | 17 +- 3 files changed, 163 insertions(+), 64 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index abe976eb22f20..adb5fe718cd34 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -1139,14 +1139,8 @@ pub mod pallet { Rewarded { account: ::AccountId, value: BalanceOf }, /// An account has been slashed for submitting an invalid signed submission. Slashed { account: ::AccountId, value: BalanceOf }, - /// The signed phase of the given round has started. - SignedPhaseStarted { round: u32 }, - /// The unsigned phase of the given round has started. - UnsignedPhaseStarted { round: u32 }, - /// The `Phase::Off` of the given round has started. - OffPhaseStarted { round: u32 }, - /// The emergency phase has started in the given round. - EmergencyPhaseStarted { round: u32 }, + /// There was a phase transition in a given round. + PhaseTransition { from: Phase, to: Phase, round: u32 }, } /// Error of the pallet that can be returned in response to dispatches. @@ -1365,16 +1359,24 @@ impl Pallet { /// Logic for `::on_initialize` when signed phase is being opened. pub fn on_initialize_open_signed() { log!(info, "Starting signed phase round {}.", Self::round()); + Self::deposit_event(Event::PhaseTransition { + from: >::get(), + to: Phase::Signed, + round: Self::round(), + }); >::put(Phase::Signed); - Self::deposit_event(Event::SignedPhaseStarted { round: Self::round() }); } /// Logic for `>::on_initialize` when unsigned phase is being opened. pub fn on_initialize_open_unsigned(enabled: bool, now: T::BlockNumber) { let round = Self::round(); log!(info, "Starting unsigned phase round {} enabled {}.", round, enabled); + Self::deposit_event(Event::PhaseTransition { + from: >::get(), + to: Phase::Unsigned((enabled, now)), + round: Self::round(), + }); >::put(Phase::Unsigned((enabled, now))); - Self::deposit_event(Event::UnsignedPhaseStarted { round }); } /// Parts of [`create_snapshot`] that happen inside of this pallet. @@ -1584,10 +1586,13 @@ impl Pallet { >::mutate(|r| *r += 1); // Phase is off now. + Self::deposit_event(Event::PhaseTransition { + from: >::get(), + to: Phase::Off, + round: Self::round(), + }); >::put(Phase::Off); - Self::deposit_event(Event::OffPhaseStarted { round: Self::round() }); - // Kill snapshots. Self::kill_snapshot(); } @@ -1667,8 +1672,12 @@ impl ElectionProvider for Pallet { }, Err(why) => { log!(error, "Entering emergency mode: {:?}", why); + Self::deposit_event(Event::PhaseTransition { + from: >::get(), + to: Phase::Emergency, + round: Self::round(), + }); >::put(Phase::Emergency); - Self::deposit_event(Event::EmergencyPhaseStarted { round: Self::round() }); Err(why) }, } @@ -1914,7 +1923,10 @@ mod tests { roll_to_signed(); assert_eq!(MultiPhase::current_phase(), Phase::Signed); - assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]); + assert_eq!( + multi_phase_events(), + vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + ); assert!(MultiPhase::snapshot().is_some()); assert_eq!(MultiPhase::round(), 1); @@ -1928,8 +1940,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 } + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, ], ); assert!(MultiPhase::snapshot().is_some()); @@ -1965,8 +1981,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { @@ -1975,9 +1995,17 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, - Event::SignedPhaseStarted { round: 2 }, - Event::UnsignedPhaseStarted { round: 2 } + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Off, + round: 2 + }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 2 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 55)), + round: 2 + }, ] ); }) @@ -2007,7 +2035,11 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { + from: Phase::Off, + to: Phase::Unsigned((true, 20)), + round: 1 + }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { @@ -2016,7 +2048,11 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 20)), + to: Phase::Off, + round: 2 + }, ] ); }); @@ -2046,7 +2082,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { @@ -2055,7 +2091,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, ] ) }); @@ -2092,7 +2128,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Off, round: 2 }, ] ); }); @@ -2105,7 +2141,10 @@ mod tests { // Signed phase started at block 15 and will end at 25. roll_to_signed(); - assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]); + assert_eq!( + multi_phase_events(), + vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + ); assert_eq!(MultiPhase::current_phase(), Phase::Signed); assert_eq!(MultiPhase::round(), 1); @@ -2116,12 +2155,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: Default::default() }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, ], ); // All storage items must be cleared. @@ -2141,7 +2180,10 @@ mod tests { // signed phase started at block 15 and will end at 25. roll_to_signed(); - assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]); + assert_eq!( + multi_phase_events(), + vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + ); assert_eq!(MultiPhase::current_phase(), Phase::Signed); assert_eq!(MultiPhase::round(), 1); @@ -2171,7 +2213,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -2210,7 +2252,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, ] ); }) @@ -2234,14 +2276,18 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFinalized { compute: ElectionCompute::Signed, score: ElectionScore { @@ -2250,7 +2296,11 @@ mod tests { sum_stake_squared: 5200 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Off, + round: 2 + }, ], ); }) @@ -2283,8 +2333,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::SolutionStored { compute: ElectionCompute::Unsigned, origin: RawOrigin::None, @@ -2298,7 +2352,11 @@ mod tests { sum_stake_squared: 5200 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Off, + round: 2 + }, ], ); }) @@ -2325,8 +2383,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { @@ -2335,7 +2397,11 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Off, + round: 2 + }, ] ); }); @@ -2355,10 +2421,18 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFailed, - Event::EmergencyPhaseStarted { round: 1 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Emergency, + round: 1 + }, ] ); }) @@ -2396,10 +2470,18 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFailed, - Event::EmergencyPhaseStarted { round: 1 }, + Event::PhaseTransition { + from: Phase::Unsigned((true, 25)), + to: Phase::Emergency, + round: 1 + }, Event::SolutionStored { compute: ElectionCompute::Fallback, origin: RawOrigin::Root, @@ -2409,7 +2491,7 @@ mod tests { compute: ElectionCompute::Fallback, score: Default::default() }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Emergency, to: Phase::Off, round: 2 }, ] ); }) @@ -2444,7 +2526,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::OffPhaseStarted { round: 2 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Off, round: 2 }, ] ); }); @@ -2473,7 +2555,10 @@ mod tests { assert_eq!( multi_phase_events(), - vec![Event::ElectionFailed, Event::EmergencyPhaseStarted { round: 1 }] + vec![ + Event::ElectionFailed, + Event::PhaseTransition { from: Phase::Off, to: Phase::Emergency, round: 1 } + ] ); }); } diff --git a/frame/election-provider-multi-phase/src/signed.rs b/frame/election-provider-multi-phase/src/signed.rs index c73cb902a3c00..cb64ef455d542 100644 --- a/frame/election-provider-multi-phase/src/signed.rs +++ b/frame/election-provider-multi-phase/src/signed.rs @@ -539,6 +539,7 @@ mod tests { use super::*; use crate::{mock::*, ElectionCompute, ElectionError, Error, Event, Perbill, Phase}; use frame_support::{assert_noop, assert_ok, assert_storage_noop}; + use frame_system::RawOrigin; #[test] fn cannot_submit_too_early() { @@ -620,7 +621,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -649,7 +650,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -684,7 +685,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -725,7 +726,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -808,7 +809,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -896,7 +897,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -1154,7 +1155,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(100), @@ -1171,7 +1172,11 @@ mod tests { prev_ejected: false }, Event::Rewarded { account: 100, value: 7 }, - Event::UnsignedPhaseStarted { round: 1 } + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, ] ); }) @@ -1224,7 +1229,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), @@ -1370,7 +1375,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, origin: RawOrigin::Signed(99), diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index e8b06de103a55..aa0c3d349b925 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -1064,6 +1064,7 @@ mod tests { use frame_support::{ assert_noop, assert_ok, bounded_vec, dispatch::Dispatchable, traits::OffchainWorker, }; + use frame_system::RawOrigin; use sp_npos_elections::ElectionScore; use sp_runtime::{ offchain::storage_lock::{BlockAndTime, StorageLock}, @@ -1321,8 +1322,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::SolutionStored { compute: ElectionCompute::Unsigned, origin: RawOrigin::None, @@ -1674,8 +1679,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::SignedPhaseStarted { round: 1 }, - Event::UnsignedPhaseStarted { round: 1 }, + Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransition { + from: Phase::Signed, + to: Phase::Unsigned((true, 25)), + round: 1 + }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { From 1685ac1ea94048494479190e47f4e69ba0987f41 Mon Sep 17 00:00:00 2001 From: gpestana Date: Tue, 3 Jan 2023 11:20:21 +0100 Subject: [PATCH 4/8] PhaseTransitioned and remove RawOrigin from event --- .../election-provider-multi-phase/src/lib.rs | 118 +++++++++--------- .../src/signed.rs | 59 +++++---- .../src/unsigned.rs | 11 +- 3 files changed, 93 insertions(+), 95 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index adb5fe718cd34..412ebba955cd5 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -241,7 +241,7 @@ use frame_support::{ weights::Weight, DefaultNoBound, EqNoBound, PartialEqNoBound, }; -use frame_system::{ensure_none, offchain::SendTransactionTypes, RawOrigin}; +use frame_system::{ensure_none, offchain::SendTransactionTypes}; use scale_info::TypeInfo; use sp_arithmetic::{ traits::{CheckedAdd, Zero}, @@ -931,7 +931,7 @@ pub mod pallet { >::put(ready); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Unsigned, - origin: RawOrigin::None, + origin: None, prev_ejected: ejected_a_solution, }); @@ -984,7 +984,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Emergency, - origin: RawOrigin::Root, + origin: None, prev_ejected: QueuedSolution::::exists(), }); @@ -1062,7 +1062,7 @@ pub mod pallet { signed_submissions.put(); Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(who), + origin: Some(who), prev_ejected: ejected_a_solution, }); Ok(()) @@ -1105,7 +1105,7 @@ pub mod pallet { Self::deposit_event(Event::SolutionStored { compute: ElectionCompute::Fallback, - origin: RawOrigin::Root, + origin: None, prev_ejected: QueuedSolution::::exists(), }); @@ -1119,14 +1119,14 @@ pub mod pallet { pub enum Event { /// A solution was stored with the given compute. /// - /// The `origin` indicates the origin of the solution. If the origin is `Signed`, the - /// solution was stored during `Phase::Signed`. If the origin is `Unsigned`, the solution - /// was stored during the `Phase::Unsigned`. `Root` signals that the solution was stored by + /// The `origin` indicates the origin of the solution. If `origin` is `Some(AccountId)`, + /// the stored solution was submited in the signed phase by a miner with the `AccountId`. + /// Otherwise, the solution was stored either during the unsigned phase or by /// `T::ForceOrigin`. The `bool` is `true` when a previous solution was ejected to make /// room for this one. SolutionStored { compute: ElectionCompute, - origin: RawOrigin, + origin: Option, prev_ejected: bool, }, /// The election has been finalized, with the given computation and score. @@ -1140,7 +1140,7 @@ pub mod pallet { /// An account has been slashed for submitting an invalid signed submission. Slashed { account: ::AccountId, value: BalanceOf }, /// There was a phase transition in a given round. - PhaseTransition { from: Phase, to: Phase, round: u32 }, + PhaseTransitioned { from: Phase, to: Phase, round: u32 }, } /// Error of the pallet that can be returned in response to dispatches. @@ -1359,7 +1359,7 @@ impl Pallet { /// Logic for `::on_initialize` when signed phase is being opened. pub fn on_initialize_open_signed() { log!(info, "Starting signed phase round {}.", Self::round()); - Self::deposit_event(Event::PhaseTransition { + Self::deposit_event(Event::PhaseTransitioned { from: >::get(), to: Phase::Signed, round: Self::round(), @@ -1371,7 +1371,7 @@ impl Pallet { pub fn on_initialize_open_unsigned(enabled: bool, now: T::BlockNumber) { let round = Self::round(); log!(info, "Starting unsigned phase round {} enabled {}.", round, enabled); - Self::deposit_event(Event::PhaseTransition { + Self::deposit_event(Event::PhaseTransitioned { from: >::get(), to: Phase::Unsigned((enabled, now)), round: Self::round(), @@ -1586,7 +1586,7 @@ impl Pallet { >::mutate(|r| *r += 1); // Phase is off now. - Self::deposit_event(Event::PhaseTransition { + Self::deposit_event(Event::PhaseTransitioned { from: >::get(), to: Phase::Off, round: Self::round(), @@ -1672,7 +1672,7 @@ impl ElectionProvider for Pallet { }, Err(why) => { log!(error, "Entering emergency mode: {:?}", why); - Self::deposit_event(Event::PhaseTransition { + Self::deposit_event(Event::PhaseTransitioned { from: >::get(), to: Phase::Emergency, round: Self::round(), @@ -1925,7 +1925,7 @@ mod tests { assert_eq!(MultiPhase::current_phase(), Phase::Signed); assert_eq!( multi_phase_events(), - vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + vec![Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }] ); assert!(MultiPhase::snapshot().is_some()); assert_eq!(MultiPhase::round(), 1); @@ -1940,8 +1940,8 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 @@ -1981,8 +1981,8 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 @@ -1995,13 +1995,13 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Off, round: 2 }, - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 2 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 2 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 55)), round: 2 @@ -2035,7 +2035,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Unsigned((true, 20)), round: 1 @@ -2048,7 +2048,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 20)), to: Phase::Off, round: 2 @@ -2082,7 +2082,7 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: ElectionScore { @@ -2091,7 +2091,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Off, round: 2 }, ] ) }); @@ -2128,7 +2128,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { from: Phase::Off, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Off, round: 2 }, ] ); }); @@ -2143,7 +2143,7 @@ mod tests { roll_to_signed(); assert_eq!( multi_phase_events(), - vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + vec![Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }] ); assert_eq!(MultiPhase::current_phase(), Phase::Signed); assert_eq!(MultiPhase::round(), 1); @@ -2155,12 +2155,12 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: Default::default() }, - Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Off, round: 2 }, ], ); // All storage items must be cleared. @@ -2182,7 +2182,7 @@ mod tests { roll_to_signed(); assert_eq!( multi_phase_events(), - vec![Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }] + vec![Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }] ); assert_eq!(MultiPhase::current_phase(), Phase::Signed); assert_eq!(MultiPhase::round(), 1); @@ -2213,30 +2213,30 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::Slashed { account: 99, value: 5 }, @@ -2252,7 +2252,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { from: Phase::Signed, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Off, round: 2 }, ] ); }) @@ -2276,14 +2276,14 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 @@ -2296,7 +2296,7 @@ mod tests { sum_stake_squared: 5200 } }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Off, round: 2 @@ -2333,15 +2333,15 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, - origin: RawOrigin::None, + origin: None, prev_ejected: false }, Event::ElectionFinalized { @@ -2352,7 +2352,7 @@ mod tests { sum_stake_squared: 5200 } }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Off, round: 2 @@ -2383,8 +2383,8 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 @@ -2397,7 +2397,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Off, round: 2 @@ -2421,14 +2421,14 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 }, Event::ElectionFailed, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Emergency, round: 1 @@ -2470,28 +2470,28 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 }, Event::ElectionFailed, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Unsigned((true, 25)), to: Phase::Emergency, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Fallback, - origin: RawOrigin::Root, + origin: None, prev_ejected: false }, Event::ElectionFinalized { compute: ElectionCompute::Fallback, score: Default::default() }, - Event::PhaseTransition { from: Phase::Emergency, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Emergency, to: Phase::Off, round: 2 }, ] ); }) @@ -2526,7 +2526,7 @@ mod tests { sum_stake_squared: 0 } }, - Event::PhaseTransition { from: Phase::Off, to: Phase::Off, round: 2 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Off, round: 2 }, ] ); }); @@ -2557,7 +2557,7 @@ mod tests { multi_phase_events(), vec![ Event::ElectionFailed, - Event::PhaseTransition { from: Phase::Off, to: Phase::Emergency, round: 1 } + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Emergency, round: 1 } ] ); }); diff --git a/frame/election-provider-multi-phase/src/signed.rs b/frame/election-provider-multi-phase/src/signed.rs index cb64ef455d542..895f3670a7f0d 100644 --- a/frame/election-provider-multi-phase/src/signed.rs +++ b/frame/election-provider-multi-phase/src/signed.rs @@ -539,7 +539,6 @@ mod tests { use super::*; use crate::{mock::*, ElectionCompute, ElectionError, Error, Event, Perbill, Phase}; use frame_support::{assert_noop, assert_ok, assert_storage_noop}; - use frame_system::RawOrigin; #[test] fn cannot_submit_too_early() { @@ -621,10 +620,10 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false } ] @@ -650,10 +649,10 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } @@ -685,10 +684,10 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::Slashed { account: 99, value: 5 } @@ -726,15 +725,15 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(999), + origin: Some(999), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } @@ -809,30 +808,30 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(100), + origin: Some(100), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(101), + origin: Some(101), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(102), + origin: Some(102), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(103), + origin: Some(103), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 }, @@ -897,15 +896,15 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: true } ] @@ -1155,24 +1154,24 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(100), + origin: Some(100), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(101), + origin: Some(101), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(102), + origin: Some(102), prev_ejected: false }, Event::Rewarded { account: 100, value: 7 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 @@ -1229,20 +1228,20 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(999), + origin: Some(999), prev_ejected: false }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(9999), + origin: Some(9999), prev_ejected: false }, Event::Slashed { account: 999, value: 5 }, @@ -1375,10 +1374,10 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, Event::SolutionStored { compute: ElectionCompute::Signed, - origin: RawOrigin::Signed(99), + origin: Some(99), prev_ejected: false }, Event::Rewarded { account: 99, value: 7 } diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index aa0c3d349b925..c4c58459fa340 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -1064,7 +1064,6 @@ mod tests { use frame_support::{ assert_noop, assert_ok, bounded_vec, dispatch::Dispatchable, traits::OffchainWorker, }; - use frame_system::RawOrigin; use sp_npos_elections::ElectionScore; use sp_runtime::{ offchain::storage_lock::{BlockAndTime, StorageLock}, @@ -1322,15 +1321,15 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 }, Event::SolutionStored { compute: ElectionCompute::Unsigned, - origin: RawOrigin::None, + origin: None, prev_ejected: false } ] @@ -1679,8 +1678,8 @@ mod tests { assert_eq!( multi_phase_events(), vec![ - Event::PhaseTransition { from: Phase::Off, to: Phase::Signed, round: 1 }, - Event::PhaseTransition { + Event::PhaseTransitioned { from: Phase::Off, to: Phase::Signed, round: 1 }, + Event::PhaseTransitioned { from: Phase::Signed, to: Phase::Unsigned((true, 25)), round: 1 From d682484d400b8dfd46753db025e2d62618428c00 Mon Sep 17 00:00:00 2001 From: gpestana Date: Tue, 3 Jan 2023 12:37:37 +0100 Subject: [PATCH 5/8] Adds helpers for epm phase transition and staking force new --- .../election-provider-multi-phase/src/lib.rs | 38 +++++++------------ .../src/unsigned.rs | 13 +++++-- frame/staking/src/pallet/impls.rs | 15 +++++--- frame/staking/src/pallet/mod.rs | 9 ++--- frame/staking/src/tests.rs | 10 ++--- 5 files changed, 39 insertions(+), 46 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 412ebba955cd5..3951ecd543644 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -1356,27 +1356,25 @@ impl Pallet { } } - /// Logic for `::on_initialize` when signed phase is being opened. - pub fn on_initialize_open_signed() { - log!(info, "Starting signed phase round {}.", Self::round()); + /// Phase transition helper. + pub fn phase_transition(to: Phase) { + log!(info, "Starting phase {:?}, round {}.", to, Self::round()); Self::deposit_event(Event::PhaseTransitioned { from: >::get(), - to: Phase::Signed, + to, round: Self::round(), }); - >::put(Phase::Signed); + >::put(to); + } + + /// Logic for `::on_initialize` when signed phase is being opened. + pub fn on_initialize_open_signed() { + Self::phase_transition(Phase::Signed); } /// Logic for `>::on_initialize` when unsigned phase is being opened. pub fn on_initialize_open_unsigned(enabled: bool, now: T::BlockNumber) { - let round = Self::round(); - log!(info, "Starting unsigned phase round {} enabled {}.", round, enabled); - Self::deposit_event(Event::PhaseTransitioned { - from: >::get(), - to: Phase::Unsigned((enabled, now)), - round: Self::round(), - }); - >::put(Phase::Unsigned((enabled, now))); + Self::phase_transition(Phase::Unsigned((enabled, now))); } /// Parts of [`create_snapshot`] that happen inside of this pallet. @@ -1586,12 +1584,7 @@ impl Pallet { >::mutate(|r| *r += 1); // Phase is off now. - Self::deposit_event(Event::PhaseTransitioned { - from: >::get(), - to: Phase::Off, - round: Self::round(), - }); - >::put(Phase::Off); + Self::phase_transition(Phase::Off); // Kill snapshots. Self::kill_snapshot(); @@ -1672,12 +1665,7 @@ impl ElectionProvider for Pallet { }, Err(why) => { log!(error, "Entering emergency mode: {:?}", why); - Self::deposit_event(Event::PhaseTransitioned { - from: >::get(), - to: Phase::Emergency, - round: Self::round(), - }); - >::put(Phase::Emergency); + Self::phase_transition(Phase::Emergency); Err(why) }, } diff --git a/frame/election-provider-multi-phase/src/unsigned.rs b/frame/election-provider-multi-phase/src/unsigned.rs index c4c58459fa340..cdc71f7bf5bf0 100644 --- a/frame/election-provider-multi-phase/src/unsigned.rs +++ b/frame/election-provider-multi-phase/src/unsigned.rs @@ -1055,7 +1055,7 @@ mod tests { Runtime, RuntimeCall, RuntimeOrigin, System, TestNposSolution, TrimHelpers, UnsignedPhase, }, - CurrentPhase, Event, InvalidTransaction, Phase, QueuedSolution, TransactionSource, + Event, InvalidTransaction, Phase, QueuedSolution, TransactionSource, TransactionValidityError, }; use codec::Decode; @@ -1128,7 +1128,7 @@ mod tests { assert!(::pre_dispatch(&call).is_ok()); // unsigned -- but not enabled. - >::put(Phase::Unsigned((false, 25))); + MultiPhase::phase_transition(Phase::Unsigned((false, 25))); assert!(MultiPhase::current_phase().is_unsigned()); assert!(matches!( ::validate_unsigned( @@ -1666,7 +1666,7 @@ mod tests { let current_block = block_plus(offchain_repeat * 2 + 2); // force the unsigned phase to start on the current block. - CurrentPhase::::set(Phase::Unsigned((true, current_block))); + MultiPhase::phase_transition(Phase::Unsigned((true, current_block))); // clear the cache and create a solution since we are on the first block of the unsigned // phase. @@ -1691,7 +1691,12 @@ mod tests { sum_stake: 0, sum_stake_squared: 0 } - } + }, + Event::PhaseTransitioned { + from: Phase::Unsigned((true, 25)), + to: Phase::Unsigned((true, 37)), + round: 1 + }, ] ); }) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index 2f0db230feb12..7af9b0aaaa04a 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -339,8 +339,7 @@ impl Pallet { if maybe_new_era_validators.is_some() && matches!(ForceEra::::get(), Forcing::ForceNew) { - ForceEra::::put(Forcing::NotForcing); - Self::deposit_event(Event::::ForceEra { mode: Forcing::NotForcing }); + Self::set_force_era(Forcing::NotForcing); } maybe_new_era_validators @@ -718,14 +717,18 @@ impl Pallet { } } + /// Helper to set a new `ForceEra` mode. + pub(crate) fn set_force_era(mode: Forcing) { + log!(info, "Setting force era mode {:?}.", mode); + ForceEra::::put(mode); + Self::deposit_event(Event::::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::::get() { Forcing::ForceAlways | Forcing::ForceNew => (), - _ => { - ForceEra::::put(Forcing::ForceNew); - Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNew }); - }, + _ => Self::set_force_era(Forcing::ForceNew), } } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index f0d6fe83cd29a..2ab8022884c8e 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -1375,8 +1375,7 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::force_no_eras())] pub fn force_no_eras(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; - ForceEra::::put(Forcing::ForceNone); - Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNone }); + Self::set_force_era(Forcing::ForceNone); Ok(()) } @@ -1400,8 +1399,7 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::force_new_era())] pub fn force_new_era(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; - ForceEra::::put(Forcing::ForceNew); - Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceNew }); + Self::set_force_era(Forcing::ForceNew); Ok(()) } @@ -1452,8 +1450,7 @@ pub mod pallet { #[pallet::weight(T::WeightInfo::force_new_era_always())] pub fn force_new_era_always(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; - ForceEra::::put(Forcing::ForceAlways); - Self::deposit_event(Event::::ForceEra { mode: Forcing::ForceAlways }); + Self::set_force_era(Forcing::ForceAlways); Ok(()) } diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 9f955b29ae620..acd41895287c5 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -899,7 +899,7 @@ fn forcing_new_era_works() { assert_eq!(active_era(), 1); // no era change. - ForceEra::::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::::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::::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::::put(Forcing::ForceNew); + Staking::set_force_era(Forcing::ForceNew); start_session(13); assert_eq!(active_era(), 5); assert_eq!(ForceEra::::get(), Forcing::NotForcing); @@ -2303,7 +2303,7 @@ fn era_is_always_same_length() { ); let session = Session::current_index(); - ForceEra::::put(Forcing::ForceNew); + Staking::set_force_era(Forcing::ForceNew); advance_session(); advance_session(); assert_eq!(current_era(), 3); From dd45479b5c0f721da81364061e93ca2576caa995 Mon Sep 17 00:00:00 2001 From: gpestana Date: Wed, 4 Jan 2023 12:51:36 +0100 Subject: [PATCH 6/8] addresses review comments --- frame/election-provider-multi-phase/src/lib.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 3951ecd543644..7037e3e95afdd 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -758,7 +758,7 @@ pub mod pallet { // NOTE: if signed-phase length is zero, second part of the if-condition fails. match Self::create_snapshot() { Ok(_) => { - Self::on_initialize_open_signed(); + Self::phase_transition(Phase::Signed); T::WeightInfo::on_initialize_open_signed() }, Err(why) => { @@ -797,7 +797,7 @@ pub mod pallet { if need_snapshot { match Self::create_snapshot() { Ok(_) => { - Self::on_initialize_open_unsigned(enabled, now); + Self::phase_transition(Phase::Unsigned((enabled, now))); T::WeightInfo::on_initialize_open_unsigned() }, Err(why) => { @@ -806,7 +806,7 @@ pub mod pallet { }, } } else { - Self::on_initialize_open_unsigned(enabled, now); + Self::phase_transition(Phase::Unsigned((enabled, now))); T::WeightInfo::on_initialize_open_unsigned() } }, @@ -1357,7 +1357,7 @@ impl Pallet { } /// Phase transition helper. - pub fn phase_transition(to: Phase) { + pub(crate) fn phase_transition(to: Phase) { log!(info, "Starting phase {:?}, round {}.", to, Self::round()); Self::deposit_event(Event::PhaseTransitioned { from: >::get(), @@ -1367,16 +1367,6 @@ impl Pallet { >::put(to); } - /// Logic for `::on_initialize` when signed phase is being opened. - pub fn on_initialize_open_signed() { - Self::phase_transition(Phase::Signed); - } - - /// Logic for `>::on_initialize` when unsigned phase is being opened. - pub fn on_initialize_open_unsigned(enabled: bool, now: T::BlockNumber) { - Self::phase_transition(Phase::Unsigned((enabled, now))); - } - /// Parts of [`create_snapshot`] that happen inside of this pallet. /// /// Extracted for easier weight calculation. From 26f7f65202e4994719596715b4fcc4578d693259 Mon Sep 17 00:00:00 2001 From: gpestana Date: Wed, 4 Jan 2023 12:55:37 +0100 Subject: [PATCH 7/8] nit: removes unecessary clone --- frame/election-provider-multi-phase/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/election-provider-multi-phase/src/lib.rs b/frame/election-provider-multi-phase/src/lib.rs index 7037e3e95afdd..6c1b1d163d0fb 100644 --- a/frame/election-provider-multi-phase/src/lib.rs +++ b/frame/election-provider-multi-phase/src/lib.rs @@ -968,7 +968,7 @@ pub mod pallet { origin: OriginFor, supports: Supports, ) -> DispatchResult { - T::ForceOrigin::ensure_origin(origin.clone())?; + T::ForceOrigin::ensure_origin(origin)?; ensure!(Self::current_phase().is_emergency(), >::CallNotAllowed); // bound supports with T::MaxWinners From 641b28743503f854738666dc4e25c07466c6bcfb Mon Sep 17 00:00:00 2001 From: gpestana Date: Mon, 9 Jan 2023 15:49:52 +0100 Subject: [PATCH 8/8] fixes benchmarks --- frame/election-provider-multi-phase/src/benchmarking.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/frame/election-provider-multi-phase/src/benchmarking.rs b/frame/election-provider-multi-phase/src/benchmarking.rs index 10041f6aec07c..16263d97da586 100644 --- a/frame/election-provider-multi-phase/src/benchmarking.rs +++ b/frame/election-provider-multi-phase/src/benchmarking.rs @@ -200,7 +200,7 @@ frame_benchmarking::benchmarks! { assert!(>::snapshot().is_none()); assert!(>::current_phase().is_off()); }: { - >::on_initialize_open_signed(); + >::phase_transition(Phase::Signed); } verify { assert!(>::snapshot().is_none()); assert!(>::current_phase().is_signed()); @@ -210,7 +210,8 @@ frame_benchmarking::benchmarks! { assert!(>::snapshot().is_none()); assert!(>::current_phase().is_off()); }: { - >::on_initialize_open_unsigned(true, 1u32.into()) + let now = frame_system::Pallet::::block_number(); + >::phase_transition(Phase::Unsigned((true, now))); } verify { assert!(>::snapshot().is_none()); assert!(>::current_phase().is_unsigned()); @@ -318,7 +319,7 @@ frame_benchmarking::benchmarks! { submit { // the queue is full and the solution is only better than the worse. >::create_snapshot().map_err(<&str>::from)?; - MultiPhase::::on_initialize_open_signed(); + >::phase_transition(Phase::Signed); >::put(1); let mut signed_submissions = SignedSubmissions::::get();