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

Commit

Permalink
Fix staking rebond weight refund (#9508)
Browse files Browse the repository at this point in the history
* Fix staking `rebond` weight refund


Comment

* use safe arithmetic

* comment
  • Loading branch information
emostov committed Aug 9, 2021
1 parent d7babec commit 91061a7
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use frame_support::{
Currency, CurrencyToVote, EnsureOrigin, EstimateNextNewSession, Get, LockIdentifier,
LockableCurrency, OnUnbalanced, UnixTime,
},
weights::{
constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS},
Weight,
},
weights::Weight,
};
use frame_system::{ensure_root, ensure_signed, offchain::SendTransactionTypes, pallet_prelude::*};
use sp_runtime::{
Expand Down Expand Up @@ -1311,18 +1308,18 @@ pub mod pallet {
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
ensure!(!ledger.unlocking.is_empty(), Error::<T>::NoUnlockChunk);

let initial_unlocking = ledger.unlocking.len() as u32;
let ledger = 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::update_ledger(&controller, &ledger);
Ok(Some(
35 * WEIGHT_PER_MICROS +
50 * WEIGHT_PER_NANOS * (ledger.unlocking.len() as Weight) +
T::DbWeight::get().reads_writes(3, 2),
)
.into())

let removed_chunks = 1u32 // for the case where the last iterated chunk is not removed
.saturating_add(initial_unlocking)
.saturating_sub(ledger.unlocking.len() as u32);
Ok(Some(T::WeightInfo::rebond(removed_chunks)).into())
}

/// Set `HistoryDepth` value. This function will delete any history information
Expand Down

0 comments on commit 91061a7

Please sign in to comment.