Fix amount emitted in rebonded event (#9866)

This commit is contained in:
Shawn Tabrizi
2021-09-26 02:26:57 -04:00
committed by GitHub
parent cf19f1f442
commit d342d282a6
3 changed files with 65 additions and 4 deletions
+4 -2
View File
@@ -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() {
@@ -499,7 +501,7 @@ impl<AccountId, Balance: HasCompact + Copy + Saturating + AtLeast32BitUnsigned>
}
}
self
(self, unlocking_balance)
}
}
+2 -2
View File
@@ -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);
+59
View File
@@ -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()