Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Balances Pallet: Emit events when TI is updated in currency impl #4936

Merged
merged 16 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
13 changes: 13 additions & 0 deletions prdoc/pr_4936.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "Balances Pallet: Emit events when TI is updated in currency impl"

doc:
- audience: Runtime Dev
description: |
Previously, in the Currency impl, the implementation of pallet_balances was not emitting any instances of Issued and Rescinded events, even though the Fungible equivalent was. This PR adds the Issued and Rescinded events in appropriate places in impl_currency along with tests.

crates:
- name: pallet-balances
bump: patch
13 changes: 12 additions & 1 deletion substrate/frame/balances/src/impl_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ use frame_support::{
};
use frame_system::pallet_prelude::BlockNumberFor;
pub use imbalances::{NegativeImbalance, PositiveImbalance};
use sp_runtime::traits::Bounded;

// wrapping these imbalances in a private module is necessary to ensure absolute privacy
// of the inner member.
mod imbalances {
use super::{result, Config, Imbalance, RuntimeDebug, Saturating, TryDrop, Zero};
use super::*;
use frame_support::traits::SameOrOther;
use sp_std::mem;

Expand Down Expand Up @@ -200,13 +201,19 @@ mod imbalances {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<super::TotalIssuance<T, I>>::mutate(|v| *v = v.saturating_add(self.0));
if !self.0.is_zero() {
bkchr marked this conversation as resolved.
Show resolved Hide resolved
Pallet::<T, I>::deposit_event(Event::<T, I>::Issued { amount: self.0 });
}
}
}

impl<T: Config<I>, I: 'static> Drop for NegativeImbalance<T, I> {
/// Basic drop handler will just square up the total issuance.
fn drop(&mut self) {
<super::TotalIssuance<T, I>>::mutate(|v| *v = v.saturating_sub(self.0));
if !self.0.is_zero() {
bkchr marked this conversation as resolved.
Show resolved Hide resolved
Pallet::<T, I>::deposit_event(Event::<T, I>::Rescinded { amount: self.0 });
}
}
}
}
Expand Down Expand Up @@ -263,6 +270,8 @@ where
Zero::zero()
});
});

Pallet::<T, I>::deposit_event(Event::<T, I>::Rescinded { amount });
PositiveImbalance::new(amount)
}

Expand All @@ -279,6 +288,8 @@ where
Self::Balance::max_value()
})
});

Pallet::<T, I>::deposit_event(Event::<T, I>::Issued { amount });
NegativeImbalance::new(amount)
}

Expand Down
29 changes: 20 additions & 9 deletions substrate/frame/balances/src/tests/currency_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,16 @@ fn reward_should_work() {
ExtBuilder::default().monied(true).build_and_execute_with(|| {
assert_eq!(Balances::total_balance(&1), 10);
assert_ok!(Balances::deposit_into_existing(&1, 10).map(drop));
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Deposit {
who: 1,
amount: 10,
}));
assert_eq!(
events(),
[
RuntimeEvent::Balances(crate::Event::Deposit {
who: 1,
amount: 10,
}),
RuntimeEvent::Balances(crate::Event::Issued { amount: 10 }),
]
);
assert_eq!(Balances::total_balance(&1), 20);
assert_eq!(Balances::total_issuance(), 120);
});
Expand Down Expand Up @@ -480,7 +486,7 @@ fn withdrawing_balance_should_work() {
let _ = Balances::deposit_creating(&2, 111);
let _ =
Balances::withdraw(&2, 11, WithdrawReasons::TRANSFER, ExistenceRequirement::KeepAlive);
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Withdraw {
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Withdraw {
who: 2,
amount: 11,
}));
Expand Down Expand Up @@ -889,6 +895,7 @@ fn emit_events_with_existential_deposit() {
[
RuntimeEvent::System(system::Event::NewAccount { account: 1 }),
RuntimeEvent::Balances(crate::Event::Endowed { account: 1, free_balance: 100 }),
RuntimeEvent::Balances(crate::Event::Issued { amount: 100 }),
RuntimeEvent::Balances(crate::Event::BalanceSet { who: 1, free: 100 }),
]
);
Expand All @@ -902,6 +909,7 @@ fn emit_events_with_existential_deposit() {
RuntimeEvent::System(system::Event::KilledAccount { account: 1 }),
RuntimeEvent::Balances(crate::Event::DustLost { account: 1, amount: 99 }),
RuntimeEvent::Balances(crate::Event::Slashed { who: 1, amount: 1 }),
RuntimeEvent::Balances(crate::Event::Rescinded { amount: 1 }),
]
);
});
Expand All @@ -918,6 +926,7 @@ fn emit_events_with_no_existential_deposit_suicide() {
RuntimeEvent::Balances(crate::Event::BalanceSet { who: 1, free: 100 }),
RuntimeEvent::System(system::Event::NewAccount { account: 1 }),
RuntimeEvent::Balances(crate::Event::Endowed { account: 1, free_balance: 100 }),
RuntimeEvent::Balances(crate::Event::Issued { amount: 100 }),
]
);

