Skip to content

Commit

Permalink
Update pallet conviction voting to support Block Number Provider (#6621)
Browse files Browse the repository at this point in the history
This PR introduces BlockNumberProvider config for the conviction voting
pallet.
closes part of #6297

---------

Co-authored-by: muharem <ismailov.m.h@gmail.com>
Co-authored-by: command-bot <>
  • Loading branch information
dharjeezy and muharem authored Feb 7, 2025
1 parent a484ccc commit 1833515
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl pallet_conviction_voting::Config for Runtime {
type MaxTurnout =
frame_support::traits::tokens::currency::ActiveIssuanceOf<Balances, Self::AccountId>;
type Polls = Referenda;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/governance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl pallet_conviction_voting::Config for Runtime {
type MaxTurnout =
frame_support::traits::tokens::currency::ActiveIssuanceOf<Balances, Self::AccountId>;
type Polls = Referenda;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
14 changes: 14 additions & 0 deletions prdoc/pr_6621.prdoc
Original file line number Diff line number Diff line change
@@ -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 block number provider used in the society pallet configurable. Before this PR, society pallet always used the system block number,
with this PR some runtime can opt to use the relay chain block number instead.

crates:
- name: pallet-conviction-voting
bump: major
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ impl pallet_conviction_voting::Config for Runtime {
type MaxVotes = ConstU32<512>;
type MaxTurnout = frame_support::traits::TotalIssuanceOf<Balances, Self::AccountId>;
type Polls = Referenda;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
25 changes: 15 additions & 10 deletions substrate/frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -55,6 +54,7 @@ pub use self::{
vote::{AccountVote, Casting, Delegating, Vote, Voting},
weights::WeightInfo,
};
use sp_runtime::traits::BlockNumberProvider;

#[cfg(test)]
mod tests;
Expand All @@ -64,19 +64,22 @@ pub mod benchmarking;

const CONVICTION_VOTING_ID: LockIdentifier = *b"pyconvot";

pub type BlockNumberFor<T, I> =
<<T as Config<I>>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type VotingOf<T, I = ()> = Voting<
BalanceOf<T, I>,
<T as frame_system::Config>::AccountId,
BlockNumberFor<T>,
BlockNumberFor<T, I>,
PollIndexOf<T, I>,
<T as Config<I>>::MaxVotes,
>;
#[allow(dead_code)]
type DelegatingOf<T, I = ()> =
Delegating<BalanceOf<T, I>, <T as frame_system::Config>::AccountId, BlockNumberFor<T>>;
Delegating<BalanceOf<T, I>, <T as frame_system::Config>::AccountId, BlockNumberFor<T, I>>;
pub type TallyOf<T, I = ()> = Tally<BalanceOf<T, I>, <T as Config<I>>::MaxTurnout>;
pub type VotesOf<T, I = ()> = BalanceOf<T, I>;
type PollIndexOf<T, I = ()> = <<T as Config<I>>::Polls as Polling<TallyOf<T, I>>>::Index;
Expand All @@ -94,7 +97,7 @@ 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]
Expand All @@ -109,14 +112,14 @@ pub mod pallet {
type WeightInfo: WeightInfo;
/// Currency type with which voting happens.
type Currency: ReservableCurrency<Self::AccountId>
+ LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>
+ LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self, I>>
+ fungible::Inspect<Self::AccountId>;

/// The implementation of the logic which conducts polls.
type Polls: Polling<
TallyOf<Self, I>,
Votes = BalanceOf<Self, I>,
Moment = BlockNumberFor<Self>,
Moment = BlockNumberFor<Self, I>,
>;

/// The maximum amount of tokens which may be used for voting. May just be
Expand All @@ -136,7 +139,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<BlockNumberFor<Self>>;
type VoteLockingPeriod: Get<BlockNumberFor<Self, I>>;
/// 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
Expand Down Expand Up @@ -479,7 +484,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let unlock_at = end.saturating_add(
T::VoteLockingPeriod::get().saturating_mul(lock_periods.into()),
);
let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
if now < unlock_at {
ensure!(
matches!(scope, UnvoteScope::Any),
Expand Down Expand Up @@ -620,7 +625,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
&class,
conviction.votes(balance),
);
let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let lock_periods = conviction.lock_periods().into();
prior.accumulate(
now.saturating_add(
Expand Down Expand Up @@ -666,7 +671,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// a security hole) but may be reduced from what they are currently.
fn update_lock(class: &ClassOf<T, I>, who: &T::AccountId) {
let class_lock_needed = VotingFor::<T, I>::mutate(who, class, |voting| {
voting.rejig(frame_system::Pallet::<T>::block_number());
voting.rejig(T::BlockNumberProvider::current_block_number());
voting.locked_balance()
});
let lock_needed = ClassLocksFor::<T, I>::mutate(who, |locks| {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/conviction-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl Config for Test {
type WeightInfo = ();
type MaxTurnout = frame_support::traits::TotalIssuanceOf<Balances, Self::AccountId>;
type Polls = TestPolls;
type BlockNumberProvider = System;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
3 changes: 2 additions & 1 deletion substrate/primitives/runtime/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,8 @@ pub trait BlockNumberProvider {
+ Debug
+ MaxEncodedLen
+ Copy
+ EncodeLike;
+ EncodeLike
+ Default;

/// Returns the current block number.
///
Expand Down

0 comments on commit 1833515

Please sign in to comment.