From c43206ad7d02edb71ee799e14a351f5f53d25ab8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 13:14:10 +0000 Subject: [PATCH 01/22] Initial migration of balances pallet --- frame/balances/src/lib.rs | 572 ++++++++++++++++++++------------------ 1 file changed, 308 insertions(+), 264 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index ef069455bbabe..210c0772259dd 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -15,13 +15,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # Balances Module +//! # Balances Pallet //! //! The Balances module provides functionality for handling accounts and balances. //! //! - [`balances::Config`](./trait.Config.html) //! - [`Call`](./enum.Call.html) -//! - [`Module`](./struct.Module.html) +//! - [`Pallet`](./struct.Pallet.html) //! //! ## Overview //! @@ -160,7 +160,7 @@ use sp_std::prelude::*; use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr}; use codec::{Codec, Encode, Decode}; use frame_support::{ - StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, ensure, + StorageValue, Parameter, traits::{ Currency, OnUnbalanced, TryDrop, StoredMap, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, @@ -168,6 +168,8 @@ use frame_support::{ ExistenceRequirement::AllowDeath, BalanceStatus as Status, } }; +#[cfg(feature = "std")] +use frame_support::traits::GenesisBuild; use sp_runtime::{ RuntimeDebug, DispatchResult, DispatchError, traits::{ @@ -179,87 +181,219 @@ use frame_system::{self as system, ensure_signed, ensure_root}; pub use self::imbalances::{PositiveImbalance, NegativeImbalance}; pub use weights::WeightInfo; -pub trait Subtrait: frame_system::Config { - /// The balance of an account. - type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + +pub use pallet::*; + +#[frame_support::pallet] +pub mod pallet { + use frame_support::pallet_prelude::*; + use frame_system::pallet_prelude::*; + use super::*; + + #[pallet::config] + pub trait Config: frame_system::Config { + /// The balance of an account. + type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + MaybeSerializeDeserialize + Debug; - /// The minimum amount required to keep an account open. - type ExistentialDeposit: Get; + /// Handler for the unbalanced reduction when removing a dust account. + type DustRemoval: OnUnbalanced>; - /// The means of storing the balances of an account. - type AccountStore: StoredMap>; + /// The overarching event type. + type Event: From> + Into<::Event>; - /// Weight information for the extrinsics in this pallet. - type WeightInfo: WeightInfo; + /// The minimum amount required to keep an account open. + #[pallet::constant] + type ExistentialDeposit: Get; - /// The maximum number of locks that should exist on an account. - /// Not strictly enforced, but used for weight estimation. - type MaxLocks: Get; -} + /// The means of storing the balances of an account. + type AccountStore: StoredMap>; -pub trait Config: frame_system::Config { - /// The balance of an account. - type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug; + /// Weight information for extrinsics in this pallet. + type WeightInfo: WeightInfo; - /// Handler for the unbalanced reduction when removing a dust account. - type DustRemoval: OnUnbalanced>; + /// The maximum number of locks that should exist on an account. + /// Not strictly enforced, but used for weight estimation. + type MaxLocks: Get; + } - /// The overarching event type. - type Event: From> + Into<::Event>; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(PhantomData<(T, I)>); - /// The minimum amount required to keep an account open. - type ExistentialDeposit: Get; + #[pallet::hooks] + impl, I: 'static> Hooks> for Pallet { + } - /// The means of storing the balances of an account. - type AccountStore: StoredMap>; + #[pallet::call] + impl, I: 'static> Pallet { + /// Transfer some liquid free balance to another account. + /// + /// `transfer` will set the `FreeBalance` of the sender and receiver. + /// It will decrease the total issuance of the system by the `TransferFee`. + /// If the sender's account is below the existential deposit as a result + /// of the transfer, the account will be reaped. + /// + /// The dispatch origin for this call must be `Signed` by the transactor. + /// + /// # + /// - Dependent on arguments but not critical, given proper implementations for + /// input config types. See related functions below. + /// - It contains a limited number of reads and writes internally and no complex computation. + /// + /// Related functions: + /// + /// - `ensure_can_withdraw` is always called internally but has a bounded complexity. + /// - Transferring balances to accounts that did not exist before will cause + /// `T::OnNewAccount::on_new_account` to be called. + /// - Removing enough funds from an account will trigger `T::DustRemoval::on_unbalanced`. + /// - `transfer_keep_alive` works the same way as `transfer`, but has an additional + /// check that the transfer will not kill the origin account. + /// --------------------------------- + /// - Base Weight: 73.64 µs, worst case scenario (account created, account removed) + /// - DB Weight: 1 Read and 1 Write to destination account + /// - Origin account is already in memory, so no DB operations for them. + /// # + #[pallet::weight(T::WeightInfo::transfer())] + pub fn transfer( + origin: OriginFor, + dest: ::Source, + #[pallet::compact] value: T::Balance + ) -> DispatchResultWithPostInfo { + let transactor = ensure_signed(origin)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&transactor, &dest, value, ExistenceRequirement::AllowDeath)?; + Ok(().into()) + } - /// Weight information for extrinsics in this pallet. - type WeightInfo: WeightInfo; + /// Set the balances of a given account. + /// + /// This will alter `FreeBalance` and `ReservedBalance` in storage. it will + /// also decrease the total issuance of the system (`TotalIssuance`). + /// If the new free or reserved balance is below the existential deposit, + /// it will reset the account nonce (`frame_system::AccountNonce`). + /// + /// The dispatch origin for this call is `root`. + /// + /// # + /// - Independent of the arguments. + /// - Contains a limited number of reads and writes. + /// --------------------- + /// - Base Weight: + /// - Creating: 27.56 µs + /// - Killing: 35.11 µs + /// - DB Weight: 1 Read, 1 Write to `who` + /// # + #[weight(T::WeightInfo::set_balance_creating() // Creates a new account. + .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. + )] + fn set_balance( + origin: OriginFor, + who: ::Source, + #[pallet::compact] new_free: T::Balance, + #[pallet::compact] new_reserved: T::Balance + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + let who = T::Lookup::lookup(who)?; + let existential_deposit = T::ExistentialDeposit::get(); - /// The maximum number of locks that should exist on an account. - /// Not strictly enforced, but used for weight estimation. - type MaxLocks: Get; -} + let wipeout = new_free + new_reserved < existential_deposit; + let new_free = if wipeout { Zero::zero() } else { new_free }; + let new_reserved = if wipeout { Zero::zero() } else { new_reserved }; -impl, I: Instance> Subtrait for T { - type Balance = T::Balance; - type ExistentialDeposit = T::ExistentialDeposit; - type AccountStore = T::AccountStore; - type WeightInfo = >::WeightInfo; - type MaxLocks = T::MaxLocks; -} + let (free, reserved) = Self::mutate_account(&who, |account| { + if new_free > account.free { + mem::drop(PositiveImbalance::::new(new_free - account.free)); + } else if new_free < account.free { + mem::drop(NegativeImbalance::::new(account.free - new_free)); + } -decl_event!( - pub enum Event where - ::AccountId, - >::Balance - { + if new_reserved > account.reserved { + mem::drop(PositiveImbalance::::new(new_reserved - account.reserved)); + } else if new_reserved < account.reserved { + mem::drop(NegativeImbalance::::new(account.reserved - new_reserved)); + } + + account.free = new_free; + account.reserved = new_reserved; + + (account.free, account.reserved) + })?; + Self::deposit_event(RawEvent::BalanceSet(who, free, reserved)); + Ok(().into()) + } + + /// Exactly as `transfer`, except the origin must be root and the source account may be + /// specified. + /// # + /// - Same as transfer, but additional read and write because the source account is + /// not assumed to be in the overlay. + /// # + #[pallet::weight(T::WeightInfo::force_transfer())] + pub fn force_transfer( + origin: OriginFor, + source: ::Source, + dest: ::Source, + #[pallet::compact] value: T::Balance + ) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + let source = T::Lookup::lookup(source)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&source, &dest, value, ExistenceRequirement::AllowDeath)?; + Ok(().into()) + } + + /// Same as the [`transfer`] call, but with a check that the transfer will not kill the + /// origin account. + /// + /// 99% of the time you want [`transfer`] instead. + /// + /// [`transfer`]: struct.Pallet.html#method.transfer + /// # + /// - Cheaper than transfer because account cannot be killed. + /// - Base Weight: 51.4 µs + /// - DB Weight: 1 Read and 1 Write to dest (sender is in overlay already) + /// # + #[pallet::weight(T::WeightInfo::transfer_keep_alive())] + pub fn transfer_keep_alive( + origin: OriginFor, + dest: ::Source, + #[pallet::compact] value: T::Balance + ) -> DispatchResultWithPostInfo { + let transactor = ensure_signed(origin)?; + let dest = T::Lookup::lookup(dest)?; + >::transfer(&transactor, &dest, value, KeepAlive)?; + Ok(().into()) + } + } + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(T::AccountId = "AccountId")] + #[pallet::metadata(T::Balance = "Balance")] + pub enum Event, I: 'static> { /// An account was created with some free balance. \[account, free_balance\] - Endowed(AccountId, Balance), + Endowed(T::AccountId, T::Balance), /// An account was removed whose balance was non-zero but below ExistentialDeposit, /// resulting in an outright loss. \[account, balance\] - DustLost(AccountId, Balance), + DustLost(T::AccountId, T::Balance), /// Transfer succeeded. \[from, to, value\] - Transfer(AccountId, AccountId, Balance), + Transfer(T::AccountId, T::AccountId, T::Balance), /// A balance was set by root. \[who, free, reserved\] - BalanceSet(AccountId, Balance, Balance), + BalanceSet(T::AccountId, T::Balance, T::Balance), /// Some amount was deposited (e.g. for transaction fees). \[who, deposit\] - Deposit(AccountId, Balance), + Deposit(T::AccountId, T::Balance), /// Some balance was reserved (moved from free to reserved). \[who, value\] - Reserved(AccountId, Balance), + Reserved(T::AccountId, T::Balance), /// Some balance was unreserved (moved from reserved to free). \[who, value\] - Unreserved(AccountId, Balance), + Unreserved(T::AccountId, T::Balance), /// Some balance was moved from the reserve of the first account to the second account. /// Final argument indicates the destination balance type. /// \[from, to, balance, destination_status\] - ReserveRepatriated(AccountId, AccountId, Balance, Status), + ReserveRepatriated(T::AccountId, T::AccountId, T::Balance, Status), } -); -decl_error! { - pub enum Error for Module, I: Instance> { + #[pallet::error] + pub enum Error { /// Vesting balance too high to send value VestingBalance, /// Account liquidity restrictions prevent withdrawal @@ -277,8 +411,108 @@ decl_error! { /// Beneficiary account must pre-exist DeadAccount, } + + // #[pallet::origin] + // TODO_ORIGIN + + // #[pallet::validate_unsigned] + // TODO_VALIDATE_UNSIGNED + + /// The total units issued in the system. + #[pallet::storage] + #[pallet::getter(fn total_issuance)] + pub type TotalIssuance, I: 'static = ()> = StorageValue<_, T::Balance, ValueQuery>; + + /// The balance of an account. + /// + /// NOTE: This is only used in the case that this module is used to store balances. + #[pallet::storage] + pub type Account, I: 'static = ()> = StorageMap<_, Blake2_128Concat, T::AccountId, AccountData, ValueQuery>; + + /// Any liquidity locks on some account balances. + /// NOTE: Should only be accessed when setting, changing and freeing a lock. + #[pallet::storage] + #[pallet::getter(fn locks)] + pub type Locks, I: 'static = ()> = StorageMap<_, Blake2_128Concat, T::AccountId, Vec>, ValueQuery>; + + /// Storage version of the pallet. + /// + /// This is set to v2.0.0 for new networks. + #[pallet::storage] + pub(super) type StorageVersion, I: 'static = ()> = StorageValue<_, Releases, ValueQuery>; + + + #[pallet::genesis_config] + pub struct GenesisConfig, I: 'static = ()> { + pub balances: Vec<(T::AccountId, T::Balance)>, + } + + #[cfg(feature = "std")] + impl, I: 'static> Default for GenesisConfig { + fn default() -> Self { + Self { + balances: Default::default(), + } + } + } + + #[pallet::genesis_build] + impl, I: 'static> GenesisBuild for GenesisConfig { + fn build(&self) { + let total = config.balances + .iter() + .fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n); + >::put(total); + + ::put(Releases::V2_0_0); + + for (_, balance) in &config.balances { + assert!( + *balance >= >::ExistentialDeposit::get(), + "the balance of any account should always be at least the existential deposit.", + ) + } + + // ensure no duplicates exist. + let endowed_accounts = config.balances.iter().map(|(x, _)| x).cloned().collect::>(); + + assert!(endowed_accounts.len() == config.balances.len(), "duplicate balances in genesis."); + + for &(ref who, free) in config.balances.iter() { + assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() }).is_ok()); + } + } + } } +// todo: do we need the subtrait? +// pub trait Subtrait: frame_system::Config { +// /// The balance of an account. +// type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + +// MaybeSerializeDeserialize + Debug; +// +// /// The minimum amount required to keep an account open. +// type ExistentialDeposit: Get; +// +// /// The means of storing the balances of an account. +// type AccountStore: StoredMap>; +// +// /// Weight information for the extrinsics in this pallet. +// type WeightInfo: WeightInfo; +// +// /// The maximum number of locks that should exist on an account. +// /// Not strictly enforced, but used for weight estimation. +// type MaxLocks: Get; +// } +// +// impl, I: 'static> Subtrait for T { +// type Balance = T::Balance; +// type ExistentialDeposit = T::ExistentialDeposit; +// type AccountStore = T::AccountStore; +// type WeightInfo = >::WeightInfo; +// type MaxLocks = T::MaxLocks; +// } + /// Simplified reasons for withdrawing balance. #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)] pub enum Reasons { @@ -381,197 +615,7 @@ impl Default for Releases { } } -decl_storage! { - trait Store for Module, I: Instance=DefaultInstance> as Balances { - /// The total units issued in the system. - pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig| { - config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n) - }): T::Balance; - - /// The balance of an account. - /// - /// NOTE: This is only used in the case that this module is used to store balances. - pub Account: map hasher(blake2_128_concat) T::AccountId => AccountData; - - /// Any liquidity locks on some account balances. - /// NOTE: Should only be accessed when setting, changing and freeing a lock. - pub Locks get(fn locks): map hasher(blake2_128_concat) T::AccountId => Vec>; - - /// Storage version of the pallet. - /// - /// This is set to v2.0.0 for new networks. - StorageVersion build(|_: &GenesisConfig| Releases::V2_0_0): Releases; - } - add_extra_genesis { - config(balances): Vec<(T::AccountId, T::Balance)>; - // ^^ begin, length, amount liquid at genesis - build(|config: &GenesisConfig| { - for (_, balance) in &config.balances { - assert!( - *balance >= >::ExistentialDeposit::get(), - "the balance of any account should always be at least the existential deposit.", - ) - } - - // ensure no duplicates exist. - let endowed_accounts = config.balances.iter().map(|(x, _)| x).cloned().collect::>(); - - assert!(endowed_accounts.len() == config.balances.len(), "duplicate balances in genesis."); - - for &(ref who, free) in config.balances.iter() { - assert!(T::AccountStore::insert(who, AccountData { free, .. Default::default() }).is_ok()); - } - }); - } -} - -decl_module! { - pub struct Module, I: Instance = DefaultInstance> for enum Call where origin: T::Origin { - type Error = Error; - - /// The minimum amount required to keep an account open. - const ExistentialDeposit: T::Balance = T::ExistentialDeposit::get(); - - fn deposit_event() = default; - - /// Transfer some liquid free balance to another account. - /// - /// `transfer` will set the `FreeBalance` of the sender and receiver. - /// It will decrease the total issuance of the system by the `TransferFee`. - /// If the sender's account is below the existential deposit as a result - /// of the transfer, the account will be reaped. - /// - /// The dispatch origin for this call must be `Signed` by the transactor. - /// - /// # - /// - Dependent on arguments but not critical, given proper implementations for - /// input config types. See related functions below. - /// - It contains a limited number of reads and writes internally and no complex computation. - /// - /// Related functions: - /// - /// - `ensure_can_withdraw` is always called internally but has a bounded complexity. - /// - Transferring balances to accounts that did not exist before will cause - /// `T::OnNewAccount::on_new_account` to be called. - /// - Removing enough funds from an account will trigger `T::DustRemoval::on_unbalanced`. - /// - `transfer_keep_alive` works the same way as `transfer`, but has an additional - /// check that the transfer will not kill the origin account. - /// --------------------------------- - /// - Base Weight: 73.64 µs, worst case scenario (account created, account removed) - /// - DB Weight: 1 Read and 1 Write to destination account - /// - Origin account is already in memory, so no DB operations for them. - /// # - #[weight = T::WeightInfo::transfer()] - pub fn transfer( - origin, - dest: ::Source, - #[compact] value: T::Balance - ) { - let transactor = ensure_signed(origin)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&transactor, &dest, value, ExistenceRequirement::AllowDeath)?; - } - - /// Set the balances of a given account. - /// - /// This will alter `FreeBalance` and `ReservedBalance` in storage. it will - /// also decrease the total issuance of the system (`TotalIssuance`). - /// If the new free or reserved balance is below the existential deposit, - /// it will reset the account nonce (`frame_system::AccountNonce`). - /// - /// The dispatch origin for this call is `root`. - /// - /// # - /// - Independent of the arguments. - /// - Contains a limited number of reads and writes. - /// --------------------- - /// - Base Weight: - /// - Creating: 27.56 µs - /// - Killing: 35.11 µs - /// - DB Weight: 1 Read, 1 Write to `who` - /// # - #[weight = T::WeightInfo::set_balance_creating() // Creates a new account. - .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. - ] - fn set_balance( - origin, - who: ::Source, - #[compact] new_free: T::Balance, - #[compact] new_reserved: T::Balance - ) { - ensure_root(origin)?; - let who = T::Lookup::lookup(who)?; - let existential_deposit = T::ExistentialDeposit::get(); - - let wipeout = new_free + new_reserved < existential_deposit; - let new_free = if wipeout { Zero::zero() } else { new_free }; - let new_reserved = if wipeout { Zero::zero() } else { new_reserved }; - - let (free, reserved) = Self::mutate_account(&who, |account| { - if new_free > account.free { - mem::drop(PositiveImbalance::::new(new_free - account.free)); - } else if new_free < account.free { - mem::drop(NegativeImbalance::::new(account.free - new_free)); - } - - if new_reserved > account.reserved { - mem::drop(PositiveImbalance::::new(new_reserved - account.reserved)); - } else if new_reserved < account.reserved { - mem::drop(NegativeImbalance::::new(account.reserved - new_reserved)); - } - - account.free = new_free; - account.reserved = new_reserved; - - (account.free, account.reserved) - })?; - Self::deposit_event(RawEvent::BalanceSet(who, free, reserved)); - } - - /// Exactly as `transfer`, except the origin must be root and the source account may be - /// specified. - /// # - /// - Same as transfer, but additional read and write because the source account is - /// not assumed to be in the overlay. - /// # - #[weight = T::WeightInfo::force_transfer()] - pub fn force_transfer( - origin, - source: ::Source, - dest: ::Source, - #[compact] value: T::Balance - ) { - ensure_root(origin)?; - let source = T::Lookup::lookup(source)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&source, &dest, value, ExistenceRequirement::AllowDeath)?; - } - - /// Same as the [`transfer`] call, but with a check that the transfer will not kill the - /// origin account. - /// - /// 99% of the time you want [`transfer`] instead. - /// - /// [`transfer`]: struct.Module.html#method.transfer - /// # - /// - Cheaper than transfer because account cannot be killed. - /// - Base Weight: 51.4 µs - /// - DB Weight: 1 Read and 1 Write to dest (sender is in overlay already) - /// # - #[weight = T::WeightInfo::transfer_keep_alive()] - pub fn transfer_keep_alive( - origin, - dest: ::Source, - #[compact] value: T::Balance - ) { - let transactor = ensure_signed(origin)?; - let dest = T::Lookup::lookup(dest)?; - >::transfer(&transactor, &dest, value, KeepAlive)?; - } - } -} - -impl, I: Instance> Module { +impl, I: 'static> Pallet { // PRIVATE MUTABLES /// Get the free balance of an account. @@ -695,12 +739,12 @@ impl, I: Instance> Module { if existed { // TODO: use Locks::::hashed_key // https://github.com/paritytech/substrate/issues/4969 - system::Module::::dec_consumers(who); + system::Pallet::::dec_consumers(who); } } else { Locks::::insert(who, locks); if !existed { - if system::Module::::inc_consumers(who).is_err() { + if system::Pallet::::inc_consumers(who).is_err() { // No providers for the locks. This is impossible under normal circumstances // since the funds that are under the lock will themselves be stored in the // account and therefore will need a reference. @@ -727,9 +771,9 @@ mod imbalances { /// funds have been created without any equal and opposite accounting. #[must_use] #[derive(RuntimeDebug, PartialEq, Eq)] - pub struct PositiveImbalance, I: Instance=DefaultInstance>(T::Balance); + pub struct PositiveImbalance, I: 'static>(T::Balance); - impl, I: Instance> PositiveImbalance { + impl, I: 'static> PositiveImbalance { /// Create a new positive imbalance from a balance. pub fn new(amount: T::Balance) -> Self { PositiveImbalance(amount) @@ -740,22 +784,22 @@ mod imbalances { /// funds have been destroyed without any equal and opposite accounting. #[must_use] #[derive(RuntimeDebug, PartialEq, Eq)] - pub struct NegativeImbalance, I: Instance=DefaultInstance>(T::Balance); + pub struct NegativeImbalance, I: 'static>(T::Balance); - impl, I: Instance> NegativeImbalance { + impl, I: 'static> NegativeImbalance { /// Create a new negative imbalance from a balance. pub fn new(amount: T::Balance) -> Self { NegativeImbalance(amount) } } - impl, I: Instance> TryDrop for PositiveImbalance { + impl, I: 'static> TryDrop for PositiveImbalance { fn try_drop(self) -> result::Result<(), Self> { self.drop_zero() } } - impl, I: Instance> Imbalance for PositiveImbalance { + impl, I: 'static> Imbalance for PositiveImbalance { type Opposite = NegativeImbalance; fn zero() -> Self { @@ -800,13 +844,13 @@ mod imbalances { } } - impl, I: Instance> TryDrop for NegativeImbalance { + impl, I: 'static> TryDrop for NegativeImbalance { fn try_drop(self) -> result::Result<(), Self> { self.drop_zero() } } - impl, I: Instance> Imbalance for NegativeImbalance { + impl, I: 'static> Imbalance for NegativeImbalance { type Opposite = PositiveImbalance; fn zero() -> Self { @@ -851,7 +895,7 @@ mod imbalances { } } - impl, I: Instance> Drop for PositiveImbalance { + impl, I: 'static> Drop for PositiveImbalance { /// Basic drop handler will just square up the total issuance. fn drop(&mut self) { >::mutate( @@ -860,7 +904,7 @@ mod imbalances { } } - impl, I: Instance> Drop for NegativeImbalance { + impl, I: 'static> Drop for NegativeImbalance { /// Basic drop handler will just square up the total issuance. fn drop(&mut self) { >::mutate( @@ -870,7 +914,7 @@ mod imbalances { } } -impl, I: Instance> Currency for Module where +impl, I: 'static> Currency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { type Balance = T::Balance; @@ -978,7 +1022,7 @@ impl, I: Instance> Currency for Module where // TODO: This is over-conservative. There may now be other providers, and this module // may not even be a provider. let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !system::Module::::is_provider_required(transactor); + let allow_death = allow_death && !system::Pallet::::is_provider_required(transactor); ensure!(allow_death || from_account.free >= ed, Error::::KeepAlive); Ok(()) @@ -1156,7 +1200,7 @@ impl, I: Instance> Currency for Module where } } -impl, I: Instance> ReservableCurrency for Module where +impl, I: 'static> ReservableCurrency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { /// Check if `who` can reserve `value` from their free balance. @@ -1295,7 +1339,7 @@ impl, I: Instance> ReservableCurrency for Module, I: Instance> LockableCurrency for Module +impl, I: 'static> LockableCurrency for Pallet where T::Balance: MaybeSerializeDeserialize + Debug { From bb0618ba29a7b747af5a5eaedc47c6f5e4c85d2e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 13:48:11 +0000 Subject: [PATCH 02/22] Fix some errors --- frame/balances/src/lib.rs | 48 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 210c0772259dd..9d2a5f6dc2fa3 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -160,7 +160,7 @@ use sp_std::prelude::*; use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr}; use codec::{Codec, Encode, Decode}; use frame_support::{ - StorageValue, Parameter, + ensure, Parameter, traits::{ Currency, OnUnbalanced, TryDrop, StoredMap, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, @@ -193,13 +193,13 @@ pub mod pallet { pub trait Config: frame_system::Config { /// The balance of an account. type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug; + MaybeSerializeDeserialize + Debug; /// Handler for the unbalanced reduction when removing a dust account. type DustRemoval: OnUnbalanced>; /// The overarching event type. - type Event: From> + Into<::Event>; + type Event: From> + IsType<::Event>; /// The minimum amount required to keep an account open. #[pallet::constant] @@ -283,8 +283,9 @@ pub mod pallet { /// - Killing: 35.11 µs /// - DB Weight: 1 Read, 1 Write to `who` /// # - #[weight(T::WeightInfo::set_balance_creating() // Creates a new account. - .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. + #[pallet::weight( + T::WeightInfo::set_balance_creating() // Creates a new account. + .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. )] fn set_balance( origin: OriginFor, @@ -318,7 +319,7 @@ pub mod pallet { (account.free, account.reserved) })?; - Self::deposit_event(RawEvent::BalanceSet(who, free, reserved)); + Self::deposit_event(Event::BalanceSet(who, free, reserved)); Ok(().into()) } @@ -368,9 +369,8 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - #[pallet::metadata(T::AccountId = "AccountId")] - #[pallet::metadata(T::Balance = "Balance")] - pub enum Event, I: 'static> { + #[pallet::metadata(T::AccountId = "AccountId", T::Balance = "Balance")] + pub enum Event, I: 'static = ()> { /// An account was created with some free balance. \[account, free_balance\] Endowed(T::AccountId, T::Balance), /// An account was removed whose balance was non-zero but below ExistentialDeposit, @@ -393,7 +393,7 @@ pub mod pallet { } #[pallet::error] - pub enum Error { + pub enum Error { /// Vesting balance too high to send value VestingBalance, /// Account liquidity restrictions prevent withdrawal @@ -459,14 +459,14 @@ pub mod pallet { #[pallet::genesis_build] impl, I: 'static> GenesisBuild for GenesisConfig { fn build(&self) { - let total = config.balances + let total = self.balances .iter() .fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n); >::put(total); - ::put(Releases::V2_0_0); + >::put(Releases::V2_0_0); - for (_, balance) in &config.balances { + for (_, balance) in &self.balances { assert!( *balance >= >::ExistentialDeposit::get(), "the balance of any account should always be at least the existential deposit.", @@ -474,11 +474,11 @@ pub mod pallet { } // ensure no duplicates exist. - let endowed_accounts = config.balances.iter().map(|(x, _)| x).cloned().collect::>(); + let endowed_accounts = self.balances.iter().map(|(x, _)| x).cloned().collect::>(); - assert!(endowed_accounts.len() == config.balances.len(), "duplicate balances in genesis."); + assert!(endowed_accounts.len() == self.balances.len(), "duplicate balances in genesis."); - for &(ref who, free) in config.balances.iter() { + for &(ref who, free) in self.balances.iter() { assert!(T::AccountStore::insert(who, AccountData { free, ..Default::default() }).is_ok()); } } @@ -659,7 +659,7 @@ impl, I: 'static> Pallet { if total < T::ExistentialDeposit::get() { if !total.is_zero() { T::DustRemoval::on_unbalanced(NegativeImbalance::new(total)); - Self::deposit_event(RawEvent::DustLost(who.clone(), total)); + Self::deposit_event(Event::DustLost(who.clone(), total)); } None } else { @@ -705,7 +705,7 @@ impl, I: 'static> Pallet { }) }).map(|(maybe_endowed, result)| { if let Some(endowed) = maybe_endowed { - Self::deposit_event(RawEvent::Endowed(who.clone(), endowed)); + Self::deposit_event(Event::Endowed(who.clone(), endowed)); } result }) @@ -762,8 +762,8 @@ impl, I: 'static> Pallet { // of the inner member. mod imbalances { use super::{ - result, DefaultInstance, Imbalance, Config, Zero, Instance, Saturating, - StorageValue, TryDrop, RuntimeDebug, + result, Imbalance, Config, Zero, Saturating, + TryDrop, RuntimeDebug, }; use sp_std::mem; @@ -1030,7 +1030,7 @@ impl, I: 'static> Currency for Pallet where })?; // Emit transfer event. - Self::deposit_event(RawEvent::Transfer(transactor.clone(), dest.clone(), value)); + Self::deposit_event(Event::Transfer(transactor.clone(), dest.clone(), value)); Ok(()) } @@ -1231,7 +1231,7 @@ impl, I: 'static> ReservableCurrency for Pallet Self::ensure_can_withdraw(&who, value.clone(), WithdrawReasons::RESERVE, account.free) })?; - Self::deposit_event(RawEvent::Reserved(who.clone(), value)); + Self::deposit_event(Event::Reserved(who.clone(), value)); Ok(()) } @@ -1259,7 +1259,7 @@ impl, I: 'static> ReservableCurrency for Pallet } }; - Self::deposit_event(RawEvent::Unreserved(who.clone(), actual.clone())); + Self::deposit_event(Event::Unreserved(who.clone(), actual.clone())); value - actual } @@ -1334,7 +1334,7 @@ impl, I: 'static> ReservableCurrency for Pallet }) })?; - Self::deposit_event(RawEvent::ReserveRepatriated(slashed.clone(), beneficiary.clone(), actual, status)); + Self::deposit_event(Event::ReserveRepatriated(slashed.clone(), beneficiary.clone(), actual, status)); Ok(value - actual) } } From b59a92c7ee53690b85c759fb5570cceb9fc835d5 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 15:18:22 +0000 Subject: [PATCH 03/22] Remove unused imports --- frame/balances/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 9d2a5f6dc2fa3..ec1f79ebe4bef 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -160,7 +160,7 @@ use sp_std::prelude::*; use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr}; use codec::{Codec, Encode, Decode}; use frame_support::{ - ensure, Parameter, + ensure, traits::{ Currency, OnUnbalanced, TryDrop, StoredMap, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, @@ -168,16 +168,14 @@ use frame_support::{ ExistenceRequirement::AllowDeath, BalanceStatus as Status, } }; -#[cfg(feature = "std")] -use frame_support::traits::GenesisBuild; use sp_runtime::{ RuntimeDebug, DispatchResult, DispatchError, traits::{ - Zero, AtLeast32BitUnsigned, StaticLookup, Member, CheckedAdd, CheckedSub, + Zero, AtLeast32BitUnsigned, StaticLookup, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Saturating, Bounded, StoredMapError, }, }; -use frame_system::{self as system, ensure_signed, ensure_root}; +use frame_system as system; pub use self::imbalances::{PositiveImbalance, NegativeImbalance}; pub use weights::WeightInfo; From 88aaa9246f86f3aae829bb33fe12fbe43d388d7a Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 15:22:14 +0000 Subject: [PATCH 04/22] Formatting and removing some todos --- frame/balances/src/lib.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index ec1f79ebe4bef..b23514e626238 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -255,7 +255,7 @@ pub mod pallet { pub fn transfer( origin: OriginFor, dest: ::Source, - #[pallet::compact] value: T::Balance + #[pallet::compact] value: T::Balance, ) -> DispatchResultWithPostInfo { let transactor = ensure_signed(origin)?; let dest = T::Lookup::lookup(dest)?; @@ -289,7 +289,7 @@ pub mod pallet { origin: OriginFor, who: ::Source, #[pallet::compact] new_free: T::Balance, - #[pallet::compact] new_reserved: T::Balance + #[pallet::compact] new_reserved: T::Balance, ) -> DispatchResultWithPostInfo { ensure_root(origin)?; let who = T::Lookup::lookup(who)?; @@ -332,7 +332,7 @@ pub mod pallet { origin: OriginFor, source: ::Source, dest: ::Source, - #[pallet::compact] value: T::Balance + #[pallet::compact] value: T::Balance, ) -> DispatchResultWithPostInfo { ensure_root(origin)?; let source = T::Lookup::lookup(source)?; @@ -356,7 +356,7 @@ pub mod pallet { pub fn transfer_keep_alive( origin: OriginFor, dest: ::Source, - #[pallet::compact] value: T::Balance + #[pallet::compact] value: T::Balance, ) -> DispatchResultWithPostInfo { let transactor = ensure_signed(origin)?; let dest = T::Lookup::lookup(dest)?; @@ -410,12 +410,6 @@ pub mod pallet { DeadAccount, } - // #[pallet::origin] - // TODO_ORIGIN - - // #[pallet::validate_unsigned] - // TODO_VALIDATE_UNSIGNED - /// The total units issued in the system. #[pallet::storage] #[pallet::getter(fn total_issuance)] @@ -425,20 +419,35 @@ pub mod pallet { /// /// NOTE: This is only used in the case that this module is used to store balances. #[pallet::storage] - pub type Account, I: 'static = ()> = StorageMap<_, Blake2_128Concat, T::AccountId, AccountData, ValueQuery>; + pub type Account, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + AccountData, + ValueQuery + >; /// Any liquidity locks on some account balances. /// NOTE: Should only be accessed when setting, changing and freeing a lock. #[pallet::storage] #[pallet::getter(fn locks)] - pub type Locks, I: 'static = ()> = StorageMap<_, Blake2_128Concat, T::AccountId, Vec>, ValueQuery>; + pub type Locks, I: 'static = ()> = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + Vec>, + ValueQuery + >; /// Storage version of the pallet. /// /// This is set to v2.0.0 for new networks. #[pallet::storage] - pub(super) type StorageVersion, I: 'static = ()> = StorageValue<_, Releases, ValueQuery>; - + pub(super) type StorageVersion, I: 'static = ()> = StorageValue< + _, + Releases, + ValueQuery + >; #[pallet::genesis_config] pub struct GenesisConfig, I: 'static = ()> { From 81150476820f89006fa0f7ecc2c90fb10e20dbbf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 15:23:44 +0000 Subject: [PATCH 05/22] Delete Subtrait --- frame/balances/src/lib.rs | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index b23514e626238..d2fc1c97ed80f 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -492,34 +492,6 @@ pub mod pallet { } } -// todo: do we need the subtrait? -// pub trait Subtrait: frame_system::Config { -// /// The balance of an account. -// type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + -// MaybeSerializeDeserialize + Debug; -// -// /// The minimum amount required to keep an account open. -// type ExistentialDeposit: Get; -// -// /// The means of storing the balances of an account. -// type AccountStore: StoredMap>; -// -// /// Weight information for the extrinsics in this pallet. -// type WeightInfo: WeightInfo; -// -// /// The maximum number of locks that should exist on an account. -// /// Not strictly enforced, but used for weight estimation. -// type MaxLocks: Get; -// } -// -// impl, I: 'static> Subtrait for T { -// type Balance = T::Balance; -// type ExistentialDeposit = T::ExistentialDeposit; -// type AccountStore = T::AccountStore; -// type WeightInfo = >::WeightInfo; -// type MaxLocks = T::MaxLocks; -// } - /// Simplified reasons for withdrawing balance. #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)] pub enum Reasons { From 9886c96188d11799a925170717872bc7ad6afd91 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 15:40:53 +0000 Subject: [PATCH 06/22] Add genesis builder impls for tests --- frame/balances/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index d2fc1c97ed80f..c7eaf3a14cfb7 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -168,6 +168,8 @@ use frame_support::{ ExistenceRequirement::AllowDeath, BalanceStatus as Status, } }; +#[cfg(feature = "std")] +use frame_support::traits::GenesisBuild; use sp_runtime::{ RuntimeDebug, DispatchResult, DispatchError, traits::{ @@ -492,6 +494,26 @@ pub mod pallet { } } +#[cfg(feature = "std")] +impl GenesisConfig { + /// Direct implementation of `GenesisBuild::build_storage`. + /// + /// Kept in order not to break dependency. + pub fn build_storage(&self) -> Result { + >::build_storage(self) + } + + /// Direct implementation of `GenesisBuild::assimilate_storage`. + /// + /// Kept in order not to break dependency. + pub fn assimilate_storage( + &self, + storage: &mut sp_runtime::Storage + ) -> Result<(), String> { + >::assimilate_storage(self, storage) + } +} + /// Simplified reasons for withdrawing balance. #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)] pub enum Reasons { From 58c981f02a44b1d9818e8b56ffdb1c1a4da3df9d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 15:52:12 +0000 Subject: [PATCH 07/22] Fix GenesisConfig impl --- frame/balances/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index c7eaf3a14cfb7..e551139f87f3c 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -495,22 +495,22 @@ pub mod pallet { } #[cfg(feature = "std")] -impl GenesisConfig { +impl, I: 'static> GenesisConfig { /// Direct implementation of `GenesisBuild::build_storage`. /// /// Kept in order not to break dependency. - pub fn build_storage(&self) -> Result { - >::build_storage(self) + pub fn build_storage(&self) -> Result { + >::build_storage(self) } /// Direct implementation of `GenesisBuild::assimilate_storage`. /// /// Kept in order not to break dependency. - pub fn assimilate_storage( + pub fn assimilate_storage( &self, storage: &mut sp_runtime::Storage ) -> Result<(), String> { - >::assimilate_storage(self, storage) + >::assimilate_storage(self, storage) } } From 5a7f0770eaffa3b0d711e2663035fdc8e5d33d8d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 16:04:17 +0000 Subject: [PATCH 08/22] Make set_balance visible to tests, rename RawEvent to Event --- frame/balances/src/lib.rs | 2 +- frame/balances/src/tests_local.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index e551139f87f3c..29629dbc84be1 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -287,7 +287,7 @@ pub mod pallet { T::WeightInfo::set_balance_creating() // Creates a new account. .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. )] - fn set_balance( + pub(crate) fn set_balance( origin: OriginFor, who: ::Source, #[pallet::compact] new_free: T::Balance, diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 762ebe871b3e8..ab294ece9777b 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -171,8 +171,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::balances(Event::Endowed(1, 100)), + Event::balances(Event::BalanceSet(1, 100, 0)), ] ); @@ -186,7 +186,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::balances(RawEvent::DustLost(1, 1)), + Event::balances(Event::DustLost(1, 1)), Event::system(system::Event::KilledAccount(1)) ] ); From 33f5d6fdffc7c2cc481efa1dd41fa6e6d4e87f9d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 16:27:14 +0000 Subject: [PATCH 09/22] Fix tests with Event rename etc. --- frame/balances/src/tests.rs | 20 ++++++++++---------- frame/balances/src/tests_local.rs | 6 +++--- frame/system/src/lib.rs | 2 +- frame/transaction-payment/src/lib.rs | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 7a1b57a7b4db1..007f7f9a00033 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -40,7 +40,7 @@ macro_rules! decl_tests { use crate::*; use sp_runtime::{FixedPointNumber, traits::{SignedExtension, BadOrigin}}; use frame_support::{ - assert_noop, assert_storage_noop, assert_ok, assert_err, + assert_noop, assert_storage_noop, assert_ok, assert_err, StorageValue, traits::{ LockableCurrency, LockIdentifier, WithdrawReasons, Currency, ReservableCurrency, ExistenceRequirement::AllowDeath @@ -485,7 +485,7 @@ macro_rules! decl_tests { assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0); assert_eq!( last_event(), - Event::balances(RawEvent::ReserveRepatriated(1, 2, 41, Status::Free)), + Event::balances(balances::Event::ReserveRepatriated(1, 2, 41, Status::Free)), ); assert_eq!(Balances::reserved_balance(1), 69); assert_eq!(Balances::free_balance(1), 0); @@ -704,7 +704,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::balances(RawEvent::Reserved(1, 10)), + Event::balances(balances::Event::Reserved(1, 10)), ); System::set_block_number(3); @@ -712,7 +712,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::balances(RawEvent::Unreserved(1, 5)), + Event::balances(balances::Event::Unreserved(1, 5)), ); System::set_block_number(4); @@ -721,7 +721,7 @@ macro_rules! decl_tests { // should only unreserve 5 assert_eq!( last_event(), - Event::balances(RawEvent::Unreserved(1, 5)), + Event::balances(balances::Event::Unreserved(1, 5)), ); }); } @@ -738,8 +738,8 @@ macro_rules! decl_tests { events(), [ Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::balances(balances::Event::Endowed(1, 100)), + Event::balances(balances::Event::BalanceSet(1, 100, 0)), ] ); @@ -748,7 +748,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::balances(RawEvent::DustLost(1, 99)), + Event::balances(balances::Event::DustLost(1, 99)), Event::system(system::Event::KilledAccount(1)) ] ); @@ -767,8 +767,8 @@ macro_rules! decl_tests { events(), [ Event::system(system::Event::NewAccount(1)), - Event::balances(RawEvent::Endowed(1, 100)), - Event::balances(RawEvent::BalanceSet(1, 100, 0)), + Event::balances(balances::Event::Endowed(1, 100)), + Event::balances(balances::Event::BalanceSet(1, 100, 0)), ] ); diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index ab294ece9777b..5d6bbd8384e4a 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -171,8 +171,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::system(system::Event::NewAccount(1)), - Event::balances(Event::Endowed(1, 100)), - Event::balances(Event::BalanceSet(1, 100, 0)), + Event::balances(balances::Event::Endowed(1, 100)), + Event::balances(balances::Event::BalanceSet(1, 100, 0)), ] ); @@ -186,7 +186,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::balances(Event::DustLost(1, 1)), + Event::balances(balances::Event::DustLost(1, 1)), Event::system(system::Event::KilledAccount(1)) ] ); diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index cdb26623734fd..b9595df5a0c01 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -277,7 +277,7 @@ pub mod pallet { #[pallet::call] impl Pallet { - /// A dispatch that will fill the block weight up to the given ratio. + /// A dispatch that will fill the block weight up to the given ratio.a // TODO: This should only be available for testing, rather than in general usage, but // that's not possible at present (since it's within the pallet macro). #[pallet::weight(*_ratio * T::BlockWeights::get().max_block)] diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 7521fcd80bf0a..95c885602bba2 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -1165,7 +1165,7 @@ mod tests { assert_eq!(Balances::free_balance(2), 0); // Transfer Event assert!(System::events().iter().any(|event| { - event.event == Event::pallet_balances(pallet_balances::RawEvent::Transfer(2, 3, 80)) + event.event == Event::pallet_balances(pallet_balances::Event::Transfer(2, 3, 80)) })); // Killed Event assert!(System::events().iter().any(|event| { From 284d8bed527ffbf0c1cbf3d091df52ef31061fb3 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 16:29:06 +0000 Subject: [PATCH 10/22] More test RawEvent renames --- frame/contracts/src/tests.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 96bcf99bf8e81..6a5e5b529a485 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -481,7 +481,7 @@ fn instantiate_and_call_and_deposit_event() { EventRecord { phase: Phase::Initialization, event: MetaEvent::balances( - pallet_balances::RawEvent::Endowed(ALICE, 1_000_000) + pallet_balances::Event::Endowed(ALICE, 1_000_000) ), topics: vec![], }, @@ -498,14 +498,14 @@ fn instantiate_and_call_and_deposit_event() { EventRecord { phase: Phase::Initialization, event: MetaEvent::balances( - pallet_balances::RawEvent::Endowed(addr.clone(), subsistence * 3) + pallet_balances::Event::Endowed(addr.clone(), subsistence * 3) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, event: MetaEvent::balances( - pallet_balances::RawEvent::Transfer(ALICE, addr.clone(), subsistence * 3) + pallet_balances::Event::Transfer(ALICE, addr.clone(), subsistence * 3) ), topics: vec![], }, @@ -658,7 +658,7 @@ fn test_set_rent_code_and_hash() { }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed( + event: MetaEvent::balances(pallet_balances::Event::Endowed( ALICE, 1_000_000 )), topics: vec![], @@ -1240,7 +1240,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(ALICE, 1_000_000)), + event: MetaEvent::balances(pallet_balances::Event::Endowed(ALICE, 1_000_000)), topics: vec![], }, EventRecord { @@ -1395,7 +1395,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)), + event: MetaEvent::balances(pallet_balances::Event::Endowed(CHARLIE, 1_000_000)), topics: vec![], }, EventRecord { @@ -1405,13 +1405,13 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage: }, EventRecord { phase: Phase::Initialization, - event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(addr_django.clone(), 30_000)), + event: MetaEvent::balances(pallet_balances::Event::Endowed(addr_django.clone(), 30_000)), topics: vec![], }, EventRecord { phase: Phase::Initialization, event: MetaEvent::balances( - pallet_balances::RawEvent::Transfer(CHARLIE, addr_django.clone(), 30_000) + pallet_balances::Event::Transfer(CHARLIE, addr_django.clone(), 30_000) ), topics: vec![], }, From 8f25812a8c7ce13f0b5cc6e10a1fb93dc33822e9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 20 Jan 2021 16:35:16 +0000 Subject: [PATCH 11/22] Even more RawEvent renames --- bin/node/executor/tests/basic.rs | 8 ++++---- bin/node/executor/tests/fees.rs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index f007ba41ccc61..5c9943612bc21 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -17,7 +17,7 @@ use codec::{Encode, Decode, Joiner}; use frame_support::{ - StorageValue, StorageMap, + StorageMap, traits::Currency, weights::{GetDispatchInfo, DispatchInfo, DispatchClass}, }; @@ -336,7 +336,7 @@ fn full_native_block_import_works() { }, EventRecord { phase: Phase::ApplyExtrinsic(1), - event: Event::pallet_balances(pallet_balances::RawEvent::Transfer( + event: Event::pallet_balances(pallet_balances::Event::Transfer( alice().into(), bob().into(), 69 * DOLLARS, @@ -389,7 +389,7 @@ fn full_native_block_import_works() { EventRecord { phase: Phase::ApplyExtrinsic(1), event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer( + pallet_balances::Event::Transfer( bob().into(), alice().into(), 5 * DOLLARS, @@ -412,7 +412,7 @@ fn full_native_block_import_works() { EventRecord { phase: Phase::ApplyExtrinsic(2), event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer( + pallet_balances::Event::Transfer( alice().into(), bob().into(), 15 * DOLLARS, diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 9d83610b689de..2e92077c4ada3 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -17,7 +17,6 @@ use codec::{Encode, Joiner}; use frame_support::{ - StorageValue, traits::Currency, weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight, IdentityFee, WeightToFeePolynomial}, }; From 6fe23138fbbc40b00bbc840cf3214da229005522 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Jan 2021 15:51:08 +0000 Subject: [PATCH 12/22] Rename module to pallet in comments --- frame/balances/src/lib.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 29629dbc84be1..81ed784c4090a 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -17,7 +17,7 @@ //! # Balances Pallet //! -//! The Balances module provides functionality for handling accounts and balances. +//! The Balances pallet provides functionality for handling accounts and balances. //! //! - [`balances::Config`](./trait.Config.html) //! - [`Call`](./enum.Call.html) @@ -25,7 +25,7 @@ //! //! ## Overview //! -//! The Balances module provides functions for: +//! The Balances pallet provides functions for: //! //! - Getting and setting free balances. //! - Retrieving total, reserved and unreserved balances. @@ -43,7 +43,7 @@ //! fall below this, then the account is said to be dead; and it loses its functionality as well as any //! prior history and all information on it is removed from the chain's state. //! No account should ever have a total balance that is strictly between 0 and the existential -//! deposit (exclusive). If this ever happens, it indicates either a bug in this module or an +//! deposit (exclusive). If this ever happens, it indicates either a bug in this pallet or an //! erroneous raw mutation of storage. //! //! - **Total Issuance:** The total number of units in existence in a system. @@ -67,8 +67,8 @@ //! //! ### Implementations //! -//! The Balances module provides implementations for the following traits. If these traits provide the functionality -//! that you need, then you can avoid coupling with the Balances module. +//! The Balances pallet provides implementations for the following traits. If these traits provide the functionality +//! that you need, then you can avoid coupling with the Balances pallet. //! //! - [`Currency`](../frame_support/traits/trait.Currency.html): Functions for dealing with a //! fungible assets system. @@ -91,11 +91,11 @@ //! //! ## Usage //! -//! The following examples show how to use the Balances module in your custom module. +//! The following examples show how to use the Balances pallet in your custom pallet. //! //! ### Examples from the FRAME //! -//! The Contract module uses the `Currency` trait to handle gas payment, and its types inherit from `Currency`: +//! The Contract pallet uses the `Currency` trait to handle gas payment, and its types inherit from `Currency`: //! //! ``` //! use frame_support::traits::Currency; @@ -109,7 +109,7 @@ //! # fn main() {} //! ``` //! -//! The Staking module uses the `LockableCurrency` trait to lock a stash account's funds: +//! The Staking pallet uses the `LockableCurrency` trait to lock a stash account's funds: //! //! ``` //! use frame_support::traits::{WithdrawReasons, LockableCurrency}; @@ -141,7 +141,7 @@ //! //! ## Genesis config //! -//! The Balances module depends on the [`GenesisConfig`](./struct.GenesisConfig.html). +//! The Balances pallet depends on the [`GenesisConfig`](./struct.GenesisConfig.html). //! //! ## Assumptions //! @@ -419,7 +419,7 @@ pub mod pallet { /// The balance of an account. /// - /// NOTE: This is only used in the case that this module is used to store balances. + /// NOTE: This is only used in the case that this pallet is used to store balances. #[pallet::storage] pub type Account, I: 'static = ()> = StorageMap< _, @@ -977,7 +977,7 @@ impl, I: 'static> Currency for Pallet where // // # // Despite iterating over a list of locks, they are limited by the number of - // lock IDs, which means the number of runtime modules that intend to use and create locks. + // lock IDs, which means the number of runtime pallets that intend to use and create locks. // # fn ensure_can_withdraw( who: &T::AccountId, @@ -1020,7 +1020,7 @@ impl, I: 'static> Currency for Pallet where from_account.free, ).map_err(|_| Error::::LiquidityRestrictions)?; - // TODO: This is over-conservative. There may now be other providers, and this module + // TODO: This is over-conservative. There may now be other providers, and this pallet // may not even be a provider. let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; let allow_death = allow_death && !system::Pallet::::is_provider_required(transactor); From c147734e3d8469aa1623a07b51d82cf48d95143b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Jan 2021 16:38:40 +0000 Subject: [PATCH 13/22] Add PalletInfo impl to avid storage collision, fixes tests --- frame/balances/src/tests_local.rs | 35 ++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 5d6bbd8384e4a..2fe45b0208ba7 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -30,6 +30,7 @@ use frame_support::traits::StorageMapShim; use frame_support::weights::{Weight, DispatchInfo, IdentityFee}; use crate::{GenesisConfig, Module, Config, decl_tests, tests::CallWithDispatchInfo}; use pallet_transaction_payment::CurrencyAdapter; +use std::any::TypeId; use frame_system as system; impl_outer_origin!{ @@ -47,6 +48,38 @@ impl_outer_event! { } } +/// Provides an implementation of `PalletInfo`. +/// +/// Usually this is generated by the `construct_runtime!` macro, but tests typically use `()`. +/// However the impl for `()` returns the same prefix "test" for all modules, which causes +/// collisions between pallets with storage items with the same name. For instance in this case +/// System and Balances both have a storage item called `Account`. +pub struct PalletInfo; + +impl frame_support::traits::PalletInfo for PalletInfo { + fn index() -> Option { + let type_id = TypeId::of::