Expand All @@ -929,6 +938,7 @@ fn emit_events_with_no_existential_deposit_suicide() {
[
RuntimeEvent::System(system::Event::KilledAccount { account: 1 }),
RuntimeEvent::Balances(crate::Event::Slashed { who: 1, amount: 100 }),
RuntimeEvent::Balances(crate::Event::Rescinded { amount: 100 }),
]
);
});
Expand All @@ -954,7 +964,7 @@ fn slash_full_works() {
assert_eq!(Balances::slash(&1, 1_000), (NegativeImbalance::new(1000), 0));
// Account is still alive
assert!(!System::account_exists(&1));
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Slashed {
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Slashed {
who: 1,
amount: 1000,
}));
Expand All @@ -969,7 +979,7 @@ fn slash_partial_works() {
assert_eq!(Balances::slash(&1, 900), (NegativeImbalance::new(900), 0));
// Account is still alive
assert!(System::account_exists(&1));
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Slashed {
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Slashed {
who: 1,
amount: 900,
}));
Expand All @@ -983,7 +993,7 @@ fn slash_dusting_works() {
// Slashed completed in full
assert_eq!(Balances::slash(&1, 950), (NegativeImbalance::new(950), 0));
assert!(!System::account_exists(&1));
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Slashed {
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Slashed {
who: 1,
amount: 950,
}));
Expand All @@ -998,7 +1008,7 @@ fn slash_does_not_take_from_reserve() {
// Slashed completed in full
assert_eq!(Balances::slash(&1, 900), (NegativeImbalance::new(800), 100));
assert_eq!(Balances::reserved_balance(&1), 100);
System::assert_last_event(RuntimeEvent::Balances(crate::Event::Slashed {
System::assert_has_event(RuntimeEvent::Balances(crate::Event::Slashed {
who: 1,
amount: 800,
}));
Expand Down Expand Up @@ -1399,6 +1409,7 @@ fn self_transfer_noop() {
Event::Deposit { who: 1, amount: 100 }.into(),
SysEvent::NewAccount { account: 1 }.into(),
Event::Endowed { account: 1, free_balance: 100 }.into(),
Event::Issued { amount: 100 }.into(),
]
);
assert_eq!(Balances::free_balance(1), 100);
Expand Down
7 changes: 4 additions & 3 deletions substrate/frame/balances/src/tests/reentrancy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn transfer_dust_removal_tst1_should_work() {
assert_eq!(Balances::free_balance(&1), 1050);

// Verify the events
assert_eq!(System::events().len(), 12);
assert_eq!(System::events().len(), 14);

System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
from: 2,
Expand Down Expand Up @@ -93,7 +93,7 @@ fn transfer_dust_removal_tst2_should_work() {
assert_eq!(Balances::free_balance(&1), 1500);

// Verify the events
assert_eq!(System::events().len(), 10);
assert_eq!(System::events().len(), 12);

System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
from: 2,
Expand Down Expand Up @@ -139,7 +139,7 @@ fn repatriating_reserved_balance_dust_removal_should_work() {
assert_eq!(Balances::free_balance(1), 1500);

// Verify the events
assert_eq!(System::events().len(), 10);
assert_eq!(System::events().len(), 12);

System::assert_has_event(RuntimeEvent::Balances(crate::Event::Transfer {
from: 2,
Expand Down Expand Up @@ -167,6 +167,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() {
[
RuntimeEvent::System(system::Event::NewAccount { account: 1 }),
RuntimeEvent::Balances(crate::Event::Endowed { account: 1, free_balance: 100 }),
RuntimeEvent::Balances(crate::Event::Issued { amount: 100 }),
RuntimeEvent::Balances(crate::Event::BalanceSet { who: 1, free: 100 }),
]
);
Expand Down
Loading