From 0552de001e7a8db1f1e9535faafa5ef253ff51f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Pestana?= Date: Tue, 2 Jan 2024 17:38:54 +0000 Subject: [PATCH 1/4] Refactors OnStakingUpdate trait to avoid issues with data updates when using it --- substrate/frame/staking/src/mock.rs | 2 +- substrate/frame/staking/src/pallet/mod.rs | 2 +- substrate/primitives/staking/src/lib.rs | 26 +++++++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/substrate/frame/staking/src/mock.rs b/substrate/frame/staking/src/mock.rs index 5332dbfdd5b2..058f6522aaa0 100644 --- a/substrate/frame/staking/src/mock.rs +++ b/substrate/frame/staking/src/mock.rs @@ -280,7 +280,7 @@ parameter_types! { } pub struct EventListenerMock; -impl OnStakingUpdate for EventListenerMock { +impl OnStakingUpdate for EventListenerMock { fn on_slash( _pool_account: &AccountId, slashed_bonded: Balance, diff --git a/substrate/frame/staking/src/pallet/mod.rs b/substrate/frame/staking/src/pallet/mod.rs index b914545a76b9..0a2ff062cbf5 100644 --- a/substrate/frame/staking/src/pallet/mod.rs +++ b/substrate/frame/staking/src/pallet/mod.rs @@ -276,7 +276,7 @@ pub mod pallet { /// receives. /// /// WARNING: this only reports slashing events for the time being. - type EventListeners: sp_staking::OnStakingUpdate>; + type EventListeners: sp_staking::OnStakingUpdate, ValidatorPrefs>; /// Some parameters of the benchmarking. type BenchmarkingConfig: BenchmarkingConfig; diff --git a/substrate/primitives/staking/src/lib.rs b/substrate/primitives/staking/src/lib.rs index c2ac5ae004b1..039da25cda8d 100644 --- a/substrate/primitives/staking/src/lib.rs +++ b/substrate/primitives/staking/src/lib.rs @@ -95,24 +95,34 @@ pub struct Stake { /// pre-action data that is needed needs to be passed to interface methods. The rest of the data can /// be retrieved by using `StakingInterface`. #[impl_trait_for_tuples::impl_for_tuples(10)] -pub trait OnStakingUpdate { +pub trait OnStakingUpdate { /// Fired when the stake amount of someone updates. /// /// This is effectively any changes to the bond amount, such as bonding more funds, and /// unbonding. - fn on_stake_update(_who: &AccountId, _prev_stake: Option>) {} + fn on_stake_update( + _who: &AccountId, + _prev_stake: Option>, + _stake: Stake, + ) { + } /// Fired when someone sets their intention to nominate. /// /// This should never be fired for existing nominators. - fn on_nominator_add(_who: &AccountId) {} + fn on_nominator_add(_who: &AccountId, _nominations: Vec) {} /// Fired when an existing nominator updates their nominations. /// /// Note that this is not fired when a nominator changes their stake. For that, /// `on_stake_update` should be used, followed by querying whether `who` was a validator or a /// nominator. - fn on_nominator_update(_who: &AccountId, _prev_nominations: Vec) {} + fn on_nominator_update( + _who: &AccountId, + _prev_nominations: Vec, + _nominations: Vec, + ) { + } /// Fired when someone removes their intention to nominate, either due to chill or validating. /// @@ -123,15 +133,13 @@ pub trait OnStakingUpdate { /// Fired when someone sets their intention to validate. /// /// Note validator preference changes are not communicated, but could be added if needed. - fn on_validator_add(_who: &AccountId) {} + fn on_validator_add(_who: &AccountId, _self_stake: Option>) {} /// Fired when an existing validator updates their preferences. - /// - /// Note validator preference changes are not communicated, but could be added if needed. - fn on_validator_update(_who: &AccountId) {} + fn on_validator_update(_who: &AccountId, _prefs: Option) {} /// Fired when someone removes their intention to validate, either due to chill or nominating. - fn on_validator_remove(_who: &AccountId) {} + fn on_validator_remove(_who: &AccountId, _self_stake: Option>) {} /// Fired when someone is fully unstaked. fn on_unstake(_who: &AccountId) {} From 967ef177350df02cdbd4cfa172242ed20d9d9f2b Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Tue, 2 Jan 2024 17:45:08 +0000 Subject: [PATCH 2/4] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/frame/staking/src/pallet/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/substrate/frame/staking/src/pallet/mod.rs b/substrate/frame/staking/src/pallet/mod.rs index 0a2ff062cbf5..9d91ee1bfe0c 100644 --- a/substrate/frame/staking/src/pallet/mod.rs +++ b/substrate/frame/staking/src/pallet/mod.rs @@ -276,7 +276,11 @@ pub mod pallet { /// receives. /// /// WARNING: this only reports slashing events for the time being. - type EventListeners: sp_staking::OnStakingUpdate, ValidatorPrefs>; + type EventListeners: sp_staking::OnStakingUpdate< + Self::AccountId, + BalanceOf, + ValidatorPrefs, + >; /// Some parameters of the benchmarking. type BenchmarkingConfig: BenchmarkingConfig; From 7f1f9dfc59db531bcda1880e02cb2bb5a7c1a543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Pestana?= Date: Tue, 2 Jan 2024 23:52:27 +0100 Subject: [PATCH 3/4] Removes validator prefs as input to on_validator_update --- substrate/frame/staking/src/mock.rs | 2 +- substrate/frame/staking/src/pallet/mod.rs | 6 +----- substrate/primitives/staking/src/lib.rs | 6 ++++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/substrate/frame/staking/src/mock.rs b/substrate/frame/staking/src/mock.rs index 058f6522aaa0..5332dbfdd5b2 100644 --- a/substrate/frame/staking/src/mock.rs +++ b/substrate/frame/staking/src/mock.rs @@ -280,7 +280,7 @@ parameter_types! { } pub struct EventListenerMock; -impl OnStakingUpdate for EventListenerMock { +impl OnStakingUpdate for EventListenerMock { fn on_slash( _pool_account: &AccountId, slashed_bonded: Balance, diff --git a/substrate/frame/staking/src/pallet/mod.rs b/substrate/frame/staking/src/pallet/mod.rs index 9d91ee1bfe0c..b914545a76b9 100644 --- a/substrate/frame/staking/src/pallet/mod.rs +++ b/substrate/frame/staking/src/pallet/mod.rs @@ -276,11 +276,7 @@ pub mod pallet { /// receives. /// /// WARNING: this only reports slashing events for the time being. - type EventListeners: sp_staking::OnStakingUpdate< - Self::AccountId, - BalanceOf, - ValidatorPrefs, - >; + type EventListeners: sp_staking::OnStakingUpdate>; /// Some parameters of the benchmarking. type BenchmarkingConfig: BenchmarkingConfig; diff --git a/substrate/primitives/staking/src/lib.rs b/substrate/primitives/staking/src/lib.rs index 039da25cda8d..f7e9f11a961f 100644 --- a/substrate/primitives/staking/src/lib.rs +++ b/substrate/primitives/staking/src/lib.rs @@ -95,7 +95,7 @@ pub struct Stake { /// pre-action data that is needed needs to be passed to interface methods. The rest of the data can /// be retrieved by using `StakingInterface`. #[impl_trait_for_tuples::impl_for_tuples(10)] -pub trait OnStakingUpdate { +pub trait OnStakingUpdate { /// Fired when the stake amount of someone updates. /// /// This is effectively any changes to the bond amount, such as bonding more funds, and @@ -136,7 +136,9 @@ pub trait OnStakingUpdate { fn on_validator_add(_who: &AccountId, _self_stake: Option>) {} /// Fired when an existing validator updates their preferences. - fn on_validator_update(_who: &AccountId, _prefs: Option) {} + /// + /// Note validator preference changes are not communicated, but could be added if needed. + fn on_validator_update(_who: &AccountId) {} /// Fired when someone removes their intention to validate, either due to chill or nominating. fn on_validator_remove(_who: &AccountId, _self_stake: Option>) {} From 735defab4039979e4101c837427c4a74912e348c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Pestana?= Date: Wed, 3 Jan 2024 00:06:32 +0100 Subject: [PATCH 4/4] Adds prdoc --- prdoc/pr_2839.prdoc | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 prdoc/pr_2839.prdoc diff --git a/prdoc/pr_2839.prdoc b/prdoc/pr_2839.prdoc new file mode 100644 index 000000000000..d1f16cb11b0d --- /dev/null +++ b/prdoc/pr_2839.prdoc @@ -0,0 +1,8 @@ +title: "Refactors `OnStakingUpdate` trait" + +doc: + - audience: Runtime Dev + description: Refactors the `sp_staking:OnStakingUpdate` to make event inputs more explicit. + +crates: + - name: "sp-staking"