From 91061a7d925b5bc597804293da283477512ba4ff Mon Sep 17 00:00:00 2001 From: Zeke Mostov <32168567+emostov@users.noreply.github.com> Date: Mon, 9 Aug 2021 04:59:00 -0700 Subject: [PATCH] Fix staking `rebond` weight refund (#9508) * Fix staking `rebond` weight refund Comment * use safe arithmetic * comment --- frame/staking/src/pallet/mod.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/frame/staking/src/pallet/mod.rs b/frame/staking/src/pallet/mod.rs index 444768dbdccfa..8af6204273f9b 100644 --- a/frame/staking/src/pallet/mod.rs +++ b/frame/staking/src/pallet/mod.rs @@ -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::{ @@ -1311,18 +1308,18 @@ pub mod pallet { let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; ensure!(!ledger.unlocking.is_empty(), Error::::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::::InsufficientBond); Self::deposit_event(Event::::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