Skip to content

Commit

Permalink
refactor subtrait/elevated trait as not needed (paritytech#7497)
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 authored and shamb0 committed Nov 21, 2020
1 parent 0473842 commit d86a662
Showing 1 changed file with 9 additions and 65 deletions.
74 changes: 9 additions & 65 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,17 +704,17 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
// of the inner member.
mod imbalances {
use super::{
result, Subtrait, DefaultInstance, Imbalance, Trait, Zero, Instance, Saturating,
result, DefaultInstance, Imbalance, Trait, Zero, Instance, Saturating,
StorageValue, TryDrop,
};
use sp_std::mem;

/// Opaque, move-only struct with private fields that serves as a token denoting that
/// funds have been created without any equal and opposite accounting.
#[must_use]
pub struct PositiveImbalance<T: Subtrait<I>, I: Instance=DefaultInstance>(T::Balance);
pub struct PositiveImbalance<T: Trait<I>, I: Instance=DefaultInstance>(T::Balance);

impl<T: Subtrait<I>, I: Instance> PositiveImbalance<T, I> {
impl<T: Trait<I>, I: Instance> PositiveImbalance<T, I> {
/// Create a new positive imbalance from a balance.
pub fn new(amount: T::Balance) -> Self {
PositiveImbalance(amount)
Expand All @@ -724,9 +724,9 @@ mod imbalances {
/// Opaque, move-only struct with private fields that serves as a token denoting that
/// funds have been destroyed without any equal and opposite accounting.
#[must_use]
pub struct NegativeImbalance<T: Subtrait<I>, I: Instance=DefaultInstance>(T::Balance);
pub struct NegativeImbalance<T: Trait<I>, I: Instance=DefaultInstance>(T::Balance);

impl<T: Subtrait<I>, I: Instance> NegativeImbalance<T, I> {
impl<T: Trait<I>, I: Instance> NegativeImbalance<T, I> {
/// Create a new negative imbalance from a balance.
pub fn new(amount: T::Balance) -> Self {
NegativeImbalance(amount)
Expand Down Expand Up @@ -835,81 +835,25 @@ mod imbalances {
}
}

impl<T: Subtrait<I>, I: Instance> Drop for PositiveImbalance<T, I> {
impl<T: Trait<I>, I: Instance> Drop for PositiveImbalance<T, I> {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<super::TotalIssuance<super::ElevatedTrait<T, I>, I>>::mutate(
<super::TotalIssuance<T, I>>::mutate(
|v| *v = v.saturating_add(self.0)
);
}
}

impl<T: Subtrait<I>, I: Instance> Drop for NegativeImbalance<T, I> {
impl<T: Trait<I>, I: Instance> Drop for NegativeImbalance<T, I> {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<super::TotalIssuance<super::ElevatedTrait<T, I>, I>>::mutate(
<super::TotalIssuance<T, I>>::mutate(
|v| *v = v.saturating_sub(self.0)
);
}
}
}

// TODO: #2052
// Somewhat ugly hack in order to gain access to module's `increase_total_issuance_by`
// using only the Subtrait (which defines only the types that are not dependent
// on Positive/NegativeImbalance). Subtrait must be used otherwise we end up with a
// circular dependency with Trait having some types be dependent on PositiveImbalance<Trait>
// and PositiveImbalance itself depending back on Trait for its Drop impl (and thus
// its type declaration).
// This works as long as `increase_total_issuance_by` doesn't use the Imbalance
// types (basically for charging fees).
// This should eventually be refactored so that the type item that
// depends on the Imbalance type (DustRemoval) is placed in its own pallet.
struct ElevatedTrait<T: Subtrait<I>, I: Instance>(T, I);
impl<T: Subtrait<I>, I: Instance> Clone for ElevatedTrait<T, I> {
fn clone(&self) -> Self { unimplemented!() }
}
impl<T: Subtrait<I>, I: Instance> PartialEq for ElevatedTrait<T, I> {
fn eq(&self, _: &Self) -> bool { unimplemented!() }
}
impl<T: Subtrait<I>, I: Instance> Eq for ElevatedTrait<T, I> {}
impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
type BaseCallFilter = T::BaseCallFilter;
type Origin = T::Origin;
type Call = T::Call;
type Index = T::Index;
type BlockNumber = T::BlockNumber;
type Hash = T::Hash;
type Hashing = T::Hashing;
type AccountId = T::AccountId;
type Lookup = T::Lookup;
type Header = T::Header;
type Event = ();
type BlockHashCount = T::BlockHashCount;
type MaximumBlockWeight = T::MaximumBlockWeight;
type DbWeight = T::DbWeight;
type BlockExecutionWeight = T::BlockExecutionWeight;
type ExtrinsicBaseWeight = T::ExtrinsicBaseWeight;
type MaximumExtrinsicWeight = T::MaximumBlockWeight;
type MaximumBlockLength = T::MaximumBlockLength;
type AvailableBlockRatio = T::AvailableBlockRatio;
type Version = T::Version;
type PalletInfo = T::PalletInfo;
type OnNewAccount = T::OnNewAccount;
type OnKilledAccount = T::OnKilledAccount;
type AccountData = T::AccountData;
type SystemWeightInfo = T::SystemWeightInfo;
}
impl<T: Subtrait<I>, I: Instance> Trait<I> for ElevatedTrait<T, I> {
type Balance = T::Balance;
type Event = ();
type DustRemoval = ();
type ExistentialDeposit = T::ExistentialDeposit;
type AccountStore = T::AccountStore;
type WeightInfo = <T as Subtrait<I>>::WeightInfo;
type MaxLocks = T::MaxLocks;
}

impl<T: Trait<I>, I: Instance> Currency<T::AccountId> for Module<T, I> where
T::Balance: MaybeSerializeDeserialize + Debug
{
Expand Down

0 comments on commit d86a662

Please sign in to comment.