pallet-referenda: make the pallet instanceable (#11089)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2022-03-25 00:36:58 +08:00
committed by GitHub
parent a4041add8a
commit 44e71d8d7e
3 changed files with 160 additions and 141 deletions
+130 -114
View File
@@ -83,15 +83,18 @@ use sp_std::{fmt::Debug, prelude::*};
mod branch;
mod types;
pub mod weights;
use branch::{BeginDecidingBranch, OneFewerDecidingBranch, ServiceBranch};
pub use pallet::*;
pub use types::{
BalanceOf, CallOf, Curve, DecidingStatus, DecidingStatusOf, Deposit, InsertSorted,
NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex, ReferendumInfo, ReferendumInfoOf,
ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf, TrackIdOf, TrackInfo,
TrackInfoOf, TracksInfo, VotesOf,
use self::branch::{BeginDecidingBranch, OneFewerDecidingBranch, ServiceBranch};
pub use self::{
pallet::*,
types::{
BalanceOf, CallOf, Curve, DecidingStatus, DecidingStatusOf, Deposit, InsertSorted,
NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex, ReferendumInfo, ReferendumInfoOf,
ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf, TrackIdOf, TrackInfo,
TrackInfoOf, TracksInfo, VotesOf,
},
weights::WeightInfo,
};
pub use weights::WeightInfo;
#[cfg(test)]
mod mock;
@@ -106,25 +109,33 @@ const ASSEMBLY_ID: LockIdentifier = *b"assembly";
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::EnsureOrigin, Parameter};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::DispatchResult;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
pub struct Pallet<T, I = ()>(_);
#[pallet::config]
pub trait Config: frame_system::Config + Sized {
pub trait Config<I: 'static = ()>: frame_system::Config + Sized {
// System level stuff.
type Call: Parameter + Dispatchable<Origin = Self::Origin> + From<Call<Self>>;
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Call: Parameter + Dispatchable<Origin = Self::Origin> + From<Call<Self, I>>;
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// The Scheduler.
type Scheduler: ScheduleAnon<Self::BlockNumber, CallOf<Self>, PalletsOriginOf<Self>, Hash = Self::Hash>
+ ScheduleNamed<Self::BlockNumber, CallOf<Self>, PalletsOriginOf<Self>, Hash = Self::Hash>;
type Scheduler: ScheduleAnon<
Self::BlockNumber,
CallOf<Self, I>,
PalletsOriginOf<Self>,
Hash = Self::Hash,
> + ScheduleNamed<
Self::BlockNumber,
CallOf<Self, I>,
PalletsOriginOf<Self>,
Hash = Self::Hash,
>;
/// Currency type for this pallet.
type Currency: ReservableCurrency<Self::AccountId>;
// Origins and unbalances.
@@ -133,7 +144,7 @@ pub mod pallet {
/// Origin from which any vote may be killed.
type KillOrigin: EnsureOrigin<Self::Origin>;
/// Handler for the unbalanced reduction when slashing a preimage deposit.
type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;
type Slash: OnUnbalanced<NegativeImbalanceOf<Self, I>>;
/// The counting type for votes. Usually just balance.
type Votes: AtLeast32BitUnsigned + Copy + Parameter + Member;
/// The tallying type.
@@ -142,7 +153,7 @@ pub mod pallet {
// Constants
/// The minimum amount to be used as a deposit for a public referendum proposal.
#[pallet::constant]
type SubmissionDeposit: Get<BalanceOf<Self>>;
type SubmissionDeposit: Get<BalanceOf<Self, I>>;
/// Maximum size of the referendum queue for a single track.
#[pallet::constant]
@@ -162,7 +173,7 @@ pub mod pallet {
// The other stuff.
/// Information concerning the different referendum tracks.
type Tracks: TracksInfo<
BalanceOf<Self>,
BalanceOf<Self, I>,
Self::BlockNumber,
Origin = <Self::Origin as OriginTrait>::PalletsOrigin,
>;
@@ -170,39 +181,40 @@ pub mod pallet {
/// The next free referendum index, aka the number of referenda started so far.
#[pallet::storage]
pub type ReferendumCount<T> = StorageValue<_, ReferendumIndex, ValueQuery>;
pub type ReferendumCount<T, I = ()> = StorageValue<_, ReferendumIndex, ValueQuery>;
/// Information concerning any given referendum.
#[pallet::storage]
pub type ReferendumInfoFor<T: Config> =
StorageMap<_, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf<T>>;
pub type ReferendumInfoFor<T: Config<I>, I: 'static = ()> =
StorageMap<_, Blake2_128Concat, ReferendumIndex, ReferendumInfoOf<T, I>>;
/// The sorted list of referenda ready to be decided but not yet being decided, ordered by
/// conviction-weighted approvals.
///
/// This should be empty if `DecidingCount` is less than `TrackInfo::max_deciding`.
#[pallet::storage]
pub type TrackQueue<T: Config> = StorageMap<
pub type TrackQueue<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Twox64Concat,
TrackIdOf<T>,
TrackIdOf<T, I>,
BoundedVec<(ReferendumIndex, T::Votes), T::MaxQueued>,
ValueQuery,
>;
/// The number of referenda being decided currently.
#[pallet::storage]
pub type DecidingCount<T: Config> = StorageMap<_, Twox64Concat, TrackIdOf<T>, u32, ValueQuery>;
pub type DecidingCount<T: Config<I>, I: 'static = ()> =
StorageMap<_, Twox64Concat, TrackIdOf<T, I>, u32, ValueQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
pub enum Event<T: Config<I>, I: 'static = ()> {
/// A referendum has being submitted.
Submitted {
/// Index of the referendum.
index: ReferendumIndex,
/// The track (and by extension proposal dispatch origin) of this referendum.
track: TrackIdOf<T>,
track: TrackIdOf<T, I>,
/// The hash of the proposal up for referendum.
proposal_hash: T::Hash,
},
@@ -213,7 +225,7 @@ pub mod pallet {
/// The account who placed the deposit.
who: T::AccountId,
/// The amount placed by the account.
amount: BalanceOf<T>,
amount: BalanceOf<T, I>,
},
/// The decision deposit has been refunded.
DecisionDepositRefunded {
@@ -222,21 +234,21 @@ pub mod pallet {
/// The account who placed the deposit.
who: T::AccountId,
/// The amount placed by the account.
amount: BalanceOf<T>,
amount: BalanceOf<T, I>,
},
/// A deposit has been slashaed.
DepositSlashed {
/// The account who placed the deposit.
who: T::AccountId,
/// The amount placed by the account.
amount: BalanceOf<T>,
amount: BalanceOf<T, I>,
},
/// A referendum has moved into the deciding phase.
DecisionStarted {
/// Index of the referendum.
index: ReferendumIndex,
/// The track (and by extension proposal dispatch origin) of this referendum.
track: TrackIdOf<T>,
track: TrackIdOf<T, I>,
/// The hash of the proposal up for referendum.
proposal_hash: T::Hash,
/// The current tally of votes in this referendum.
@@ -293,7 +305,7 @@ pub mod pallet {
}
#[pallet::error]
pub enum Error<T> {
pub enum Error<T, I = ()> {
/// Referendum is not ongoing.
NotOngoing,
/// Referendum's decision deposit is already paid.
@@ -319,7 +331,7 @@ pub mod pallet {
}
#[pallet::call]
impl<T: Config> Pallet<T> {
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Propose a referendum on a privileged action.
///
/// - `origin`: must be `Signed` and the account must have `SubmissionDeposit` funds
@@ -338,9 +350,10 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;
let track = T::Tracks::track_for(&proposal_origin).map_err(|_| Error::<T>::NoTrack)?;
let track =
T::Tracks::track_for(&proposal_origin).map_err(|_| Error::<T, I>::NoTrack)?;
let submission_deposit = Self::take_deposit(who, T::SubmissionDeposit::get())?;
let index = ReferendumCount::<T>::mutate(|x| {
let index = ReferendumCount::<T, I>::mutate(|x| {
let r = *x;
*x += 1;
r
@@ -360,9 +373,9 @@ pub mod pallet {
in_queue: false,
alarm: Self::set_alarm(nudge_call, now.saturating_add(T::UndecidingTimeout::get())),
};
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
Self::deposit_event(Event::<T>::Submitted { index, track, proposal_hash });
Self::deposit_event(Event::<T, I>::Submitted { index, track, proposal_hash });
Ok(())
}
@@ -374,24 +387,24 @@ pub mod pallet {
/// posted.
///
/// Emits `DecisionDepositPlaced`.
#[pallet::weight(ServiceBranch::max_weight_of_deposit::<T>())]
#[pallet::weight(ServiceBranch::max_weight_of_deposit::<T, I>())]
pub fn place_decision_deposit(
origin: OriginFor<T>,
index: ReferendumIndex,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let mut status = Self::ensure_ongoing(index)?;
ensure!(status.decision_deposit.is_none(), Error::<T>::HasDeposit);
let track = Self::track(status.track).ok_or(Error::<T>::NoTrack)?;
ensure!(status.decision_deposit.is_none(), Error::<T, I>::HasDeposit);
let track = Self::track(status.track).ok_or(Error::<T, I>::NoTrack)?;
status.decision_deposit =
Some(Self::take_deposit(who.clone(), track.decision_deposit)?);
let now = frame_system::Pallet::<T>::block_number();
let (info, _, branch) = Self::service_referendum(now, index, status);
ReferendumInfoFor::<T>::insert(index, info);
ReferendumInfoFor::<T, I>::insert(index, info);
let e =
Event::<T>::DecisionDepositPlaced { index, who, amount: track.decision_deposit };
Event::<T, I>::DecisionDepositPlaced { index, who, amount: track.decision_deposit };
Self::deposit_event(e);
Ok(branch.weight_of_deposit::<T>().into())
Ok(branch.weight_of_deposit::<T, I>().into())
}
/// Refund the Decision Deposit for a closed referendum back to the depositor.
@@ -407,14 +420,15 @@ pub mod pallet {
index: ReferendumIndex,
) -> DispatchResult {
ensure_signed_or_root(origin)?;
let mut info = ReferendumInfoFor::<T>::get(index).ok_or(Error::<T>::BadReferendum)?;
let mut info =
ReferendumInfoFor::<T, I>::get(index).ok_or(Error::<T, I>::BadReferendum)?;
let deposit = info
.take_decision_deposit()
.map_err(|_| Error::<T>::Unfinished)?
.ok_or(Error::<T>::NoDeposit)?;
.map_err(|_| Error::<T, I>::Unfinished)?
.ok_or(Error::<T, I>::NoDeposit)?;
Self::refund_deposit(Some(deposit.clone()));
ReferendumInfoFor::<T>::insert(index, info);
let e = Event::<T>::DecisionDepositRefunded {
ReferendumInfoFor::<T, I>::insert(index, info);
let e = Event::<T, I>::DecisionDepositRefunded {
index,
who: deposit.who,
amount: deposit.amount,
@@ -437,13 +451,13 @@ pub mod pallet {
let _ = T::Scheduler::cancel(last_alarm);
}
Self::note_one_fewer_deciding(status.track);
Self::deposit_event(Event::<T>::Cancelled { index, tally: status.tally });
Self::deposit_event(Event::<T, I>::Cancelled { index, tally: status.tally });
let info = ReferendumInfo::Cancelled(
frame_system::Pallet::<T>::block_number(),
status.submission_deposit,
status.decision_deposit,
);
ReferendumInfoFor::<T>::insert(index, info);
ReferendumInfoFor::<T, I>::insert(index, info);
Ok(())
}
@@ -461,11 +475,11 @@ pub mod pallet {
let _ = T::Scheduler::cancel(last_alarm);
}
Self::note_one_fewer_deciding(status.track);
Self::deposit_event(Event::<T>::Killed { index, tally: status.tally });
Self::deposit_event(Event::<T, I>::Killed { index, tally: status.tally });
Self::slash_deposit(Some(status.submission_deposit.clone()));
Self::slash_deposit(status.decision_deposit.clone());
let info = ReferendumInfo::Killed(frame_system::Pallet::<T>::block_number());
ReferendumInfoFor::<T>::insert(index, info);
ReferendumInfoFor::<T, I>::insert(index, info);
Ok(())
}
@@ -473,7 +487,7 @@ pub mod pallet {
///
/// - `origin`: must be `Root`.
/// - `index`: the referendum to be advanced.
#[pallet::weight(ServiceBranch::max_weight_of_nudge::<T>())]
#[pallet::weight(ServiceBranch::max_weight_of_nudge::<T, I>())]
pub fn nudge_referendum(
origin: OriginFor<T>,
index: ReferendumIndex,
@@ -485,9 +499,9 @@ pub mod pallet {
status.alarm = None;
let (info, dirty, branch) = Self::service_referendum(now, index, status);
if dirty {
ReferendumInfoFor::<T>::insert(index, info);
ReferendumInfoFor::<T, I>::insert(index, info);
}
Ok(Some(branch.weight_of_nudge::<T>()).into())
Ok(Some(branch.weight_of_nudge::<T, I>()).into())
}
/// Advance a track onto its next logical state. Only used internally.
@@ -499,14 +513,14 @@ pub mod pallet {
/// `DecidingCount` is not yet updated. This means that we should either:
/// - begin deciding another referendum (and leave `DecidingCount` alone); or
/// - decrement `DecidingCount`.
#[pallet::weight(OneFewerDecidingBranch::max_weight::<T>())]
#[pallet::weight(OneFewerDecidingBranch::max_weight::<T, I>())]
pub fn one_fewer_deciding(
origin: OriginFor<T>,
track: TrackIdOf<T>,
track: TrackIdOf<T, I>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let track_info = T::Tracks::info(track).ok_or(Error::<T>::BadTrack)?;
let mut track_queue = TrackQueue::<T>::get(track);
let track_info = T::Tracks::info(track).ok_or(Error::<T, I>::BadTrack)?;
let mut track_queue = TrackQueue::<T, I>::get(track);
let branch =
if let Some((index, mut status)) = Self::next_for_deciding(&mut track_queue) {
let now = frame_system::Pallet::<T>::block_number();
@@ -515,23 +529,23 @@ pub mod pallet {
if let Some(set_alarm) = maybe_alarm {
Self::ensure_alarm_at(&mut status, index, set_alarm);
}
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));
TrackQueue::<T>::insert(track, track_queue);
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
TrackQueue::<T, I>::insert(track, track_queue);
branch.into()
} else {
DecidingCount::<T>::mutate(track, |x| x.saturating_dec());
DecidingCount::<T, I>::mutate(track, |x| x.saturating_dec());
OneFewerDecidingBranch::QueueEmpty
};
Ok(Some(branch.weight::<T>()).into())
Ok(Some(branch.weight::<T, I>()).into())
}
}
}
impl<T: Config> Polling<T::Tally> for Pallet<T> {
impl<T: Config<I>, I: 'static> Polling<T::Tally> for Pallet<T, I> {
type Index = ReferendumIndex;
type Votes = VotesOf<T>;
type Votes = VotesOf<T, I>;
type Moment = T::BlockNumber;
type Class = TrackIdOf<T>;
type Class = TrackIdOf<T, I>;
fn classes() -> Vec<Self::Class> {
T::Tracks::tracks().iter().map(|x| x.0).collect()
@@ -539,14 +553,14 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
fn access_poll<R>(
index: Self::Index,
f: impl FnOnce(PollStatus<&mut T::Tally, T::BlockNumber, TrackIdOf<T>>) -> R,
f: impl FnOnce(PollStatus<&mut T::Tally, T::BlockNumber, TrackIdOf<T, I>>) -> R,
) -> R {
match ReferendumInfoFor::<T>::get(index) {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(mut status)) => {
let result = f(PollStatus::Ongoing(&mut status.tally, status.track));
let now = frame_system::Pallet::<T>::block_number();
Self::ensure_alarm_at(&mut status, index, now + One::one());
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
result
},
Some(ReferendumInfo::Approved(end, ..)) => f(PollStatus::Completed(end, true)),
@@ -558,15 +572,15 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
fn try_access_poll<R>(
index: Self::Index,
f: impl FnOnce(
PollStatus<&mut T::Tally, T::BlockNumber, TrackIdOf<T>>,
PollStatus<&mut T::Tally, T::BlockNumber, TrackIdOf<T, I>>,
) -> Result<R, DispatchError>,
) -> Result<R, DispatchError> {
match ReferendumInfoFor::<T>::get(index) {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(mut status)) => {
let result = f(PollStatus::Ongoing(&mut status.tally, status.track))?;
let now = frame_system::Pallet::<T>::block_number();
Self::ensure_alarm_at(&mut status, index, now + One::one());
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
Ok(result)
},
Some(ReferendumInfo::Approved(end, ..)) => f(PollStatus::Completed(end, true)),
@@ -575,13 +589,13 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
}
}
fn as_ongoing(index: Self::Index) -> Option<(T::Tally, TrackIdOf<T>)> {
fn as_ongoing(index: Self::Index) -> Option<(T::Tally, TrackIdOf<T, I>)> {
Self::ensure_ongoing(index).ok().map(|x| (x.tally, x.track))
}
#[cfg(feature = "runtime-benchmarks")]
fn create_ongoing(class: Self::Class) -> Result<Self::Index, ()> {
let index = ReferendumCount::<T>::mutate(|x| {
let index = ReferendumCount::<T, I>::mutate(|x| {
let r = *x;
*x += 1;
r
@@ -590,7 +604,7 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
let dummy_account_id =
codec::Decode::decode(&mut sp_runtime::traits::TrailingZeroInput::new(&b"dummy"[..]))
.expect("infinite length input; no invalid inputs for type; qed");
let mut status = ReferendumStatusOf::<T> {
let mut status = ReferendumStatusOf::<T, I> {
track: class,
origin: frame_support::dispatch::RawOrigin::Root.into(),
proposal_hash: <T::Hashing as sp_runtime::traits::Hash>::hash_of(&index),
@@ -604,7 +618,7 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
alarm: None,
};
Self::ensure_alarm_at(&mut status, index, sp_runtime::traits::Bounded::max_value());
ReferendumInfoFor::<T>::insert(index, ReferendumInfo::Ongoing(status));
ReferendumInfoFor::<T, I>::insert(index, ReferendumInfo::Ongoing(status));
Ok(index)
}
@@ -619,7 +633,7 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
} else {
ReferendumInfo::Rejected(now, status.submission_deposit, status.decision_deposit)
};
ReferendumInfoFor::<T>::insert(index, info);
ReferendumInfoFor::<T, I>::insert(index, info);
Ok(())
}
@@ -633,20 +647,22 @@ impl<T: Config> Polling<T::Tally> for Pallet<T> {
}
}
impl<T: Config> Pallet<T> {
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Check that referendum `index` is in the `Ongoing` state and return the `ReferendumStatus`
/// value, or `Err` otherwise.
pub fn ensure_ongoing(index: ReferendumIndex) -> Result<ReferendumStatusOf<T>, DispatchError> {
match ReferendumInfoFor::<T>::get(index) {
pub fn ensure_ongoing(
index: ReferendumIndex,
) -> Result<ReferendumStatusOf<T, I>, DispatchError> {
match ReferendumInfoFor::<T, I>::get(index) {
Some(ReferendumInfo::Ongoing(status)) => Ok(status),
_ => Err(Error::<T>::NotOngoing.into()),
_ => Err(Error::<T, I>::NotOngoing.into()),
}
}
// Enqueue a proposal from a referendum which has presumably passed.
fn schedule_enactment(
index: ReferendumIndex,
track: &TrackInfoOf<T>,
track: &TrackInfoOf<T, I>,
desired: DispatchTime<T::BlockNumber>,
origin: PalletsOriginOf<T>,
call_hash: T::Hash,
@@ -668,9 +684,9 @@ impl<T: Config> Pallet<T> {
/// Set an alarm to dispatch `call` at block number `when`.
fn set_alarm(
call: impl Into<CallOf<T>>,
call: impl Into<CallOf<T, I>>,
when: T::BlockNumber,
) -> Option<(T::BlockNumber, ScheduleAddressOf<T>)> {
) -> Option<(T::BlockNumber, ScheduleAddressOf<T, I>)> {
let alarm_interval = T::AlarmInterval::get().max(One::one());
let when = (when + alarm_interval - One::one()) / alarm_interval * alarm_interval;
let maybe_result = T::Scheduler::schedule(
@@ -698,10 +714,10 @@ impl<T: Config> Pallet<T> {
///
/// This will properly set up the `confirming` item.
fn begin_deciding(
status: &mut ReferendumStatusOf<T>,
status: &mut ReferendumStatusOf<T, I>,
index: ReferendumIndex,
now: T::BlockNumber,
track: &TrackInfoOf<T>,
track: &TrackInfoOf<T, I>,
) -> (Option<T::BlockNumber>, BeginDecidingBranch) {
let is_passing = Self::is_passing(
&status.tally,
@@ -711,14 +727,14 @@ impl<T: Config> Pallet<T> {
&track.min_approval,
);
status.in_queue = false;
Self::deposit_event(Event::<T>::DecisionStarted {
Self::deposit_event(Event::<T, I>::DecisionStarted {
index,
tally: status.tally.clone(),
proposal_hash: status.proposal_hash.clone(),
track: status.track.clone(),
});
let confirming = if is_passing {
Self::deposit_event(Event::<T>::ConfirmStarted { index });
Self::deposit_event(Event::<T, I>::ConfirmStarted { index });
Some(now.saturating_add(track.confirm_period))
} else {
None
@@ -737,21 +753,21 @@ impl<T: Config> Pallet<T> {
/// If `None`, then it is queued and should be nudged automatically as the queue gets drained.
fn ready_for_deciding(
now: T::BlockNumber,
track: &TrackInfoOf<T>,
track: &TrackInfoOf<T, I>,
index: ReferendumIndex,
status: &mut ReferendumStatusOf<T>,
status: &mut ReferendumStatusOf<T, I>,
) -> (Option<T::BlockNumber>, ServiceBranch) {
let deciding_count = DecidingCount::<T>::get(status.track);
let deciding_count = DecidingCount::<T, I>::get(status.track);
if deciding_count < track.max_deciding {
// Begin deciding.
DecidingCount::<T>::insert(status.track, deciding_count.saturating_add(1));
DecidingCount::<T, I>::insert(status.track, deciding_count.saturating_add(1));
let r = Self::begin_deciding(status, index, now, track);
(r.0, r.1.into())
} else {
// Add to queue.
let item = (index, status.tally.ayes());
status.in_queue = true;
TrackQueue::<T>::mutate(status.track, |q| q.insert_sorted_by_key(item, |x| x.1));
TrackQueue::<T, I>::mutate(status.track, |q| q.insert_sorted_by_key(item, |x| x.1));
(None, ServiceBranch::Queued)
}
}
@@ -759,8 +775,8 @@ impl<T: Config> Pallet<T> {
/// Grab the index and status for the referendum which is the highest priority of those for the
/// given track which are ready for being decided.
fn next_for_deciding(
track_queue: &mut BoundedVec<(u32, VotesOf<T>), T::MaxQueued>,
) -> Option<(ReferendumIndex, ReferendumStatusOf<T>)> {
track_queue: &mut BoundedVec<(u32, VotesOf<T, I>), T::MaxQueued>,
) -> Option<(ReferendumIndex, ReferendumStatusOf<T, I>)> {
loop {
let (index, _) = track_queue.pop()?;
match Self::ensure_ongoing(index) {
@@ -773,7 +789,7 @@ impl<T: Config> Pallet<T> {
/// Schedule a call to `one_fewer_deciding` function via the dispatchable
/// `defer_one_fewer_deciding`. We could theoretically call it immediately (and it would be
/// overall more efficient), however the weights become rather less easy to measure.
fn note_one_fewer_deciding(track: TrackIdOf<T>) {
fn note_one_fewer_deciding(track: TrackIdOf<T, I>) {
// Set an alarm call for the next block to nudge the track along.
let now = frame_system::Pallet::<T>::block_number();
let next_block = now + One::one();
@@ -801,7 +817,7 @@ impl<T: Config> Pallet<T> {
///
/// Returns `false` if nothing changed.
fn ensure_alarm_at(
status: &mut ReferendumStatusOf<T>,
status: &mut ReferendumStatusOf<T, I>,
index: ReferendumIndex,
alarm: T::BlockNumber,
) -> bool {
@@ -839,8 +855,8 @@ impl<T: Config> Pallet<T> {
fn service_referendum(
now: T::BlockNumber,
index: ReferendumIndex,
mut status: ReferendumStatusOf<T>,
) -> (ReferendumInfoOf<T>, bool, ServiceBranch) {
mut status: ReferendumStatusOf<T, I>,
) -> (ReferendumInfoOf<T, I>, bool, ServiceBranch) {
let mut dirty = false;
// Should it begin being decided?
let track = match Self::track(status.track) {
@@ -857,7 +873,7 @@ impl<T: Config> Pallet<T> {
if status.in_queue {
// Does our position in the queue need updating?
let ayes = status.tally.ayes();
let mut queue = TrackQueue::<T>::get(status.track);
let mut queue = TrackQueue::<T, I>::get(status.track);
let maybe_old_pos = queue.iter().position(|(x, _)| *x == index);
let new_pos = queue.binary_search_by_key(&ayes, |x| x.1).unwrap_or_else(|x| x);
branch = if maybe_old_pos.is_none() && new_pos > 0 {
@@ -872,7 +888,7 @@ impl<T: Config> Pallet<T> {
} else {
ServiceBranch::NotQueued
};
TrackQueue::<T>::insert(status.track, queue);
TrackQueue::<T, I>::insert(status.track, queue);
} else {
// Are we ready for deciding?
branch = if status.decision_deposit.is_some() {
@@ -897,7 +913,7 @@ impl<T: Config> Pallet<T> {
if status.deciding.is_none() && now >= timeout {
// Too long without being decided - end it.
Self::ensure_no_alarm(&mut status);
Self::deposit_event(Event::<T>::TimedOut { index, tally: status.tally });
Self::deposit_event(Event::<T, I>::TimedOut { index, tally: status.tally });
return (
ReferendumInfo::TimedOut(
now,
@@ -931,7 +947,7 @@ impl<T: Config> Pallet<T> {
status.origin,
call_hash,
);
Self::deposit_event(Event::<T>::Confirmed {
Self::deposit_event(Event::<T, I>::Confirmed {
index,
tally: status.tally,
});
@@ -950,7 +966,7 @@ impl<T: Config> Pallet<T> {
// Start confirming
dirty = true;
deciding.confirming = Some(now.saturating_add(track.confirm_period));
Self::deposit_event(Event::<T>::ConfirmStarted { index });
Self::deposit_event(Event::<T, I>::ConfirmStarted { index });
ServiceBranch::BeginConfirming
},
}
@@ -959,7 +975,7 @@ impl<T: Config> Pallet<T> {
// Failed!
Self::ensure_no_alarm(&mut status);
Self::note_one_fewer_deciding(status.track);
Self::deposit_event(Event::<T>::Rejected { index, tally: status.tally });
Self::deposit_event(Event::<T, I>::Rejected { index, tally: status.tally });
return (
ReferendumInfo::Rejected(
now,
@@ -974,7 +990,7 @@ impl<T: Config> Pallet<T> {
// Stop confirming
dirty = true;
deciding.confirming = None;
Self::deposit_event(Event::<T>::ConfirmAborted { index });
Self::deposit_event(Event::<T, I>::ConfirmAborted { index });
ServiceBranch::EndConfirming
} else {
ServiceBranch::ContinueNotConfirming
@@ -993,7 +1009,7 @@ impl<T: Config> Pallet<T> {
fn decision_time(
deciding: &DecidingStatusOf<T>,
tally: &T::Tally,
track: &TrackInfoOf<T>,
track: &TrackInfoOf<T, I>,
) -> T::BlockNumber {
deciding.confirming.unwrap_or_else(|| {
// Set alarm to the point where the current voting would make it pass.
@@ -1007,7 +1023,7 @@ impl<T: Config> Pallet<T> {
}
/// Cancel the alarm in `status`, if one exists.
fn ensure_no_alarm(status: &mut ReferendumStatusOf<T>) {
fn ensure_no_alarm(status: &mut ReferendumStatusOf<T, I>) {
if let Some((_, last_alarm)) = status.alarm.take() {
// Incorrect alarm - cancel it.
let _ = T::Scheduler::cancel(last_alarm);
@@ -1017,29 +1033,29 @@ impl<T: Config> Pallet<T> {
/// Reserve a deposit and return the `Deposit` instance.
fn take_deposit(
who: T::AccountId,
amount: BalanceOf<T>,
) -> Result<Deposit<T::AccountId, BalanceOf<T>>, DispatchError> {
amount: BalanceOf<T, I>,
) -> Result<Deposit<T::AccountId, BalanceOf<T, I>>, DispatchError> {
T::Currency::reserve(&who, amount)?;
Ok(Deposit { who, amount })
}
/// Return a deposit, if `Some`.
fn refund_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T>>>) {
fn refund_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>) {
if let Some(Deposit { who, amount }) = deposit {
T::Currency::unreserve(&who, amount);
}
}
/// Slash a deposit, if `Some`.
fn slash_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T>>>) {
fn slash_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>) {
if let Some(Deposit { who, amount }) = deposit {
T::Slash::on_unbalanced(T::Currency::slash_reserved(&who, amount).0);
Self::deposit_event(Event::<T>::DepositSlashed { who, amount });
Self::deposit_event(Event::<T, I>::DepositSlashed { who, amount });
}
}
/// Get the track info value for the track `id`.
fn track(id: TrackIdOf<T>) -> Option<&'static TrackInfoOf<T>> {
fn track(id: TrackIdOf<T, I>) -> Option<&'static TrackInfoOf<T, I>> {
let tracks = T::Tracks::tracks();
let index = tracks.binary_search_by_key(&id, |x| x.0).unwrap_or_else(|x| x);
Some(&tracks[index].1)