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

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
aurexav committed Jun 10, 2022
1 parent 4b9e8e4 commit 4330a52
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 40 deletions.
46 changes: 41 additions & 5 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ pub mod pallet {
Ok(())
})?;
<TotalIssuance<T, I>>::mutate(|t| *t += amount);
Self::deposit_event(Event::Deposit(who.clone(), amount));
Ok(())
}

Expand All @@ -272,6 +273,7 @@ pub mod pallet {
},
)?;
<TotalIssuance<T, I>>::mutate(|t| *t -= actual);
Self::deposit_event(Event::Withdraw(who.clone(), amount));
Ok(actual)
}
}
Expand Down Expand Up @@ -339,7 +341,14 @@ pub mod pallet {

impl<T: Config<I>, I: 'static> Unbalanced<T::AccountId> for Pallet<T, I> {
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(())
}

Expand Down Expand Up @@ -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\]
Expand All @@ -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]
Expand Down Expand Up @@ -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(_) => (),
}
}
Expand Down Expand Up @@ -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))
},
)
Expand Down Expand Up @@ -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))
},
)
Expand Down Expand Up @@ -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))
},
)
Expand Down Expand Up @@ -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)
},
)
Expand Down Expand Up @@ -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(_) => (),
}
}
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 11 additions & 9 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,22 +774,22 @@ 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);
assert_eq!(Ring::unreserve(&1, 6), 1);

// should only unreserve 5
System::assert_last_event(
Event::Ring(darwinia_balances::Event::Unreserved(1, 5)),
Event::Ring(crate::Event::Unreserved(1, 5)),
);
});
}
Expand All @@ -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)),
]
);

Expand All @@ -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))
]
);
});
Expand All @@ -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)),
]
);

Expand All @@ -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))
]
);
});
Expand Down
31 changes: 17 additions & 14 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ 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)),
]
);

let res = Ring::slash(&1, 98);
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));
Expand All @@ -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)),
]
);
});
Expand All @@ -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)),
]
);

Expand All @@ -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)),
]
);

Expand All @@ -265,25 +267,26 @@ 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);

assert_eq!(
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))
]
);
});
Expand Down
14 changes: 7 additions & 7 deletions frame/balances/src/tests_reentrancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
});
}

Expand All @@ -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)));
});
}

Expand Down Expand Up @@ -246,15 +246,15 @@ 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,
1,
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)));
});
}
2 changes: 1 addition & 1 deletion node/runtime/pangolin/src/pallets/session.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
4 changes: 2 additions & 2 deletions node/runtime/pangolin/src/pallets/staking.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions node/runtime/pangoro/src/pallets/staking.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4330a52

Please sign in to comment.