diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 8580bfd895102..0aaf319336291 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -205,6 +205,8 @@ impl balances::Trait for Runtime { type Balance = Balance; /// What to do if an account's free balance gets zeroed. type OnFreeBalanceZero = (); + /// What to do if an account is fully reaped from the system. + type OnReapAccount = System; /// What to do if a new account is created. type OnNewAccount = Indices; /// The ubiquitous event type. diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index f83800944e8ae..dba5c703b6397 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -174,6 +174,7 @@ parameter_types! { impl pallet_balances::Trait for Runtime { type Balance = Balance; type OnFreeBalanceZero = ((Staking, Contracts), Session); + type OnReapAccount = System; type OnNewAccount = Indices; type Event = Event; type DustRemoval = (); diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 85e589481062d..1f8e099880b7d 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -165,7 +165,7 @@ use codec::{Codec, Encode, Decode}; use frame_support::{ StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, traits::{ - UpdateBalanceOutcome, Currency, OnFreeBalanceZero, OnUnbalanced, TryDrop, + UpdateBalanceOutcome, Currency, OnFreeBalanceZero, OnReapAccount, OnUnbalanced, TryDrop, WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, Imbalance, SignedImbalance, ReservableCurrency, Get, VestingCurrency, }, @@ -198,6 +198,12 @@ pub trait Subtrait: frame_system::Trait { /// Gives a chance to clean up resources associated with the given account. type OnFreeBalanceZero: OnFreeBalanceZero; + /// A function that is invoked when the free-balance and the reserved-balance has fallen below + /// the existential deposit and both have been reduced to zero. + /// + /// All resources should be cleaned up all resources associated with the given account. + type OnReapAccount: OnReapAccount; + /// Handler for when a new account is created. type OnNewAccount: OnNewAccount; @@ -222,6 +228,12 @@ pub trait Trait: frame_system::Trait { /// Gives a chance to clean up resources associated with the given account. type OnFreeBalanceZero: OnFreeBalanceZero; + /// A function that is invoked when the free-balance and the reserved-balance has fallen below + /// the existential deposit and both have been reduced to zero. + /// + /// All resources should be cleaned up all resources associated with the given account. + type OnReapAccount: OnReapAccount; + /// Handler for when a new account is created. type OnNewAccount: OnNewAccount; @@ -248,6 +260,7 @@ pub trait Trait: frame_system::Trait { impl, I: Instance> Subtrait for T { type Balance = T::Balance; type OnFreeBalanceZero = T::OnFreeBalanceZero; + type OnReapAccount = T::OnReapAccount; type OnNewAccount = T::OnNewAccount; type ExistentialDeposit = T::ExistentialDeposit; type TransferFee = T::TransferFee; @@ -597,7 +610,7 @@ impl, I: Instance> Module { /// /// This just removes the nonce and leaves an event. fn reap_account(who: &T::AccountId, dust: T::Balance) { - >::remove(who); + T::OnReapAccount::on_reap_account(who); Self::deposit_event(RawEvent::ReapedAccount(who.clone(), dust)); } @@ -850,6 +863,7 @@ impl, I: Instance> frame_system::Trait for ElevatedTrait { impl, I: Instance> Trait for ElevatedTrait { type Balance = T::Balance; type OnFreeBalanceZero = T::OnFreeBalanceZero; + type OnReapAccount = T::OnReapAccount; type OnNewAccount = T::OnNewAccount; type Event = (); type TransferPayment = (); diff --git a/frame/balances/src/mock.rs b/frame/balances/src/mock.rs index c511b4db18cf9..5a3d671e8dbc9 100644 --- a/frame/balances/src/mock.rs +++ b/frame/balances/src/mock.rs @@ -93,6 +93,7 @@ impl pallet_transaction_payment::Trait for Test { impl Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type DustRemoval = (); diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index ca158980099c9..ebc7218cc1aa3 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -122,6 +122,7 @@ impl frame_system::Trait for Test { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = Contract; + type OnReapAccount = System; type OnNewAccount = (); type Event = MetaEvent; type DustRemoval = (); diff --git a/frame/democracy/src/lib.rs b/frame/democracy/src/lib.rs index 3766aa2207ead..354e93cc292ee 100644 --- a/frame/democracy/src/lib.rs +++ b/frame/democracy/src/lib.rs @@ -1205,6 +1205,7 @@ mod tests { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index e3243491b1ca2..18b010295c307 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -824,6 +824,7 @@ mod tests { type Balance = u64; type OnNewAccount = (); type OnFreeBalanceZero = (); + type OnReapAccount = System; type Event = Event; type TransferPayment = (); type DustRemoval = (); diff --git a/frame/elections/src/mock.rs b/frame/elections/src/mock.rs index 1226c0671ccd3..178637e088883 100644 --- a/frame/elections/src/mock.rs +++ b/frame/elections/src/mock.rs @@ -65,6 +65,7 @@ impl pallet_balances::Trait for Test { type Balance = u64; type OnNewAccount = (); type OnFreeBalanceZero = (); + type OnReapAccount = System; type Event = Event; type TransferPayment = (); type DustRemoval = (); diff --git a/frame/example/src/lib.rs b/frame/example/src/lib.rs index 1d694be27c293..853674f6fd065 100644 --- a/frame/example/src/lib.rs +++ b/frame/example/src/lib.rs @@ -695,6 +695,7 @@ mod tests { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); @@ -706,6 +707,7 @@ mod tests { impl Trait for Test { type Event = (); } + type System = frame_system::Module; type Example = Module; // This function basically just builds a genesis storage key/value store according to diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 1ac67d05e2c0d..a7dc021aeaabc 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -425,6 +425,7 @@ mod tests { impl pallet_balances::Trait for Runtime { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = MetaEvent; type DustRemoval = (); diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index 1b9b32b2346d8..38aa3f9771bed 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -921,6 +921,7 @@ mod tests { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); @@ -950,6 +951,7 @@ mod tests { type RegistrarOrigin = EnsureSignedBy; type ForceOrigin = EnsureSignedBy; } + type System = frame_system::Module; type Balances = pallet_balances::Module; type Identity = Module; diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index bee6629b3b5fd..de39736a5a042 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -294,6 +294,7 @@ mod tests { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); @@ -319,6 +320,7 @@ mod tests { type MinLength = MinLength; type MaxLength = MaxLength; } + type System = frame_system::Module; type Balances = pallet_balances::Module; type Nicks = Module; diff --git a/frame/scored-pool/src/mock.rs b/frame/scored-pool/src/mock.rs index bc44bf5e62202..dd59bbc84fe64 100644 --- a/frame/scored-pool/src/mock.rs +++ b/frame/scored-pool/src/mock.rs @@ -77,6 +77,7 @@ impl frame_system::Trait for Test { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); @@ -118,13 +119,16 @@ impl Trait for Test { type KickOrigin = EnsureSignedBy; type MembershipInitialized = TestChangeMembers; type MembershipChanged = TestChangeMembers; - type Currency = pallet_balances::Module; + type Currency = Balances; type CandidateDeposit = CandidateDeposit; type Period = Period; type Score = u64; type ScoreOrigin = EnsureSignedBy; } +type System = frame_system::Module; +type Balances = pallet_balances::Module; + // This function basically just builds a genesis storage key/value store according to // our desired mockup. pub fn new_test_ext() -> sp_io::TestExternalities { diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 2fdd62457e215..3c238b56ed317 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -146,6 +146,7 @@ parameter_types! { impl pallet_balances::Trait for Test { type Balance = Balance; type OnFreeBalanceZero = Staking; + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 51367ee95564f..eaaad7b8c49d5 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -67,10 +67,17 @@ pub trait Contains { /// The account with the given id was killed. #[impl_trait_for_tuples::impl_for_tuples(30)] pub trait OnFreeBalanceZero { - /// The account was the given id was killed. + /// The account with the given id was killed. fn on_free_balance_zero(who: &AccountId); } +/// The account with the given id was reaped. +#[impl_trait_for_tuples::impl_for_tuples(30)] +pub trait OnReapAccount { + /// The account with the given id was reaped. + fn on_reap_account(who: &AccountId); +} + /// Outcome of a balance update. pub enum UpdateBalanceOutcome { /// Account balance was simply updated. diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 5c194f625706f..f2902a11cfc81 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -113,7 +113,7 @@ use sp_runtime::{ use sp_core::storage::well_known_keys; use frame_support::{ decl_module, decl_event, decl_storage, decl_error, storage, Parameter, - traits::{Contains, Get, ModuleToIndex}, + traits::{Contains, Get, ModuleToIndex, OnReapAccount}, weights::{Weight, DispatchInfo, DispatchClass, SimpleDispatchInfo}, }; use codec::{Encode, Decode}; @@ -789,6 +789,13 @@ impl Module { } } +impl OnReapAccount for Module { + /// Remove the nonce for the account. Account is considered fully removed from the system. + fn on_reap_account(who: &T::AccountId) { + >::remove(who); + } +} + /// resource limit check. #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct CheckWeight(PhantomData); diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index c3611640f1a71..bae3096f3cdd1 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -311,6 +311,7 @@ mod tests { impl pallet_balances::Trait for Runtime { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); diff --git a/frame/treasury/src/lib.rs b/frame/treasury/src/lib.rs index 69d43bf4bc28d..f86c5383263ca 100644 --- a/frame/treasury/src/lib.rs +++ b/frame/treasury/src/lib.rs @@ -766,6 +766,7 @@ mod tests { type Balance = u64; type OnNewAccount = (); type OnFreeBalanceZero = (); + type OnReapAccount = System; type Event = (); type TransferPayment = (); type DustRemoval = (); diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 8103939a5c6a3..ddbebda31f90b 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -698,6 +698,7 @@ mod tests { impl pallet_balances::Trait for Test { type Balance = u64; type OnFreeBalanceZero = (); + type OnReapAccount = System; type OnNewAccount = (); type Event = TestEvent; type TransferPayment = (); @@ -719,6 +720,7 @@ mod tests { type MultisigDepositFactor = MultisigDepositFactor; type MaxSignatories = MaxSignatories; } + type System = frame_system::Module; type Balances = pallet_balances::Module; type Utility = Module;