From 98e5b5a35e9b84245713418b4ed137965ab4b06a Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sun, 1 Dec 2024 03:10:03 +0100 Subject: [PATCH 1/5] Block number provider for pallet nomination pool --- polkadot/runtime/westend/src/lib.rs | 1 + substrate/bin/node/runtime/src/lib.rs | 1 + substrate/frame/delegated-staking/src/mock.rs | 1 + .../test-staking-e2e/src/mock.rs | 1 + .../nomination-pools/benchmarking/src/mock.rs | 1 + substrate/frame/nomination-pools/src/lib.rs | 18 ++++++++++++------ substrate/frame/nomination-pools/src/mock.rs | 1 + .../test-delegate-stake/src/mock.rs | 1 + .../test-transfer-stake/src/mock.rs | 1 + 9 files changed, 20 insertions(+), 6 deletions(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 9f0b701f20be..d0d72ee6ab5a 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1516,6 +1516,7 @@ impl pallet_nomination_pools::Config for Runtime { type PalletId = PoolsPalletId; type MaxPointsToBalance = MaxPointsToBalance; type AdminOrigin = EitherOf, StakingAdmin>; + type BlockNumberProvider = System; } parameter_types! { diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index bff263548087..68264a1ed47b 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -963,6 +963,7 @@ impl pallet_nomination_pools::Config for Runtime { EnsureRoot, pallet_collective::EnsureProportionAtLeast, >; + type BlockNumberProvider = System; } parameter_types! { diff --git a/substrate/frame/delegated-staking/src/mock.rs b/substrate/frame/delegated-staking/src/mock.rs index 811d5739f4e9..2d4f40b75017 100644 --- a/substrate/frame/delegated-staking/src/mock.rs +++ b/substrate/frame/delegated-staking/src/mock.rs @@ -160,6 +160,7 @@ impl pallet_nomination_pools::Config for Runtime { type StakeAdapter = pallet_nomination_pools::adapter::DelegateStake; type AdminOrigin = frame_system::EnsureRoot; + type BlockNumberProvider = System; } frame_support::construct_runtime!( diff --git a/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs b/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs index eaab848c1694..0cff17f49778 100644 --- a/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs +++ b/substrate/frame/election-provider-multi-phase/test-staking-e2e/src/mock.rs @@ -272,6 +272,7 @@ impl pallet_nomination_pools::Config for Runtime { type MaxUnbonding = MaxUnbonding; type MaxPointsToBalance = frame_support::traits::ConstU8<10>; type AdminOrigin = frame_system::EnsureRoot; + type BlockNumberProvider = System; } parameter_types! { diff --git a/substrate/frame/nomination-pools/benchmarking/src/mock.rs b/substrate/frame/nomination-pools/benchmarking/src/mock.rs index 15d9e2c56031..27da1bec72f6 100644 --- a/substrate/frame/nomination-pools/benchmarking/src/mock.rs +++ b/substrate/frame/nomination-pools/benchmarking/src/mock.rs @@ -139,6 +139,7 @@ impl pallet_nomination_pools::Config for Runtime { type PalletId = PoolsPalletId; type MaxPointsToBalance = MaxPointsToBalance; type AdminOrigin = frame_system::EnsureRoot; + type BlockNumberProvider = System; } parameter_types! { diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs index dc82bf3a37c6..1e3594970ca4 100644 --- a/substrate/frame/nomination-pools/src/lib.rs +++ b/substrate/frame/nomination-pools/src/lib.rs @@ -368,7 +368,6 @@ use frame_support::{ }, DefaultNoBound, PalletError, }; -use frame_system::pallet_prelude::BlockNumberFor; use scale_info::TypeInfo; use sp_core::U256; use sp_runtime::{ @@ -406,6 +405,7 @@ pub mod migration; pub mod weights; pub use pallet::*; +use sp_runtime::traits::BlockNumberProvider; pub use weights::WeightInfo; /// The balance type used by the currency system. @@ -416,6 +416,9 @@ pub type PoolId = u32; type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; +pub type BlockNumberFor = +<::BlockNumberProvider as BlockNumberProvider>::BlockNumber; + pub const POINTS_TO_BALANCE_INIT_RATIO: u32 = 1; /// Possible operations on the configuration values of this pallet. @@ -779,7 +782,7 @@ impl Commission { } else { // throttling if blocks passed is less than `min_delay`. let blocks_surpassed = - >::block_number().saturating_sub(f); + T::BlockNumberProvider::current_block_number().saturating_sub(f); blocks_surpassed < t.min_delay } }, @@ -892,7 +895,7 @@ impl Commission { /// Updates a commission's `throttle_from` field to the current block. fn register_update(&mut self) { - self.throttle_from = Some(>::block_number()); + self.throttle_from = Some(T::BlockNumberProvider::current_block_number()); } /// Checks whether a change rate is less restrictive than the current change rate, if any. @@ -1565,7 +1568,7 @@ impl Get for TotalUnbondingPools { pub mod pallet { use super::*; use frame_support::traits::StorageVersion; - use frame_system::{ensure_signed, pallet_prelude::*}; + use frame_system::{pallet_prelude::{OriginFor, ensure_root, ensure_signed}}; use sp_runtime::Perbill; /// The in-code storage version. @@ -1650,6 +1653,9 @@ pub mod pallet { /// The origin that can manage pool configurations. type AdminOrigin: EnsureOrigin; + + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider; } /// The sum of funds across all pools. @@ -3092,9 +3098,9 @@ pub mod pallet { } #[pallet::hooks] - impl Hooks> for Pallet { + impl Hooks> for Pallet { #[cfg(feature = "try-runtime")] - fn try_state(_n: BlockNumberFor) -> Result<(), TryRuntimeError> { + fn try_state(_n: frame_system::pallet_prelude::BlockNumberFor) -> Result<(), TryRuntimeError> { Self::do_try_state(u8::MAX) } diff --git a/substrate/frame/nomination-pools/src/mock.rs b/substrate/frame/nomination-pools/src/mock.rs index cc942039760c..d92c603f4cc8 100644 --- a/substrate/frame/nomination-pools/src/mock.rs +++ b/substrate/frame/nomination-pools/src/mock.rs @@ -302,6 +302,7 @@ impl pools::Config for Runtime { type MaxUnbonding = MaxUnbonding; type MaxPointsToBalance = frame_support::traits::ConstU8<10>; type AdminOrigin = EnsureSignedBy; + type BlockNumberProvider = System; } type Block = frame_system::mocking::MockBlock; diff --git a/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs b/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs index d1bc4ef8ff28..ca1f6205d61d 100644 --- a/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs +++ b/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs @@ -265,6 +265,7 @@ impl pallet_nomination_pools::Config for Runtime { type MaxPointsToBalance = ConstU8<10>; type PalletId = PoolsPalletId; type AdminOrigin = EnsureRoot; + type BlockNumberProvider = System; } parameter_types! { diff --git a/substrate/frame/nomination-pools/test-transfer-stake/src/mock.rs b/substrate/frame/nomination-pools/test-transfer-stake/src/mock.rs index d913c5fe6948..2efde35fd6fe 100644 --- a/substrate/frame/nomination-pools/test-transfer-stake/src/mock.rs +++ b/substrate/frame/nomination-pools/test-transfer-stake/src/mock.rs @@ -145,6 +145,7 @@ impl pallet_nomination_pools::Config for Runtime { type MaxPointsToBalance = ConstU8<10>; type PalletId = PoolsPalletId; type AdminOrigin = frame_system::EnsureRoot; + type BlockNumberProvider = System; } type Block = frame_system::mocking::MockBlock; From 2c2e28f992bdae8a86a0650cb4463229e5b36c50 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sun, 1 Dec 2024 03:11:15 +0100 Subject: [PATCH 2/5] fmt --- substrate/frame/nomination-pools/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs index 1e3594970ca4..379189601643 100644 --- a/substrate/frame/nomination-pools/src/lib.rs +++ b/substrate/frame/nomination-pools/src/lib.rs @@ -417,7 +417,7 @@ pub type PoolId = u32; type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; pub type BlockNumberFor = -<::BlockNumberProvider as BlockNumberProvider>::BlockNumber; + <::BlockNumberProvider as BlockNumberProvider>::BlockNumber; pub const POINTS_TO_BALANCE_INIT_RATIO: u32 = 1; @@ -1568,7 +1568,7 @@ impl Get for TotalUnbondingPools { pub mod pallet { use super::*; use frame_support::traits::StorageVersion; - use frame_system::{pallet_prelude::{OriginFor, ensure_root, ensure_signed}}; + use frame_system::pallet_prelude::{ensure_root, ensure_signed, OriginFor}; use sp_runtime::Perbill; /// The in-code storage version. @@ -3100,7 +3100,9 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { #[cfg(feature = "try-runtime")] - fn try_state(_n: frame_system::pallet_prelude::BlockNumberFor) -> Result<(), TryRuntimeError> { + fn try_state( + _n: frame_system::pallet_prelude::BlockNumberFor, + ) -> Result<(), TryRuntimeError> { Self::do_try_state(u8::MAX) } From 139bab6df05dd82eae49787a5ea9e6601d00edd6 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sun, 1 Dec 2024 03:14:39 +0100 Subject: [PATCH 3/5] pr doc --- prdoc/prdoc/pr_6715.prdoc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 prdoc/prdoc/pr_6715.prdoc diff --git a/prdoc/prdoc/pr_6715.prdoc b/prdoc/prdoc/pr_6715.prdoc new file mode 100644 index 000000000000..ee4f6a157af4 --- /dev/null +++ b/prdoc/prdoc/pr_6715.prdoc @@ -0,0 +1,14 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Update Nomination Pool Pallet to Support Block Number Provider + +doc: + - audience: Runtime Dev + description: | + This PR makes the nomination pool pallet use the relay chain as a block provider for a parachain on a regular schedule. + To migrate existing nomination pool implementations, simply add `type BlockNumberProvider = System` to have the same behavior as before. + +crates: +- name: pallet-nomination-pools + bump: minor \ No newline at end of file From 04559e1ae4a2e09fcd5047c565f3cb851fd9489a Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sun, 1 Dec 2024 17:19:31 +0100 Subject: [PATCH 4/5] review changes --- prdoc/prdoc/pr_6715.prdoc | 2 +- substrate/frame/nomination-pools/src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/prdoc/prdoc/pr_6715.prdoc b/prdoc/prdoc/pr_6715.prdoc index ee4f6a157af4..1219f3d10228 100644 --- a/prdoc/prdoc/pr_6715.prdoc +++ b/prdoc/prdoc/pr_6715.prdoc @@ -11,4 +11,4 @@ doc: crates: - name: pallet-nomination-pools - bump: minor \ No newline at end of file + bump: major \ No newline at end of file diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs index 379189601643..27e3e0b54b82 100644 --- a/substrate/frame/nomination-pools/src/lib.rs +++ b/substrate/frame/nomination-pools/src/lib.rs @@ -1568,7 +1568,9 @@ impl Get for TotalUnbondingPools { pub mod pallet { use super::*; use frame_support::traits::StorageVersion; - use frame_system::pallet_prelude::{ensure_root, ensure_signed, OriginFor}; + use frame_system::pallet_prelude::{ + ensure_root, ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor, + }; use sp_runtime::Perbill; /// The in-code storage version. @@ -3098,11 +3100,9 @@ pub mod pallet { } #[pallet::hooks] - impl Hooks> for Pallet { + impl Hooks> for Pallet { #[cfg(feature = "try-runtime")] - fn try_state( - _n: frame_system::pallet_prelude::BlockNumberFor, - ) -> Result<(), TryRuntimeError> { + fn try_state(_n: SystemBlockNumberFor) -> Result<(), TryRuntimeError> { Self::do_try_state(u8::MAX) } From c9297a1607f9a651774d93809fea6a587f105219 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Tue, 3 Dec 2024 10:09:02 +0100 Subject: [PATCH 5/5] doc review changes --- prdoc/{prdoc => }/pr_6715.prdoc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) rename prdoc/{prdoc => }/pr_6715.prdoc (54%) diff --git a/prdoc/prdoc/pr_6715.prdoc b/prdoc/pr_6715.prdoc similarity index 54% rename from prdoc/prdoc/pr_6715.prdoc rename to prdoc/pr_6715.prdoc index 1219f3d10228..89cc2be5d4d9 100644 --- a/prdoc/prdoc/pr_6715.prdoc +++ b/prdoc/pr_6715.prdoc @@ -6,9 +6,8 @@ title: Update Nomination Pool Pallet to Support Block Number Provider doc: - audience: Runtime Dev description: | - This PR makes the nomination pool pallet use the relay chain as a block provider for a parachain on a regular schedule. - To migrate existing nomination pool implementations, simply add `type BlockNumberProvider = System` to have the same behavior as before. - + This PR makes the block number provider used in the nomination pool pallet configurable. Before this PR, nomination pool pallet always used the system block number, + with this PR some runtime can opt to use the relay chain block number instead. crates: - name: pallet-nomination-pools bump: major \ No newline at end of file