Skip to content

Commit

Permalink
Migrate pallet-balances to umbrella style
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Dec 21, 2024
1 parent d0c8a07 commit d5b22b5
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 238 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cumulus/bin/pov-validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ sc-executor.workspace = true
sp-core.workspace = true
sp-io.workspace = true
sp-maybe-compressed-blob.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true

[lints]
workspace = true
32 changes: 7 additions & 25 deletions substrate/frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,28 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { features = ["derive", "max-encoded-len"], workspace = true }
docify = { workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
scale-info = { features = ["derive"], workspace = true }
sp-runtime = { workspace = true }

[dev-dependencies]
frame-support = { features = ["experimental"], workspace = true, default-features = true }
pallet-transaction-payment = { workspace = true, default-features = true }
paste = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-transaction-payment/std",
"frame/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
# Enable support for setting the existential deposit to zero.
insecure_zero_ed = []
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame/runtime-benchmarks",
"pallet-transaction-payment/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-transaction-payment/try-runtime",
"sp-runtime/try-runtime",
]

# Enable support for setting the existential deposit to zero.
insecure_zero_ed = []
9 changes: 2 additions & 7 deletions substrate/frame/balances/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use crate::Pallet as Balances;

use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use types::ExtraFlags;
use super::{*, Pallet as Balances};
use frame::benchmarking::prelude::*;

const SEED: u32 = 0;
// existential deposit multiplier
Expand Down
35 changes: 14 additions & 21 deletions substrate/frame/balances/src/impl_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,21 @@
//! Note that `WithdrawReasons` are intentionally not used for anything in this implementation and
//! are expected to be removed in the near future, once migration to `fungible::*` traits is done.
pub use imbalances::{NegativeImbalance, PositiveImbalance};

