Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pallet conviction voting to support Block Number Provider #6621

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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 conviction voting pallet uses the relay chain as a block provider for a parachain on a regular schedule.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't force the conviction voting pallet to use the relay chain block provider,

Suggested change
This PR makes the conviction voting pallet uses the relay chain as a block provider for a parachain on a regular schedule.
This PR makes the block number provider used in the conviction voting pallet configurable. Before this PR, conviction voting pallet always used the system block number, with this PR some runtime can opt to use the relay chain block number instead.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's follow rust semver

Suggested change
bump: minor
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 @@ -977,6 +977,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
27 changes: 17 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,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]
Expand All @@ -109,14 +114,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 +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<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 +486,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 +627,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 +673,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 @@ -2350,7 +2350,8 @@ pub trait BlockNumberProvider {
+ Debug
+ MaxEncodedLen
+ Copy
+ EncodeLike;
+ EncodeLike
+ Default;

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