From 10b77c95429c98412a2bb0a39436519cbf3b0950 Mon Sep 17 00:00:00 2001
From: dharjeezy <dharjeezy@gmail.com>
Date: Sat, 8 Feb 2025 05:44:46 +0100
Subject: [PATCH] Update pallet nomination pool to support Block Number
 Provider (#6715)

This PR introduces BlockNumberProvider config for the nomination pool
pallet.
closes part of https://github.com/paritytech/polkadot-sdk/issues/6297
---
 polkadot/runtime/westend/src/lib.rs           |  1 +
 prdoc/pr_6715.prdoc                           | 14 +++++++++++++
 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   | 20 +++++++++++++------
 substrate/frame/nomination-pools/src/mock.rs  |  1 +
 .../test-delegate-stake/src/mock.rs           |  1 +
 9 files changed, 35 insertions(+), 6 deletions(-)
 create mode 100644 prdoc/pr_6715.prdoc

diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs
index 51eede7c734..7fd2ac53530 100644
--- a/polkadot/runtime/westend/src/lib.rs
+++ b/polkadot/runtime/westend/src/lib.rs
@@ -1534,6 +1534,7 @@ impl pallet_nomination_pools::Config for Runtime {
 	type PalletId = PoolsPalletId;
 	type MaxPointsToBalance = MaxPointsToBalance;
 	type AdminOrigin = EitherOf<EnsureRoot<AccountId>, StakingAdmin>;
+	type BlockNumberProvider = System;
 }
 
 parameter_types! {
diff --git a/prdoc/pr_6715.prdoc b/prdoc/pr_6715.prdoc
new file mode 100644
index 00000000000..d7b55509f3f
--- /dev/null
+++ b/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 block number provider in the Society pallet configurable so that runtimes can choose between using the system block number or an alternative source like the relay chain’s block number.
+       If you want to keep the existing behavior, simply set the provider to System. For scenarios that require a different notion of block number—such as using a relay chain number you can select another provider,
+       ensuring flexibility in how the nomination pools pallet references the current block.
+crates:
+- name: pallet-nomination-pools
+  bump: major
\ No newline at end of file
diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs
index 4e04dc22468..8986d61da6b 100644
--- a/substrate/bin/node/runtime/src/lib.rs
+++ b/substrate/bin/node/runtime/src/lib.rs
@@ -990,6 +990,7 @@ impl pallet_nomination_pools::Config for Runtime {
 		EnsureRoot<AccountId>,
 		pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 3, 4>,
 	>;
+	type BlockNumberProvider = System;
 }
 
 parameter_types! {
diff --git a/substrate/frame/delegated-staking/src/mock.rs b/substrate/frame/delegated-staking/src/mock.rs
index a4546e57dab..42b876d049a 100644
--- a/substrate/frame/delegated-staking/src/mock.rs
+++ b/substrate/frame/delegated-staking/src/mock.rs
@@ -161,6 +161,7 @@ impl pallet_nomination_pools::Config for Runtime {
 	type StakeAdapter =
 		pallet_nomination_pools::adapter::DelegateStake<Self, Staking, DelegatedStaking>;
 	type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
+	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 3a649643618..8118dfa2045 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
@@ -277,6 +277,7 @@ impl pallet_nomination_pools::Config for Runtime {
 	type MaxUnbonding = MaxUnbonding;
 	type MaxPointsToBalance = frame_support::traits::ConstU8<10>;
 	type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
+	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 7c09cf22ad5..39ff6fb7a09 100644
--- a/substrate/frame/nomination-pools/benchmarking/src/mock.rs
+++ b/substrate/frame/nomination-pools/benchmarking/src/mock.rs
@@ -140,6 +140,7 @@ impl pallet_nomination_pools::Config for Runtime {
 	type PalletId = PoolsPalletId;
 	type MaxPointsToBalance = MaxPointsToBalance;
 	type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
+	type BlockNumberProvider = System;
 }
 
 parameter_types! {
diff --git a/substrate/frame/nomination-pools/src/lib.rs b/substrate/frame/nomination-pools/src/lib.rs
index 04736e6c1aa..b45861289a5 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<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
 
+pub type BlockNumberFor<T> =
+	<<T as Config>::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<T: Config> Commission<T> {
 					} else {
 						// throttling if blocks passed is less than `min_delay`.
 						let blocks_surpassed =
-							<frame_system::Pallet<T>>::block_number().saturating_sub(f);
+							T::BlockNumberProvider::current_block_number().saturating_sub(f);
 						blocks_surpassed < t.min_delay
 					}
 				},
@@ -892,7 +895,7 @@ impl<T: Config> Commission<T> {
 
 	/// Updates a commission's `throttle_from` field to the current block.
 	fn register_update(&mut self) {
-		self.throttle_from = Some(<frame_system::Pallet<T>>::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,9 @@ impl<T: Config> Get<u32> for TotalUnbondingPools<T> {
 pub mod pallet {
 	use super::*;
 	use frame_support::traits::StorageVersion;
-	use frame_system::{ensure_signed, pallet_prelude::*};
+	use frame_system::pallet_prelude::{
+		ensure_root, ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
+	};
 	use sp_runtime::Perbill;
 
 	/// The in-code storage version.
@@ -1650,6 +1655,9 @@ pub mod pallet {
 
 		/// The origin that can manage pool configurations.
 		type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
+
+		/// Provider for the block number. Normally this is the `frame_system` pallet.
+		type BlockNumberProvider: BlockNumberProvider;
 	}
 
 	/// The sum of funds across all pools.
@@ -3143,9 +3151,9 @@ pub mod pallet {
 	}
 
 	#[pallet::hooks]
-	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
+	impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
 		#[cfg(feature = "try-runtime")]
-		fn try_state(_n: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
+		fn try_state(_n: SystemBlockNumberFor<T>) -> 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 f4552389a26..88ef82a1562 100644
--- a/substrate/frame/nomination-pools/src/mock.rs
+++ b/substrate/frame/nomination-pools/src/mock.rs
@@ -477,6 +477,7 @@ impl pools::Config for Runtime {
 	type MaxUnbonding = MaxUnbonding;
 	type MaxPointsToBalance = frame_support::traits::ConstU8<10>;
 	type AdminOrigin = EnsureSignedBy<Admin, AccountId>;
+	type BlockNumberProvider = System;
 }
 
 type Block = frame_system::mocking::MockBlock<Runtime>;
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 7aa8019b9c4..77a57e5a815 100644
--- a/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs
+++ b/substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs
@@ -269,6 +269,7 @@ impl pallet_nomination_pools::Config for Runtime {
 	type MaxPointsToBalance = ConstU8<10>;
 	type PalletId = PoolsPalletId;
 	type AdminOrigin = EnsureRoot<AccountId>;
+	type BlockNumberProvider = System;
 }
 
 parameter_types! {
-- 
GitLab