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

Commit e595a64

Browse files
committed
test account events and fix some bugs
1 parent 4e1aeaa commit e595a64

File tree

5 files changed

+127
-12
lines changed

5 files changed

+127
-12
lines changed

frame/balances/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,14 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
12301230
/// storage (which is the default in most examples and tests) then there's no need.**
12311231
impl<T: Trait<I>, I: Instance> OnKilledAccount<T::AccountId> for Module<T, I> {
12321232
fn on_killed_account(who: &T::AccountId) {
1233-
Account::<T, I>::remove(who);
1233+
Account::<T, I>::mutate_exists(who, |account| {
1234+
let total = account.as_ref().map(|acc| acc.total()).unwrap_or_default();
1235+
if !total.is_zero() {
1236+
T::DustRemoval::on_unbalanced(NegativeImbalance::new(total));
1237+
Self::deposit_event(RawEvent::DustLost(who.clone(), total));
1238+
}
1239+
*account = None;
1240+
});
12341241
}
12351242
}
12361243

frame/balances/src/tests.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ macro_rules! decl_tests {
6161
DispatchInfo { weight: w, ..Default::default() }
6262
}
6363

64+
fn events() -> Vec<Event> {
65+
let evt = System::events().into_iter().map(|evt| evt.event).collect::<Vec<_>>();
66+
67+
System::reset_events();
68+
69+
evt
70+
}
71+
6472
#[test]
6573
fn basic_locking_should_work() {
6674
<$ext_builder>::default().existential_deposit(1).monied(true).build().execute_with(|| {
@@ -674,5 +682,68 @@ macro_rules! decl_tests {
674682
assert_eq!(Balances::reserved_balance(1), 0);
675683
});
676684
}
685+
686+
#[test]
687+
fn emit_events_with_existential_deposit() {
688+
<$ext_builder>::default()
689+
.existential_deposit(100)
690+
.build()
691+
.execute_with(|| {
692+
assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0));
693+
694+
assert_eq!(
695+
events(),
696+
[
697+
Event::system(system::RawEvent::NewAccount(1)),
698+
Event::balances(RawEvent::Endowed(1, 100)),
699+
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
700+
]
701+
);
702+
703+
let _ = Balances::slash(&1, 1);
704+
705+
assert_eq!(
706+
events(),
707+
[
708+
Event::balances(RawEvent::DustLost(1, 99)),
709+
Event::system(system::RawEvent::KilledAccount(1))
710+
]
711+
);
712+
});
713+
}
714+
715+
#[test]
716+
fn emit_events_with_no_existential_deposit() {
717+
<$ext_builder>::default()
718+
.existential_deposit(0)
719+
.build()
720+
.execute_with(|| {
721+
assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0));
722+
723+
assert_eq!(
724+
events(),
725+
[
726+
Event::system(system::RawEvent::NewAccount(1)),
727+
Event::balances(RawEvent::Endowed(1, 100)),
728+
Event::balances(RawEvent::BalanceSet(1, 100, 0)),
729+
]
730+
);
731+
732+
let _ = Balances::slash(&1, 99);
733+
734+
// no events
735+
assert_eq!(events(), []);
736+
737+
assert_ok!(System::suicide(Origin::signed(1)));
738+
739+
assert_eq!(
740+
events(),
741+
[
742+
Event::balances(RawEvent::DustLost(1, 1)),
743+
Event::system(system::RawEvent::KilledAccount(1))
744+
]
745+
);
746+
});
747+
}
677748
}
678749
}

frame/balances/src/tests_composite.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use sp_runtime::{
2626
};
2727
use sp_core::H256;
2828
use sp_io;
29-
use frame_support::{impl_outer_origin, parameter_types};
29+
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
3030
use frame_support::traits::Get;
3131
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
3232
use std::cell::RefCell;
@@ -37,6 +37,17 @@ impl_outer_origin!{
3737
pub enum Origin for Test {}
3838
}
3939

