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 690bb974bd17..60cb29a64e6f 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/people.rs @@ -63,6 +63,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 47551f6d4bdc..b5f5d7f8a3fa 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/people.rs @@ -63,6 +63,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 8a76a138305e..34f54796c327 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -303,6 +303,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 5bcde612cf1b..7d930ac43bdc 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -741,6 +741,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 970ef5318994..5213d0467e59 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -977,6 +977,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/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 diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 12e8dc3e5077..e0dea88dc3cf 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< @@ -1598,6 +1600,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! { @@ -2101,6 +2104,7 @@ impl pallet_collective::Config for Runtime { type DisapproveOrigin = EnsureRoot; type KillOrigin = EnsureRoot; type Consideration = (); + type BlockNumberProvider = frame_system::Pallet; } parameter_types! { @@ -2144,6 +2148,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 625cabf3457f..4a55292feead 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! { @@ -124,6 +125,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)] @@ -233,6 +235,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..e2103b54ba71 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,10 @@ 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 11b43f958c4e..80bed3bb0e8f 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -130,7 +130,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, @@ -228,6 +229,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(2); @@ -1198,7 +1202,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let _ = ensure_signed(origin)?; if let Some((who, expiration, provider)) = PendingUsernames::::take(&username) { - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); ensure!(now > expiration, Error::::NotExpired); let actual_weight = match provider { Provider::AuthorityDeposit(deposit) => { @@ -1517,7 +1521,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, provider: ProviderOf) { - 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, provider)); 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 a095085a8188..d4041d1b5ab1 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -92,6 +92,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 {}