From 339781d669980601f229f6d76c105f58ce67bab0 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:59:38 +0530 Subject: [PATCH 1/3] Adds BlockNumberProvider in alliance, collective and identity pallets --- .../collectives/collectives-westend/src/lib.rs | 2 ++ .../runtimes/people/people-rococo/src/people.rs | 1 + .../runtimes/people/people-westend/src/people.rs | 1 + polkadot/runtime/common/src/integration_tests.rs | 1 + polkadot/runtime/rococo/src/lib.rs | 1 + polkadot/runtime/westend/src/lib.rs | 1 + substrate/bin/node/runtime/src/lib.rs | 5 +++++ substrate/frame/alliance/src/lib.rs | 9 ++++++--- substrate/frame/alliance/src/mock.rs | 3 +++ substrate/frame/collective/src/lib.rs | 9 ++++++--- substrate/frame/collective/src/tests.rs | 3 +++ substrate/frame/identity/src/lib.rs | 10 +++++++--- substrate/frame/identity/src/tests.rs | 1 + substrate/frame/utility/src/tests.rs | 1 + 14 files changed, 39 insertions(+), 9 deletions(-) diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs index 8cb2e42cb31b..977af01c658f 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs @@ -560,6 +560,7 @@ impl pallet_collective::Config for Runtime { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } pub const MAX_FELLOWS: u32 = ALLIANCE_MAX_MEMBERS; @@ -595,6 +596,7 @@ impl pallet_alliance::Config for Runtime { type MaxMembersCount = ConstU32; type AllyDeposit = AllyDeposit; type WeightInfo = weights::pallet_alliance::WeightInfo; + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/people.rs b/cumulus/parachains/runtimes/people/people-rococo/src/people.rs index 8211447d68c8..4e9345699cb3 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/people.rs @@ -60,6 +60,7 @@ impl pallet_identity::Config for Runtime { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = weights::pallet_identity::WeightInfo; + type BlockNumberProvider = frame_system::Pallet; } /// The fields that we use to identify the owner of an account with. Each corresponds to a field diff --git a/cumulus/parachains/runtimes/people/people-westend/src/people.rs b/cumulus/parachains/runtimes/people/people-westend/src/people.rs index 0255fd074b11..1f512512163c 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/people.rs @@ -60,6 +60,7 @@ impl pallet_identity::Config for Runtime { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = weights::pallet_identity::WeightInfo; + type BlockNumberProvider = frame_system::Pallet; } /// The fields that we use to identify the owner of an account with. Each corresponds to a field diff --git a/polkadot/runtime/common/src/integration_tests.rs b/polkadot/runtime/common/src/integration_tests.rs index bfeed04a919f..b7f01a6b065a 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -301,6 +301,7 @@ impl pallet_identity::Config for Test { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = (); + type BlockNumberProvider = frame_system::Pallet; } impl identity_migrator::Config for Test { diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 44dd820f8c3c..6bd12542857c 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -737,6 +737,7 @@ impl pallet_identity::Config for Runtime { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = weights::pallet_identity::WeightInfo; + type BlockNumberProvider = frame_system::Pallet; } impl pallet_utility::Config for Runtime { diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 4941d91df57d..20d6af4074cf 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -973,6 +973,7 @@ impl pallet_identity::Config for Runtime { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = weights::pallet_identity::WeightInfo; + type BlockNumberProvider = frame_system::Pallet; } impl pallet_utility::Config for Runtime { diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 2dbc6ab39e73..53c941158f0e 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -1148,6 +1148,7 @@ impl pallet_collective::Config for Runtime { >, u32, >; + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { @@ -1212,6 +1213,7 @@ impl pallet_collective::Config for Runtime { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } type EnsureRootOrHalfCouncil = EitherOfDiverse< @@ -1595,6 +1597,7 @@ impl pallet_identity::Config for Runtime { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = pallet_identity::weights::SubstrateWeight; + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { @@ -2098,6 +2101,7 @@ impl pallet_collective::Config for Runtime { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { @@ -2141,6 +2145,7 @@ impl pallet_alliance::Config for Runtime { type AllyDeposit = AllyDeposit; type WeightInfo = pallet_alliance::weights::SubstrateWeight; type RetirementPeriod = RetirementPeriod; + type BlockNumberProvider = frame_system::Pallet; } impl frame_benchmarking_pallet_pov::Config for Runtime { diff --git a/substrate/frame/alliance/src/lib.rs b/substrate/frame/alliance/src/lib.rs index be65f49e6e4e..91871dd1c1e5 100644 --- a/substrate/frame/alliance/src/lib.rs +++ b/substrate/frame/alliance/src/lib.rs @@ -101,7 +101,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use sp_runtime::{ - traits::{Dispatchable, Saturating, StaticLookup, Zero}, + traits::{BlockNumberProvider, Dispatchable, Saturating, StaticLookup, Zero}, DispatchError, RuntimeDebug, }; @@ -308,6 +308,9 @@ pub mod pallet { /// The number of blocks a member must wait between giving a retirement notice and retiring. /// Supposed to be greater than time required to `kick_member`. type RetirementPeriod: Get>; + + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::error] @@ -763,7 +766,7 @@ pub mod pallet { Self::add_member(&who, MemberRole::Retiring)?; >::insert( &who, - frame_system::Pallet::::block_number() + T::BlockNumberProvider::current_block_number() .saturating_add(T::RetirementPeriod::get()), ); @@ -781,7 +784,7 @@ pub mod pallet { let retirement_period_end = RetiringMembers::::get(&who) .ok_or(Error::::RetirementNoticeNotGiven)?; ensure!( - frame_system::Pallet::::block_number() >= retirement_period_end, + T::BlockNumberProvider::current_block_number() >= retirement_period_end, Error::::RetirementPeriodNotPassed ); diff --git a/substrate/frame/alliance/src/mock.rs b/substrate/frame/alliance/src/mock.rs index 5442e8779020..89a835cf68e5 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -80,6 +80,7 @@ impl pallet_collective::Config for Test { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { @@ -120,6 +121,7 @@ impl pallet_identity::Config for Test { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = (); + type BlockNumberProvider = frame_system::Pallet; } #[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)] @@ -229,6 +231,7 @@ impl Config for Test { type AllyDeposit = AllyDeposit; type WeightInfo = (); type RetirementPeriod = RetirementPeriod; + type BlockNumberProvider = frame_system::Pallet; } type Block = frame_system::mocking::MockBlock; diff --git a/substrate/frame/collective/src/lib.rs b/substrate/frame/collective/src/lib.rs index 8e533a7b2904..b29394c02fdd 100644 --- a/substrate/frame/collective/src/lib.rs +++ b/substrate/frame/collective/src/lib.rs @@ -49,7 +49,7 @@ use core::{marker::PhantomData, result}; use scale_info::TypeInfo; use sp_io::storage; use sp_runtime::{ - traits::{Dispatchable, Hash}, + traits::{BlockNumberProvider, Dispatchable, Hash}, DispatchError, RuntimeDebug, }; @@ -387,6 +387,9 @@ pub mod pallet { /// consider using a constant cost (e.g., [`crate::deposit::Constant`]) equal to the minimum /// balance under the `runtime-benchmarks` feature. type Consideration: MaybeConsideration; + + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::genesis_config] @@ -967,7 +970,7 @@ impl, I: 'static> Pallet { >::mutate(|i| *i += 1); >::insert(proposal_hash, proposal); let votes = { - let end = frame_system::Pallet::::block_number() + T::MotionDuration::get(); + let end = T::BlockNumberProvider::current_block_number() + T::MotionDuration::get(); Votes { index, threshold, ayes: vec![], nays: vec![], end } }; >::insert(proposal_hash, votes); @@ -1077,7 +1080,7 @@ impl, I: 'static> Pallet { } // Only allow actual closing of the proposal after the voting period has ended. - ensure!(frame_system::Pallet::::block_number() >= voting.end, Error::::TooEarly); + ensure!(T::BlockNumberProvider::current_block_number() >= voting.end, Error::::TooEarly); let prime_vote = Prime::::get().map(|who| voting.ayes.iter().any(|a| a == &who)); diff --git a/substrate/frame/collective/src/tests.rs b/substrate/frame/collective/src/tests.rs index c4ed17821ae8..a1189958396c 100644 --- a/substrate/frame/collective/src/tests.rs +++ b/substrate/frame/collective/src/tests.rs @@ -133,6 +133,7 @@ impl Config for Test { type KillOrigin = EnsureRoot; type Consideration = HoldConsideration; + type BlockNumberProvider = frame_system::Pallet; } type CollectiveMajorityDeposit = deposit::Linear, ProposalDepositBase>; @@ -153,6 +154,7 @@ impl Config for Test { type KillOrigin = EnsureRoot; type Consideration = HoldConsideration; + type BlockNumberProvider = frame_system::Pallet; } impl mock_democracy::Config for Test { type RuntimeEvent = RuntimeEvent; @@ -181,6 +183,7 @@ impl Config for Test { type KillOrigin = EnsureRoot; type Consideration = HoldConsideration; + type BlockNumberProvider = frame_system::Pallet; } pub struct ExtBuilder { diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 08e29ddffd12..9ccdccdfe1a8 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -115,7 +115,8 @@ use frame_support::{ use frame_system::pallet_prelude::*; pub use pallet::*; use sp_runtime::traits::{ - AppendZerosInput, Hash, IdentifyAccount, Saturating, StaticLookup, Verify, Zero, + AppendZerosInput, BlockNumberProvider, Hash, IdentifyAccount, Saturating, StaticLookup, Verify, + Zero, }; pub use types::{ Data, IdentityInformationProvider, Judgement, RegistrarIndex, RegistrarInfo, Registration, @@ -202,6 +203,9 @@ pub mod pallet { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider>; } const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -1148,7 +1152,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let _ = ensure_signed(origin)?; if let Some((who, expiration)) = PendingUsernames::::take(&username) { - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); ensure!(now > expiration, Error::::NotExpired); Self::deposit_event(Event::PreapprovalExpired { whose: who.clone() }); Ok(Pays::No.into()) @@ -1394,7 +1398,7 @@ impl Pallet { /// A username was granted by an authority, but must be accepted by `who`. Put the username /// into a queue for acceptance. pub fn queue_acceptance(who: &T::AccountId, username: Username) { - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let expiration = now.saturating_add(T::PendingUsernameExpiration::get()); PendingUsernames::::insert(&username, (who.clone(), expiration)); Self::deposit_event(Event::UsernameQueued { who: who.clone(), username, expiration }); diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index 3adb823ad5da..2ad2aea7f3b9 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -90,6 +90,7 @@ impl pallet_identity::Config for Test { type MaxSuffixLength = ConstU32<7>; type MaxUsernameLength = ConstU32<32>; type WeightInfo = (); + type BlockNumberProvider = frame_system::Pallet; } pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/substrate/frame/utility/src/tests.rs b/substrate/frame/utility/src/tests.rs index 274a90d77cf0..9feda8168172 100644 --- a/substrate/frame/utility/src/tests.rs +++ b/substrate/frame/utility/src/tests.rs @@ -193,6 +193,7 @@ impl pallet_collective::Config for Test { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } impl example::Config for Test {} From 81a9bd16e05beac10b62d39dae0090b5f4c338f5 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:08:28 +0530 Subject: [PATCH 2/3] Add PrDoc --- prdoc/pr_6282.prdoc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 prdoc/pr_6282.prdoc diff --git a/prdoc/pr_6282.prdoc b/prdoc/pr_6282.prdoc new file mode 100644 index 000000000000..430d7e52979e --- /dev/null +++ b/prdoc/pr_6282.prdoc @@ -0,0 +1,25 @@ +title: Adds `BlockNumberProvider` in alliance, collective and identity pallets + +doc: + - audience: Runtime Dev + description: | + This PR adds the ability for these pallets to specify their source of the block number. + This is useful when these pallets are migrated from the relay chain to a parachain and + vice versa. + + This change is backwards compatible: + 1. If the `BlockNumberProvider` continues to use the system pallet's block number + 2. When a pallet deployed on the relay chain is moved to a parachain, but still uses the + relay chain's block number + + However, we would need migrations if the deployed pallets are upgraded on an existing parachain, + and the `BlockNumberProvider` uses the relay chain block number. + +crates: + - name: pallet-alliance + bump: major + - name: pallet-collective + bump: major + - name: pallet-identity + bump: major + \ No newline at end of file From 0007fbdd3b2363eae12926d7f7975b3a83ececd9 Mon Sep 17 00:00:00 2001 From: gupnik <17176722+gupnik@users.noreply.github.com> Date: Wed, 30 Oct 2024 13:17:12 +0530 Subject: [PATCH 3/3] FMT --- substrate/frame/collective/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/substrate/frame/collective/src/lib.rs b/substrate/frame/collective/src/lib.rs index b29394c02fdd..e2103b54ba71 100644 --- a/substrate/frame/collective/src/lib.rs +++ b/substrate/frame/collective/src/lib.rs @@ -1080,7 +1080,10 @@ impl, I: 'static> Pallet { } // Only allow actual closing of the proposal after the voting period has ended. - ensure!(T::BlockNumberProvider::current_block_number() >= voting.end, Error::::TooEarly); + ensure!( + T::BlockNumberProvider::current_block_number() >= voting.end, + Error::::TooEarly + ); let prime_vote = Prime::::get().map(|who| voting.ayes.iter().any(|a| a == &who));