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 Referenda to support Block Number Provider #6338

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
65 changes: 32 additions & 33 deletions substrate/frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ use frame_support::{
},
BoundedVec,
};
use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Bounded, Dispatchable, One, Saturating, Zero},
Expand All @@ -101,7 +100,7 @@ pub use self::{
BalanceOf, BoundedCallOf, CallOf, Curve, DecidingStatus, DecidingStatusOf, Deposit,
InsertSorted, NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex, ReferendumInfo,
ReferendumInfoOf, ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf,
TrackIdOf, TrackInfo, TrackInfoOf, TracksInfo, VotesOf,
TrackIdOf, TrackInfo, TrackInfoOf, TracksInfo, VotesOf, BlockNumberFor
},
weights::WeightInfo,
};
Expand Down Expand Up @@ -145,7 +144,7 @@ const ASSEMBLY_ID: LockIdentifier = *b"assembly";
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{OriginFor, BlockNumberFor as SystemBlockNumberFor, ensure_root, ensure_signed_or_root, ensure_signed};
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this could be replaced with:

	use super::{*, BlockNumberFor};
	use frame_support::{pallet_prelude::*, traits::EnsureOriginWithArg};
	use frame_system::pallet_prelude::{*, BlockNumberFor as SystemBlockNumberFor};

But as you prefer.


