Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Introduce OnReapAccount (#4585)
Browse files Browse the repository at this point in the history
* Initial run and gun at `OnReapAccount`

* Fix some imports

* More fixes

* Whitespace

* More wack-a-mole

* Gotta catch em all

* Update lib.rs

* Small doc update

* Whitespace
  • Loading branch information
shawntabrizi authored and gavofyork committed Jan 10, 2020
1 parent dd72e2a commit f1cc671
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
18 changes: 16 additions & 2 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -198,6 +198,12 @@ pub trait Subtrait<I: Instance = DefaultInstance>: frame_system::Trait {
/// Gives a chance to clean up resources associated with the given account.
type OnFreeBalanceZero: OnFreeBalanceZero<Self::AccountId>;

/// 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<Self::AccountId>;

/// Handler for when a new account is created.
type OnNewAccount: OnNewAccount<Self::AccountId>;

Expand All @@ -222,6 +228,12 @@ pub trait Trait<I: Instance = DefaultInstance>: frame_system::Trait {
/// Gives a chance to clean up resources associated with the given account.
type OnFreeBalanceZero: OnFreeBalanceZero<Self::AccountId>;

/// 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<Self::AccountId>;

/// Handler for when a new account is created.
type OnNewAccount: OnNewAccount<Self::AccountId>;

Expand All @@ -248,6 +260,7 @@ pub trait Trait<I: Instance = DefaultInstance>: frame_system::Trait {
impl<T: Trait<I>, I: Instance> Subtrait<I> 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;
Expand Down Expand Up @@ -597,7 +610,7 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
///
/// This just removes the nonce and leaves an event.
fn reap_account(who: &T::AccountId, dust: T::Balance) {
<frame_system::AccountNonce<T>>::remove(who);
T::OnReapAccount::on_reap_account(who);
Self::deposit_event(RawEvent::ReapedAccount(who.clone(), dust));
}

Expand Down Expand Up @@ -850,6 +863,7 @@ impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
impl<T: Subtrait<I>, I: Instance> Trait<I> for ElevatedTrait<T, I> {
type Balance = T::Balance;
type OnFreeBalanceZero = T::OnFreeBalanceZero;
type OnReapAccount = T::OnReapAccount;
type OnNewAccount = T::OnNewAccount;
type Event = ();
type TransferPayment = ();
Expand Down
1 change: 1 addition & 0 deletions frame/balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
1 change: 1 addition & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
1 change: 1 addition & 0 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
1 change: 1 addition & 0 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ mod tests {
type Balance = u64;
type OnNewAccount = ();
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type Event = Event;
type TransferPayment = ();
type DustRemoval = ();
Expand Down
1 change: 1 addition & 0 deletions frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
2 changes: 2 additions & 0 deletions frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand All @@ -706,6 +707,7 @@ mod tests {
impl Trait for Test {
type Event = ();
}
type System = frame_system::Module<Test>;
type Example = Module<Test>;

// This function basically just builds a genesis storage key/value store according to
Expand Down
1 change: 1 addition & 0 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
2 changes: 2 additions & 0 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down Expand Up @@ -950,6 +951,7 @@ mod tests {
type RegistrarOrigin = EnsureSignedBy<One, u64>;
type ForceOrigin = EnsureSignedBy<Two, u64>;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Identity = Module<Test>;

Expand Down
2 changes: 2 additions & 0 deletions frame/nicks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand All @@ -319,6 +320,7 @@ mod tests {
type MinLength = MinLength;
type MaxLength = MaxLength;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Nicks = Module<Test>;

Expand Down
6 changes: 5 additions & 1 deletion frame/scored-pool/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down Expand Up @@ -118,13 +119,16 @@ impl Trait for Test {
type KickOrigin = EnsureSignedBy<KickOrigin, u64>;
type MembershipInitialized = TestChangeMembers;
type MembershipChanged = TestChangeMembers;
type Currency = pallet_balances::Module<Self>;
type Currency = Balances;
type CandidateDeposit = CandidateDeposit;
type Period = Period;
type Score = u64;
type ScoreOrigin = EnsureSignedBy<ScoreOrigin, u64>;
}

type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;

// This function basically just builds a genesis storage key/value store according to
// our desired mockup.
pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
1 change: 1 addition & 0 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
9 changes: 8 additions & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ pub trait Contains<T: Ord> {
/// The account with the given id was killed.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnFreeBalanceZero<AccountId> {
/// 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<AccountId> {
/// 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.
Expand Down
9 changes: 8 additions & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -789,6 +789,13 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> OnReapAccount<T::AccountId> for Module<T> {
/// Remove the nonce for the account. Account is considered fully removed from the system.
fn on_reap_account(who: &T::AccountId) {
<AccountNonce<T>>::remove(who);
}
}

/// resource limit check.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
pub struct CheckWeight<T: Trait + Send + Sync>(PhantomData<T>);
Expand Down
1 change: 1 addition & 0 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand Down
1 change: 1 addition & 0 deletions frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ mod tests {
type Balance = u64;
type OnNewAccount = ();
type OnFreeBalanceZero = ();
type OnReapAccount = System;
type Event = ();
type TransferPayment = ();
type DustRemoval = ();
Expand Down
2 changes: 2 additions & 0 deletions frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ();
Expand All @@ -719,6 +720,7 @@ mod tests {
type MultisigDepositFactor = MultisigDepositFactor;
type MaxSignatories = MaxSignatories;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Utility = Module<Test>;

Expand Down

0 comments on commit f1cc671

Please sign in to comment.