Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test account events and fix some bugs
  • Loading branch information
xlc committed May 30, 2020
commit 5504c7b75469b93602f3eb7c06500e8b99763c53
9 changes: 8 additions & 1 deletion frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,14 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
/// storage (which is the default in most examples and tests) then there's no need.**
impl<T: Trait<I>, I: Instance> OnKilledAccount<T::AccountId> for Module<T, I> {
fn on_killed_account(who: &T::AccountId) {
Account::<T, I>::remove(who);
Account::<T, I>::mutate_exists(who, |account| {
let total = account.as_ref().map(|acc| acc.total()).unwrap_or_default();
if !total.is_zero() {
T::DustRemoval::on_unbalanced(NegativeImbalance::new(total));
Self::deposit_event(RawEvent::DustLost(who.clone(), total));
}
*account = None;
});
}
}

Expand Down
70 changes: 70 additions & 0 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ macro_rules! decl_tests {
DispatchInfo { weight: w, ..Default::default() }
}

fn events() -> Vec<Event> {
let evt = System::events().into_iter().map(|evt| evt.event).collect::<Vec<_>>();

System::reset_events();

evt
}

#[test]
fn basic_locking_should_work() {
<$ext_builder>::default().existential_deposit(1).monied(true).build().execute_with(|| {
Expand Down Expand Up @@ -674,5 +682,67 @@ macro_rules! decl_tests {
assert_eq!(Balances::reserved_balance(1), 0);
});
}

#[test]
fn emit_events_with_existential_deposit() {
<$ext_builder>::default()
.existential_deposit(100)
.build()
.execute_with(|| {
assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0));

assert_eq!(
events(),
[
Event::system(system::RawEvent::NewAccount(1)),
Event::balances(RawEvent::Endowed(1, 100)),
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
]
);

let _ = Balances::slash(&1, 1);

assert_eq!(
events(),
[
Event::balances(RawEvent::DustLost(1, 99)),
Event::system(system::RawEvent::KilledAccount(1))
]
);
});
}

#[test]
fn emit_events_with_no_existential_deposit_suicide() {
<$ext_builder>::default()
.existential_deposit(0)
.build()
.execute_with(|| {
assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0));

assert_eq!(
events(),
[
Event::system(system::RawEvent::NewAccount(1)),
Event::balances(RawEvent::Endowed(1, 100)),
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
]
);

let _ = Balances::slash(&1, 100);

// no events
assert_eq!(events(), []);

assert_ok!(System::suicide(Origin::signed(1)));

assert_eq!(
events(),
[
Event::system(system::RawEvent::KilledAccount(1))
]
);
});
}
}
}
22 changes: 18 additions & 4 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_runtime::{
};
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, parameter_types};
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::traits::Get;
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use std::cell::RefCell;
Expand All @@ -37,6 +37,17 @@ impl_outer_origin!{
pub enum Origin for Test {}
}

mod balances {
pub use crate::Event;
}

impl_outer_event! {
pub enum Event for Test {
system<T>,
balances<T>,
}
}

thread_local! {
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
}
Expand Down Expand Up @@ -65,7 +76,7 @@ impl frame_system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
Expand Down Expand Up @@ -93,7 +104,7 @@ impl pallet_transaction_payment::Trait for Test {
impl Trait for Test {
type Balance = u64;
type DustRemoval = ();
type Event = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = system::Module<Test>;
}
Expand Down Expand Up @@ -138,7 +149,10 @@ impl ExtBuilder {
vec![]
},
}.assimilate_storage(&mut t).unwrap();
t.into()

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}

Expand Down
56 changes: 52 additions & 4 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_runtime::{
};
use sp_core::H256;
use sp_io;
use frame_support::{impl_outer_origin, parameter_types};
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
use frame_support::traits::{Get, StorageMapShim};
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
use std::cell::RefCell;
Expand All @@ -37,6 +37,17 @@ impl_outer_origin!{
pub enum Origin for Test {}
}

mod balances {
pub use crate::Event;
}

impl_outer_event! {
pub enum Event for Test {
system<T>,
balances<T>,
}
}

thread_local! {
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
}
Expand Down Expand Up @@ -65,7 +76,7 @@ impl frame_system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = Event;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
Expand Down Expand Up @@ -93,7 +104,7 @@ impl pallet_transaction_payment::Trait for Test {
impl Trait for Test {
type Balance = u64;
type DustRemoval = ();
type Event = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = StorageMapShim<
super::Account<Test>,
Expand Down Expand Up @@ -146,8 +157,45 @@ impl ExtBuilder {
vec![]
},
}.assimilate_storage(&mut t).unwrap();
t.into()

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
}

decl_tests!{ Test, ExtBuilder, EXISTENTIAL_DEPOSIT }

#[test]
fn emit_events_with_no_existential_deposit_suicide_with_dust() {
<ExtBuilder>::default()
.existential_deposit(0)
.build()
.execute_with(|| {
assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0));

assert_eq!(
events(),
[
Event::system(system::RawEvent::NewAccount(1)),
Event::balances(RawEvent::Endowed(1, 100)),
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
]
);

let _ = Balances::slash(&1, 99);

// no events
assert_eq!(events(), []);

assert_ok!(System::suicide(Origin::signed(1)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not support for composite setup because suicide requires account data to be default. i.e. total is zero.


assert_eq!(
events(),
[
Event::balances(RawEvent::DustLost(1, 1)),
Event::system(system::RawEvent::KilledAccount(1))
]
);
});
}
15 changes: 12 additions & 3 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,12 @@ decl_module! {
/// No DB Read or Write operations because caller is already in overlay
/// # </weight>
#[weight = (10_000_000, DispatchClass::Operational)]
fn suicide(origin) {
pub fn suicide(origin) {
let who = ensure_signed(origin)?;
let account = Account::<T>::get(&who);
ensure!(account.refcount == 0, Error::<T>::NonZeroRefCount);
ensure!(account.data == T::AccountData::default(), Error::<T>::NonDefaultComposite);
Account::<T>::remove(who);
Self::kill_account(&who);
}
}
}
Expand Down Expand Up @@ -1131,6 +1131,15 @@ impl<T: Trait> Module<T> {
AllExtrinsicsLen::put(len as u32);
}

/// Reset events. Can be used as an alternative to
/// `initialize` for tests that don't need to bother with the other environment entries.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn reset_events() {
<Events<T>>::kill();
EventCount::kill();
<EventTopics<T>>::remove_all();
}

/// Return the chain's current runtime version.
pub fn runtime_version() -> RuntimeVersion { T::Version::get() }

Expand Down Expand Up @@ -1222,8 +1231,8 @@ impl<T: Trait> Module<T> {
"WARNING: Referenced account deleted. This is probably a bug."
);
}
Module::<T>::on_killed_account(who.clone());
}
Module::<T>::on_killed_account(who.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats up with this change?
Its going to trigger this function even if there was no Account entry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches to the behavior on create account, which does not actually create Account entry

Module::<T>::on_created_account(who.clone());

Otherwise the behavior between local and composite will be different.

}

/// Determine whether or not it is possible to update the code.
Expand Down