40+
mod balances {
41+
pub use crate::Event;
42+
}
43+
44+
impl_outer_event! {
45+
pub enum Event for Test {
46+
system<T>,
47+
balances<T>,
48+
}
49+
}
50+
4051
thread_local! {
4152
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
4253
}
@@ -65,7 +76,7 @@ impl frame_system::Trait for Test {
6576
type AccountId = u64;
6677
type Lookup = IdentityLookup<Self::AccountId>;
6778
type Header = Header;
68-
type Event = ();
79+
type Event = Event;
6980
type BlockHashCount = BlockHashCount;
7081
type MaximumBlockWeight = MaximumBlockWeight;
7182
type DbWeight = ();
@@ -93,7 +104,7 @@ impl pallet_transaction_payment::Trait for Test {
93104
impl Trait for Test {
94105
type Balance = u64;
95106
type DustRemoval = ();
96-
type Event = ();
107+
type Event = Event;
97108
type ExistentialDeposit = ExistentialDeposit;
98109
type AccountStore = system::Module<Test>;
99110
}
@@ -138,7 +149,10 @@ impl ExtBuilder {
138149
vec![]
139150
},
140151
}.assimilate_storage(&mut t).unwrap();
141-
t.into()
152+
153+
let mut ext = sp_io::TestExternalities::new(t);
154+
ext.execute_with(|| System::set_block_number(1));
155+
ext
142156
}
143157
}
144158

frame/balances/src/tests_local.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use sp_runtime::{
2626
};
2727
use sp_core::H256;
2828
use sp_io;
29-
use frame_support::{impl_outer_origin, parameter_types};
29+
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types};
3030
use frame_support::traits::{Get, StorageMapShim};
3131
use frame_support::weights::{Weight, DispatchInfo, IdentityFee};
3232
use std::cell::RefCell;
@@ -37,6 +37,17 @@ impl_outer_origin!{
3737
pub enum Origin for Test {}
3838
}
3939

40+
mod balances {
41+
pub use crate::Event;
42+
}
43+
44+
impl_outer_event! {
45+
pub enum Event for Test {
46+
system<T>,
47+
balances<T>,
48+
}
49+
}
50+
4051
thread_local! {
4152
static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
4253
}
@@ -65,7 +76,7 @@ impl frame_system::Trait for Test {
6576
type AccountId = u64;
6677
type Lookup = IdentityLookup<Self::AccountId>;
6778
type Header = Header;
68-
type Event = ();
79+
type Event = Event;
6980
type BlockHashCount = BlockHashCount;
7081
type MaximumBlockWeight = MaximumBlockWeight;
7182
type DbWeight = ();
@@ -93,7 +104,7 @@ impl pallet_transaction_payment::Trait for Test {
93104
impl Trait for Test {
94105
type Balance = u64;
95106
type DustRemoval = ();
96-
type Event = ();
107+
type Event = Event;
97108
type ExistentialDeposit = ExistentialDeposit;
98109
type AccountStore = StorageMapShim<
99110
super::Account<Test>,
@@ -146,7 +157,10 @@ impl ExtBuilder {
146157
vec![]
147158
},
148159
}.assimilate_storage(&mut t).unwrap();
149-
t.into()
160+
161+
let mut ext = sp_io::TestExternalities::new(t);
162+
ext.execute_with(|| System::set_block_number(1));
163+
ext
150164
}
151165
}
152166

frame/system/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,12 @@ decl_module! {
742742
/// No DB Read or Write operations because caller is already in overlay
743743
/// # </weight>
744744
#[weight = (10_000_000, DispatchClass::Operational)]
745-
fn suicide(origin) {
745+
pub fn suicide(origin) {
746746
let who = ensure_signed(origin)?;
747747
let account = Account::<T>::get(&who);
748748
ensure!(account.refcount == 0, Error::<T>::NonZeroRefCount);
749749
ensure!(account.data == T::AccountData::default(), Error::<T>::NonDefaultComposite);
750-
Account::<T>::remove(who);
750+
Self::kill_account(&who);
751751
}
752752
}
753753
}
@@ -1131,6 +1131,15 @@ impl<T: Trait> Module<T> {
11311131
AllExtrinsicsLen::put(len as u32);
11321132
}
11331133

1134+
/// Reset events. Can be used as an alternative to
1135+
/// `initialize` for tests that don't need to bother with the other environment entries.
1136+
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
1137+
pub fn reset_events() {
1138+
<Events<T>>::kill();
1139+
EventCount::kill();
1140+
<EventTopics<T>>::remove_all();
1141+
}
1142+
11341143
/// Return the chain's current runtime version.
11351144
pub fn runtime_version() -> RuntimeVersion { T::Version::get() }
11361145

@@ -1222,8 +1231,8 @@ impl<T: Trait> Module<T> {
12221231
"WARNING: Referenced account deleted. This is probably a bug."
12231232
);
12241233
}
1225-
Module::<T>::on_killed_account(who.clone());
12261234
}
1235+
Module::<T>::on_killed_account(who.clone());
12271236
}
12281237

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

0 commit comments

Comments
 (0)