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

Commit

Permalink
Add events for account new/reap
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork committed Aug 26, 2018
1 parent bf93a26 commit 90b003a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion demo/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl staking::Trait for Concrete {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = Balance;
type AccountIndex = AccountIndex;
type OnAccountKill = ();
type OnFreeBalanceZero = ();
type Event = Event;
}

Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/contract/src/account_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<T: Trait> AccountDb<T> for DirectAccountDb {
if let staking::UpdateBalanceOutcome::AccountKilled =
staking::Module::<T>::set_free_balance_creating(&address, balance)
{
// Account killed. This will ultimately lead to calling `OnAccountKill` callback
// Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback
// which will make removal of CodeOf and StorageOf for this account.
// In order to avoid writing over the deleted properties we `continue` here.
continue;
Expand Down
4 changes: 2 additions & 2 deletions substrate/runtime/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> staking::OnAccountKill<T::AccountId> for Module<T> {
fn on_account_kill(who: &T::AccountId) {
impl<T: Trait> staking::OnFreeBalanceZero<T::AccountId> for Module<T> {
fn on_free_balance_zero(who: &T::AccountId) {
<CodeOf<T>>::remove(who);
<StorageOf<T>>::remove_prefix(who.clone());
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl staking::Trait for Test {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = u64;
type AccountIndex = u64;
type OnAccountKill = Contract;
type OnFreeBalanceZero = Contract;
type Event = ();
}
impl session::Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ mod tests {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = u64;
type AccountIndex = u64;
type OnAccountKill = ();
type OnFreeBalanceZero = ();
type Event = ();
}
impl democracy::Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mod tests {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = u64;
type AccountIndex = u64;
type OnAccountKill = ();
type OnFreeBalanceZero = ();
type Event = ();
}
impl timestamp::Trait for Test {
Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod tests {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = u64;
type AccountIndex = u64;
type OnAccountKill = ();
type OnFreeBalanceZero = ();
type Event = MetaEvent;
}
impl timestamp::Trait for Test {
Expand Down
59 changes: 43 additions & 16 deletions substrate/runtime/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ const RECLAIM_INDEX_MAGIC: usize = 0x69;

pub type Address<T> = RawAddress<<T as system::Trait>::AccountId, <T as Trait>::AccountIndex>;

pub type Event<T> = RawEvent<<T as Trait>::Balance, <T as system::Trait>::AccountId>;
pub type Event<T> = RawEvent<
<T as Trait>::Balance,
<T as system::Trait>::AccountId,
<T as Trait>::AccountIndex
>;

#[cfg(test)]
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -96,13 +100,13 @@ pub enum LockStatus<BlockNumber: PartialEq + Clone> {
}

/// The account was the given id was killed.
pub trait OnAccountKill<AccountId> {
pub trait OnFreeBalanceZero<AccountId> {
/// The account was the given id was killed.
fn on_account_kill(who: &AccountId);
fn on_free_balance_zero(who: &AccountId);
}

impl<AccountId> OnAccountKill<AccountId> for () {
fn on_account_kill(_who: &AccountId) {}
impl<AccountId> OnFreeBalanceZero<AccountId> for () {
fn on_free_balance_zero(_who: &AccountId) {}
}

/// Preference of what happens on a slash event.
Expand Down Expand Up @@ -144,10 +148,11 @@ pub trait Trait: system::Trait + session::Trait {
/// Type used for storing an account's index; implies the maximum number of accounts the system
/// can hold.
type AccountIndex: Parameter + Member + Codec + SimpleArithmetic + As<u8> + As<u16> + As<u32> + As<u64> + As<usize> + Copy;
/// A function which is invoked when the given account is dead.
/// A function which is invoked when the free-balance has fallen below the existential deposit and
/// has been reduced to zero.
///
/// Gives a chance to clean up resources associated with the given account.
type OnAccountKill: OnAccountKill<Self::AccountId>;
type OnFreeBalanceZero: OnFreeBalanceZero<Self::AccountId>;

/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
Expand Down Expand Up @@ -181,17 +186,21 @@ decl_module! {
/// An event in this module.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
pub enum RawEvent<Balance, AccountId> {
pub enum RawEvent<Balance, AccountId, AccountIndex> {
/// All validators have been rewarded by the given balance.
Reward(Balance),
/// One validator (and their nominators) has been given a offline-warning (they're still within
/// their grace). The accrued number of slashes is recorded, too.
OfflineWarning(AccountId, u32),
/// One validator (and their nominators) has been slashed by the given amount.
OfflineSlash(AccountId, Balance),
/// A new account was created.
NewAccount(AccountId, AccountIndex, NewAccountOutcome),
/// An account was reaped.
ReapedAccount(AccountId),
}
impl<B, A> From<RawEvent<B, A>> for () {
fn from(_: RawEvent<B, A>) -> () { () }
impl<B, A, I> From<RawEvent<B, A, I>> for () {
fn from(_: RawEvent<B, A, I>) -> () { () }
}

decl_storage! {
Expand Down Expand Up @@ -262,7 +271,7 @@ decl_storage! {
// This is the only balance that matters in terms of most operations on tokens. It is
// alone used to determine the balance when in the contract execution environment. When this
// balance falls below the value of `ExistentialDeposit`, then the "current account" is
// deleted: specifically, `Bondage` and `FreeBalance`. Furthermore, `OnAccountKill` callback
// deleted: specifically, `Bondage` and `FreeBalance`. Furthermore, `OnFreeBalanceZero` callback
// is invoked, giving a chance to external modules to cleanup data associated with
// the deleted account.
//
Expand All @@ -288,7 +297,10 @@ decl_storage! {
pub Bondage get(bondage): b"sta:bon:" => default map [ T::AccountId => T::BlockNumber ];
}

enum NewAccountOutcome {
/// Whatever happened about the hint given when creating the new account.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Encode, Decode, PartialEq, Eq, Clone, Copy)]
pub enum NewAccountOutcome {
NoHint,
GoodHint,
BadHint,
Expand Down Expand Up @@ -955,7 +967,9 @@ impl<T: Trait> Module<T> {
try_set[item_index] = who.clone();
<EnumSet<T>>::insert(set_index, try_set);

return NewAccountOutcome::GoodHint;
Self::deposit_event(RawEvent::NewAccount(who.clone(), try_index, NewAccountOutcome::GoodHint));

return NewAccountOutcome::GoodHint
}
}
NewAccountOutcome::BadHint
Expand All @@ -975,6 +989,8 @@ impl<T: Trait> Module<T> {
set_index += One::one();
};

let index = T::AccountIndex::sa(set_index.as_() * ENUM_SET_SIZE + set.len());

// update set.
set.push(who.clone());

Expand All @@ -986,27 +1002,38 @@ impl<T: Trait> Module<T> {
// write set.
<EnumSet<T>>::insert(set_index, set);

Self::deposit_event(RawEvent::NewAccount(who.clone(), index, ret));

ret
}

fn reap_account(who: &T::AccountId) {
<system::AccountNonce<T>>::remove(who);
Self::deposit_event(RawEvent::ReapedAccount(who.clone()));
}

/// Kill an account's free portion.
fn on_free_too_low(who: &T::AccountId) {
Self::decrease_total_stake_by(Self::free_balance(who));
<FreeBalance<T>>::remove(who);
<Bondage<T>>::remove(who);
T::OnAccountKill::on_account_kill(who);

// TODO: rename this to on_free_account_kill or some such, since it only pertains to removing the
// free-balance part of the account, not the whole thing.
T::OnFreeBalanceZero::on_free_balance_zero(who);

if Self::reserved_balance(who).is_zero() {
<system::AccountNonce<T>>::remove(who);
Self::reap_account(who);
}
}

/// Kill an account's reserved portion.
fn on_reserved_too_low(who: &T::AccountId) {
Self::decrease_total_stake_by(Self::reserved_balance(who));
<ReservedBalance<T>>::remove(who);

if Self::free_balance(who).is_zero() {
<system::AccountNonce<T>>::remove(who);
Self::reap_account(who);
}
}

Expand Down
2 changes: 1 addition & 1 deletion substrate/runtime/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Trait for Test {
const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1;
type Balance = u64;
type AccountIndex = u64;
type OnAccountKill = ();
type OnFreeBalanceZero = ();
type Event = ();
}

Expand Down

0 comments on commit 90b003a

Please sign in to comment.