From e37002f9abba90184685b9be0b76dd6f4d019371 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 29 Jul 2021 18:46:29 +0200 Subject: [PATCH 1/4] Add some important events to batch & staking to ensure ease of analysis --- frame/staking/src/pallet/impls.rs | 11 +++++++---- frame/staking/src/pallet/mod.rs | 12 +++++++----- frame/staking/src/slashing.rs | 2 +- frame/staking/src/tests.rs | 10 +++++----- frame/utility/src/lib.rs | 4 ++++ 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/frame/staking/src/pallet/impls.rs b/frame/staking/src/pallet/impls.rs index b42ab4551602a..accd7a0cf02eb 100644 --- a/frame/staking/src/pallet/impls.rs +++ b/frame/staking/src/pallet/impls.rs @@ -154,11 +154,13 @@ impl Pallet { let validator_exposure_part = Perbill::from_rational(exposure.own, exposure.total); let validator_staking_payout = validator_exposure_part * validator_leftover_payout; + Self::deposit_event(Event::::PayoutStarted(era, ledger.stash.clone())); + // We can now make total validator payout: if let Some(imbalance) = Self::make_payout(&ledger.stash, validator_staking_payout + validator_commission_payout) { - Self::deposit_event(Event::::Reward(ledger.stash, imbalance.peek())); + Self::deposit_event(Event::::Rewarded(ledger.stash, imbalance.peek())); } // Track the number of payout ops to nominators. Note: `WeightInfo::payout_stakers_alive_staked` @@ -176,7 +178,8 @@ impl Pallet { if let Some(imbalance) = Self::make_payout(&nominator.who, nominator_reward) { // Note: this logic does not count payouts for `RewardDestination::None`. nominator_payout_count += 1; - Self::deposit_event(Event::::Reward(nominator.who.clone(), imbalance.peek())); + let e = Event::::Rewarded(nominator.who.clone(), imbalance.peek()); + Self::deposit_event(e); } } @@ -354,7 +357,7 @@ impl Pallet { let issuance = T::Currency::total_issuance(); let (validator_payout, rest) = T::EraPayout::era_payout(staked, issuance, era_duration); - Self::deposit_event(Event::::EraPayout(active_era.index, validator_payout, rest)); + Self::deposit_event(Event::::EraPaid(active_era.index, validator_payout, rest)); // Set ending era reward. >::insert(&active_era.index, validator_payout); @@ -446,7 +449,7 @@ impl Pallet { return None } - Self::deposit_event(Event::StakingElection); + Self::deposit_event(Event::StakersElected); Some(Self::trigger_new_era(start_session_index, exposures)) } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 4e7f06ebab188..1a6691e693177 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -525,17 +525,17 @@ pub mod pallet { /// The era payout has been set; the first balance is the validator-payout; the second is /// the remainder from the maximum amount of reward. /// \[era_index, validator_payout, remainder\] - EraPayout(EraIndex, BalanceOf, BalanceOf), - /// The staker has been rewarded by this amount. \[stash, amount\] - Reward(T::AccountId, BalanceOf), + EraPaid(EraIndex, BalanceOf, BalanceOf), + /// The nominator has been rewarded by this amount. \[era_index, stash, amount\] + Rewarded(T::AccountId, BalanceOf), /// One validator (and its nominators) has been slashed by the given amount. /// \[validator, amount\] - Slash(T::AccountId, BalanceOf), + Slashed(T::AccountId, BalanceOf), /// An old slashing report from a prior era was discarded because it could /// not be processed. \[session_index\] OldSlashingReportDiscarded(SessionIndex), /// A new set of stakers was elected. - StakingElection, + StakersElected, /// An account has bonded this amount. \[stash, amount\] /// /// NOTE: This event is only emitted when funds are bonded via a dispatchable. Notably, @@ -553,6 +553,8 @@ pub mod pallet { /// An account has stopped participating as either a validator or nominator. /// \[stash\] Chilled(T::AccountId), + /// The stakers' rewards are getting paid. \[era_index, validator_stash\] + PayoutStarted(EraIndex, T::AccountId), } #[pallet::error] diff --git a/frame/staking/src/slashing.rs b/frame/staking/src/slashing.rs index 332c9ffc39069..3da79924d0a0e 100644 --- a/frame/staking/src/slashing.rs +++ b/frame/staking/src/slashing.rs @@ -584,7 +584,7 @@ pub fn do_slash( >::update_ledger(&controller, &ledger); // trigger the event - >::deposit_event(super::Event::::Slash(stash.clone(), value)); + >::deposit_event(super::Event::::Slashed(stash.clone(), value)); } } diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 69ce4e335f4b6..3cb7a74e8982b 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -257,7 +257,7 @@ fn rewards_should_work() { ); assert_eq!( *mock::staking_events().last().unwrap(), - Event::EraPayout(0, total_payout_0, maximum_payout - total_payout_0) + Event::EraPaid(0, total_payout_0, maximum_payout - total_payout_0) ); mock::make_all_reward_payment(0); @@ -295,7 +295,7 @@ fn rewards_should_work() { ); assert_eq!( *mock::staking_events().last().unwrap(), - Event::EraPayout(1, total_payout_1, maximum_payout - total_payout_1) + Event::EraPaid(1, total_payout_1, maximum_payout - total_payout_1) ); mock::make_all_reward_payment(1); @@ -3942,7 +3942,7 @@ mod election_data_provider { run_to_block(20); assert_eq!(Staking::next_election_prediction(System::block_number()), 45); assert_eq!(staking_events().len(), 1); - assert_eq!(*staking_events().last().unwrap(), Event::StakingElection); + assert_eq!(*staking_events().last().unwrap(), Event::StakersElected); for b in 21..45 { run_to_block(b); @@ -3953,7 +3953,7 @@ mod election_data_provider { run_to_block(45); assert_eq!(Staking::next_election_prediction(System::block_number()), 70); assert_eq!(staking_events().len(), 3); - assert_eq!(*staking_events().last().unwrap(), Event::StakingElection); + assert_eq!(*staking_events().last().unwrap(), Event::StakersElected); Staking::force_no_eras(Origin::root()).unwrap(); assert_eq!(Staking::next_election_prediction(System::block_number()), u64::MAX); @@ -3976,7 +3976,7 @@ mod election_data_provider { 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::StakingElection); + assert_eq!(*staking_events().last().unwrap(), Event::StakersElected); // The new era has been planned, forcing is changed from `ForceNew` to `NotForcing`. assert_eq!(ForceEra::::get(), Forcing::NotForcing); }) diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 1133bd8698574..81cc3c65238b9 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -108,6 +108,8 @@ pub mod pallet { BatchInterrupted(u32, DispatchError), /// Batch of dispatches completed fully with no error. BatchCompleted, + /// A single item within a Batch of dispatches has completed with no error. + ItemCompleted, } #[pallet::call] @@ -173,6 +175,7 @@ pub mod pallet { // Return the actual used weight + base_weight of this call. return Ok(Some(base_weight + weight).into()) } + Self::deposit_event(Event::ItemCompleted); } Self::deposit_event(Event::BatchCompleted); let base_weight = T::WeightInfo::batch(calls_len as u32); @@ -289,6 +292,7 @@ pub mod pallet { err.post_info = Some(base_weight + weight).into(); err })?; + Self::deposit_event(Event::ItemCompleted); } Self::deposit_event(Event::BatchCompleted); let base_weight = T::WeightInfo::batch_all(calls_len as u32); From a0759e054f805bc09be6d4dde85b98d1049c0c8a Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Thu, 29 Jul 2021 19:45:14 +0200 Subject: [PATCH 2/4] Update frame/staking/src/pallet/mod.rs Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> --- frame/staking/src/pallet/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 1a6691e693177..444768dbdccfa 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -526,7 +526,7 @@ pub mod pallet { /// the remainder from the maximum amount of reward. /// \[era_index, validator_payout, remainder\] EraPaid(EraIndex, BalanceOf, BalanceOf), - /// The nominator has been rewarded by this amount. \[era_index, stash, amount\] + /// The nominator has been rewarded by this amount. \[stash, amount\] Rewarded(T::AccountId, BalanceOf), /// One validator (and its nominators) has been slashed by the given amount. /// \[validator, amount\] From d67232c761224323c823a4514969dcfa926d910a Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 29 Jul 2021 19:48:58 +0200 Subject: [PATCH 3/4] Update lib.rs --- frame/offences/benchmarking/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index d68e29047a7c2..35e3c1aec9403 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -289,7 +289,7 @@ benchmarks! { let slash_amount = slash_fraction * bond_amount; let reward_amount = slash_amount * (1 + n) / 2; let slash = |id| core::iter::once( - ::Event::from(StakingEvent::::Slash(id, BalanceOf::::from(slash_amount))) + ::Event::from(StakingEvent::::Slashed(id, BalanceOf::::from(slash_amount))) ); let chill = |id| core::iter::once( ::Event::from(StakingEvent::::Chilled(id)) From 32c97c66999c4f60a23846c145aced3d8d8fb09f Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 29 Jul 2021 20:20:27 +0200 Subject: [PATCH 4/4] fix test --- frame/contracts/src/exec.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/frame/contracts/src/exec.rs b/frame/contracts/src/exec.rs index a862a98802e49..16c4886746d1d 100644 --- a/frame/contracts/src/exec.rs +++ b/frame/contracts/src/exec.rs @@ -2506,6 +2506,11 @@ mod tests { event: MetaEvent::System(frame_system::Event::Remarked(BOB, remark_hash)), topics: vec![], }, + EventRecord { + phase: Phase::Initialization, + event: MetaEvent::Utility(pallet_utility::Event::ItemCompleted), + topics: vec![], + }, EventRecord { phase: Phase::Initialization, event: MetaEvent::Utility(pallet_utility::Event::BatchInterrupted(