diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 2e7a39d747..7667f8f00b 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -251,6 +251,7 @@ pub mod pallet { Ok(()) })?; >::mutate(|t| *t += amount); + Self::deposit_event(Event::Deposit(who.clone(), amount)); Ok(()) } @@ -272,6 +273,7 @@ pub mod pallet { }, )?; >::mutate(|t| *t -= actual); + Self::deposit_event(Event::Withdraw(who.clone(), amount)); Ok(actual) } } @@ -339,7 +341,14 @@ pub mod pallet { impl, I: 'static> Unbalanced for Pallet { fn set_balance(who: &T::AccountId, amount: Self::Balance) -> DispatchResult { - Self::mutate_account(who, |account| account.set_free(amount))?; + Self::mutate_account(who, |account| { + account.set_free(amount); + Self::deposit_event(Event::BalanceSet( + who.clone(), + account.free(), + account.reserved(), + )); + })?; Ok(()) } @@ -629,8 +638,6 @@ pub mod pallet { Transfer(T::AccountId, T::AccountId, T::Balance), /// A balance was set by root. \[who, free, reserved\] BalanceSet(T::AccountId, T::Balance, T::Balance), - /// Some amount was deposited (e.g. for transaction fees). \[who, deposit\] - Deposit(T::AccountId, T::Balance), /// Some balance was reserved (moved from free to reserved). \[who, value\] Reserved(T::AccountId, T::Balance), /// Some balance was unreserved (moved from reserved to free). \[who, value\] @@ -639,6 +646,14 @@ pub mod pallet { /// Final argument indicates the destination balance type. /// \[from, to, balance, destination_status\] ReserveRepatriated(T::AccountId, T::AccountId, T::Balance, BalanceStatus), + /// Some amount was deposited into the account (e.g. for transaction fees). \[who, + /// deposit\] + Deposit(T::AccountId, T::Balance), + /// Some amount was withdrawn from the account (e.g. for transaction fees). \[who, value\] + Withdraw(T::AccountId, T::Balance), + /// Some amount was removed from the account (e.g. for misbehavior). \[who, + /// amount_slashed\] + Slashed(T::AccountId, T::Balance), } #[pallet::error] @@ -1532,7 +1547,13 @@ pub mod pallet { } }, ) { - Ok(r) => return r, + Ok((imbalance, not_slashed)) => { + Self::deposit_event(Event::Slashed( + who.clone(), + value.saturating_sub(not_slashed), + )); + return (imbalance, not_slashed); + }, Err(_) => (), } } @@ -1562,6 +1583,7 @@ pub mod pallet { account.set_free( account.free().checked_add(&value).ok_or(ArithmeticError::Overflow)?, ); + Self::deposit_event(Event::Deposit(who.clone(), value)); Ok(PositiveImbalance::new(value)) }, ) @@ -1597,6 +1619,7 @@ pub mod pallet { None => return Ok(Self::PositiveImbalance::zero()), }); + Self::deposit_event(Event::Deposit(who.clone(), value)); Ok(PositiveImbalance::new(value)) }, ) @@ -1644,6 +1667,7 @@ pub mod pallet { account.set_free(new_free_account); + Self::deposit_event(Event::Withdraw(who.clone(), value)); Ok(NegativeImbalance::new(value)) }, ) @@ -1682,6 +1706,11 @@ pub mod pallet { SignedImbalance::Negative(NegativeImbalance::new(account.free() - value)) }; account.set_free(value); + Self::deposit_event(Event::BalanceSet( + who.clone(), + account.free(), + account.reserved(), + )); Ok(imbalance) }, ) @@ -1742,7 +1771,13 @@ pub mod pallet { // here. (NegativeImbalance::new(actual), value - actual) }) { - Ok(r) => return r, + Ok((imbalance, not_slashed)) => { + Self::deposit_event(Event::Slashed( + who.clone(), + value.saturating_sub(not_slashed), + )); + return (imbalance, not_slashed); + }, Err(_) => (), } } @@ -2029,6 +2064,7 @@ pub mod pallet { // `actual <= to_change` and `to_change <= amount`; qed; reserves[index].amount -= actual; + Self::deposit_event(Event::Slashed(who.clone(), actual)); (imb, value - actual) }, Err(_) => (NegativeImbalance::zero(), value), diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 6ba7fe5b5e..47834a6302 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -774,14 +774,14 @@ macro_rules! decl_tests { assert_ok!(Ring::reserve(&1, 10)); System::assert_last_event( - Event::Ring(darwinia_balances::Event::Reserved(1, 10)), + Event::Ring(crate::Event::Reserved(1, 10)), ); System::set_block_number(3); assert!(Ring::unreserve(&1, 5).is_zero()); System::assert_last_event( - Event::Ring(darwinia_balances::Event::Unreserved(1, 5)), + Event::Ring(crate::Event::Unreserved(1, 5)), ); System::set_block_number(4); @@ -789,7 +789,7 @@ macro_rules! decl_tests { // should only unreserve 5 System::assert_last_event( - Event::Ring(darwinia_balances::Event::Unreserved(1, 5)), + Event::Ring(crate::Event::Unreserved(1, 5)), ); }); } @@ -806,8 +806,8 @@ macro_rules! decl_tests { events(), [ Event::System(frame_system::Event::NewAccount(1)), - Event::Ring(darwinia_balances::Event::Endowed(1, 100)), - Event::Ring(darwinia_balances::Event::BalanceSet(1, 100, 0)), + Event::Ring(crate::Event::Endowed(1, 100)), + Event::Ring(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -818,7 +818,8 @@ macro_rules! decl_tests { events(), [ Event::System(frame_system::Event::KilledAccount(1)), - Event::Ring(darwinia_balances::Event::DustLost(1, 99)) + Event::Ring(crate::Event::DustLost(1, 99)), + Event::Ring(crate::Event::Slashed(1, 1)) ] ); }); @@ -836,8 +837,8 @@ macro_rules! decl_tests { events(), [ Event::System(frame_system::Event::NewAccount(1)), - Event::Ring(darwinia_balances::Event::Endowed(1, 100)), - Event::Ring(darwinia_balances::Event::BalanceSet(1, 100, 0)), + Event::Ring(crate::Event::Endowed(1, 100)), + Event::Ring(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -847,7 +848,8 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::System(frame_system::Event::KilledAccount(1)) + Event::System(frame_system::Event::KilledAccount(1)), + Event::Ring(crate::Event::Slashed(1, 100)) ] ); }); diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 19a5c4c3d1..0a4d77169c 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -208,8 +208,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::System(frame_system::Event::NewAccount(1)), - Event::Ring(darwinia_balances::Event::Endowed(1, 100)), - Event::Ring(darwinia_balances::Event::BalanceSet(1, 100, 0)), + Event::Ring(crate::Event::Endowed(1, 100)), + Event::Ring(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -217,7 +217,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!(res, (NegativeImbalance::new(98), 0)); // no events - assert_eq!(events(), []); + assert_eq!(events(), [Event::Ring(crate::Event::Slashed(1, 98))]); let res = Ring::slash(&1, 1); assert_eq!(res, (NegativeImbalance::new(1), 0)); @@ -226,7 +226,8 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { events(), [ Event::System(frame_system::Event::KilledAccount(1)), - Event::Ring(darwinia_balances::Event::DustLost(1, 1)) + Event::Ring(crate::Event::DustLost(1, 1)), + Event::Ring(crate::Event::Slashed(1, 1)), ] ); }); @@ -241,8 +242,8 @@ fn dust_collector_should_work() { events(), [ Event::System(frame_system::Event::NewAccount(1)), - Event::Ring(darwinia_balances::Event::Endowed(1, 100)), - Event::Ring(darwinia_balances::Event::BalanceSet(1, 100, 0)), + Event::Ring(crate::Event::Endowed(1, 100)), + Event::Ring(crate::Event::BalanceSet(1, 100, 0)), ] ); @@ -252,7 +253,8 @@ fn dust_collector_should_work() { events(), [ Event::System(frame_system::Event::KilledAccount(1)), - Event::Ring(darwinia_balances::Event::DustLost(1, 99)) + Event::Ring(crate::Event::DustLost(1, 99)), + Event::Ring(crate::Event::Slashed(1, 1)), ] ); @@ -265,16 +267,16 @@ fn dust_collector_should_work() { events(), [ Event::System(frame_system::Event::NewAccount(1)), - Event::Ring(darwinia_balances::Event::Endowed(1, 100)), - Event::Ring(darwinia_balances::Event::BalanceSet(1, 100, 0)), - Event::Kton(darwinia_balances::Event::Endowed(1, 100)), - Event::Kton(darwinia_balances::Event::BalanceSet(1, 100, 0)), + Event::Ring(crate::Event::Endowed(1, 100)), + Event::Ring(crate::Event::BalanceSet(1, 100, 0)), + Event::Kton(crate::Event::Endowed(1, 100)), + Event::Kton(crate::Event::BalanceSet(1, 100, 0)), ] ); let _ = Ring::slash(&1, 1); - assert_eq!(events(), []); + assert_eq!(events(), [Event::Ring(crate::Event::Slashed(1, 1))]); let _ = Kton::slash(&1, 1); @@ -282,8 +284,9 @@ fn dust_collector_should_work() { events(), [ Event::System(frame_system::Event::KilledAccount(1)), - Event::Ring(darwinia_balances::Event::DustLost(1, 99)), - Event::Kton(darwinia_balances::Event::DustLost(1, 99)), + Event::Ring(crate::Event::DustLost(1, 99)), + Event::Kton(crate::Event::DustLost(1, 99)), + Event::Kton(crate::Event::Slashed(1, 1)) ] ); }); diff --git a/frame/balances/src/tests_reentrancy.rs b/frame/balances/src/tests_reentrancy.rs index c5ae7487b4..94191a176a 100644 --- a/frame/balances/src/tests_reentrancy.rs +++ b/frame/balances/src/tests_reentrancy.rs @@ -181,11 +181,11 @@ fn transfer_dust_removal_tst1_should_work() { assert_eq!(Ring::free_balance(&1), 1050); // Verify the events - // Number of events expected is 8 - assert_eq!(System::events().len(), 11); + assert_eq!(System::events().len(), 12); System::assert_has_event(Event::Ring(crate::Event::Transfer(2, 3, 450))); System::assert_has_event(Event::Ring(crate::Event::DustLost(2, 50))); + System::assert_has_event(Event::Ring(crate::Event::Deposit(1, 50))); }); } @@ -209,11 +209,11 @@ fn transfer_dust_removal_tst2_should_work() { assert_eq!(Ring::free_balance(&1), 1500); // Verify the events - // Number of events expected is 8 - assert_eq!(System::events().len(), 9); + assert_eq!(System::events().len(), 10); System::assert_has_event(Event::Ring(crate::Event::Transfer(2, 1, 450))); System::assert_has_event(Event::Ring(crate::Event::DustLost(2, 50))); + System::assert_has_event(Event::Ring(crate::Event::Deposit(1, 50))); }); } @@ -246,8 +246,7 @@ fn repatriating_reserved_balance_dust_removal_should_work() { assert_eq!(Ring::free_balance(1), 1500); // Verify the events - // Number of events expected is 10 - assert_eq!(System::events().len(), 10); + assert_eq!(System::events().len(), 11); System::assert_has_event(Event::Ring(crate::Event::ReserveRepatriated( 2, @@ -255,6 +254,7 @@ fn repatriating_reserved_balance_dust_removal_should_work() { 450, BalanceStatus::Free, ))); - System::assert_last_event(Event::Ring(crate::Event::DustLost(2, 50))); + System::assert_has_event(Event::Ring(crate::Event::DustLost(2, 50))); + System::assert_last_event(Event::Ring(crate::Event::Deposit(1, 50))); }); } diff --git a/node/runtime/pangolin/src/pallets/session.rs b/node/runtime/pangolin/src/pallets/session.rs index af81fd5c0d..c05a07ad32 100644 --- a/node/runtime/pangolin/src/pallets/session.rs +++ b/node/runtime/pangolin/src/pallets/session.rs @@ -1,6 +1,6 @@ // --- paritytech --- use pallet_session::{historical::NoteHistoricalRoot, Config}; -use sp_runtime::{traits::OpaqueKeys, }; +use sp_runtime::traits::OpaqueKeys; use sp_std::prelude::*; // --- darwinia-network --- use crate::*; diff --git a/node/runtime/pangolin/src/pallets/staking.rs b/node/runtime/pangolin/src/pallets/staking.rs index 6baf0626b9..4f2e0f3473 100644 --- a/node/runtime/pangolin/src/pallets/staking.rs +++ b/node/runtime/pangolin/src/pallets/staking.rs @@ -1,8 +1,8 @@ // --- paritytech --- use frame_support::PalletId; use sp_npos_elections::NposSolution; -use sp_staking::SessionIndex; use sp_runtime::Perbill; +use sp_staking::SessionIndex; // --- darwinia-network --- use crate::*; use darwinia_staking::{Config, EraIndex, UseNominatorsMap}; @@ -35,8 +35,8 @@ impl Config for Runtime { // send the slashed funds to the treasury. type KtonSlash = KtonTreasury; type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; - type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type NextNewSession = Session; + type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type PalletId = StakingPalletId; type RingCurrency = Ring; // rewards are minted from the void diff --git a/node/runtime/pangoro/src/pallets/staking.rs b/node/runtime/pangoro/src/pallets/staking.rs index cf6b8ba713..11b44d730a 100644 --- a/node/runtime/pangoro/src/pallets/staking.rs +++ b/node/runtime/pangoro/src/pallets/staking.rs @@ -1,8 +1,8 @@ // --- paritytech --- use frame_support::PalletId; use sp_npos_elections::NposSolution; -use sp_staking::SessionIndex; use sp_runtime::Perbill; +use sp_staking::SessionIndex; // --- darwinia-network --- use crate::*; use darwinia_staking::{Config, EraIndex, UseNominatorsMap}; @@ -36,8 +36,8 @@ impl Config for Runtime { // send the slashed funds to the treasury. type KtonSlash = (); type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator; - type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type NextNewSession = Session; + type OffendingValidatorsThreshold = OffendingValidatorsThreshold; type PalletId = StakingPalletId; type RingCurrency = Ring; // rewards are minted from the void