use super::*;
use frame_support::{
ensure,
pallet_prelude::DispatchResult,
traits::{
tokens::{fungible, BalanceStatus as Status, Fortitude::Polite, Precision::BestEffort},
Currency, DefensiveSaturating, ExistenceRequirement,
ExistenceRequirement::AllowDeath,
Get, Imbalance, InspectLockableCurrency, LockIdentifier, LockableCurrency,
NamedReservableCurrency, ReservableCurrency, SignedImbalance, TryDrop, WithdrawReasons,
},
use core::cmp;
use frame::traits::{
tokens::{imbalance::TryMerge, Precision::*},
DefensiveSaturating, Imbalance, InspectLockableCurrency, LockIdentifier, LockableCurrency,
NamedReservableCurrency, SameOrOther, SignedImbalance, TryDrop, WithdrawReasons,
};
use frame_system::pallet_prelude::BlockNumberFor;
pub use imbalances::{NegativeImbalance, PositiveImbalance};
use sp_runtime::traits::Bounded;
use ExistenceRequirement::*;

// wrapping these imbalances in a private module is necessary to ensure absolute privacy
// of the inner member.
mod imbalances {
use super::*;
use core::mem;
use frame_support::traits::{tokens::imbalance::TryMerge, SameOrOther};

/// Opaque, move-only struct with private fields that serves as a token denoting that
/// funds have been created without any equal and opposite accounting.
Expand Down Expand Up @@ -70,7 +63,7 @@ mod imbalances {
}

impl<T: Config<I>, I: 'static> TryDrop for PositiveImbalance<T, I> {
fn try_drop(self) -> result::Result<(), Self> {
fn try_drop(self) -> Result<(), Self> {
self.drop_zero()
}
}
Expand All @@ -87,7 +80,7 @@ mod imbalances {
fn zero() -> Self {
Self(Zero::zero())
}
fn drop_zero(self) -> result::Result<(), Self> {
fn drop_zero(self) -> Result<(), Self> {
if self.0.is_zero() {
Ok(())
} else {
Expand Down Expand Up @@ -140,7 +133,7 @@ mod imbalances {
}

impl<T: Config<I>, I: 'static> TryDrop for NegativeImbalance<T, I> {
fn try_drop(self) -> result::Result<(), Self> {
fn try_drop(self) -> Result<(), Self> {
self.drop_zero()
}
}
Expand All @@ -157,7 +150,7 @@ mod imbalances {
fn zero() -> Self {
Self(Zero::zero())
}
fn drop_zero(self) -> result::Result<(), Self> {
fn drop_zero(self) -> Result<(), Self> {
if self.0.is_zero() {
Ok(())
} else {
Expand Down Expand Up @@ -366,7 +359,7 @@ where
|account, _is_new| -> Result<(Self::NegativeImbalance, Self::Balance), DispatchError> {
// Best value is the most amount we can slash following liveness rules.
let ed = T::ExistentialDeposit::get();
let actual = match system::Pallet::<T>::can_dec_provider(who) {
let actual = match frame_system::Pallet::<T>::can_dec_provider(who) {
true => value.min(account.free),
false => value.min(account.free.saturating_sub(ed)),
};
Expand Down Expand Up @@ -451,7 +444,7 @@ where
value: Self::Balance,
reasons: WithdrawReasons,
liveness: ExistenceRequirement,
) -> result::Result<Self::NegativeImbalance, DispatchError> {
) -> Result<Self::NegativeImbalance, DispatchError> {
if value.is_zero() {
return Ok(NegativeImbalance::zero())
}
Expand Down
9 changes: 5 additions & 4 deletions substrate/frame/balances/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
// limitations under the License.

//! Implementation of `fungible` traits for Balances pallet.
use super::*;
use frame_support::traits::{
use frame::traits::{
tokens::{
Fortitude,
Preservation::{self, Preserve, Protect},
Provenance::{self, Minted},
DepositConsequence, Preservation,
Provenance::{self, *},
WithdrawConsequence,
},
AccountTouch,
};
Expand Down
94 changes: 38 additions & 56 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
//! token.
//!
//! It makes heavy use of concepts such as Holds and Freezes from the
//! [`frame_support::traits::fungible`] traits, therefore you should read and understand those docs
//! [`frame::traits::fungible`] traits, therefore you should read and understand those docs
//! as a prerequisite to understanding this pallet.
//!
//! Also see the [`frame_tokens`] reference docs for higher level information regarding the
//! place of this palet in FRAME.
//! place of this pallet in FRAME.
//!
//! ## Overview
//!
Expand Down Expand Up @@ -67,11 +67,11 @@
//!
//! - [`Currency`]: Functions for dealing with a fungible assets system.
//! - [`ReservableCurrency`]
//! - [`NamedReservableCurrency`](frame_support::traits::NamedReservableCurrency):
//! - [`NamedReservableCurrency`](frame::traits::NamedReservableCurrency):
//! Functions for dealing with assets that can be reserved from an account.
//! - [`LockableCurrency`](frame_support::traits::LockableCurrency): Functions for
//! - [`LockableCurrency`](frame::traits::LockableCurrency): Functions for
//! dealing with accounts that allow liquidity restrictions.
//! - [`Imbalance`](frame_support::traits::Imbalance): Functions for handling
//! - [`Imbalance`](frame::traits::Imbalance): Functions for handling
//! imbalances between total issuance in the system and account balances. Must be used when a
//! function creates new funds (e.g. a reward) or destroys some funds (e.g. a system fee).
//!
Expand All @@ -85,7 +85,7 @@
//! `Currency`:
//!
//! ```
//! use frame_support::traits::Currency;
//! use frame::traits::Currency;
//! # pub trait Config: frame_system::Config {
//! # type Currency: Currency<Self::AccountId>;
//! # }
Expand All @@ -99,14 +99,14 @@
//! The Staking pallet uses the `LockableCurrency` trait to lock a stash account's funds:
//!
//! ```
//! use frame_support::traits::{WithdrawReasons, LockableCurrency};
//! use frame::traits::{WithdrawReasons, LockableCurrency};
//! use sp_runtime::traits::Bounded;
//! pub trait Config: frame_system::Config {
//! type Currency: LockableCurrency<Self::AccountId, Moment=frame_system::pallet_prelude::BlockNumberFor<Self>>;
//! }
//! # struct StakingLedger<T: Config> {
//! # stash: <T as frame_system::Config>::AccountId,
//! # total: <<T as Config>::Currency as frame_support::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance,
//! # total: <<T as Config>::Currency as frame::traits::Currency<<T as frame_system::Config>::AccountId>>::Balance,
//! # phantom: std::marker::PhantomData<T>,
//! # }
//! # const STAKING_ID: [u8; 8] = *b"staking ";
Expand Down Expand Up @@ -142,77 +142,60 @@
//! [`frame_tokens`]: ../polkadot_sdk_docs/reference_docs/frame_tokens/index.html
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

pub mod migration;
pub mod weights;

mod benchmarking;
mod impl_currency;
mod impl_fungible;
pub mod migration;
mod tests;
mod types;
pub mod weights;

extern crate alloc;
pub use impl_currency::{NegativeImbalance, PositiveImbalance};
pub use pallet::*;
pub use types::{
AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
};
pub use weights::WeightInfo;

use alloc::vec::Vec;
use codec::{Codec, MaxEncodedLen};
use core::{cmp, fmt::Debug, mem, result};
use frame_support::{
ensure,
pallet_prelude::DispatchResult,
use core::mem;
use frame::{
prelude::*,
traits::{
fungible::{self, Credit},
tokens::{
fungible, BalanceStatus as Status, DepositConsequence,
Fortitude::{self, Force, Polite},
IdAmount,
Preservation::{Expendable, Preserve, Protect},
WithdrawConsequence,
Fortitude::{self, *},
IdAmount, Precision,
Preservation::*,
},
Currency, Defensive, Get, OnUnbalanced, ReservableCurrency, StoredMap,
BalanceStatus as Status, Currency, OnUnbalanced, ReservableCurrency, StoredMap,
VariantCount,
},
BoundedSlice, WeakBoundedVec,
};
use frame_system as system;
pub use impl_currency::{NegativeImbalance, PositiveImbalance};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating,
StaticLookup, Zero,
},
ArithmeticError, DispatchError, FixedPointOperand, Perbill, RuntimeDebug, TokenError,
};
pub use types::{
AccountData, AdjustmentDirection, BalanceLock, DustCleaner, ExtraFlags, Reasons, ReserveData,
};
pub use weights::WeightInfo;

pub use pallet::*;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type CreditOf<T, I> = Credit<<T as frame_system::Config>::AccountId, Pallet<T, I>>;

const LOG_TARGET: &str = "runtime::balances";

type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::{
pallet_prelude::*,
traits::{fungible::Credit, tokens::Precision, VariantCount, VariantCountOf},
};
use frame_system::pallet_prelude::*;

pub type CreditOf<T, I> = Credit<<T as frame_system::Config>::AccountId, Pallet<T, I>>;

/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
pub mod config_preludes {
use super::*;
use frame_support::derive_impl;

pub struct TestDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
#[frame::derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
impl frame_system::DefaultConfig for TestDefaultConfig {}

#[frame_support::register_default_impl(TestDefaultConfig)]
#[frame::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
#[inject_runtime_type]
type RuntimeEvent = ();
Expand Down Expand Up @@ -324,8 +307,7 @@ pub mod pallet {
}

/// The in-code storage version.
const STORAGE_VERSION: frame_support::traits::StorageVersion =
frame_support::traits::StorageVersion::new(1);
const STORAGE_VERSION: frame::traits::StorageVersion = frame::traits::StorageVersion::new(1);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down Expand Up @@ -862,7 +844,7 @@ pub mod pallet {
}
a.flags.set_new_logic();
if !a.reserved.is_zero() && a.frozen.is_zero() {
if system::Pallet::<T>::providers(who) == 0 {
if frame_system::Pallet::<T>::providers(who) == 0 {
// Gah!! We have no provider refs :(
// This shouldn't practically happen, but we need a failsafe anyway: let's give
// them enough for an ED.
Expand All @@ -872,9 +854,9 @@ pub mod pallet {
who
);
a.free = a.free.max(Self::ed());
system::Pallet::<T>::inc_providers(who);
frame_system::Pallet::<T>::inc_providers(who);
}
let _ = system::Pallet::<T>::inc_consumers_without_limit(who).defensive();
let _ = frame_system::Pallet::<T>::inc_consumers_without_limit(who).defensive();
}
// Should never fail - we're only setting a bit.
let _ = T::AccountStore::try_mutate_exists(who, |account| -> DispatchResult {
Expand Down
Loading

0 comments on commit d5b22b5

Please sign in to comment.