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

Refactors referenda pallet to use fungible traits #1785

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Adds the OnSlash handler back
gpestana committed Oct 4, 2023
commit fe11b964b6b4195b79770456790945e03433514a
23 changes: 16 additions & 7 deletions substrate/frame/referenda/src/lib.rs
Original file line number Diff line number Diff line change
@@ -75,8 +75,8 @@ use frame_support::{
DispatchTime,
},
tokens::Precision,
Currency, LockIdentifier, OriginTrait, PollStatus, Polling, QueryPreimage, StorePreimage,
VoteTally,
LockIdentifier, OnUnbalanced, OriginTrait, PollStatus, Polling, QueryPreimage,
StorePreimage, VoteTally,
},
BoundedVec,
};
@@ -97,10 +97,10 @@ use self::branch::{BeginDecidingBranch, OneFewerDecidingBranch, ServiceBranch};
pub use self::{
pallet::*,
types::{
BalanceOf, BoundedCallOf, CallOf, Curve, DecidingStatus, DecidingStatusOf, Deposit,
InsertSorted, NegativeImbalanceOf, PalletsOriginOf, ReferendumIndex, ReferendumInfo,
ReferendumInfoOf, ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf,
TrackIdOf, TrackInfo, TrackInfoOf, TracksInfo, VotesOf,
BalanceOf, BoundedCallOf, CallOf, CreditOf, Curve, DecidingStatus, DecidingStatusOf,
Deposit, InsertSorted, PalletsOriginOf, ReferendumIndex, ReferendumInfo, ReferendumInfoOf,
ReferendumStatus, ReferendumStatusOf, ScheduleAddressOf, TallyOf, TrackIdOf, TrackInfo,
TrackInfoOf, TracksInfo, VotesOf,
},
weights::WeightInfo,
};
@@ -208,6 +208,9 @@ pub mod pallet {
/// Origin from which any vote may be killed.
type KillOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Handler for the unbalanced reduction when slashing a preimage deposit.
type OnSlash: OnUnbalanced<CreditOf<Self, I>>;

/// The counting type for votes. Usually just balance.
type Votes: AtLeast32BitUnsigned + Copy + Parameter + Member + MaxEncodedLen;

@@ -1293,7 +1296,13 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Slash a deposit, if `Some`.
fn slash_deposit(deposit: Option<Deposit<T::AccountId, BalanceOf<T, I>>>) {
if let Some(Deposit { who, amount }) = deposit {
let (_, non_slashed) = T::Currency::slash(&HoldReason::Referendum.into(), &who, amount);
let (imbalance, non_slashed) = <T::Currency as FnBalanced<T::AccountId>>::slash(
&HoldReason::Referendum.into(),
&who,
amount,
);
T::OnSlash::on_unbalanced(imbalance);

Self::deposit_event(Event::<T, I>::DepositSlashed {
who,
amount: amount.saturating_sub(non_slashed),
Copy link
Member

Choose a reason for hiding this comment

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

I think its possible to first emit the event and use imbalance.peek(), that makes it a bit more understandable IMHO.

1 change: 1 addition & 0 deletions substrate/frame/referenda/src/mock.rs
Original file line number Diff line number Diff line change
@@ -216,6 +216,7 @@ impl Config for Test {
type SubmitOrigin = frame_system::EnsureSigned<u64>;
type CancelOrigin = EnsureSignedBy<Four, u64>;
type KillOrigin = EnsureRoot<u64>;
type OnSlash = ();
type Votes = u32;
type Tally = Tally;
type SubmissionDeposit = ConstU64<2>;
9 changes: 5 additions & 4 deletions substrate/frame/referenda/src/types.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,9 @@
use super::*;
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{
traits::{fungible::Inspect as FnInspect, schedule::v3::Anon, Bounded},
traits::{
fungible::Inspect as FnInspect, schedule::v3::Anon, tokens::fungible::Credit, Bounded,
},
Parameter,
};
use scale_info::TypeInfo;
@@ -30,9 +32,8 @@ use sp_std::fmt::Debug;

pub type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as FnInspect<<T as frame_system::Config>::AccountId>>::Balance;
pub type NegativeImbalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
pub type CreditOf<T, I> =
Credit<<T as frame_system::Config>::AccountId, <T as Config<I>>::Currency>;
pub type CallOf<T, I> = <T as Config<I>>::RuntimeCall;
pub type BoundedCallOf<T, I> =
Bounded<<T as Config<I>>::RuntimeCall, <T as frame_system::Config>::Hashing>;