From d4042d74c28e6535f885b19a9d4fc68695220486 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Thu, 1 Jul 2021 07:14:16 -0700 Subject: [PATCH 1/6] Add `Chilled` event to staking chill extrinsics --- frame/staking/src/lib.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index ec7da1be18714..4738c5f911515 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -1329,6 +1329,9 @@ pub mod pallet { Kicked(T::AccountId, T::AccountId), /// The election failed. No new era is planned. StakingElectionFailed, + /// An account has stopped participating as either a validator or nominator. + /// \[stash\] + Chilled(T::AccountId), } #[pallet::error] @@ -2523,8 +2526,11 @@ impl Pallet { /// Chill a stash account. fn chill_stash(stash: &T::AccountId) { - Self::do_remove_validator(stash); - Self::do_remove_nominator(stash); + let chilled_as_validator = Self::do_remove_validator(stash); + let chilled_as_nominator = Self::do_remove_nominator(stash); + if chilled_as_validator || chilled_as_nominator { + Self::deposit_event(Event::::Chilled(stash.clone())); + } } /// Actually make a payment to a staker. This uses the currency's reward function @@ -3014,10 +3020,13 @@ impl Pallet { /// This function will remove a nominator from the `Nominators` storage map, /// and keep track of the `CounterForNominators`. - pub fn do_remove_nominator(who: &T::AccountId) { + pub fn do_remove_nominator(who: &T::AccountId) -> bool { if Nominators::::contains_key(who) { Nominators::::remove(who); CounterForNominators::::mutate(|x| x.saturating_dec()); + true + } else { + false } } @@ -3034,10 +3043,13 @@ impl Pallet { /// This function will remove a validator from the `Validators` storage map, /// and keep track of the `CounterForValidators`. - pub fn do_remove_validator(who: &T::AccountId) { + pub fn do_remove_validator(who: &T::AccountId) -> bool { if Validators::::contains_key(who) { Validators::::remove(who); CounterForValidators::::mutate(|x| x.saturating_dec()); + true + } else { + false } } } From c3010f5fcda58e30d35e38869abe8e750e9be957 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Thu, 1 Jul 2021 08:21:31 -0700 Subject: [PATCH 2/6] Update do_remove_{nom, val} doc comments --- frame/staking/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 4738c5f911515..1f22275bde9c3 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -3020,6 +3020,8 @@ impl Pallet { /// This function will remove a nominator from the `Nominators` storage map, /// and keep track of the `CounterForNominators`. + /// + /// Returns true if `who` was removed from `Nominators`, otherwise false. pub fn do_remove_nominator(who: &T::AccountId) -> bool { if Nominators::::contains_key(who) { Nominators::::remove(who); @@ -3043,6 +3045,8 @@ impl Pallet { /// This function will remove a validator from the `Validators` storage map, /// and keep track of the `CounterForValidators`. + /// + /// Returns true if `who` was removed from `Validators`, otherwise false. pub fn do_remove_validator(who: &T::AccountId) -> bool { if Validators::::contains_key(who) { Validators::::remove(who); From 8d134c52c7ca1a54588ad92a5701d274bdbeecb3 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Sat, 3 Jul 2021 15:14:56 -0700 Subject: [PATCH 3/6] Not working: trying to match on event --- frame/offences/benchmarking/src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index e7c61bfd989b4..333490c3f5679 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -209,7 +209,12 @@ fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) #[cfg(test)] fn check_events::Event>>(expected: I) { let events = System::::events() .into_iter() - .map(|frame_system::EventRecord { event, .. }| event).collect::>(); + .map(|frame_system::EventRecord { event, .. }| event) + .filter(|event| match event { + ::Event::from(StakingEvent::::Chilled(_)) => false, + _ => true + }) + .collect::>(); let expected = expected.collect::>(); let lengths = (events.len(), expected.len()); let length_mismatch = if lengths.0 != lengths.1 { From a87a72b0ca47bc45e6014a8e9603014703abea61 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Sat, 3 Jul 2021 16:39:28 -0700 Subject: [PATCH 4/6] Account for chilled event in offences benchmarking --- frame/offences/benchmarking/src/lib.rs | 28 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 333490c3f5679..d424cfc751eef 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -208,12 +208,9 @@ fn make_offenders_im_online(num_offenders: u32, num_nominators: u32) #[cfg(test)] fn check_events::Event>>(expected: I) { - let events = System::::events() .into_iter() + let events = System::::events() + .into_iter() .map(|frame_system::EventRecord { event, .. }| event) - .filter(|event| match event { - ::Event::from(StakingEvent::::Chilled(_)) => false, - _ => true - }) .collect::>(); let expected = expected.collect::>(); let lengths = (events.len(), expected.len()); @@ -278,13 +275,19 @@ benchmarks! { let bond_amount: u32 = UniqueSaturatedInto::::unique_saturated_into(bond_amount::()); 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))) + ); + let chill = |id| core::iter::once( + ::Event::from(StakingEvent::::Chilled(id)) + ); let mut slash_events = raw_offenders.into_iter() .flat_map(|offender| { - core::iter::once(offender.stash).chain(offender.nominator_stashes.into_iter()) + let nom_slashes = offender.nominator_stashes.into_iter().flat_map(|nom| slash(nom)); + chill(offender.stash.clone()) + .chain(slash(offender.stash)) + .chain(nom_slashes) }) - .map(|stash| ::Event::from( - StakingEvent::::Slash(stash, BalanceOf::::from(slash_amount)) - )) .collect::>(); let reward_events = reporters.into_iter() .flat_map(|reporter| vec![ @@ -294,8 +297,9 @@ benchmarks! { ).into() ]); - // rewards are applied after first offender and it's nominators - let slash_rest = slash_events.split_off(1 + n as usize); + // Rewards are applied after first offender and it's nominators. + // We split after: offender slash + offender chill + nominator slashes. + let slash_rest = slash_events.split_off(2 + n as usize); // make sure that all slashes have been applied #[cfg(test)] @@ -343,6 +347,7 @@ benchmarks! { + 1 // offence + 2 // reporter (reward + endowment) + 1 // offenders slashed + + 1 // offenders chilled + n // nominators slashed ); } @@ -377,6 +382,7 @@ benchmarks! { + 1 // offence + 2 // reporter (reward + endowment) + 1 // offenders slashed + + 1 // offenders chilled + n // nominators slashed ); } From 0d6726ff99cee23dfd3c6c5c9145f4100baed400 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Sun, 4 Jul 2021 12:39:12 -0700 Subject: [PATCH 5/6] trigger ci --- frame/offences/benchmarking/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index d424cfc751eef..7afa7b1179a8c 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -393,3 +393,4 @@ impl_benchmark_test_suite!( crate::mock::new_test_ext(), crate::mock::Test, ); + From 26667481d5dd3941c0bde30e3612b6a18175863a Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Sun, 4 Jul 2021 15:45:23 -0700 Subject: [PATCH 6/6] correct --- frame/offences/benchmarking/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 7afa7b1179a8c..d424cfc751eef 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -393,4 +393,3 @@ impl_benchmark_test_suite!( crate::mock::new_test_ext(), crate::mock::Test, ); -