From daf6dd878e6bdc19377f190af25c4b653cd3c452 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sat, 23 Nov 2024 00:42:49 +0100 Subject: [PATCH 1/2] Update pallet conviction voting to support Block Number Provider --- substrate/frame/conviction-voting/src/lib.rs | 27 ++++++++++++------- .../frame/conviction-voting/src/tests.rs | 1 + .../primitives/runtime/src/traits/mod.rs | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/substrate/frame/conviction-voting/src/lib.rs b/substrate/frame/conviction-voting/src/lib.rs index 31bd6b85ec86..4d181344fbe4 100644 --- a/substrate/frame/conviction-voting/src/lib.rs +++ b/substrate/frame/conviction-voting/src/lib.rs @@ -37,7 +37,6 @@ use frame_support::{ ReservableCurrency, WithdrawReasons, }, }; -use frame_system::pallet_prelude::BlockNumberFor; use sp_runtime::{ traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero}, ArithmeticError, DispatchError, Perbill, @@ -55,6 +54,7 @@ pub use self::{ vote::{AccountVote, Casting, Delegating, Vote, Voting}, weights::WeightInfo, }; +use sp_runtime::traits::BlockNumberProvider; #[cfg(test)] mod tests; @@ -64,19 +64,22 @@ pub mod benchmarking; const CONVICTION_VOTING_ID: LockIdentifier = *b"pyconvot"; +pub type BlockNumberFor = +<>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; + type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; type BalanceOf = <>::Currency as Currency<::AccountId>>::Balance; type VotingOf = Voting< BalanceOf, ::AccountId, - BlockNumberFor, + BlockNumberFor, PollIndexOf, >::MaxVotes, >; #[allow(dead_code)] type DelegatingOf = - Delegating, ::AccountId, BlockNumberFor>; + Delegating, ::AccountId, BlockNumberFor>; pub type TallyOf = Tally, >::MaxTurnout>; pub type VotesOf = BalanceOf; type PollIndexOf = <>::Polls as Polling>>::Index; @@ -94,7 +97,9 @@ pub mod pallet { traits::ClassCountOf, Twox64Concat, }; - use frame_system::pallet_prelude::*; + use frame_system::pallet_prelude::{ + ensure_signed, OriginFor, + }; use sp_runtime::BoundedVec; #[pallet::pallet] @@ -109,14 +114,14 @@ pub mod pallet { type WeightInfo: WeightInfo; /// Currency type with which voting happens. type Currency: ReservableCurrency - + LockableCurrency> + + LockableCurrency> + fungible::Inspect; /// The implementation of the logic which conducts polls. type Polls: Polling< TallyOf, Votes = BalanceOf, - Moment = BlockNumberFor, + Moment = BlockNumberFor, >; /// The maximum amount of tokens which may be used for voting. May just be @@ -136,7 +141,9 @@ pub mod pallet { /// It should be no shorter than enactment period to ensure that in the case of an approval, /// those successful voters are locked into the consequences that their votes entail. #[pallet::constant] - type VoteLockingPeriod: Get>; + type VoteLockingPeriod: Get>; + /// Provider for the block number. Normally this is the `frame_system` pallet. + type BlockNumberProvider: BlockNumberProvider; } /// All voting for a particular voter in a particular voting class. We store the balance for the @@ -479,7 +486,7 @@ impl, I: 'static> Pallet { let unlock_at = end.saturating_add( T::VoteLockingPeriod::get().saturating_mul(lock_periods.into()), ); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); if now < unlock_at { ensure!( matches!(scope, UnvoteScope::Any), @@ -620,7 +627,7 @@ impl, I: 'static> Pallet { &class, conviction.votes(balance), ); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let lock_periods = conviction.lock_periods().into(); prior.accumulate( now.saturating_add( @@ -666,7 +673,7 @@ impl, I: 'static> Pallet { /// a security hole) but may be reduced from what they are currently. fn update_lock(class: &ClassOf, who: &T::AccountId) { let class_lock_needed = VotingFor::::mutate(who, class, |voting| { - voting.rejig(frame_system::Pallet::::block_number()); + voting.rejig(T::BlockNumberProvider::current_block_number()); voting.locked_balance() }); let lock_needed = ClassLocksFor::::mutate(who, |locks| { diff --git a/substrate/frame/conviction-voting/src/tests.rs b/substrate/frame/conviction-voting/src/tests.rs index dd9ee33ee183..c6c50dc5d964 100644 --- a/substrate/frame/conviction-voting/src/tests.rs +++ b/substrate/frame/conviction-voting/src/tests.rs @@ -154,6 +154,7 @@ impl Config for Test { type WeightInfo = (); type MaxTurnout = frame_support::traits::TotalIssuanceOf; type Polls = TestPolls; + type BlockNumberProvider = System; } pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/substrate/primitives/runtime/src/traits/mod.rs b/substrate/primitives/runtime/src/traits/mod.rs index 02bc7adc8ba5..10a2c9ccb0f7 100644 --- a/substrate/primitives/runtime/src/traits/mod.rs +++ b/substrate/primitives/runtime/src/traits/mod.rs @@ -2350,7 +2350,8 @@ pub trait BlockNumberProvider { + Debug + MaxEncodedLen + Copy - + EncodeLike; + + EncodeLike + + Default; /// Returns the current block number. /// From e5e5054cf69a11c05171e635d37d1669669911f4 Mon Sep 17 00:00:00 2001 From: dharjeezy Date: Sat, 23 Nov 2024 00:53:14 +0100 Subject: [PATCH 2/2] pr doc and runtime impl --- polkadot/runtime/rococo/src/governance/mod.rs | 1 + polkadot/runtime/westend/src/governance/mod.rs | 1 + prdoc/prdoc/pr_6621.prdoc | 14 ++++++++++++++ substrate/bin/node/runtime/src/lib.rs | 1 + 4 files changed, 17 insertions(+) create mode 100644 prdoc/prdoc/pr_6621.prdoc diff --git a/polkadot/runtime/rococo/src/governance/mod.rs b/polkadot/runtime/rococo/src/governance/mod.rs index ef2adf60753d..ca92151e7d45 100644 --- a/polkadot/runtime/rococo/src/governance/mod.rs +++ b/polkadot/runtime/rococo/src/governance/mod.rs @@ -47,6 +47,7 @@ impl pallet_conviction_voting::Config for Runtime { type MaxTurnout = frame_support::traits::tokens::currency::ActiveIssuanceOf; type Polls = Referenda; + type BlockNumberProvider = System; } parameter_types! { diff --git a/polkadot/runtime/westend/src/governance/mod.rs b/polkadot/runtime/westend/src/governance/mod.rs index d027f788d71f..352f1ea69191 100644 --- a/polkadot/runtime/westend/src/governance/mod.rs +++ b/polkadot/runtime/westend/src/governance/mod.rs @@ -44,6 +44,7 @@ impl pallet_conviction_voting::Config for Runtime { type MaxTurnout = frame_support::traits::tokens::currency::ActiveIssuanceOf; type Polls = Referenda; + type BlockNumberProvider = System; } parameter_types! { diff --git a/prdoc/prdoc/pr_6621.prdoc b/prdoc/prdoc/pr_6621.prdoc new file mode 100644 index 000000000000..4302b5f7ae84 --- /dev/null +++ b/prdoc/prdoc/pr_6621.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 Conviction Voting Pallet to Support Block Number Provider + +doc: + - audience: Runtime Dev + description: | + This PR makes the conviction voting pallet uses the relay chain as a block provider for a parachain on a regular schedule. + To migrate existing conviction voting implementations, simply add `type BlockNumberProvider = System` to have the same behavior as before. + +crates: +- name: pallet-conviction-voting + bump: minor \ 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 bff263548087..86c879feda65 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -977,6 +977,7 @@ impl pallet_conviction_voting::Config for Runtime { type MaxVotes = ConstU32<512>; type MaxTurnout = frame_support::traits::TotalIssuanceOf; type Polls = Referenda; + type BlockNumberProvider = System; } parameter_types! {