/// The in-code storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
Expand All @@ -168,12 +167,12 @@ pub mod pallet {
type WeightInfo: WeightInfo;
/// The Scheduler.
type Scheduler: ScheduleAnon<
BlockNumberFor<Self>,
BlockNumberFor<Self, I>,
CallOf<Self, I>,
PalletsOriginOf<Self>,
Hasher = Self::Hashing,
> + ScheduleNamed<
BlockNumberFor<Self>,
BlockNumberFor<Self, I>,
CallOf<Self, I>,
PalletsOriginOf<Self>,
Hasher = Self::Hashing,
Expand Down Expand Up @@ -216,33 +215,33 @@ pub mod pallet {
/// The number of blocks after submission that a referendum must begin being decided by.
/// Once this passes, then anyone may cancel the referendum.
#[pallet::constant]
type UndecidingTimeout: Get<BlockNumberFor<Self>>;
type UndecidingTimeout: Get<BlockNumberFor<Self, I>>;

/// Quantization level for the referendum wakeup scheduler. A higher number will result in
/// fewer storage reads/writes needed for smaller voters, but also result in delays to the
/// automatic referendum status changes. Explicit servicing instructions are unaffected.
#[pallet::constant]
type AlarmInterval: Get<BlockNumberFor<Self>>;
type AlarmInterval: Get<BlockNumberFor<Self, I>>;

// The other stuff.
/// Information concerning the different referendum tracks.
#[pallet::constant]
type Tracks: Get<
Vec<(
<Self::Tracks as TracksInfo<BalanceOf<Self, I>, BlockNumberFor<Self>>>::Id,
TrackInfo<BalanceOf<Self, I>, BlockNumberFor<Self>>,
<Self::Tracks as TracksInfo<BalanceOf<Self, I>, BlockNumberFor<Self, I>>>::Id,
TrackInfo<BalanceOf<Self, I>, BlockNumberFor<Self, I>>,
)>,
> + TracksInfo<
BalanceOf<Self, I>,
BlockNumberFor<Self>,
BlockNumberFor<Self, I>,
RuntimeOrigin = <Self::RuntimeOrigin as OriginTrait>::PalletsOrigin,
>;

/// The preimage provider.
type Preimages: QueryPreimage<H = Self::Hashing> + StorePreimage;

/// Provider for the block number. Normally this is the `frame_system` pallet.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
type BlockNumberProvider: BlockNumberProvider;
}

/// The next free referendum index, aka the number of referenda started so far.
Expand Down Expand Up @@ -436,9 +435,9 @@ pub mod pallet {
}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
impl<T: Config<I>, I: 'static> Hooks<SystemBlockNumberFor<T>> for Pallet<T, I> {
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
fn try_state(_n: SystemBlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
Self::do_try_state()?;
Ok(())
}
Expand Down Expand Up @@ -466,7 +465,7 @@ pub mod pallet {
origin: OriginFor<T>,
proposal_origin: Box<PalletsOriginOf<T>>,
proposal: BoundedCallOf<T, I>,
enactment_moment: DispatchTime<BlockNumberFor<T>>,
enactment_moment: DispatchTime<BlockNumberFor<T, I>>,
) -> DispatchResult {
let proposal_origin = *proposal_origin;
let who = T::SubmitOrigin::ensure_origin(origin, &proposal_origin)?;
Expand Down Expand Up @@ -748,7 +747,7 @@ pub mod pallet {
impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
type Index = ReferendumIndex;
type Votes = VotesOf<T, I>;
type Moment = BlockNumberFor<T>;
type Moment = BlockNumberFor<T, I>;
type Class = TrackIdOf<T, I>;

fn classes() -> Vec<Self::Class> {
Expand All @@ -757,7 +756,7 @@ impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {

fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>>) -> R,
f: impl FnOnce(PollStatus<&mut T::Tally, BlockNumberFor<T, I>, TrackIdOf<T, I>>) -> R,
) -> R {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(mut status)) => {
Expand All @@ -776,7 +775,7 @@ impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(
PollStatus<&mut T::Tally, BlockNumberFor<T>, TrackIdOf<T, I>>,
PollStatus<&mut T::Tally, BlockNumberFor<T, I>, TrackIdOf<T, I>>,
) -> Result<R, DispatchError>,
) -> Result<R, DispatchError> {
match ReferendumInfoFor::<T, I>::get(index) {
Expand Down Expand Up @@ -893,7 +892,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn schedule_enactment(
index: ReferendumIndex,
track: &TrackInfoOf<T, I>,
desired: DispatchTime<BlockNumberFor<T>>,
desired: DispatchTime<BlockNumberFor<T, I>>,
origin: PalletsOriginOf<T>,
call: BoundedCallOf<T, I>,
) {
Expand All @@ -916,8 +915,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Set an alarm to dispatch `call` at block number `when`.
fn set_alarm(
call: BoundedCallOf<T, I>,
when: BlockNumberFor<T>,
) -> Option<(BlockNumberFor<T>, ScheduleAddressOf<T, I>)> {
when: BlockNumberFor<T, I>,
) -> Option<(BlockNumberFor<T, I>, ScheduleAddressOf<T, I>)> {
let alarm_interval = T::AlarmInterval::get().max(One::one());
// Alarm must go off no earlier than `when`.
// This rounds `when` upwards to the next multiple of `alarm_interval`.
Expand Down Expand Up @@ -950,9 +949,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn begin_deciding(
status: &mut ReferendumStatusOf<T, I>,
index: ReferendumIndex,
now: BlockNumberFor<T>,
now: BlockNumberFor<T, I>,
track: &TrackInfoOf<T, I>,
) -> (Option<BlockNumberFor<T>>, BeginDecidingBranch) {
) -> (Option<BlockNumberFor<T, I>>, BeginDecidingBranch) {
let is_passing = Self::is_passing(
&status.tally,
Zero::zero(),
Expand Down Expand Up @@ -988,11 +987,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
///
/// If `None`, then it is queued and should be nudged automatically as the queue gets drained.
fn ready_for_deciding(
now: BlockNumberFor<T>,
now: BlockNumberFor<T, I>,
track: &TrackInfoOf<T, I>,
index: ReferendumIndex,
status: &mut ReferendumStatusOf<T, I>,
) -> (Option<BlockNumberFor<T>>, ServiceBranch) {
) -> (Option<BlockNumberFor<T, I>>, ServiceBranch) {
let deciding_count = DecidingCount::<T, I>::get(status.track);
if deciding_count < track.max_deciding {
// Begin deciding.
Expand Down Expand Up @@ -1049,7 +1048,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn ensure_alarm_at(
status: &mut ReferendumStatusOf<T, I>,
index: ReferendumIndex,
alarm: BlockNumberFor<T>,
alarm: BlockNumberFor<T, I>,
) -> bool {
if status.alarm.as_ref().map_or(true, |&(when, _)| when != alarm) {
// Either no alarm or one that was different
Expand Down Expand Up @@ -1094,7 +1093,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// `TrackQueue`. Basically this happens when a referendum is in the deciding queue and receives
/// a vote, or when it moves into the deciding queue.
fn service_referendum(
now: BlockNumberFor<T>,
now: BlockNumberFor<T, I>,
index: ReferendumIndex,
mut status: ReferendumStatusOf<T, I>,
) -> (ReferendumInfoOf<T, I>, bool, ServiceBranch) {
Expand All @@ -1106,7 +1105,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
};
// Default the alarm to the end of the world.
let timeout = status.submitted + T::UndecidingTimeout::get();
let mut alarm = BlockNumberFor::<T>::max_value();
let mut alarm = BlockNumberFor::<T, I>::max_value();
let branch;
match &mut status.deciding {
None => {
Expand Down Expand Up @@ -1237,7 +1236,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
},
}

let dirty_alarm = if alarm < BlockNumberFor::<T>::max_value() {
let dirty_alarm = if alarm < BlockNumberFor::<T, I>::max_value() {
Self::ensure_alarm_at(&mut status, index, alarm)
} else {
Self::ensure_no_alarm(&mut status)
Expand All @@ -1248,11 +1247,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Determine the point at which a referendum will be accepted, move into confirmation with the
/// given `tally` or end with rejection (whichever happens sooner).
fn decision_time(
deciding: &DecidingStatusOf<T>,
deciding: &DecidingStatusOf<T, I>,
tally: &T::Tally,
track_id: TrackIdOf<T, I>,
track: &TrackInfoOf<T, I>,
) -> BlockNumberFor<T> {
) -> BlockNumberFor<T, I> {
deciding.confirming.unwrap_or_else(|| {
// Set alarm to the point where the current voting would make it pass.
let approval = tally.approval(track_id);
Expand Down Expand Up @@ -1311,8 +1310,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// `approval_needed`.
fn is_passing(
tally: &T::Tally,
elapsed: BlockNumberFor<T>,
period: BlockNumberFor<T>,
elapsed: BlockNumberFor<T, I>,
period: BlockNumberFor<T, I>,
support_needed: &Curve,
approval_needed: &Curve,
id: TrackIdOf<T, I>,
Expand Down Expand Up @@ -1381,7 +1380,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
if let Some(deciding) = status.deciding {
ensure!(
deciding.since <
deciding.confirming.unwrap_or(BlockNumberFor::<T>::max_value()),
deciding.confirming.unwrap_or(BlockNumberFor::<T, I>::max_value()),
"Deciding status cannot begin before confirming stage."
)
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/referenda/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod v0 {
pub type ReferendumInfoOf<T, I> = ReferendumInfo<
TrackIdOf<T, I>,
PalletsOriginOf<T>,
frame_system::pallet_prelude::BlockNumberFor<T>,
BlockNumberFor<T, I>,
BoundedCallOf<T, I>,
BalanceOf<T, I>,
TallyOf<T, I>,
Expand Down
15 changes: 9 additions & 6 deletions substrate/frame/referenda/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ use sp_runtime::{FixedI64, PerThing, RuntimeDebug};

pub type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

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

pub type NegativeImbalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
Expand All @@ -43,7 +46,7 @@ pub type PalletsOriginOf<T> =
pub type ReferendumInfoOf<T, I> = ReferendumInfo<
TrackIdOf<T, I>,
PalletsOriginOf<T>,
BlockNumberFor<T>,
BlockNumberFor<T, I>,
BoundedCallOf<T, I>,
BalanceOf<T, I>,
TallyOf<T, I>,
Expand All @@ -53,19 +56,19 @@ pub type ReferendumInfoOf<T, I> = ReferendumInfo<
pub type ReferendumStatusOf<T, I> = ReferendumStatus<
TrackIdOf<T, I>,
PalletsOriginOf<T>,
BlockNumberFor<T>,
BlockNumberFor<T, I>,
BoundedCallOf<T, I>,
BalanceOf<T, I>,
TallyOf<T, I>,
<T as frame_system::Config>::AccountId,
ScheduleAddressOf<T, I>,
>;
pub type DecidingStatusOf<T> = DecidingStatus<BlockNumberFor<T>>;
pub type TrackInfoOf<T, I = ()> = TrackInfo<BalanceOf<T, I>, BlockNumberFor<T>>;
pub type DecidingStatusOf<T, I> = DecidingStatus<BlockNumberFor<T, I>>;
pub type TrackInfoOf<T, I = ()> = TrackInfo<BalanceOf<T, I>, BlockNumberFor<T, I>>;
pub type TrackIdOf<T, I> =
<<T as Config<I>>::Tracks as TracksInfo<BalanceOf<T, I>, BlockNumberFor<T>>>::Id;
<<T as Config<I>>::Tracks as TracksInfo<BalanceOf<T, I>, BlockNumberFor<T, I>>>::Id;
pub type ScheduleAddressOf<T, I> = <<T as Config<I>>::Scheduler as Anon<
BlockNumberFor<T>,
BlockNumberFor<T, I>,
CallOf<T, I>,
PalletsOriginOf<T>,
>>::Address;
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 @@ -2348,7 +2348,8 @@ pub trait BlockNumberProvider {
+ TypeInfo
+ Debug
+ MaxEncodedLen
+ Copy;
+ Copy
+ EncodeLike;

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