From 84e91c12f2bde3efb91f6606d08644ee503bfec9 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Sat, 25 Sep 2021 17:15:54 -0400 Subject: [PATCH] Fix amount emitted in rebonded event --- frame/staking/src/lib.rs | 6 ++-- frame/staking/src/pallet/mod.rs | 4 +-- frame/staking/src/tests.rs | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 136515a5d6168..d8e72e267ea9a 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -478,7 +478,9 @@ impl } /// 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 } } - self + (self, unlocking_balance) } } diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index c71130a3492b1..dad958ccaea2f 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -1348,11 +1348,11 @@ pub mod pallet { ensure!(!ledger.unlocking.is_empty(), Error::::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::::InsufficientBond); - Self::deposit_event(Event::::Bonded(ledger.stash.clone(), value)); + Self::deposit_event(Event::::Bonded(ledger.stash.clone(), rebonded_value)); // NOTE: ledger must be updated prior to calling `Self::weight_of`. Self::update_ledger(&controller, &ledger); diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 5e7fe3d6266aa..38b760896d7d8 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -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()