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

Commit

Permalink
Fix amount emitted in rebonded event (#9866)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi authored Sep 26, 2021
1 parent 45f8ec6 commit bba169c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
6 changes: 4 additions & 2 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,9 @@ impl<AccountId, Balance: HasCompact + Copy + Saturating + AtLeast32BitUnsigned>
}

/// Re-bond funds that were scheduled for unlocking.
fn rebond(mut self, value: Balance) -> Self {
///
/// Returns the updated ledger, and the amount actually rebonded.
fn rebond(mut self, value: Balance) -> (Self, Balance) {
let mut unlocking_balance: Balance = Zero::zero();

while let Some(last) = self.unlocking.last_mut() {
Expand All @@ -499,7 +501,7 @@ impl<AccountId, Balance: HasCompact + Copy + Saturating + AtLeast32BitUnsigned>
}
}

self
(self, unlocking_balance)
}
}

Expand Down
4 changes: 2 additions & 2 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,11 +1348,11 @@ pub mod pallet {
ensure!(!ledger.unlocking.is_empty(), Error::<T>::NoUnlockChunk);

let initial_unlocking = ledger.unlocking.len() as u32;
let ledger = ledger.rebond(value);
let (ledger, rebonded_value) = ledger.rebond(value);
// Last check: the new active amount of ledger must be more than ED.
ensure!(ledger.active >= T::Currency::minimum_balance(), Error::<T>::InsufficientBond);

Self::deposit_event(Event::<T>::Bonded(ledger.stash.clone(), value));
Self::deposit_event(Event::<T>::Bonded(ledger.stash.clone(), rebonded_value));

// NOTE: ledger must be updated prior to calling `Self::weight_of`.
Self::update_ledger(&controller, &ledger);
Expand Down
59 changes: 59 additions & 0 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,65 @@ fn rebond_is_fifo() {
})
}

#[test]
fn rebond_emits_right_value_in_event() {
// When a user calls rebond with more than can be rebonded, things succeed,
// and the rebond event emits the actual value rebonded.
ExtBuilder::default().nominate(false).build_and_execute(|| {
// Set payee to controller. avoids confusion
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));

// Give account 11 some large free balance greater than total
let _ = Balances::make_free_balance_be(&11, 1000000);

// confirm that 10 is a normal validator and gets paid at the end of the era.
mock::start_active_era(1);

// Unbond almost all of the funds in stash.
Staking::unbond(Origin::signed(10), 900).unwrap();
assert_eq!(
Staking::ledger(&10),
Some(StakingLedger {
stash: 11,
total: 1000,
active: 100,
unlocking: vec![UnlockChunk { value: 900, era: 1 + 3 }],
claimed_rewards: vec![],
})
);

// Re-bond less than the total
Staking::rebond(Origin::signed(10), 100).unwrap();
assert_eq!(
Staking::ledger(&10),
Some(StakingLedger {
stash: 11,
total: 1000,
active: 200,
unlocking: vec![UnlockChunk { value: 800, era: 1 + 3 }],
claimed_rewards: vec![],
})
);
// Event emitted should be correct
assert_eq!(*staking_events().last().unwrap(), Event::Bonded(11, 100));

// Re-bond way more than available
Staking::rebond(Origin::signed(10), 100_000).unwrap();
assert_eq!(
Staking::ledger(&10),
Some(StakingLedger {
stash: 11,
total: 1000,
active: 1000,
unlocking: vec![],
claimed_rewards: vec![],
})
);
// Event emitted should be correct, only 800
assert_eq!(*staking_events().last().unwrap(), Event::Bonded(11, 800));
});
}

#[test]
fn reward_to_stake_works() {
ExtBuilder::default()
Expand Down

0 comments on commit bba169c

Please sign in to comment.