Skip to content

Commit 1833515

Browse files
dharjeezymuharem
andauthored
Update pallet conviction voting to support Block Number Provider (#6621)
This PR introduces BlockNumberProvider config for the conviction voting pallet. closes part of #6297 --------- Co-authored-by: muharem <[email protected]> Co-authored-by: command-bot <>
1 parent a484ccc commit 1833515

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

polkadot/runtime/rococo/src/governance/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl pallet_conviction_voting::Config for Runtime {
4747
type MaxTurnout =
4848
frame_support::traits::tokens::currency::ActiveIssuanceOf<Balances, Self::AccountId>;
4949
type Polls = Referenda;
50+
type BlockNumberProvider = System;
5051
}
5152

5253
parameter_types! {

polkadot/runtime/westend/src/governance/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl pallet_conviction_voting::Config for Runtime {
4444
type MaxTurnout =
4545
frame_support::traits::tokens::currency::ActiveIssuanceOf<Balances, Self::AccountId>;
4646
type Polls = Referenda;
47+
type BlockNumberProvider = System;
4748
}
4849

4950
parameter_types! {

prdoc/pr_6621.prdoc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Update Conviction Voting Pallet to Support Block Number Provider
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: |
9+
This PR makes the block number provider used in the society pallet configurable. Before this PR, society pallet always used the system block number,
10+
with this PR some runtime can opt to use the relay chain block number instead.
11+
12+
crates:
13+
- name: pallet-conviction-voting
14+
bump: major

substrate/bin/node/runtime/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ impl pallet_conviction_voting::Config for Runtime {
10041004
type MaxVotes = ConstU32<512>;
10051005
type MaxTurnout = frame_support::traits::TotalIssuanceOf<Balances, Self::AccountId>;
10061006
type Polls = Referenda;
1007+
type BlockNumberProvider = System;
10071008
}
10081009

10091010
parameter_types! {

substrate/frame/conviction-voting/src/lib.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use frame_support::{
3737
ReservableCurrency, WithdrawReasons,
3838
},
3939
};
40-
use frame_system::pallet_prelude::BlockNumberFor;
4140
use sp_runtime::{
4241
traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero},
4342
ArithmeticError, DispatchError, Perbill,
@@ -55,6 +54,7 @@ pub use self::{
5554
vote::{AccountVote, Casting, Delegating, Vote, Voting},
5655
weights::WeightInfo,
5756
};
57+
use sp_runtime::traits::BlockNumberProvider;
5858

5959
#[cfg(test)]
6060
mod tests;
@@ -64,19 +64,22 @@ pub mod benchmarking;
6464

6565
const CONVICTION_VOTING_ID: LockIdentifier = *b"pyconvot";
6666

67+
pub type BlockNumberFor<T, I> =
68+
<<T as Config<I>>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
69+
6770
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
6871
type BalanceOf<T, I = ()> =
6972
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
7073
type VotingOf<T, I = ()> = Voting<
7174
BalanceOf<T, I>,
7275
<T as frame_system::Config>::AccountId,
73-
BlockNumberFor<T>,
76+
BlockNumberFor<T, I>,
7477
PollIndexOf<T, I>,
7578
<T as Config<I>>::MaxVotes,
7679
>;
7780
#[allow(dead_code)]
7881
type DelegatingOf<T, I = ()> =
79-
Delegating<BalanceOf<T, I>, <T as frame_system::Config>::AccountId, BlockNumberFor<T>>;
82+
Delegating<BalanceOf<T, I>, <T as frame_system::Config>::AccountId, BlockNumberFor<T, I>>;
8083
pub type TallyOf<T, I = ()> = Tally<BalanceOf<T, I>, <T as Config<I>>::MaxTurnout>;
8184
pub type VotesOf<T, I = ()> = BalanceOf<T, I>;
8285
type PollIndexOf<T, I = ()> = <<T as Config<I>>::Polls as Polling<TallyOf<T, I>>>::Index;
@@ -94,7 +97,7 @@ pub mod pallet {
9497
traits::ClassCountOf,
9598
Twox64Concat,
9699
};
97-
use frame_system::pallet_prelude::*;
100+
use frame_system::pallet_prelude::{ensure_signed, OriginFor};
98101
use sp_runtime::BoundedVec;
99102

100103
#[pallet::pallet]
@@ -109,14 +112,14 @@ pub mod pallet {
109112
type WeightInfo: WeightInfo;
110113
/// Currency type with which voting happens.
111114
type Currency: ReservableCurrency<Self::AccountId>
112-
+ LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>
115+
+ LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self, I>>
113116
+ fungible::Inspect<Self::AccountId>;
114117

115118
/// The implementation of the logic which conducts polls.
116119
type Polls: Polling<
117120
TallyOf<Self, I>,
118121
Votes = BalanceOf<Self, I>,
119-
Moment = BlockNumberFor<Self>,
122+
Moment = BlockNumberFor<Self, I>,
120123
>;
121124

122125
/// The maximum amount of tokens which may be used for voting. May just be
@@ -136,7 +139,9 @@ pub mod pallet {
136139
/// It should be no shorter than enactment period to ensure that in the case of an approval,
137140
/// those successful voters are locked into the consequences that their votes entail.
138141
#[pallet::constant]
139-
type VoteLockingPeriod: Get<BlockNumberFor<Self>>;
142+
type VoteLockingPeriod: Get<BlockNumberFor<Self, I>>;
143+
/// Provider for the block number. Normally this is the `frame_system` pallet.
144+
type BlockNumberProvider: BlockNumberProvider;
140145
}
141146

142147
/// All voting for a particular voter in a particular voting class. We store the balance for the
@@ -479,7 +484,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
479484
let unlock_at = end.saturating_add(
480485
T::VoteLockingPeriod::get().saturating_mul(lock_periods.into()),
481486
);
482-
let now = frame_system::Pallet::<T>::block_number();
487+
let now = T::BlockNumberProvider::current_block_number();
483488
if now < unlock_at {
484489
ensure!(
485490
matches!(scope, UnvoteScope::Any),
@@ -620,7 +625,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
620625
&class,
621626
conviction.votes(balance),
622627
);
623-
let now = frame_system::Pallet::<T>::block_number();
628+
let now = T::BlockNumberProvider::current_block_number();
624629
let lock_periods = conviction.lock_periods().into();
625630
prior.accumulate(
626631
now.saturating_add(
@@ -666,7 +671,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
666671
/// a security hole) but may be reduced from what they are currently.
667672
fn update_lock(class: &ClassOf<T, I>, who: &T::AccountId) {
668673
let class_lock_needed = VotingFor::<T, I>::mutate(who, class, |voting| {
669-
voting.rejig(frame_system::Pallet::<T>::block_number());
674+
voting.rejig(T::BlockNumberProvider::current_block_number());
670675
voting.locked_balance()
671676
});
672677
let lock_needed = ClassLocksFor::<T, I>::mutate(who, |locks| {

substrate/frame/conviction-voting/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl Config for Test {
154154
type WeightInfo = ();
155155
type MaxTurnout = frame_support::traits::TotalIssuanceOf<Balances, Self::AccountId>;
156156
type Polls = TestPolls;
157+
type BlockNumberProvider = System;
157158
}
158159

159160
pub fn new_test_ext() -> sp_io::TestExternalities {

substrate/primitives/runtime/src/traits/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,8 @@ pub trait BlockNumberProvider {
23512351
+ Debug
23522352
+ MaxEncodedLen
23532353
+ Copy
2354-
+ EncodeLike;
2354+
+ EncodeLike
2355+
+ Default;
23552356

23562357
/// Returns the current block number.
23572358
///

0 commit comments

Comments
 (0)