(); + if type_id == TypeId::of::>() { + return Some(0) + } + if type_id == TypeId::of::>() { + return Some(1) + } + None + } + + fn name() -> Option<&'static str> { + let type_id = TypeId::of::

(); + if type_id == TypeId::of::>() { + return Some("System") + } + if type_id == TypeId::of::>() { + return Some("Balances") + } + None + } +} + // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. #[derive(Clone, PartialEq, Eq, Debug)] pub struct Test; @@ -73,7 +106,7 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); From 05e52ebd7be0e9e0b919cee681380bbed474b381 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 21 Jan 2021 17:23:13 +0000 Subject: [PATCH 14/22] Apply review suggestion: remove trailing a Co-authored-by: David --- frame/system/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index b9595df5a0c01..cdb26623734fd 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -277,7 +277,7 @@ pub mod pallet { #[pallet::call] impl Pallet { - /// A dispatch that will fill the block weight up to the given ratio.a + /// A dispatch that will fill the block weight up to the given ratio. // TODO: This should only be available for testing, rather than in general usage, but // that's not possible at present (since it's within the pallet macro). #[pallet::weight(*_ratio * T::BlockWeights::get().max_block)] From c6a86567acb339feeb1ca9ef2ef37ee06c59e78c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Jan 2021 16:57:09 +0000 Subject: [PATCH 15/22] BalancesEvent alias --- frame/balances/src/tests.rs | 19 ++++++++++--------- frame/balances/src/tests_local.rs | 7 +++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 7f248232805cb..efe001c9b3387 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -34,6 +34,7 @@ macro_rules! decl_tests { }; use pallet_transaction_payment::{ChargeTransactionPayment, Multiplier}; use frame_system::RawOrigin; + use crate::Event as BalancesEvent; const ID_1: LockIdentifier = *b"1 "; const ID_2: LockIdentifier = *b"2 "; @@ -469,7 +470,7 @@ macro_rules! decl_tests { assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0); assert_eq!( last_event(), - Event::pallet_balances(balances::Event::ReserveRepatriated(1, 2, 41, Status::Free)), + Event::pallet_balances(BalancesEvent::ReserveRepatriated(1, 2, 41, Status::Free)), ); assert_eq!(Balances::reserved_balance(1), 69); assert_eq!(Balances::free_balance(1), 0); @@ -688,7 +689,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(balances::Event::Reserved(1, 10)), + Event::pallet_balances(BalancesEvent::Reserved(1, 10)), ); System::set_block_number(3); @@ -696,7 +697,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(balances::Event::Unreserved(1, 5)), + Event::pallet_balances(BalancesEvent::Unreserved(1, 5)), ); System::set_block_number(4); @@ -705,7 +706,7 @@ macro_rules! decl_tests { // should only unreserve 5 assert_eq!( last_event(), - Event::pallet_balances(balances::Event::Unreserved(1, 5)), + Event::pallet_balances(BalancesEvent::Unreserved(1, 5)), ); }); } @@ -722,8 +723,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::frame_system(balances::Event::Endowed(1, 100)), - Event::frame_system(balances::Event::BalanceSet(1, 100, 0)), + Event::pallet_beealances(BalancesEvent::Endowed(1, 100)), + Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), ] ); @@ -732,7 +733,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::pallet_balances(balances::Event::DustLost(1, 99)), + Event::pallet_balances(BalancesEvent::DustLost(1, 99)), Event::frame_system(system::Event::KilledAccount(1)) ] ); @@ -751,8 +752,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(balances::Event::Endowed(1, 100)), - Event::pallet_balances(balances::Event::BalanceSet(1, 100, 0)), + Event::pallet_balances(BalancesEvent::Endowed(1, 100)), + Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), ] ); diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 21b3ba7d38385..511d67dfd474a 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -33,7 +33,6 @@ use crate::{ Module, Config, decl_tests, }; use pallet_transaction_payment::CurrencyAdapter; -use std::any::TypeId; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -170,8 +169,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(balances::Event::Endowed(1, 100)), - Event::pallet_balances(balances::Event::BalanceSet(1, 100, 0)), + Event::pallet_balances(BalancesEvent::Endowed(1, 100)), + Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), ] ); @@ -185,7 +184,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::pallet_balances(balances::Event::DustLost(1, 1)), + Event::pallet_balances(BalancesEvent::DustLost(1, 1)), Event::frame_system(system::Event::KilledAccount(1)) ] ); From 7e6d5a25d92632edfbb8665b22f3f8dfe21deadd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 25 Jan 2021 16:59:09 +0000 Subject: [PATCH 16/22] Remove BalancesEvent alias --- frame/balances/src/tests.rs | 19 +++++++++---------- frame/balances/src/tests_local.rs | 6 +++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index efe001c9b3387..c860a0364d4bc 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -34,7 +34,6 @@ macro_rules! decl_tests { }; use pallet_transaction_payment::{ChargeTransactionPayment, Multiplier}; use frame_system::RawOrigin; - use crate::Event as BalancesEvent; const ID_1: LockIdentifier = *b"1 "; const ID_2: LockIdentifier = *b"2 "; @@ -470,7 +469,7 @@ macro_rules! decl_tests { assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0); assert_eq!( last_event(), - Event::pallet_balances(BalancesEvent::ReserveRepatriated(1, 2, 41, Status::Free)), + Event::pallet_balances(crate::Event::ReserveRepatriated(1, 2, 41, Status::Free)), ); assert_eq!(Balances::reserved_balance(1), 69); assert_eq!(Balances::free_balance(1), 0); @@ -689,7 +688,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(BalancesEvent::Reserved(1, 10)), + Event::pallet_balances(crate::Event::Reserved(1, 10)), ); System::set_block_number(3); @@ -697,7 +696,7 @@ macro_rules! decl_tests { assert_eq!( last_event(), - Event::pallet_balances(BalancesEvent::Unreserved(1, 5)), + Event::pallet_balances(crate::Event::Unreserved(1, 5)), ); System::set_block_number(4); @@ -706,7 +705,7 @@ macro_rules! decl_tests { // should only unreserve 5 assert_eq!( last_event(), - Event::pallet_balances(BalancesEvent::Unreserved(1, 5)), + Event::pallet_balances(crate::Event::Unreserved(1, 5)), ); }); } @@ -723,8 +722,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_beealances(BalancesEvent::Endowed(1, 100)), - Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -733,7 +732,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::pallet_balances(BalancesEvent::DustLost(1, 99)), + Event::pallet_balances(crate::Event::DustLost(1, 99)), Event::frame_system(system::Event::KilledAccount(1)) ] ); @@ -752,8 +751,8 @@ macro_rules! decl_tests { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(BalancesEvent::Endowed(1, 100)), - Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 511d67dfd474a..ffefc6c4d88fc 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -169,8 +169,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::frame_system(system::Event::NewAccount(1)), - Event::pallet_balances(BalancesEvent::Endowed(1, 100)), - Event::pallet_balances(BalancesEvent::BalanceSet(1, 100, 0)), + Event::pallet_balances(crate::Event::Endowed(1, 100)), + Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -184,7 +184,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::pallet_balances(BalancesEvent::DustLost(1, 1)), + Event::pallet_balances(crate::Event::DustLost(1, 1)), Event::frame_system(system::Event::KilledAccount(1)) ] ); From 94c840437315005aeda2d82ce034f70294f00a38 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Jan 2021 10:53:05 +0000 Subject: [PATCH 17/22] Review suggestion: remove redundant comment --- frame/balances/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 81ed784c4090a..5898e99524b50 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -617,8 +617,6 @@ impl Default for Releases { } impl, I: 'static> Pallet { - // PRIVATE MUTABLES - /// Get the free balance of an account. pub fn free_balance(who: impl sp_std::borrow::Borrow) -> T::Balance { Self::account(who.borrow()).free From 35e34ad8207d93eae9457f1abfd630e809b02b48 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Jan 2021 11:03:37 +0000 Subject: [PATCH 18/22] Apply review suggestion: make vis super --- frame/balances/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 5898e99524b50..df7d974fbed2a 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -287,7 +287,7 @@ pub mod pallet { T::WeightInfo::set_balance_creating() // Creates a new account. .max(T::WeightInfo::set_balance_killing()) // Kills an existing account. )] - pub(crate) fn set_balance( + pub(super) fn set_balance( origin: OriginFor, who: ::Source, #[pallet::compact] new_free: T::Balance, From 1a0b4e503fa4a0c288b30a4180ca014da3053818 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Jan 2021 15:07:25 +0000 Subject: [PATCH 19/22] Fis doc links --- frame/balances/src/lib.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index df7d974fbed2a..e7886690e5c45 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -19,9 +19,9 @@ //! //! The Balances pallet provides functionality for handling accounts and balances. //! -//! - [`balances::Config`](./trait.Config.html) -//! - [`Call`](./enum.Call.html) -//! - [`Pallet`](./struct.Pallet.html) +//! - [`Config`] +//! - [`Call`] +//! - [`Pallet`] //! //! ## Overview //! @@ -70,17 +70,15 @@ //! The Balances pallet provides implementations for the following traits. If these traits provide the functionality //! that you need, then you can avoid coupling with the Balances pallet. //! -//! - [`Currency`](../frame_support/traits/trait.Currency.html): Functions for dealing with a +//! - [`Currency`](frame_support::traits::Currency): Functions for dealing with a //! fungible assets system. -//! - [`ReservableCurrency`](../frame_support/traits/trait.ReservableCurrency.html): +//! - [`ReservableCurrency`](frame_support::traits::ReservableCurrency): //! Functions for dealing with assets that can be reserved from an account. -//! - [`LockableCurrency`](../frame_support/traits/trait.LockableCurrency.html): Functions for +//! - [`LockableCurrency`](frame_support::traits::LockableCurrency): Functions for //! dealing with accounts that allow liquidity restrictions. -//! - [`Imbalance`](../frame_support/traits/trait.Imbalance.html): Functions for handling +//! - [`Imbalance`](frame_support::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). -//! - [`IsDeadAccount`](../frame_support/traits/trait.IsDeadAccount.html): Determiner to say whether a -//! given account is unused. //! //! ## Interface //! @@ -141,7 +139,7 @@ //! //! ## Genesis config //! -//! The Balances pallet depends on the [`GenesisConfig`](./struct.GenesisConfig.html). +//! The Balances pallet depends on the [`GenesisConfig`]. //! //! ## Assumptions //! From 46a4de6930f1e40db017b78deabe153423411e35 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Jan 2021 15:58:06 +0000 Subject: [PATCH 20/22] Add RawEvent alias --- frame/balances/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index e7886690e5c45..6d733a4471f82 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -390,6 +390,10 @@ pub mod pallet { ReserveRepatriated(T::AccountId, T::AccountId, T::Balance, Status), } + /// Old name generated by `decl_event`. + #[deprecated(note = "use `Event` instead")] + pub type RawEvent = Event; + #[pallet::error] pub enum Error { /// Vesting balance too high to send value From e497f3fdb0fb96dbc2c7572b4cfaa19889d473fa Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 26 Jan 2021 16:35:03 +0000 Subject: [PATCH 21/22] Add missing Instance parameter to deprecated RawEvent alias --- frame/balances/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 6d733a4471f82..e3eb9478b6493 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -392,7 +392,7 @@ pub mod pallet { /// Old name generated by `decl_event`. #[deprecated(note = "use `Event` instead")] - pub type RawEvent = Event; + pub type RawEvent = Event; #[pallet::error] pub enum Error { From a610b3906c632ee97a96b825b39244710d508ae8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 9 Feb 2021 16:18:51 +0000 Subject: [PATCH 22/22] Fix RawEvent deprecation warnings --- frame/contracts/src/tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 3795651da07f0..62768641ac165 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -485,7 +485,7 @@ fn instantiate_and_call_and_deposit_event() { EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Endowed(addr.clone(), subsistence * 100) + pallet_balances::Event::Endowed(addr.clone(), subsistence * 100) ), topics: vec![], }, @@ -1214,14 +1214,14 @@ fn restoration( EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Endowed(addr_bob.clone(), 30_000) + pallet_balances::Event::Endowed(addr_bob.clone(), 30_000) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer(ALICE, addr_bob.clone(), 30_000) + pallet_balances::Event::Transfer(ALICE, addr_bob.clone(), 30_000) ), topics: vec![], }, @@ -1259,14 +1259,14 @@ fn restoration( EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Endowed(addr_dummy.clone(), 20_000) + pallet_balances::Event::Endowed(addr_dummy.clone(), 20_000) ), topics: vec![], }, EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer(ALICE, addr_dummy.clone(), 20_000) + pallet_balances::Event::Transfer(ALICE, addr_dummy.clone(), 20_000) ), topics: vec![], }, @@ -1711,7 +1711,7 @@ fn self_destruct_works() { EventRecord { phase: Phase::Initialization, event: Event::pallet_balances( - pallet_balances::RawEvent::Transfer(addr.clone(), DJANGO, 93_654) + pallet_balances::Event::Transfer(addr.clone(), DJANGO, 93_654) ), topics: vec![], },