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

Staking: Add dest to Rewarded to aid in reward calculations #1602

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 15 additions & 8 deletions substrate/frame/staking/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@ impl<T: Config> Pallet<T> {

let mut total_imbalance = PositiveImbalanceOf::<T>::zero();
// We can now make total validator payout:
if let Some(imbalance) =
if let Some((imbalance, dest)) =
Self::make_payout(&ledger.stash, validator_staking_payout + validator_commission_payout)
{
Self::deposit_event(Event::<T>::Rewarded {
stash: ledger.stash,
dest,
amount: imbalance.peek(),
});
total_imbalance.subsume(imbalance);
Expand All @@ -259,11 +260,14 @@ impl<T: Config> Pallet<T> {
let nominator_reward: BalanceOf<T> =
nominator_exposure_part * validator_leftover_payout;
// We can now make nominator payout:
if let Some(imbalance) = Self::make_payout(&nominator.who, nominator_reward) {
if let Some((imbalance, dest)) = Self::make_payout(&nominator.who, nominator_reward) {
// Note: this logic does not count payouts for `RewardDestination::None`.
nominator_payout_count += 1;
let e =
Event::<T>::Rewarded { stash: nominator.who.clone(), amount: imbalance.peek() };
let e = Event::<T>::Rewarded {
stash: nominator.who.clone(),
dest,
amount: imbalance.peek(),
};
Self::deposit_event(e);
total_imbalance.subsume(imbalance);
}
Expand Down Expand Up @@ -293,9 +297,11 @@ impl<T: Config> Pallet<T> {

/// Actually make a payment to a staker. This uses the currency's reward function
/// to pay the right payee for the given staker account.
fn make_payout(stash: &T::AccountId, amount: BalanceOf<T>) -> Option<PositiveImbalanceOf<T>> {
let dest = Self::payee(stash);
match dest {
fn make_payout(
stash: &T::AccountId,
amount: BalanceOf<T>,
) -> Option<(PositiveImbalanceOf<T>, RewardDestination<T::AccountId>)> {
let maybe_imbalance = match Self::payee(stash) {
RewardDestination::Controller => Self::bonded(stash)
.map(|controller| T::Currency::deposit_creating(&controller, amount)),
RewardDestination::Stash => T::Currency::deposit_into_existing(stash, amount).ok(),
Expand All @@ -311,7 +317,8 @@ impl<T: Config> Pallet<T> {
RewardDestination::Account(dest_account) =>
Some(T::Currency::deposit_creating(&dest_account, amount)),
RewardDestination::None => None,
}
};
maybe_imbalance.map(|imbalance| (imbalance, Self::payee(stash)))
}

/// Plan a new session potentially trigger a new era.
Expand Down
8 changes: 6 additions & 2 deletions substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,12 @@ pub mod pallet {
/// The era payout has been set; the first balance is the validator-payout; the second is
/// the remainder from the maximum amount of reward.
EraPaid { era_index: EraIndex, validator_payout: BalanceOf<T>, remainder: BalanceOf<T> },
/// The nominator has been rewarded by this amount.
Rewarded { stash: T::AccountId, amount: BalanceOf<T> },
/// The nominator has been rewarded by this amount to this destination.
Rewarded {
stash: T::AccountId,
dest: RewardDestination<T::AccountId>,
amount: BalanceOf<T>,
},
/// A staker (validator or nominator) has been slashed by the given amount.
Slashed { staker: T::AccountId, amount: BalanceOf<T> },
/// A slash for the given validator, for the given percentage of their stake, at the given
Expand Down
10 changes: 10 additions & 0 deletions substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3759,6 +3759,16 @@ fn test_payout_stakers() {
);
assert!(RewardOnUnbalanceWasCalled::get());

// `Rewarded` events are being executed.
assert!(matches!(
staking_events_since_last_call().as_slice(),
&[
..,
Event::Rewarded { stash: 1037, dest: RewardDestination::Controller, amount: 108 },
Event::Rewarded { stash: 1036, dest: RewardDestination::Controller, amount: 108 }
]
));

// Top 64 nominators of validator 11 automatically paid out, including the validator
// Validator payout goes to controller.
assert!(Balances::free_balance(&11) > balance);
Expand Down