pallet_staking_async/pallet/
mod.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! `pallet-staking-async`'s main `pallet` module.
19
20use crate::{
21	asset, slashing, weights::WeightInfo, AccountIdLookupOf, ActiveEraInfo, BalanceOf, EraPayout,
22	EraRewardPoints, ExposurePage, Forcing, LedgerIntegrityState, MaxNominationsOf,
23	NegativeImbalanceOf, Nominations, NominationsQuota, PositiveImbalanceOf, RewardDestination,
24	StakingLedger, UnappliedSlash, UnlockChunk, ValidatorPrefs,
25};
26use alloc::{format, vec::Vec};
27use codec::Codec;
28use frame_election_provider_support::{ElectionProvider, SortedListProvider, VoteWeight};
29use frame_support::{
30	assert_ok,
31	pallet_prelude::*,
32	traits::{
33		fungible::{
34			hold::{Balanced as FunHoldBalanced, Mutate as FunHoldMutate},
35			Mutate, Mutate as FunMutate,
36		},
37		Contains, Defensive, DefensiveSaturating, EnsureOrigin, Get, InspectLockableCurrency,
38		Nothing, OnUnbalanced,
39	},
40	weights::Weight,
41	BoundedBTreeSet, BoundedVec,
42};
43use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
44pub use impls::*;
45use rand::seq::SliceRandom;
46use rand_chacha::{
47	rand_core::{RngCore, SeedableRng},
48	ChaChaRng,
49};
50use sp_core::{sr25519::Pair as SrPair, Pair};
51use sp_runtime::{
52	traits::{StaticLookup, Zero},
53	ArithmeticError, Perbill, Percent,
54};
55use sp_staking::{
56	EraIndex, Page, SessionIndex,
57	StakingAccount::{self, Controller, Stash},
58	StakingInterface,
59};
60
61mod impls;
62
63#[frame_support::pallet]
64pub mod pallet {
65	use core::ops::Deref;
66
67	use super::*;
68	use crate::{session_rotation, PagedExposureMetadata, SnapshotStatus};
69	use codec::HasCompact;
70	use frame_election_provider_support::{ElectionDataProvider, PageIndex};
71	use frame_support::DefaultNoBound;
72
73	/// The in-code storage version.
74	const STORAGE_VERSION: StorageVersion = StorageVersion::new(17);
75
76	#[pallet::pallet]
77	#[pallet::storage_version(STORAGE_VERSION)]
78	pub struct Pallet<T>(_);
79
80	/// Possible operations on the configuration values of this pallet.
81	#[derive(TypeInfo, Debug, Clone, Encode, Decode, DecodeWithMemTracking, PartialEq)]
82	pub enum ConfigOp<T: Default + Codec> {
83		/// Don't change.
84		Noop,
85		/// Set the given value.
86		Set(T),
87		/// Remove from storage.
88		Remove,
89	}
90
91	#[pallet::config(with_default)]
92	pub trait Config: frame_system::Config {
93		/// The old trait for staking balance. Deprecated and only used for migrating old ledgers.
94		#[pallet::no_default]
95		type OldCurrency: InspectLockableCurrency<
96			Self::AccountId,
97			Moment = BlockNumberFor<Self>,
98			Balance = Self::CurrencyBalance,
99		>;
100
101		/// The staking balance.
102		#[pallet::no_default]
103		type Currency: FunHoldMutate<
104				Self::AccountId,
105				Reason = Self::RuntimeHoldReason,
106				Balance = Self::CurrencyBalance,
107			> + FunMutate<Self::AccountId, Balance = Self::CurrencyBalance>
108			+ FunHoldBalanced<Self::AccountId, Balance = Self::CurrencyBalance>;
109
110		/// Overarching hold reason.
111		#[pallet::no_default_bounds]
112		type RuntimeHoldReason: From<HoldReason>;
113
114		/// Just the `Currency::Balance` type; we have this item to allow us to constrain it to
115		/// `From<u64>`.
116		type CurrencyBalance: sp_runtime::traits::AtLeast32BitUnsigned
117			+ codec::FullCodec
118			+ DecodeWithMemTracking
119			+ HasCompact<Type: DecodeWithMemTracking>
120			+ Copy
121			+ MaybeSerializeDeserialize
122			+ core::fmt::Debug
123			+ Default
124			+ From<u64>
125			+ TypeInfo
126			+ Send
127			+ Sync
128			+ MaxEncodedLen;
129
130		/// Convert a balance into a number used for election calculation. This must fit into a
131		/// `u64` but is allowed to be sensibly lossy. The `u64` is used to communicate with the
132		/// [`frame_election_provider_support`] crate which accepts u64 numbers and does operations
133		/// in 128.
134		/// Consequently, the backward convert is used convert the u128s from sp-elections back to a
135		/// [`BalanceOf`].
136		#[pallet::no_default_bounds]
137		type CurrencyToVote: sp_staking::currency_to_vote::CurrencyToVote<BalanceOf<Self>>;
138
139		/// Something that provides the election functionality.
140		#[pallet::no_default]
141		type ElectionProvider: ElectionProvider<
142			AccountId = Self::AccountId,
143			BlockNumber = BlockNumberFor<Self>,
144			// we only accept an election provider that has staking as data provider.
145			DataProvider = Pallet<Self>,
146		>;
147
148		/// Something that defines the maximum number of nominations per nominator.
149		#[pallet::no_default_bounds]
150		type NominationsQuota: NominationsQuota<BalanceOf<Self>>;
151
152		/// Number of eras to keep in history.
153		///
154		/// Following information is kept for eras in `[current_era -
155		/// HistoryDepth, current_era]`: `ErasValidatorPrefs`, `ErasValidatorReward`,
156		/// `ErasRewardPoints`, `ErasTotalStake`, `ClaimedRewards`,
157		/// `ErasStakersPaged`, `ErasStakersOverview`.
158		///
159		/// Must be more than the number of eras delayed by session.
160		/// I.e. active era must always be in history. I.e. `active_era >
161		/// current_era - history_depth` must be guaranteed.
162		///
163		/// If migrating an existing pallet from storage value to config value,
164		/// this should be set to same value or greater as in storage.
165		#[pallet::constant]
166		type HistoryDepth: Get<u32>;
167
168		/// Tokens have been minted and are unused for validator-reward.
169		/// See [Era payout](./index.html#era-payout).
170		#[pallet::no_default_bounds]
171		type RewardRemainder: OnUnbalanced<NegativeImbalanceOf<Self>>;
172
173		/// Handler for the unbalanced reduction when slashing a staker.
174		#[pallet::no_default_bounds]
175		type Slash: OnUnbalanced<NegativeImbalanceOf<Self>>;
176
177		/// Handler for the unbalanced increment when rewarding a staker.
178		/// NOTE: in most cases, the implementation of `OnUnbalanced` should modify the total
179		/// issuance.
180		#[pallet::no_default_bounds]
181		type Reward: OnUnbalanced<PositiveImbalanceOf<Self>>;
182
183		/// Number of sessions per era, as per the preferences of the **relay chain**.
184		#[pallet::constant]
185		type SessionsPerEra: Get<SessionIndex>;
186
187		/// Number of sessions before the end of an era when the election for the next era will
188		/// start.
189		///
190		/// - This determines how many sessions **before** the last session of the era the staking
191		///   election process should begin.
192		/// - The value is bounded between **1** (election starts at the beginning of the last
193		///   session) and `SessionsPerEra` (election starts at the beginning of the first session
194		///   of the era).
195		///
196		/// ### Example:
197		/// - If `SessionsPerEra = 6` and `PlanningEraOffset = 1`, the election starts at the
198		///   beginning of session `6 - 1 = 5`.
199		/// - If `PlanningEraOffset = 6`, the election starts at the beginning of session `6 - 6 =
200		///   0`, meaning it starts at the very beginning of the era.
201		#[pallet::constant]
202		type PlanningEraOffset: Get<SessionIndex>;
203
204		/// Number of eras that staked funds must remain bonded for.
205		#[pallet::constant]
206		type BondingDuration: Get<EraIndex>;
207
208		/// Number of eras that slashes are deferred by, after computation.
209		///
210		/// This should be less than the bonding duration. Set to 0 if slashes
211		/// should be applied immediately, without opportunity for intervention.
212		#[pallet::constant]
213		type SlashDeferDuration: Get<EraIndex>;
214
215		/// The origin which can manage less critical staking parameters that does not require root.
216		///
217		/// Supported actions: (1) cancel deferred slash, (2) set minimum commission.
218		#[pallet::no_default]
219		type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
220
221		/// The payout for validators and the system for the current era.
222		/// See [Era payout](./index.html#era-payout).
223		#[pallet::no_default]
224		type EraPayout: EraPayout<BalanceOf<Self>>;
225
226		/// The maximum size of each `T::ExposurePage`.
227		///
228		/// An `ExposurePage` is weakly bounded to a maximum of `MaxExposurePageSize`
229		/// nominators.
230		///
231		/// For older non-paged exposure, a reward payout was restricted to the top
232		/// `MaxExposurePageSize` nominators. This is to limit the i/o cost for the
233		/// nominator payout.
234		///
235		/// Note: `MaxExposurePageSize` is used to bound `ClaimedRewards` and is unsafe to
236		/// reduce without handling it in a migration.
237		#[pallet::constant]
238		type MaxExposurePageSize: Get<u32>;
239
240		/// The absolute maximum of winner validators this pallet should return.
241		///
242		/// As this pallet supports multi-block election, the set of winner validators *per
243		/// election* is bounded by this type.
244		#[pallet::constant]
245		type MaxValidatorSet: Get<u32>;
246
247		/// Something that provides a best-effort sorted list of voters aka electing nominators,
248		/// used for NPoS election.
249		///
250		/// The changes to nominators are reported to this. Moreover, each validator's self-vote is
251		/// also reported as one independent vote.
252		///
253		/// To keep the load off the chain as much as possible, changes made to the staked amount
254		/// via rewards and slashes are not reported and thus need to be manually fixed by the
255		/// staker. In case of `bags-list`, this always means using `rebag` and `putInFrontOf`.
256		///
257		/// Invariant: what comes out of this list will always be a nominator.
258		#[pallet::no_default]
259		type VoterList: SortedListProvider<Self::AccountId, Score = VoteWeight>;
260
261		/// WIP: This is a noop as of now, the actual business logic that's described below is going
262		/// to be introduced in a follow-up PR.
263		///
264		/// Something that provides a best-effort sorted list of targets aka electable validators,
265		/// used for NPoS election.
266		///
267		/// The changes to the approval stake of each validator are reported to this. This means any
268		/// change to:
269		/// 1. The stake of any validator or nominator.
270		/// 2. The targets of any nominator
271		/// 3. The role of any staker (e.g. validator -> chilled, nominator -> validator, etc)
272		///
273		/// Unlike `VoterList`, the values in this list are always kept up to date with reward and
274		/// slash as well, and thus represent the accurate approval stake of all account being
275		/// nominated by nominators.
276		///
277		/// Note that while at the time of nomination, all targets are checked to be real
278		/// validators, they can chill at any point, and their approval stakes will still be
279		/// recorded. This implies that what comes out of iterating this list MIGHT NOT BE AN ACTIVE
280		/// VALIDATOR.
281		#[pallet::no_default]
282		type TargetList: SortedListProvider<Self::AccountId, Score = BalanceOf<Self>>;
283
284		/// The maximum number of `unlocking` chunks a [`StakingLedger`] can
285		/// have. Effectively determines how many unique eras a staker may be
286		/// unbonding in.
287		///
288		/// Note: `MaxUnlockingChunks` is used as the upper bound for the
289		/// `BoundedVec` item `StakingLedger.unlocking`. Setting this value
290		/// lower than the existing value can lead to inconsistencies in the
291		/// `StakingLedger` and will need to be handled properly in a runtime
292		/// migration. The test `reducing_max_unlocking_chunks_abrupt` shows
293		/// this effect.
294		#[pallet::constant]
295		type MaxUnlockingChunks: Get<u32>;
296
297		/// The maximum amount of controller accounts that can be deprecated in one call.
298		type MaxControllersInDeprecationBatch: Get<u32>;
299
300		/// Something that listens to staking updates and performs actions based on the data it
301		/// receives.
302		///
303		/// WARNING: this only reports slashing and withdraw events for the time being.
304		#[pallet::no_default_bounds]
305		type EventListeners: sp_staking::OnStakingUpdate<Self::AccountId, BalanceOf<Self>>;
306
307		/// Maximum number of invulnerable validators.
308		#[pallet::constant]
309		type MaxInvulnerables: Get<u32>;
310
311		/// Maximum number of disabled validators.
312		#[pallet::constant]
313		type MaxDisabledValidators: Get<u32>;
314
315		/// Maximum allowed era duration in milliseconds.
316		///
317		/// This provides a defensive upper bound to cap the effective era duration, preventing
318		/// excessively long eras from causing runaway inflation (e.g., due to bugs). If the actual
319		/// era duration exceeds this value, it will be clamped to this maximum.
320		///
321		/// Example: For an ideal era duration of 24 hours (86,400,000 ms),
322		/// this can be set to 604,800,000 ms (7 days).
323		#[pallet::constant]
324		type MaxEraDuration: Get<u64>;
325
326		/// Interface to talk to the RC-Client pallet, possibly sending election results to the
327		/// relay chain.
328		#[pallet::no_default]
329		type RcClientInterface: pallet_staking_async_rc_client::RcClientInterface<
330			AccountId = Self::AccountId,
331		>;
332
333		#[pallet::no_default_bounds]
334		/// Filter some accounts from participating in staking.
335		///
336		/// This is useful for example to blacklist an account that is participating in staking in
337		/// another way (such as pools).
338		type Filter: Contains<Self::AccountId>;
339
340		/// Weight information for extrinsics in this pallet.
341		type WeightInfo: WeightInfo;
342	}
343
344	/// A reason for placing a hold on funds.
345	#[pallet::composite_enum]
346	pub enum HoldReason {
347		/// Funds on stake by a nominator or a validator.
348		#[codec(index = 0)]
349		Staking,
350	}
351
352	/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
353	pub mod config_preludes {
354		use super::*;
355		use frame_support::{derive_impl, parameter_types, traits::ConstU32};
356		pub struct TestDefaultConfig;
357
358		#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
359		impl frame_system::DefaultConfig for TestDefaultConfig {}
360
361		parameter_types! {
362			pub const SessionsPerEra: SessionIndex = 3;
363			pub const BondingDuration: EraIndex = 3;
364		}
365
366		#[frame_support::register_default_impl(TestDefaultConfig)]
367		impl DefaultConfig for TestDefaultConfig {
368			#[inject_runtime_type]
369			type RuntimeHoldReason = ();
370			type CurrencyBalance = u128;
371			type CurrencyToVote = ();
372			type NominationsQuota = crate::FixedNominationsQuota<16>;
373			type HistoryDepth = ConstU32<84>;
374			type RewardRemainder = ();
375			type Slash = ();
376			type Reward = ();
377			type SessionsPerEra = SessionsPerEra;
378			type BondingDuration = BondingDuration;
379			type PlanningEraOffset = ConstU32<1>;
380			type SlashDeferDuration = ();
381			type MaxExposurePageSize = ConstU32<64>;
382			type MaxUnlockingChunks = ConstU32<32>;
383			type MaxValidatorSet = ConstU32<100>;
384			type MaxControllersInDeprecationBatch = ConstU32<100>;
385			type MaxInvulnerables = ConstU32<20>;
386			type MaxDisabledValidators = ConstU32<100>;
387			type MaxEraDuration = ();
388			type EventListeners = ();
389			type Filter = Nothing;
390			type WeightInfo = ();
391		}
392	}
393
394	/// The ideal number of active validators.
395	#[pallet::storage]
396	pub type ValidatorCount<T> = StorageValue<_, u32, ValueQuery>;
397
398	/// Any validators that may never be slashed or forcibly kicked. It's a Vec since they're
399	/// easy to initialize and the performance hit is minimal (we expect no more than four
400	/// invulnerables) and restricted to testnets.
401	#[pallet::storage]
402	pub type Invulnerables<T: Config> =
403		StorageValue<_, BoundedVec<T::AccountId, T::MaxInvulnerables>, ValueQuery>;
404
405	/// Map from all locked "stash" accounts to the controller account.
406	///
407	/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
408	#[pallet::storage]
409	pub type Bonded<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, T::AccountId>;
410
411	/// The minimum active bond to become and maintain the role of a nominator.
412	#[pallet::storage]
413	pub type MinNominatorBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
414
415	/// The minimum active bond to become and maintain the role of a validator.
416	#[pallet::storage]
417	pub type MinValidatorBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
418
419	/// The minimum active nominator stake of the last successful election.
420	#[pallet::storage]
421	pub type MinimumActiveStake<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;
422
423	/// The minimum amount of commission that validators can set.
424	///
425	/// If set to `0`, no limit exists.
426	#[pallet::storage]
427	pub type MinCommission<T: Config> = StorageValue<_, Perbill, ValueQuery>;
428
429	/// Map from all (unlocked) "controller" accounts to the info regarding the staking.
430	///
431	/// Note: All the reads and mutations to this storage *MUST* be done through the methods exposed
432	/// by [`StakingLedger`] to ensure data and lock consistency.
433	#[pallet::storage]
434	pub type Ledger<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, StakingLedger<T>>;
435
436	/// Where the reward payment should be made. Keyed by stash.
437	///
438	/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
439	#[pallet::storage]
440	pub type Payee<T: Config> =
441		StorageMap<_, Twox64Concat, T::AccountId, RewardDestination<T::AccountId>, OptionQuery>;
442
443	/// The map from (wannabe) validator stash key to the preferences of that validator.
444	///
445	/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
446	#[pallet::storage]
447	pub type Validators<T: Config> =
448		CountedStorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>;
449
450	/// The maximum validator count before we stop allowing new validators to join.
451	///
452	/// When this value is not set, no limits are enforced.
453	#[pallet::storage]
454	pub type MaxValidatorsCount<T> = StorageValue<_, u32, OptionQuery>;
455
456	/// The map from nominator stash key to their nomination preferences, namely the validators that
457	/// they wish to support.
458	///
459	/// Note that the keys of this storage map might become non-decodable in case the
460	/// account's [`NominationsQuota::MaxNominations`] configuration is decreased.
461	/// In this rare case, these nominators
462	/// are still existent in storage, their key is correct and retrievable (i.e. `contains_key`
463	/// indicates that they exist), but their value cannot be decoded. Therefore, the non-decodable
464	/// nominators will effectively not-exist, until they re-submit their preferences such that it
465	/// is within the bounds of the newly set `Config::MaxNominations`.
466	///
467	/// This implies that `::iter_keys().count()` and `::iter().count()` might return different
468	/// values for this map. Moreover, the main `::count()` is aligned with the former, namely the
469	/// number of keys that exist.
470	///
471	/// Lastly, if any of the nominators become non-decodable, they can be chilled immediately via
472	/// [`Call::chill_other`] dispatchable by anyone.
473	///
474	/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
475	#[pallet::storage]
476	pub type Nominators<T: Config> =
477		CountedStorageMap<_, Twox64Concat, T::AccountId, Nominations<T>>;
478
479	/// Stakers whose funds are managed by other pallets.
480	///
481	/// This pallet does not apply any locks on them, therefore they are only virtually bonded. They
482	/// are expected to be keyless accounts and hence should not be allowed to mutate their ledger
483	/// directly via this pallet. Instead, these accounts are managed by other pallets and accessed
484	/// via low level apis. We keep track of them to do minimal integrity checks.
485	#[pallet::storage]
486	pub type VirtualStakers<T: Config> = CountedStorageMap<_, Twox64Concat, T::AccountId, ()>;
487
488	/// The maximum nominator count before we stop allowing new validators to join.
489	///
490	/// When this value is not set, no limits are enforced.
491	#[pallet::storage]
492	pub type MaxNominatorsCount<T> = StorageValue<_, u32, OptionQuery>;
493
494	// --- AUDIT NOTE: the following storage items should only be controlled by `Rotator`
495
496	/// The current planned era index.
497	///
498	/// This is the latest planned era, depending on how the Session pallet queues the validator
499	/// set, it might be active or not.
500	#[pallet::storage]
501	pub type CurrentEra<T> = StorageValue<_, EraIndex>;
502
503	/// The active era information, it holds index and start.
504	///
505	/// The active era is the era being currently rewarded. Validator set of this era must be
506	/// equal to what is RC's session pallet.
507	#[pallet::storage]
508	pub type ActiveEra<T> = StorageValue<_, ActiveEraInfo>;
509
510	/// Custom bound for [`BondedEras`] which is equal to [`Config::BondingDuration`] + 1.
511	pub struct BondedErasBound<T>(core::marker::PhantomData<T>);
512	impl<T: Config> Get<u32> for BondedErasBound<T> {
513		fn get() -> u32 {
514			T::BondingDuration::get().saturating_add(1)
515		}
516	}
517
518	/// A mapping from still-bonded eras to the first session index of that era.
519	///
520	/// Must contains information for eras for the range:
521	/// `[active_era - bounding_duration; active_era]`
522	#[pallet::storage]
523	pub type BondedEras<T: Config> =
524		StorageValue<_, BoundedVec<(EraIndex, SessionIndex), BondedErasBound<T>>, ValueQuery>;
525
526	// --- AUDIT Note: end of storage items controlled by `Rotator`.
527
528	/// Summary of validator exposure at a given era.
529	///
530	/// This contains the total stake in support of the validator and their own stake. In addition,
531	/// it can also be used to get the number of nominators backing this validator and the number of
532	/// exposure pages they are divided into. The page count is useful to determine the number of
533	/// pages of rewards that needs to be claimed.
534	///
535	/// This is keyed first by the era index to allow bulk deletion and then the stash account.
536	/// Should only be accessed through `Eras`.
537	///
538	/// Is it removed after [`Config::HistoryDepth`] eras.
539	/// If stakers hasn't been set or has been removed then empty overview is returned.
540	#[pallet::storage]
541	pub type ErasStakersOverview<T: Config> = StorageDoubleMap<
542		_,
543		Twox64Concat,
544		EraIndex,
545		Twox64Concat,
546		T::AccountId,
547		PagedExposureMetadata<BalanceOf<T>>,
548		OptionQuery,
549	>;
550
551	/// A bounded wrapper for [`sp_staking::ExposurePage`].
552	///
553	/// It has `Deref` and `DerefMut` impls that map it back [`sp_staking::ExposurePage`] for all
554	/// purposes. This is done in such a way because we prefer to keep the types in [`sp_staking`]
555	/// pure, and not polluted by pallet-specific bounding logic.
556	///
557	/// It encoded and decodes exactly the same as [`sp_staking::ExposurePage`], and provides a
558	/// manual `MaxEncodedLen` implementation, to be used in benchmarking
559	#[derive(PartialEqNoBound, Encode, Decode, DebugNoBound, TypeInfo, DefaultNoBound)]
560	#[scale_info(skip_type_params(T))]
561	pub struct BoundedExposurePage<T: Config>(pub ExposurePage<T::AccountId, BalanceOf<T>>);
562	impl<T: Config> Deref for BoundedExposurePage<T> {
563		type Target = ExposurePage<T::AccountId, BalanceOf<T>>;
564
565		fn deref(&self) -> &Self::Target {
566			&self.0
567		}
568	}
569
570	impl<T: Config> core::ops::DerefMut for BoundedExposurePage<T> {
571		fn deref_mut(&mut self) -> &mut Self::Target {
572			&mut self.0
573		}
574	}
575
576	impl<T: Config> codec::MaxEncodedLen for BoundedExposurePage<T> {
577		fn max_encoded_len() -> usize {
578			let max_exposure_page_size = T::MaxExposurePageSize::get() as usize;
579			let individual_size =
580				T::AccountId::max_encoded_len() + BalanceOf::<T>::max_encoded_len();
581
582			// 1 balance for `total`
583			BalanceOf::<T>::max_encoded_len() +
584			// individual_size multiplied by page size
585				max_exposure_page_size.saturating_mul(individual_size)
586		}
587	}
588
589	impl<T: Config> From<ExposurePage<T::AccountId, BalanceOf<T>>> for BoundedExposurePage<T> {
590		fn from(value: ExposurePage<T::AccountId, BalanceOf<T>>) -> Self {
591			Self(value)
592		}
593	}
594
595	impl<T: Config> From<BoundedExposurePage<T>> for ExposurePage<T::AccountId, BalanceOf<T>> {
596		fn from(value: BoundedExposurePage<T>) -> Self {
597			value.0
598		}
599	}
600
601	impl<T: Config> codec::EncodeLike<BoundedExposurePage<T>>
602		for ExposurePage<T::AccountId, BalanceOf<T>>
603	{
604	}
605
606	/// Paginated exposure of a validator at given era.
607	///
608	/// This is keyed first by the era index to allow bulk deletion, then stash account and finally
609	/// the page. Should only be accessed through `Eras`.
610	///
611	/// This is cleared after [`Config::HistoryDepth`] eras.
612	#[pallet::storage]
613	pub type ErasStakersPaged<T: Config> = StorageNMap<
614		_,
615		(
616			NMapKey<Twox64Concat, EraIndex>,
617			NMapKey<Twox64Concat, T::AccountId>,
618			NMapKey<Twox64Concat, Page>,
619		),
620		BoundedExposurePage<T>,
621		OptionQuery,
622	>;
623
624	pub struct ClaimedRewardsBound<T>(core::marker::PhantomData<T>);
625	impl<T: Config> Get<u32> for ClaimedRewardsBound<T> {
626		fn get() -> u32 {
627			let max_total_nominators_per_validator =
628				<T::ElectionProvider as ElectionProvider>::MaxBackersPerWinner::get();
629			let exposure_page_size = T::MaxExposurePageSize::get();
630			max_total_nominators_per_validator
631				.saturating_div(exposure_page_size)
632				.saturating_add(1)
633		}
634	}
635
636	/// History of claimed paged rewards by era and validator.
637	///
638	/// This is keyed by era and validator stash which maps to the set of page indexes which have
639	/// been claimed.
640	///
641	/// It is removed after [`Config::HistoryDepth`] eras.
642	#[pallet::storage]
643	pub type ClaimedRewards<T: Config> = StorageDoubleMap<
644		_,
645		Twox64Concat,
646		EraIndex,
647		Twox64Concat,
648		T::AccountId,
649		WeakBoundedVec<Page, ClaimedRewardsBound<T>>,
650		ValueQuery,
651	>;
652
653	/// Exposure of validator at era with the preferences of validators.
654	///
655	/// This is keyed first by the era index to allow bulk deletion and then the stash account.
656	///
657	/// Is it removed after [`Config::HistoryDepth`] eras.
658	// If prefs hasn't been set or has been removed then 0 commission is returned.
659	#[pallet::storage]
660	pub type ErasValidatorPrefs<T: Config> = StorageDoubleMap<
661		_,
662		Twox64Concat,
663		EraIndex,
664		Twox64Concat,
665		T::AccountId,
666		ValidatorPrefs,
667		ValueQuery,
668	>;
669
670	/// The total validator era payout for the last [`Config::HistoryDepth`] eras.
671	///
672	/// Eras that haven't finished yet or has been removed doesn't have reward.
673	#[pallet::storage]
674	pub type ErasValidatorReward<T: Config> = StorageMap<_, Twox64Concat, EraIndex, BalanceOf<T>>;
675
676	/// Rewards for the last [`Config::HistoryDepth`] eras.
677	/// If reward hasn't been set or has been removed then 0 reward is returned.
678	#[pallet::storage]
679	pub type ErasRewardPoints<T: Config> =
680		StorageMap<_, Twox64Concat, EraIndex, EraRewardPoints<T>, ValueQuery>;
681
682	/// The total amount staked for the last [`Config::HistoryDepth`] eras.
683	/// If total hasn't been set or has been removed then 0 stake is returned.
684	#[pallet::storage]
685	pub type ErasTotalStake<T: Config> =
686		StorageMap<_, Twox64Concat, EraIndex, BalanceOf<T>, ValueQuery>;
687
688	/// Mode of era forcing.
689	#[pallet::storage]
690	pub type ForceEra<T> = StorageValue<_, Forcing, ValueQuery>;
691
692	/// Maximum staked rewards, i.e. the percentage of the era inflation that
693	/// is used for stake rewards.
694	/// See [Era payout](./index.html#era-payout).
695	#[pallet::storage]
696	pub type MaxStakedRewards<T> = StorageValue<_, Percent, OptionQuery>;
697
698	/// The percentage of the slash that is distributed to reporters.
699	///
700	/// The rest of the slashed value is handled by the `Slash`.
701	#[pallet::storage]
702	pub type SlashRewardFraction<T> = StorageValue<_, Perbill, ValueQuery>;
703
704	/// The amount of currency given to reporters of a slash event which was
705	/// canceled by extraordinary circumstances (e.g. governance).
706	#[pallet::storage]
707	pub type CanceledSlashPayout<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
708
709	/// Stores reported offences in a queue until they are processed in subsequent blocks.
710	///
711	/// Each offence is recorded under the corresponding era index and the offending validator's
712	/// account. If an offence spans multiple pages, only one page is processed at a time. Offences
713	/// are handled sequentially, with their associated slashes computed and stored in
714	/// `UnappliedSlashes`. These slashes are then applied in a future era as determined by
715	/// `SlashDeferDuration`.
716	///
717	/// Any offences tied to an era older than `BondingDuration` are automatically dropped.
718	/// Processing always prioritizes the oldest era first.
719	#[pallet::storage]
720	pub type OffenceQueue<T: Config> = StorageDoubleMap<
721		_,
722		Twox64Concat,
723		EraIndex,
724		Twox64Concat,
725		T::AccountId,
726		slashing::OffenceRecord<T::AccountId>,
727	>;
728
729	/// Tracks the eras that contain offences in `OffenceQueue`, sorted from **earliest to latest**.
730	///
731	/// - This ensures efficient retrieval of the oldest offence without iterating through
732	/// `OffenceQueue`.
733	/// - When a new offence is added to `OffenceQueue`, its era is **inserted in sorted order**
734	/// if not already present.
735	/// - When all offences for an era are processed, it is **removed** from this list.
736	/// - The maximum length of this vector is bounded by `BondingDuration`.
737	///
738	/// This eliminates the need for expensive iteration and sorting when fetching the next offence
739	/// to process.
740	#[pallet::storage]
741	pub type OffenceQueueEras<T: Config> = StorageValue<_, BoundedVec<u32, T::BondingDuration>>;
742
743	/// Tracks the currently processed offence record from the `OffenceQueue`.
744	///
745	/// - When processing offences, an offence record is **popped** from the oldest era in
746	///   `OffenceQueue` and stored here.
747	/// - The function `process_offence` reads from this storage, processing one page of exposure at
748	///   a time.
749	/// - After processing a page, the `exposure_page` count is **decremented** until it reaches
750	///   zero.
751	/// - Once fully processed, the offence record is removed from this storage.
752	///
753	/// This ensures that offences are processed incrementally, preventing excessive computation
754	/// in a single block while maintaining correct slashing behavior.
755	#[pallet::storage]
756	pub type ProcessingOffence<T: Config> =
757		StorageValue<_, (EraIndex, T::AccountId, slashing::OffenceRecord<T::AccountId>)>;
758
759	/// All unapplied slashes that are queued for later.
760	#[pallet::storage]
761	pub type UnappliedSlashes<T: Config> = StorageDoubleMap<
762		_,
763		Twox64Concat,
764		EraIndex,
765		Twox64Concat,
766		// Unique key for unapplied slashes: (validator, slash fraction, page index).
767		(T::AccountId, Perbill, u32),
768		UnappliedSlash<T>,
769		OptionQuery,
770	>;
771
772	/// All slashing events on validators, mapped by era to the highest slash proportion
773	/// and slash value of the era.
774	#[pallet::storage]
775	pub type ValidatorSlashInEra<T: Config> = StorageDoubleMap<
776		_,
777		Twox64Concat,
778		EraIndex,
779		Twox64Concat,
780		T::AccountId,
781		(Perbill, BalanceOf<T>),
782	>;
783
784	/// All slashing events on nominators, mapped by era to the highest slash value of the era.
785	#[pallet::storage]
786	pub type NominatorSlashInEra<T: Config> =
787		StorageDoubleMap<_, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, BalanceOf<T>>;
788
789	/// The threshold for when users can start calling `chill_other` for other validators /
790	/// nominators. The threshold is compared to the actual number of validators / nominators
791	/// (`CountFor*`) in the system compared to the configured max (`Max*Count`).
792	#[pallet::storage]
793	pub type ChillThreshold<T: Config> = StorageValue<_, Percent, OptionQuery>;
794
795	/// Voter snapshot progress status.
796	///
797	/// If the status is `Ongoing`, it keeps a cursor of the last voter retrieved to proceed when
798	/// creating the next snapshot page.
799	#[pallet::storage]
800	pub type VoterSnapshotStatus<T: Config> =
801		StorageValue<_, SnapshotStatus<T::AccountId>, ValueQuery>;
802
803	/// Keeps track of an ongoing multi-page election solution request.
804	///
805	/// If `Some(_)``, it is the next page that we intend to elect. If `None`, we are not in the
806	/// election process.
807	///
808	/// This is only set in multi-block elections. Should always be `None` otherwise.
809	#[pallet::storage]
810	pub type NextElectionPage<T: Config> = StorageValue<_, PageIndex, OptionQuery>;
811
812	/// A bounded list of the "electable" stashes that resulted from a successful election.
813	#[pallet::storage]
814	pub type ElectableStashes<T: Config> =
815		StorageValue<_, BoundedBTreeSet<T::AccountId, T::MaxValidatorSet>, ValueQuery>;
816
817	#[pallet::genesis_config]
818	#[derive(frame_support::DefaultNoBound, frame_support::DebugNoBound)]
819	pub struct GenesisConfig<T: Config> {
820		pub validator_count: u32,
821		pub invulnerables: BoundedVec<T::AccountId, T::MaxInvulnerables>,
822		pub force_era: Forcing,
823		pub slash_reward_fraction: Perbill,
824		pub canceled_payout: BalanceOf<T>,
825		pub stakers: Vec<(T::AccountId, BalanceOf<T>, crate::StakerStatus<T::AccountId>)>,
826		pub min_nominator_bond: BalanceOf<T>,
827		pub min_validator_bond: BalanceOf<T>,
828		pub max_validator_count: Option<u32>,
829		pub max_nominator_count: Option<u32>,
830		/// Create the given number of validators and nominators.
831		///
832		/// These account need not be in the endowment list of balances, and are auto-topped up
833		/// here.
834		///
835		/// Useful for testing genesis config.
836		pub dev_stakers: Option<(u32, u32)>,
837		/// initial active era, corresponding session index and start timestamp.
838		pub active_era: (u32, u32, u64),
839	}
840
841	impl<T: Config> GenesisConfig<T> {
842		fn generate_endowed_bonded_account(derivation: &str, rng: &mut ChaChaRng) -> T::AccountId {
843			let pair: SrPair = Pair::from_string(&derivation, None)
844				.expect(&format!("Failed to parse derivation string: {derivation}"));
845			let who = T::AccountId::decode(&mut &pair.public().encode()[..])
846				.expect(&format!("Failed to decode public key from pair: {:?}", pair.public()));
847
848			let (min, max) = T::VoterList::range();
849			let stake = BalanceOf::<T>::from(rng.next_u64().min(max).max(min));
850			let two: BalanceOf<T> = 2u32.into();
851
852			assert_ok!(T::Currency::mint_into(&who, stake * two));
853			assert_ok!(<Pallet<T>>::bond(
854				T::RuntimeOrigin::from(Some(who.clone()).into()),
855				stake,
856				RewardDestination::Staked,
857			));
858			who
859		}
860	}
861
862	#[pallet::genesis_build]
863	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
864		fn build(&self) {
865			crate::log!(trace, "initializing with {:?}", self);
866			ValidatorCount::<T>::put(self.validator_count);
867			assert!(
868				self.invulnerables.len() as u32 <= T::MaxInvulnerables::get(),
869				"Too many invulnerable validators at genesis."
870			);
871			<Invulnerables<T>>::put(&self.invulnerables);
872			ForceEra::<T>::put(self.force_era);
873			CanceledSlashPayout::<T>::put(self.canceled_payout);
874			SlashRewardFraction::<T>::put(self.slash_reward_fraction);
875			MinNominatorBond::<T>::put(self.min_nominator_bond);
876			MinValidatorBond::<T>::put(self.min_validator_bond);
877			if let Some(x) = self.max_validator_count {
878				MaxValidatorsCount::<T>::put(x);
879			}
880			if let Some(x) = self.max_nominator_count {
881				MaxNominatorsCount::<T>::put(x);
882			}
883
884			for &(ref stash, balance, ref status) in &self.stakers {
885				crate::log!(
886					trace,
887					"inserting genesis staker: {:?} => {:?} => {:?}",
888					stash,
889					balance,
890					status
891				);
892				assert!(
893					asset::free_to_stake::<T>(stash) >= balance,
894					"Stash does not have enough balance to bond."
895				);
896				assert_ok!(<Pallet<T>>::bond(
897					T::RuntimeOrigin::from(Some(stash.clone()).into()),
898					balance,
899					RewardDestination::Staked,
900				));
901				assert_ok!(match status {
902					crate::StakerStatus::Validator => <Pallet<T>>::validate(
903						T::RuntimeOrigin::from(Some(stash.clone()).into()),
904						Default::default(),
905					),
906					crate::StakerStatus::Nominator(votes) => <Pallet<T>>::nominate(
907						T::RuntimeOrigin::from(Some(stash.clone()).into()),
908						votes.iter().map(|l| T::Lookup::unlookup(l.clone())).collect(),
909					),
910					_ => Ok(()),
911				});
912				assert!(
913					ValidatorCount::<T>::get() <=
914						<T::ElectionProvider as ElectionProvider>::MaxWinnersPerPage::get() *
915							<T::ElectionProvider as ElectionProvider>::Pages::get()
916				);
917			}
918
919			// all voters are reported to the `VoterList`.
920			assert_eq!(
921				T::VoterList::count(),
922				Nominators::<T>::count() + Validators::<T>::count(),
923				"not all genesis stakers were inserted into sorted list provider, something is wrong."
924			);
925
926			// now generate the dev stakers, after all else is setup
927			if let Some((validators, nominators)) = self.dev_stakers {
928				crate::log!(
929					debug,
930					"generating dev stakers: validators: {}, nominators: {}",
931					validators,
932					nominators
933				);
934				let base_derivation = "//staker//{}";
935
936				// it is okay for the randomness to be the same on every call. If we want different,
937				// we can make `base_derivation` configurable.
938				let mut rng =
939					ChaChaRng::from_seed(base_derivation.using_encoded(sp_core::blake2_256));
940
941				let validators = (0..validators)
942					.map(|index| {
943						let derivation =
944							base_derivation.replace("{}", &format!("validator{}", index));
945						let who = Self::generate_endowed_bonded_account(&derivation, &mut rng);
946						assert_ok!(<Pallet<T>>::validate(
947							T::RuntimeOrigin::from(Some(who.clone()).into()),
948							Default::default(),
949						));
950						who
951					})
952					.collect::<Vec<_>>();
953
954				(0..nominators).for_each(|index| {
955					let derivation = base_derivation.replace("{}", &format!("nominator{}", index));
956					let who = Self::generate_endowed_bonded_account(&derivation, &mut rng);
957
958					let random_nominations = validators
959						.choose_multiple(&mut rng, MaxNominationsOf::<T>::get() as usize)
960						.map(|v| v.clone())
961						.collect::<Vec<_>>();
962
963					assert_ok!(<Pallet<T>>::nominate(
964						T::RuntimeOrigin::from(Some(who.clone()).into()),
965						random_nominations.iter().map(|l| T::Lookup::unlookup(l.clone())).collect(),
966					));
967				})
968			}
969
970			let (active_era, session_index, timestamp) = self.active_era;
971			ActiveEra::<T>::put(ActiveEraInfo { index: active_era, start: Some(timestamp) });
972			// at genesis, we do not have any new planned era.
973			CurrentEra::<T>::put(active_era);
974			// set the bonded genesis era
975			BondedEras::<T>::put(
976				BoundedVec::<_, BondedErasBound<T>>::try_from(
977					alloc::vec![(active_era, session_index)]
978				)
979				.expect("bound for BondedEras is BondingDuration + 1; can contain at least one element; qed")
980			);
981		}
982	}
983
984	#[pallet::event]
985	#[pallet::generate_deposit(pub fn deposit_event)]
986	pub enum Event<T: Config> {
987		/// The era payout has been set; the first balance is the validator-payout; the second is
988		/// the remainder from the maximum amount of reward.
989		EraPaid {
990			era_index: EraIndex,
991			validator_payout: BalanceOf<T>,
992			remainder: BalanceOf<T>,
993		},
994		/// The nominator has been rewarded by this amount to this destination.
995		Rewarded {
996			stash: T::AccountId,
997			dest: RewardDestination<T::AccountId>,
998			amount: BalanceOf<T>,
999		},
1000		/// A staker (validator or nominator) has been slashed by the given amount.
1001		Slashed {
1002			staker: T::AccountId,
1003			amount: BalanceOf<T>,
1004		},
1005		/// An old slashing report from a prior era was discarded because it could
1006		/// not be processed.
1007		OldSlashingReportDiscarded {
1008			session_index: SessionIndex,
1009		},
1010		/// An account has bonded this amount. \[stash, amount\]
1011		///
1012		/// NOTE: This event is only emitted when funds are bonded via a dispatchable. Notably,
1013		/// it will not be emitted for staking rewards when they are added to stake.
1014		Bonded {
1015			stash: T::AccountId,
1016			amount: BalanceOf<T>,
1017		},
1018		/// An account has unbonded this amount.
1019		Unbonded {
1020			stash: T::AccountId,
1021			amount: BalanceOf<T>,
1022		},
1023		/// An account has called `withdraw_unbonded` and removed unbonding chunks worth `Balance`
1024		/// from the unlocking queue.
1025		Withdrawn {
1026			stash: T::AccountId,
1027			amount: BalanceOf<T>,
1028		},
1029		/// A subsequent event of `Withdrawn`, indicating that `stash` was fully removed from the
1030		/// system.
1031		StakerRemoved {
1032			stash: T::AccountId,
1033		},
1034		/// A nominator has been kicked from a validator.
1035		Kicked {
1036			nominator: T::AccountId,
1037			stash: T::AccountId,
1038		},
1039		/// An account has stopped participating as either a validator or nominator.
1040		Chilled {
1041			stash: T::AccountId,
1042		},
1043		/// A Page of stakers rewards are getting paid. `next` is `None` if all pages are claimed.
1044		PayoutStarted {
1045			era_index: EraIndex,
1046			validator_stash: T::AccountId,
1047			page: Page,
1048			next: Option<Page>,
1049		},
1050		/// A validator has set their preferences.
1051		ValidatorPrefsSet {
1052			stash: T::AccountId,
1053			prefs: ValidatorPrefs,
1054		},
1055		/// Voters size limit reached.
1056		SnapshotVotersSizeExceeded {
1057			size: u32,
1058		},
1059		/// Targets size limit reached.
1060		SnapshotTargetsSizeExceeded {
1061			size: u32,
1062		},
1063		ForceEra {
1064			mode: Forcing,
1065		},
1066		/// Report of a controller batch deprecation.
1067		ControllerBatchDeprecated {
1068			failures: u32,
1069		},
1070		/// Staking balance migrated from locks to holds, with any balance that could not be held
1071		/// is force withdrawn.
1072		CurrencyMigrated {
1073			stash: T::AccountId,
1074			force_withdraw: BalanceOf<T>,
1075		},
1076		/// A page from a multi-page election was fetched. A number of these are followed by
1077		/// `StakersElected`.
1078		///
1079		/// `Ok(count)` indicates the give number of stashes were added.
1080		/// `Err(index)` indicates that the stashes after index were dropped.
1081		/// `Err(0)` indicates that an error happened but no stashes were dropped nor added.
1082		///
1083		/// The error indicates that a number of validators were dropped due to excess size, but
1084		/// the overall election will continue.
1085		PagedElectionProceeded {
1086			page: PageIndex,
1087			result: Result<u32, u32>,
1088		},
1089		/// An offence for the given validator, for the given percentage of their stake, at the
1090		/// given era as been reported.
1091		OffenceReported {
1092			offence_era: EraIndex,
1093			validator: T::AccountId,
1094			fraction: Perbill,
1095		},
1096		/// An offence has been processed and the corresponding slash has been computed.
1097		SlashComputed {
1098			offence_era: EraIndex,
1099			slash_era: EraIndex,
1100			offender: T::AccountId,
1101			page: u32,
1102		},
1103		/// An unapplied slash has been cancelled.
1104		SlashCancelled {
1105			slash_era: EraIndex,
1106			slash_key: (T::AccountId, Perbill, u32),
1107			payout: BalanceOf<T>,
1108		},
1109		/// Session change has been triggered.
1110		///
1111		/// If planned_era is one era ahead of active_era, it implies new era is being planned and
1112		/// election is ongoing.
1113		SessionRotated {
1114			starting_session: SessionIndex,
1115			active_era: EraIndex,
1116			planned_era: EraIndex,
1117		},
1118		/// Something occurred that should never happen under normal operation.
1119		/// Logged as an event for fail-safe observability.
1120		Unexpected(UnexpectedKind),
1121	}
1122
1123	/// Represents unexpected or invariant-breaking conditions encountered during execution.
1124	///
1125	/// These variants are emitted as [`Event::Unexpected`] and indicate a defensive check has
1126	/// failed. While these should never occur under normal operation, they are useful for
1127	/// diagnosing issues in production or test environments.
1128	#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, TypeInfo, RuntimeDebug)]
1129	pub enum UnexpectedKind {
1130		/// Emitted when calculated era duration exceeds the configured maximum.
1131		EraDurationBoundExceeded,
1132		/// Received a validator activation event that is not recognized.
1133		UnknownValidatorActivation,
1134	}
1135
1136	#[pallet::error]
1137	#[derive(PartialEq)]
1138	pub enum Error<T> {
1139		/// Not a controller account.
1140		NotController,
1141		/// Not a stash account.
1142		NotStash,
1143		/// Stash is already bonded.
1144		AlreadyBonded,
1145		/// Controller is already paired.
1146		AlreadyPaired,
1147		/// Targets cannot be empty.
1148		EmptyTargets,
1149		/// Duplicate index.
1150		DuplicateIndex,
1151		/// Slash record not found.
1152		InvalidSlashRecord,
1153		/// Cannot bond, nominate or validate with value less than the minimum defined by
1154		/// governance (see `MinValidatorBond` and `MinNominatorBond`). If unbonding is the
1155		/// intention, `chill` first to remove one's role as validator/nominator.
1156		InsufficientBond,
1157		/// Can not schedule more unlock chunks.
1158		NoMoreChunks,
1159		/// Can not rebond without unlocking chunks.
1160		NoUnlockChunk,
1161		/// Attempting to target a stash that still has funds.
1162		FundedTarget,
1163		/// Invalid era to reward.
1164		InvalidEraToReward,
1165		/// Invalid number of nominations.
1166		InvalidNumberOfNominations,
1167		/// Rewards for this era have already been claimed for this validator.
1168		AlreadyClaimed,
1169		/// No nominators exist on this page.
1170		InvalidPage,
1171		/// Incorrect previous history depth input provided.
1172		IncorrectHistoryDepth,
1173		/// Internal state has become somehow corrupted and the operation cannot continue.
1174		BadState,
1175		/// Too many nomination targets supplied.
1176		TooManyTargets,
1177		/// A nomination target was supplied that was blocked or otherwise not a validator.
1178		BadTarget,
1179		/// The user has enough bond and thus cannot be chilled forcefully by an external person.
1180		CannotChillOther,
1181		/// There are too many nominators in the system. Governance needs to adjust the staking
1182		/// settings to keep things safe for the runtime.
1183		TooManyNominators,
1184		/// There are too many validator candidates in the system. Governance needs to adjust the
1185		/// staking settings to keep things safe for the runtime.
1186		TooManyValidators,
1187		/// Commission is too low. Must be at least `MinCommission`.
1188		CommissionTooLow,
1189		/// Some bound is not met.
1190		BoundNotMet,
1191		/// Used when attempting to use deprecated controller account logic.
1192		ControllerDeprecated,
1193		/// Cannot reset a ledger.
1194		CannotRestoreLedger,
1195		/// Provided reward destination is not allowed.
1196		RewardDestinationRestricted,
1197		/// Not enough funds available to withdraw.
1198		NotEnoughFunds,
1199		/// Operation not allowed for virtual stakers.
1200		VirtualStakerNotAllowed,
1201		/// Stash could not be reaped as other pallet might depend on it.
1202		CannotReapStash,
1203		/// The stake of this account is already migrated to `Fungible` holds.
1204		AlreadyMigrated,
1205		/// Era not yet started.
1206		EraNotStarted,
1207		/// Account is restricted from participation in staking. This may happen if the account is
1208		/// staking in another way already, such as via pool.
1209		Restricted,
1210	}
1211
1212	impl<T: Config> Pallet<T> {
1213		/// Apply previously-unapplied slashes on the beginning of a new era, after a delay.
1214		pub fn apply_unapplied_slashes(active_era: EraIndex) -> Weight {
1215			let mut slashes = UnappliedSlashes::<T>::iter_prefix(&active_era).take(1);
1216			if let Some((key, slash)) = slashes.next() {
1217				crate::log!(
1218					debug,
1219					"🦹 found slash {:?} scheduled to be executed in era {:?}",
1220					slash,
1221					active_era,
1222				);
1223				let offence_era = active_era.saturating_sub(T::SlashDeferDuration::get());
1224				slashing::apply_slash::<T>(slash, offence_era);
1225				// remove the slash
1226				UnappliedSlashes::<T>::remove(&active_era, &key);
1227				T::WeightInfo::apply_slash()
1228			} else {
1229				T::DbWeight::get().reads(1)
1230			}
1231		}
1232	}
1233
1234	#[pallet::hooks]
1235	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
1236		fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
1237			// process our queue.
1238			let mut consumed_weight = slashing::process_offence::<T>();
1239
1240			// apply any pending slashes after `SlashDeferDuration`.
1241			consumed_weight.saturating_accrue(T::DbWeight::get().reads(1));
1242			if let Some(active_era) = ActiveEra::<T>::get() {
1243				let slash_weight = Self::apply_unapplied_slashes(active_era.index);
1244				consumed_weight.saturating_accrue(slash_weight);
1245			}
1246
1247			// maybe plan eras and stuff. Note that this is benchmark as a part of the
1248			// election-provider's benchmarks.
1249			session_rotation::EraElectionPlanner::<T>::maybe_fetch_election_results();
1250			consumed_weight
1251		}
1252
1253		fn integrity_test() {
1254			// ensure that we funnel the correct value to the `DataProvider::MaxVotesPerVoter`;
1255			assert_eq!(
1256				MaxNominationsOf::<T>::get(),
1257				<Self as ElectionDataProvider>::MaxVotesPerVoter::get()
1258			);
1259			// and that MaxNominations is always greater than 1, since we count on this.
1260			assert!(!MaxNominationsOf::<T>::get().is_zero());
1261
1262			assert!(
1263				T::SlashDeferDuration::get() < T::BondingDuration::get() || T::BondingDuration::get() == 0,
1264				"As per documentation, slash defer duration ({}) should be less than bonding duration ({}).",
1265				T::SlashDeferDuration::get(),
1266				T::BondingDuration::get(),
1267			);
1268		}
1269
1270		#[cfg(feature = "try-runtime")]
1271		fn try_state(n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
1272			Self::do_try_state(n)
1273		}
1274	}
1275
1276	#[pallet::call]
1277	impl<T: Config> Pallet<T> {
1278		/// Take the origin account as a stash and lock up `value` of its balance. `controller` will
1279		/// be the account that controls it.
1280		///
1281		/// `value` must be more than the `minimum_balance` specified by `T::Currency`.
1282		///
1283		/// The dispatch origin for this call must be _Signed_ by the stash account.
1284		///
1285		/// Emits `Bonded`.
1286		///
1287		/// NOTE: Two of the storage writes (`Self::bonded`, `Self::payee`) are _never_ cleaned
1288		/// unless the `origin` falls below _existential deposit_ (or equal to 0) and gets removed
1289		/// as dust.
1290		#[pallet::call_index(0)]
1291		#[pallet::weight(T::WeightInfo::bond())]
1292		pub fn bond(
1293			origin: OriginFor<T>,
1294			#[pallet::compact] value: BalanceOf<T>,
1295			payee: RewardDestination<T::AccountId>,
1296		) -> DispatchResult {
1297			let stash = ensure_signed(origin)?;
1298
1299			ensure!(!T::Filter::contains(&stash), Error::<T>::Restricted);
1300
1301			if StakingLedger::<T>::is_bonded(StakingAccount::Stash(stash.clone())) {
1302				return Err(Error::<T>::AlreadyBonded.into());
1303			}
1304
1305			// An existing controller cannot become a stash.
1306			if StakingLedger::<T>::is_bonded(StakingAccount::Controller(stash.clone())) {
1307				return Err(Error::<T>::AlreadyPaired.into());
1308			}
1309
1310			// Reject a bond which is lower than the minimum bond.
1311			if value < Self::min_chilled_bond() {
1312				return Err(Error::<T>::InsufficientBond.into());
1313			}
1314
1315			let stash_balance = asset::free_to_stake::<T>(&stash);
1316			let value = value.min(stash_balance);
1317			Self::deposit_event(Event::<T>::Bonded { stash: stash.clone(), amount: value });
1318			let ledger = StakingLedger::<T>::new(stash.clone(), value);
1319
1320			// You're auto-bonded forever, here. We might improve this by only bonding when
1321			// you actually validate/nominate and remove once you unbond __everything__.
1322			ledger.bond(payee)?;
1323
1324			Ok(())
1325		}
1326
1327		/// Add some extra amount that have appeared in the stash `free_balance` into the balance up
1328		/// for staking.
1329		///
1330		/// The dispatch origin for this call must be _Signed_ by the stash, not the controller.
1331		///
1332		/// Use this if there are additional funds in your stash account that you wish to bond.
1333		/// Unlike [`bond`](Self::bond) or [`unbond`](Self::unbond) this function does not impose
1334		/// any limitation on the amount that can be added.
1335		///
1336		/// Emits `Bonded`.
1337		#[pallet::call_index(1)]
1338		#[pallet::weight(T::WeightInfo::bond_extra())]
1339		pub fn bond_extra(
1340			origin: OriginFor<T>,
1341			#[pallet::compact] max_additional: BalanceOf<T>,
1342		) -> DispatchResult {
1343			let stash = ensure_signed(origin)?;
1344			ensure!(!T::Filter::contains(&stash), Error::<T>::Restricted);
1345			Self::do_bond_extra(&stash, max_additional)
1346		}
1347
1348		/// Schedule a portion of the stash to be unlocked ready for transfer out after the bond
1349		/// period ends. If this leaves an amount actively bonded less than
1350		/// [`asset::existential_deposit`], then it is increased to the full amount.
1351		///
1352		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1353		///
1354		/// Once the unlock period is done, you can call `withdraw_unbonded` to actually move
1355		/// the funds out of management ready for transfer.
1356		///
1357		/// No more than a limited number of unlocking chunks (see `MaxUnlockingChunks`)
1358		/// can co-exists at the same time. If there are no unlocking chunks slots available
1359		/// [`Call::withdraw_unbonded`] is called to remove some of the chunks (if possible).
1360		///
1361		/// If a user encounters the `InsufficientBond` error when calling this extrinsic,
1362		/// they should call `chill` first in order to free up their bonded funds.
1363		///
1364		/// Emits `Unbonded`.
1365		///
1366		/// See also [`Call::withdraw_unbonded`].
1367		#[pallet::call_index(2)]
1368		#[pallet::weight(
1369            T::WeightInfo::withdraw_unbonded_kill().saturating_add(T::WeightInfo::unbond()))
1370        ]
1371		pub fn unbond(
1372			origin: OriginFor<T>,
1373			#[pallet::compact] value: BalanceOf<T>,
1374		) -> DispatchResultWithPostInfo {
1375			let controller = ensure_signed(origin)?;
1376			let unlocking =
1377				Self::ledger(Controller(controller.clone())).map(|l| l.unlocking.len())?;
1378
1379			// if there are no unlocking chunks available, try to withdraw chunks older than
1380			// `BondingDuration` to proceed with the unbonding.
1381			let maybe_withdraw_weight = {
1382				if unlocking == T::MaxUnlockingChunks::get() as usize {
1383					Some(Self::do_withdraw_unbonded(&controller)?)
1384				} else {
1385					None
1386				}
1387			};
1388
1389			// we need to fetch the ledger again because it may have been mutated in the call
1390			// to `Self::do_withdraw_unbonded` above.
1391			let mut ledger = Self::ledger(Controller(controller))?;
1392			let mut value = value.min(ledger.active);
1393			let stash = ledger.stash.clone();
1394
1395			ensure!(
1396				ledger.unlocking.len() < T::MaxUnlockingChunks::get() as usize,
1397				Error::<T>::NoMoreChunks,
1398			);
1399
1400			if !value.is_zero() {
1401				ledger.active -= value;
1402
1403				// Avoid there being a dust balance left in the staking system.
1404				if ledger.active < asset::existential_deposit::<T>() {
1405					value += ledger.active;
1406					ledger.active = Zero::zero();
1407				}
1408
1409				let min_active_bond = if Nominators::<T>::contains_key(&stash) {
1410					Self::min_nominator_bond()
1411				} else if Validators::<T>::contains_key(&stash) {
1412					Self::min_validator_bond()
1413				} else {
1414					// staker is chilled, no min bond.
1415					Zero::zero()
1416				};
1417
1418				// Make sure that the user maintains enough active bond for their role.
1419				// If a user runs into this error, they should chill first.
1420				ensure!(ledger.active >= min_active_bond, Error::<T>::InsufficientBond);
1421
1422				// Note: in case there is no current era it is fine to bond one era more.
1423				let era = CurrentEra::<T>::get()
1424					.unwrap_or(0)
1425					.defensive_saturating_add(T::BondingDuration::get());
1426				if let Some(chunk) = ledger.unlocking.last_mut().filter(|chunk| chunk.era == era) {
1427					// To keep the chunk count down, we only keep one chunk per era. Since
1428					// `unlocking` is a FiFo queue, if a chunk exists for `era` we know that it will
1429					// be the last one.
1430					chunk.value = chunk.value.defensive_saturating_add(value)
1431				} else {
1432					ledger
1433						.unlocking
1434						.try_push(UnlockChunk { value, era })
1435						.map_err(|_| Error::<T>::NoMoreChunks)?;
1436				};
1437				// NOTE: ledger must be updated prior to calling `Self::weight_of`.
1438				ledger.update()?;
1439
1440				// update this staker in the sorted list, if they exist in it.
1441				if T::VoterList::contains(&stash) {
1442					let _ = T::VoterList::on_update(&stash, Self::weight_of(&stash));
1443				}
1444
1445				Self::deposit_event(Event::<T>::Unbonded { stash, amount: value });
1446			}
1447
1448			let actual_weight = if let Some(withdraw_weight) = maybe_withdraw_weight {
1449				Some(T::WeightInfo::unbond().saturating_add(withdraw_weight))
1450			} else {
1451				Some(T::WeightInfo::unbond())
1452			};
1453
1454			Ok(actual_weight.into())
1455		}
1456
1457		/// Remove any unlocked chunks from the `unlocking` queue from our management.
1458		///
1459		/// This essentially frees up that balance to be used by the stash account to do whatever
1460		/// it wants.
1461		///
1462		/// The dispatch origin for this call must be _Signed_ by the controller.
1463		///
1464		/// Emits `Withdrawn`.
1465		///
1466		/// See also [`Call::unbond`].
1467		///
1468		/// ## Parameters
1469		///
1470		/// - `num_slashing_spans`: **Deprecated**. This parameter is retained for backward
1471		/// compatibility. It no longer has any effect.
1472		#[pallet::call_index(3)]
1473		#[pallet::weight(T::WeightInfo::withdraw_unbonded_kill())]
1474		pub fn withdraw_unbonded(
1475			origin: OriginFor<T>,
1476			_num_slashing_spans: u32,
1477		) -> DispatchResultWithPostInfo {
1478			let controller = ensure_signed(origin)?;
1479
1480			let actual_weight = Self::do_withdraw_unbonded(&controller)?;
1481			Ok(Some(actual_weight).into())
1482		}
1483
1484		/// Declare the desire to validate for the origin controller.
1485		///
1486		/// Effects will be felt at the beginning of the next era.
1487		///
1488		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1489		#[pallet::call_index(4)]
1490		#[pallet::weight(T::WeightInfo::validate())]
1491		pub fn validate(origin: OriginFor<T>, prefs: ValidatorPrefs) -> DispatchResult {
1492			let controller = ensure_signed(origin)?;
1493
1494			let ledger = Self::ledger(Controller(controller))?;
1495
1496			ensure!(ledger.active >= Self::min_validator_bond(), Error::<T>::InsufficientBond);
1497			let stash = &ledger.stash;
1498
1499			// ensure their commission is correct.
1500			ensure!(prefs.commission >= MinCommission::<T>::get(), Error::<T>::CommissionTooLow);
1501
1502			// Only check limits if they are not already a validator.
1503			if !Validators::<T>::contains_key(stash) {
1504				// If this error is reached, we need to adjust the `MinValidatorBond` and start
1505				// calling `chill_other`. Until then, we explicitly block new validators to protect
1506				// the runtime.
1507				if let Some(max_validators) = MaxValidatorsCount::<T>::get() {
1508					ensure!(
1509						Validators::<T>::count() < max_validators,
1510						Error::<T>::TooManyValidators
1511					);
1512				}
1513			}
1514
1515			Self::do_remove_nominator(stash);
1516			Self::do_add_validator(stash, prefs.clone());
1517			Self::deposit_event(Event::<T>::ValidatorPrefsSet { stash: ledger.stash, prefs });
1518
1519			Ok(())
1520		}
1521
1522		/// Declare the desire to nominate `targets` for the origin controller.
1523		///
1524		/// Effects will be felt at the beginning of the next era.
1525		///
1526		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1527		#[pallet::call_index(5)]
1528		#[pallet::weight(T::WeightInfo::nominate(targets.len() as u32))]
1529		pub fn nominate(
1530			origin: OriginFor<T>,
1531			targets: Vec<AccountIdLookupOf<T>>,
1532		) -> DispatchResult {
1533			let controller = ensure_signed(origin)?;
1534
1535			let ledger = Self::ledger(StakingAccount::Controller(controller.clone()))?;
1536
1537			ensure!(ledger.active >= Self::min_nominator_bond(), Error::<T>::InsufficientBond);
1538			let stash = &ledger.stash;
1539
1540			// Only check limits if they are not already a nominator.
1541			if !Nominators::<T>::contains_key(stash) {
1542				// If this error is reached, we need to adjust the `MinNominatorBond` and start
1543				// calling `chill_other`. Until then, we explicitly block new nominators to protect
1544				// the runtime.
1545				if let Some(max_nominators) = MaxNominatorsCount::<T>::get() {
1546					ensure!(
1547						Nominators::<T>::count() < max_nominators,
1548						Error::<T>::TooManyNominators
1549					);
1550				}
1551			}
1552
1553			// dedup targets
1554			let mut targets = targets
1555				.into_iter()
1556				.map(|t| T::Lookup::lookup(t).map_err(DispatchError::from))
1557				.collect::<Result<Vec<_>, _>>()?;
1558			targets.sort();
1559			targets.dedup();
1560
1561			ensure!(!targets.is_empty(), Error::<T>::EmptyTargets);
1562			ensure!(
1563				targets.len() <= T::NominationsQuota::get_quota(ledger.active) as usize,
1564				Error::<T>::TooManyTargets
1565			);
1566
1567			let old = Nominators::<T>::get(stash).map_or_else(Vec::new, |x| x.targets.into_inner());
1568
1569			let targets: BoundedVec<_, _> = targets
1570				.into_iter()
1571				.map(|n| {
1572					if old.contains(&n) || !Validators::<T>::get(&n).blocked {
1573						Ok(n)
1574					} else {
1575						Err(Error::<T>::BadTarget.into())
1576					}
1577				})
1578				.collect::<Result<Vec<_>, DispatchError>>()?
1579				.try_into()
1580				.map_err(|_| Error::<T>::TooManyNominators)?;
1581
1582			let nominations = Nominations {
1583				targets,
1584				// Initial nominations are considered submitted at era 0. See `Nominations` doc.
1585				submitted_in: CurrentEra::<T>::get().unwrap_or(0),
1586				suppressed: false,
1587			};
1588
1589			Self::do_remove_validator(stash);
1590			Self::do_add_nominator(stash, nominations);
1591			Ok(())
1592		}
1593
1594		/// Declare no desire to either validate or nominate.
1595		///
1596		/// Effects will be felt at the beginning of the next era.
1597		///
1598		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1599		///
1600		/// ## Complexity
1601		/// - Independent of the arguments. Insignificant complexity.
1602		/// - Contains one read.
1603		/// - Writes are limited to the `origin` account key.
1604		#[pallet::call_index(6)]
1605		#[pallet::weight(T::WeightInfo::chill())]
1606		pub fn chill(origin: OriginFor<T>) -> DispatchResult {
1607			let controller = ensure_signed(origin)?;
1608
1609			let ledger = Self::ledger(StakingAccount::Controller(controller))?;
1610
1611			Self::chill_stash(&ledger.stash);
1612			Ok(())
1613		}
1614
1615		/// (Re-)set the payment target for a controller.
1616		///
1617		/// Effects will be felt instantly (as soon as this function is completed successfully).
1618		///
1619		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1620		#[pallet::call_index(7)]
1621		#[pallet::weight(T::WeightInfo::set_payee())]
1622		pub fn set_payee(
1623			origin: OriginFor<T>,
1624			payee: RewardDestination<T::AccountId>,
1625		) -> DispatchResult {
1626			let controller = ensure_signed(origin)?;
1627			let ledger = Self::ledger(Controller(controller.clone()))?;
1628
1629			ensure!(
1630				(payee != {
1631					#[allow(deprecated)]
1632					RewardDestination::Controller
1633				}),
1634				Error::<T>::ControllerDeprecated
1635			);
1636
1637			let _ = ledger
1638				.set_payee(payee)
1639				.defensive_proof("ledger was retrieved from storage, thus it's bonded; qed.")?;
1640
1641			Ok(())
1642		}
1643
1644		/// (Re-)sets the controller of a stash to the stash itself. This function previously
1645		/// accepted a `controller` argument to set the controller to an account other than the
1646		/// stash itself. This functionality has now been removed, now only setting the controller
1647		/// to the stash, if it is not already.
1648		///
1649		/// Effects will be felt instantly (as soon as this function is completed successfully).
1650		///
1651		/// The dispatch origin for this call must be _Signed_ by the stash, not the controller.
1652		#[pallet::call_index(8)]
1653		#[pallet::weight(T::WeightInfo::set_controller())]
1654		pub fn set_controller(origin: OriginFor<T>) -> DispatchResult {
1655			let stash = ensure_signed(origin)?;
1656
1657			Self::ledger(StakingAccount::Stash(stash.clone())).map(|ledger| {
1658				let controller = ledger.controller()
1659                    .defensive_proof("Ledger's controller field didn't exist. The controller should have been fetched using StakingLedger.")
1660                    .ok_or(Error::<T>::NotController)?;
1661
1662				if controller == stash {
1663					// Stash is already its own controller.
1664					return Err(Error::<T>::AlreadyPaired.into())
1665				}
1666
1667				let _ = ledger.set_controller_to_stash()?;
1668				Ok(())
1669			})?
1670		}
1671
1672		/// Sets the ideal number of validators.
1673		///
1674		/// The dispatch origin must be Root.
1675		#[pallet::call_index(9)]
1676		#[pallet::weight(T::WeightInfo::set_validator_count())]
1677		pub fn set_validator_count(
1678			origin: OriginFor<T>,
1679			#[pallet::compact] new: u32,
1680		) -> DispatchResult {
1681			ensure_root(origin)?;
1682
1683			ensure!(new <= T::MaxValidatorSet::get(), Error::<T>::TooManyValidators);
1684
1685			ValidatorCount::<T>::put(new);
1686			Ok(())
1687		}
1688
1689		/// Increments the ideal number of validators up to maximum of
1690		/// `T::MaxValidatorSet`.
1691		///
1692		/// The dispatch origin must be Root.
1693		#[pallet::call_index(10)]
1694		#[pallet::weight(T::WeightInfo::set_validator_count())]
1695		pub fn increase_validator_count(
1696			origin: OriginFor<T>,
1697			#[pallet::compact] additional: u32,
1698		) -> DispatchResult {
1699			ensure_root(origin)?;
1700			let old = ValidatorCount::<T>::get();
1701			let new = old.checked_add(additional).ok_or(ArithmeticError::Overflow)?;
1702
1703			ensure!(new <= T::MaxValidatorSet::get(), Error::<T>::TooManyValidators);
1704
1705			ValidatorCount::<T>::put(new);
1706			Ok(())
1707		}
1708
1709		/// Scale up the ideal number of validators by a factor up to maximum of
1710		/// `T::MaxValidatorSet`.
1711		///
1712		/// The dispatch origin must be Root.
1713		#[pallet::call_index(11)]
1714		#[pallet::weight(T::WeightInfo::set_validator_count())]
1715		pub fn scale_validator_count(origin: OriginFor<T>, factor: Percent) -> DispatchResult {
1716			ensure_root(origin)?;
1717			let old = ValidatorCount::<T>::get();
1718			let new = old.checked_add(factor.mul_floor(old)).ok_or(ArithmeticError::Overflow)?;
1719
1720			ensure!(new <= T::MaxValidatorSet::get(), Error::<T>::TooManyValidators);
1721
1722			ValidatorCount::<T>::put(new);
1723			Ok(())
1724		}
1725
1726		/// Force there to be no new eras indefinitely.
1727		///
1728		/// The dispatch origin must be Root.
1729		///
1730		/// # Warning
1731		///
1732		/// The election process starts multiple blocks before the end of the era.
1733		/// Thus the election process may be ongoing when this is called. In this case the
1734		/// election will continue until the next era is triggered.
1735		#[pallet::call_index(12)]
1736		#[pallet::weight(T::WeightInfo::force_no_eras())]
1737		pub fn force_no_eras(origin: OriginFor<T>) -> DispatchResult {
1738			ensure_root(origin)?;
1739			Self::set_force_era(Forcing::ForceNone);
1740			Ok(())
1741		}
1742
1743		/// Force there to be a new era at the end of the next session. After this, it will be
1744		/// reset to normal (non-forced) behaviour.
1745		///
1746		/// The dispatch origin must be Root.
1747		///
1748		/// # Warning
1749		///
1750		/// The election process starts multiple blocks before the end of the era.
1751		/// If this is called just before a new era is triggered, the election process may not
1752		/// have enough blocks to get a result.
1753		#[pallet::call_index(13)]
1754		#[pallet::weight(T::WeightInfo::force_new_era())]
1755		pub fn force_new_era(origin: OriginFor<T>) -> DispatchResult {
1756			ensure_root(origin)?;
1757			Self::set_force_era(Forcing::ForceNew);
1758			Ok(())
1759		}
1760
1761		/// Set the validators who cannot be slashed (if any).
1762		///
1763		/// The dispatch origin must be Root.
1764		#[pallet::call_index(14)]
1765		#[pallet::weight(T::WeightInfo::set_invulnerables(invulnerables.len() as u32))]
1766		pub fn set_invulnerables(
1767			origin: OriginFor<T>,
1768			invulnerables: Vec<T::AccountId>,
1769		) -> DispatchResult {
1770			ensure_root(origin)?;
1771			let invulnerables =
1772				BoundedVec::try_from(invulnerables).map_err(|_| Error::<T>::BoundNotMet)?;
1773			<Invulnerables<T>>::put(invulnerables);
1774			Ok(())
1775		}
1776
1777		/// Force a current staker to become completely unstaked, immediately.
1778		///
1779		/// The dispatch origin must be Root.
1780		/// ## Parameters
1781		///
1782		/// - `stash`: The stash account to be unstaked.
1783		/// - `num_slashing_spans`: **Deprecated**. This parameter is retained for backward
1784		/// compatibility. It no longer has any effect.
1785		#[pallet::call_index(15)]
1786		#[pallet::weight(T::WeightInfo::force_unstake())]
1787		pub fn force_unstake(
1788			origin: OriginFor<T>,
1789			stash: T::AccountId,
1790			_num_slashing_spans: u32,
1791		) -> DispatchResult {
1792			ensure_root(origin)?;
1793
1794			// Remove all staking-related information and lock.
1795			Self::kill_stash(&stash)?;
1796
1797			Ok(())
1798		}
1799
1800		/// Force there to be a new era at the end of sessions indefinitely.
1801		///
1802		/// The dispatch origin must be Root.
1803		///
1804		/// # Warning
1805		///
1806		/// The election process starts multiple blocks before the end of the era.
1807		/// If this is called just before a new era is triggered, the election process may not
1808		/// have enough blocks to get a result.
1809		#[pallet::call_index(16)]
1810		#[pallet::weight(T::WeightInfo::force_new_era_always())]
1811		pub fn force_new_era_always(origin: OriginFor<T>) -> DispatchResult {
1812			ensure_root(origin)?;
1813			Self::set_force_era(Forcing::ForceAlways);
1814			Ok(())
1815		}
1816
1817		/// Cancels scheduled slashes for a given era before they are applied.
1818		///
1819		/// This function allows `T::AdminOrigin` to selectively remove pending slashes from
1820		/// the `UnappliedSlashes` storage, preventing their enactment.
1821		///
1822		/// ## Parameters
1823		/// - `era`: The staking era for which slashes were deferred.
1824		/// - `slash_keys`: A list of slash keys identifying the slashes to remove. This is a tuple
1825		/// of `(stash, slash_fraction, page_index)`.
1826		#[pallet::call_index(17)]
1827		#[pallet::weight(T::WeightInfo::cancel_deferred_slash(slash_keys.len() as u32))]
1828		pub fn cancel_deferred_slash(
1829			origin: OriginFor<T>,
1830			era: EraIndex,
1831			slash_keys: Vec<(T::AccountId, Perbill, u32)>,
1832		) -> DispatchResult {
1833			T::AdminOrigin::ensure_origin(origin)?;
1834			ensure!(!slash_keys.is_empty(), Error::<T>::EmptyTargets);
1835
1836			// Remove the unapplied slashes.
1837			slash_keys.into_iter().for_each(|i| {
1838				UnappliedSlashes::<T>::take(&era, &i).map(|unapplied_slash| {
1839					Self::deposit_event(Event::<T>::SlashCancelled {
1840						slash_era: era,
1841						slash_key: i,
1842						payout: unapplied_slash.payout,
1843					});
1844				});
1845			});
1846			Ok(())
1847		}
1848
1849		/// Pay out next page of the stakers behind a validator for the given era.
1850		///
1851		/// - `validator_stash` is the stash account of the validator.
1852		/// - `era` may be any era between `[current_era - history_depth; current_era]`.
1853		///
1854		/// The origin of this call must be _Signed_. Any account can call this function, even if
1855		/// it is not one of the stakers.
1856		///
1857		/// The reward payout could be paged in case there are too many nominators backing the
1858		/// `validator_stash`. This call will payout unpaid pages in an ascending order. To claim a
1859		/// specific page, use `payout_stakers_by_page`.`
1860		///
1861		/// If all pages are claimed, it returns an error `InvalidPage`.
1862		#[pallet::call_index(18)]
1863		#[pallet::weight(T::WeightInfo::payout_stakers_alive_staked(T::MaxExposurePageSize::get()))]
1864		pub fn payout_stakers(
1865			origin: OriginFor<T>,
1866			validator_stash: T::AccountId,
1867			era: EraIndex,
1868		) -> DispatchResultWithPostInfo {
1869			ensure_signed(origin)?;
1870
1871			Self::do_payout_stakers(validator_stash, era)
1872		}
1873
1874		/// Rebond a portion of the stash scheduled to be unlocked.
1875		///
1876		/// The dispatch origin must be signed by the controller.
1877		#[pallet::call_index(19)]
1878		#[pallet::weight(T::WeightInfo::rebond(T::MaxUnlockingChunks::get() as u32))]
1879		pub fn rebond(
1880			origin: OriginFor<T>,
1881			#[pallet::compact] value: BalanceOf<T>,
1882		) -> DispatchResultWithPostInfo {
1883			let controller = ensure_signed(origin)?;
1884			let ledger = Self::ledger(Controller(controller))?;
1885
1886			ensure!(!T::Filter::contains(&ledger.stash), Error::<T>::Restricted);
1887			ensure!(!ledger.unlocking.is_empty(), Error::<T>::NoUnlockChunk);
1888
1889			let initial_unlocking = ledger.unlocking.len() as u32;
1890			let (ledger, rebonded_value) = ledger.rebond(value);
1891			// Last check: the new active amount of ledger must be more than min bond.
1892			ensure!(ledger.active >= Self::min_chilled_bond(), Error::<T>::InsufficientBond);
1893
1894			Self::deposit_event(Event::<T>::Bonded {
1895				stash: ledger.stash.clone(),
1896				amount: rebonded_value,
1897			});
1898
1899			let stash = ledger.stash.clone();
1900			let final_unlocking = ledger.unlocking.len();
1901
1902			// NOTE: ledger must be updated prior to calling `Self::weight_of`.
1903			ledger.update()?;
1904			if T::VoterList::contains(&stash) {
1905				let _ = T::VoterList::on_update(&stash, Self::weight_of(&stash));
1906			}
1907
1908			let removed_chunks = 1u32 // for the case where the last iterated chunk is not removed
1909				.saturating_add(initial_unlocking)
1910				.saturating_sub(final_unlocking as u32);
1911			Ok(Some(T::WeightInfo::rebond(removed_chunks)).into())
1912		}
1913
1914		/// Remove all data structures concerning a staker/stash once it is at a state where it can
1915		/// be considered `dust` in the staking system. The requirements are:
1916		///
1917		/// 1. the `total_balance` of the stash is below minimum bond.
1918		/// 2. or, the `ledger.total` of the stash is below minimum bond.
1919		/// 3. or, existential deposit is zero and either `total_balance` or `ledger.total` is zero.
1920		///
1921		/// The former can happen in cases like a slash; the latter when a fully unbonded account
1922		/// is still receiving staking rewards in `RewardDestination::Staked`.
1923		///
1924		/// It can be called by anyone, as long as `stash` meets the above requirements.
1925		///
1926		/// Refunds the transaction fees upon successful execution.
1927		///
1928		/// ## Parameters
1929		///
1930		/// - `stash`: The stash account to be reaped.
1931		/// - `num_slashing_spans`: **Deprecated**. This parameter is retained for backward
1932		/// compatibility. It no longer has any effect.
1933		#[pallet::call_index(20)]
1934		#[pallet::weight(T::WeightInfo::reap_stash())]
1935		pub fn reap_stash(
1936			origin: OriginFor<T>,
1937			stash: T::AccountId,
1938			_num_slashing_spans: u32,
1939		) -> DispatchResultWithPostInfo {
1940			let _ = ensure_signed(origin)?;
1941
1942			// virtual stakers should not be allowed to be reaped.
1943			ensure!(!Self::is_virtual_staker(&stash), Error::<T>::VirtualStakerNotAllowed);
1944
1945			let min_chilled_bond = Self::min_chilled_bond();
1946			let origin_balance = asset::total_balance::<T>(&stash);
1947			let ledger_total =
1948				Self::ledger(Stash(stash.clone())).map(|l| l.total).unwrap_or_default();
1949			let reapable = origin_balance < min_chilled_bond ||
1950				origin_balance.is_zero() ||
1951				ledger_total < min_chilled_bond ||
1952				ledger_total.is_zero();
1953			ensure!(reapable, Error::<T>::FundedTarget);
1954
1955			// Remove all staking-related information and lock.
1956			Self::kill_stash(&stash)?;
1957
1958			Ok(Pays::No.into())
1959		}
1960
1961		/// Remove the given nominations from the calling validator.
1962		///
1963		/// Effects will be felt at the beginning of the next era.
1964		///
1965		/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
1966		///
1967		/// - `who`: A list of nominator stash accounts who are nominating this validator which
1968		///   should no longer be nominating this validator.
1969		///
1970		/// Note: Making this call only makes sense if you first set the validator preferences to
1971		/// block any further nominations.
1972		#[pallet::call_index(21)]
1973		#[pallet::weight(T::WeightInfo::kick(who.len() as u32))]
1974		pub fn kick(origin: OriginFor<T>, who: Vec<AccountIdLookupOf<T>>) -> DispatchResult {
1975			let controller = ensure_signed(origin)?;
1976			let ledger = Self::ledger(Controller(controller))?;
1977			let stash = &ledger.stash;
1978
1979			for nom_stash in who
1980				.into_iter()
1981				.map(T::Lookup::lookup)
1982				.collect::<Result<Vec<T::AccountId>, _>>()?
1983				.into_iter()
1984			{
1985				Nominators::<T>::mutate(&nom_stash, |maybe_nom| {
1986					if let Some(ref mut nom) = maybe_nom {
1987						if let Some(pos) = nom.targets.iter().position(|v| v == stash) {
1988							nom.targets.swap_remove(pos);
1989							Self::deposit_event(Event::<T>::Kicked {
1990								nominator: nom_stash.clone(),
1991								stash: stash.clone(),
1992							});
1993						}
1994					}
1995				});
1996			}
1997
1998			Ok(())
1999		}
2000
2001		/// Update the various staking configurations .
2002		///
2003		/// * `min_nominator_bond`: The minimum active bond needed to be a nominator.
2004		/// * `min_validator_bond`: The minimum active bond needed to be a validator.
2005		/// * `max_nominator_count`: The max number of users who can be a nominator at once. When
2006		///   set to `None`, no limit is enforced.
2007		/// * `max_validator_count`: The max number of users who can be a validator at once. When
2008		///   set to `None`, no limit is enforced.
2009		/// * `chill_threshold`: The ratio of `max_nominator_count` or `max_validator_count` which
2010		///   should be filled in order for the `chill_other` transaction to work.
2011		/// * `min_commission`: The minimum amount of commission that each validators must maintain.
2012		///   This is checked only upon calling `validate`. Existing validators are not affected.
2013		///
2014		/// RuntimeOrigin must be Root to call this function.
2015		///
2016		/// NOTE: Existing nominators and validators will not be affected by this update.
2017		/// to kick people under the new limits, `chill_other` should be called.
2018		// We assume the worst case for this call is either: all items are set or all items are
2019		// removed.
2020		#[pallet::call_index(22)]
2021		#[pallet::weight(
2022			T::WeightInfo::set_staking_configs_all_set()
2023				.max(T::WeightInfo::set_staking_configs_all_remove())
2024		)]
2025		pub fn set_staking_configs(
2026			origin: OriginFor<T>,
2027			min_nominator_bond: ConfigOp<BalanceOf<T>>,
2028			min_validator_bond: ConfigOp<BalanceOf<T>>,
2029			max_nominator_count: ConfigOp<u32>,
2030			max_validator_count: ConfigOp<u32>,
2031			chill_threshold: ConfigOp<Percent>,
2032			min_commission: ConfigOp<Perbill>,
2033			max_staked_rewards: ConfigOp<Percent>,
2034		) -> DispatchResult {
2035			ensure_root(origin)?;
2036
2037			macro_rules! config_op_exp {
2038				($storage:ty, $op:ident) => {
2039					match $op {
2040						ConfigOp::Noop => (),
2041						ConfigOp::Set(v) => <$storage>::put(v),
2042						ConfigOp::Remove => <$storage>::kill(),
2043					}
2044				};
2045			}
2046
2047			config_op_exp!(MinNominatorBond<T>, min_nominator_bond);
2048			config_op_exp!(MinValidatorBond<T>, min_validator_bond);
2049			config_op_exp!(MaxNominatorsCount<T>, max_nominator_count);
2050			config_op_exp!(MaxValidatorsCount<T>, max_validator_count);
2051			config_op_exp!(ChillThreshold<T>, chill_threshold);
2052			config_op_exp!(MinCommission<T>, min_commission);
2053			config_op_exp!(MaxStakedRewards<T>, max_staked_rewards);
2054			Ok(())
2055		}
2056		/// Declare a `controller` to stop participating as either a validator or nominator.
2057		///
2058		/// Effects will be felt at the beginning of the next era.
2059		///
2060		/// The dispatch origin for this call must be _Signed_, but can be called by anyone.
2061		///
2062		/// If the caller is the same as the controller being targeted, then no further checks are
2063		/// enforced, and this function behaves just like `chill`.
2064		///
2065		/// If the caller is different than the controller being targeted, the following conditions
2066		/// must be met:
2067		///
2068		/// * `controller` must belong to a nominator who has become non-decodable,
2069		///
2070		/// Or:
2071		///
2072		/// * A `ChillThreshold` must be set and checked which defines how close to the max
2073		///   nominators or validators we must reach before users can start chilling one-another.
2074		/// * A `MaxNominatorCount` and `MaxValidatorCount` must be set which is used to determine
2075		///   how close we are to the threshold.
2076		/// * A `MinNominatorBond` and `MinValidatorBond` must be set and checked, which determines
2077		///   if this is a person that should be chilled because they have not met the threshold
2078		///   bond required.
2079		///
2080		/// This can be helpful if bond requirements are updated, and we need to remove old users
2081		/// who do not satisfy these requirements.
2082		#[pallet::call_index(23)]
2083		#[pallet::weight(T::WeightInfo::chill_other())]
2084		pub fn chill_other(origin: OriginFor<T>, stash: T::AccountId) -> DispatchResult {
2085			// Anyone can call this function.
2086			let caller = ensure_signed(origin)?;
2087			let ledger = Self::ledger(Stash(stash.clone()))?;
2088			let controller = ledger
2089				.controller()
2090				.defensive_proof(
2091					"Ledger's controller field didn't exist. The controller should have been fetched using StakingLedger.",
2092				)
2093				.ok_or(Error::<T>::NotController)?;
2094
2095			// In order for one user to chill another user, the following conditions must be met:
2096			//
2097			// * `controller` belongs to a nominator who has become non-decodable,
2098			//
2099			// Or
2100			//
2101			// * A `ChillThreshold` is set which defines how close to the max nominators or
2102			//   validators we must reach before users can start chilling one-another.
2103			// * A `MaxNominatorCount` and `MaxValidatorCount` which is used to determine how close
2104			//   we are to the threshold.
2105			// * A `MinNominatorBond` and `MinValidatorBond` which is the final condition checked to
2106			//   determine this is a person that should be chilled because they have not met the
2107			//   threshold bond required.
2108			//
2109			// Otherwise, if caller is the same as the controller, this is just like `chill`.
2110
2111			if Nominators::<T>::contains_key(&stash) && Nominators::<T>::get(&stash).is_none() {
2112				Self::chill_stash(&stash);
2113				return Ok(());
2114			}
2115
2116			if caller != controller {
2117				let threshold = ChillThreshold::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
2118				let min_active_bond = if Nominators::<T>::contains_key(&stash) {
2119					let max_nominator_count =
2120						MaxNominatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
2121					let current_nominator_count = Nominators::<T>::count();
2122					ensure!(
2123						threshold * max_nominator_count < current_nominator_count,
2124						Error::<T>::CannotChillOther
2125					);
2126					Self::min_nominator_bond()
2127				} else if Validators::<T>::contains_key(&stash) {
2128					let max_validator_count =
2129						MaxValidatorsCount::<T>::get().ok_or(Error::<T>::CannotChillOther)?;
2130					let current_validator_count = Validators::<T>::count();
2131					ensure!(
2132						threshold * max_validator_count < current_validator_count,
2133						Error::<T>::CannotChillOther
2134					);
2135					Self::min_validator_bond()
2136				} else {
2137					Zero::zero()
2138				};
2139
2140				ensure!(ledger.active < min_active_bond, Error::<T>::CannotChillOther);
2141			}
2142
2143			Self::chill_stash(&stash);
2144			Ok(())
2145		}
2146
2147		/// Force a validator to have at least the minimum commission. This will not affect a
2148		/// validator who already has a commission greater than or equal to the minimum. Any account
2149		/// can call this.
2150		#[pallet::call_index(24)]
2151		#[pallet::weight(T::WeightInfo::force_apply_min_commission())]
2152		pub fn force_apply_min_commission(
2153			origin: OriginFor<T>,
2154			validator_stash: T::AccountId,
2155		) -> DispatchResult {
2156			ensure_signed(origin)?;
2157			let min_commission = MinCommission::<T>::get();
2158			Validators::<T>::try_mutate_exists(validator_stash, |maybe_prefs| {
2159				maybe_prefs
2160					.as_mut()
2161					.map(|prefs| {
2162						(prefs.commission < min_commission)
2163							.then(|| prefs.commission = min_commission)
2164					})
2165					.ok_or(Error::<T>::NotStash)
2166			})?;
2167			Ok(())
2168		}
2169
2170		/// Sets the minimum amount of commission that each validators must maintain.
2171		///
2172		/// This call has lower privilege requirements than `set_staking_config` and can be called
2173		/// by the `T::AdminOrigin`. Root can always call this.
2174		#[pallet::call_index(25)]
2175		#[pallet::weight(T::WeightInfo::set_min_commission())]
2176		pub fn set_min_commission(origin: OriginFor<T>, new: Perbill) -> DispatchResult {
2177			T::AdminOrigin::ensure_origin(origin)?;
2178			MinCommission::<T>::put(new);
2179			Ok(())
2180		}
2181
2182		/// Pay out a page of the stakers behind a validator for the given era and page.
2183		///
2184		/// - `validator_stash` is the stash account of the validator.
2185		/// - `era` may be any era between `[current_era - history_depth; current_era]`.
2186		/// - `page` is the page index of nominators to pay out with value between 0 and
2187		///   `num_nominators / T::MaxExposurePageSize`.
2188		///
2189		/// The origin of this call must be _Signed_. Any account can call this function, even if
2190		/// it is not one of the stakers.
2191		///
2192		/// If a validator has more than [`Config::MaxExposurePageSize`] nominators backing
2193		/// them, then the list of nominators is paged, with each page being capped at
2194		/// [`Config::MaxExposurePageSize`.] If a validator has more than one page of nominators,
2195		/// the call needs to be made for each page separately in order for all the nominators
2196		/// backing a validator to receive the reward. The nominators are not sorted across pages
2197		/// and so it should not be assumed the highest staker would be on the topmost page and vice
2198		/// versa. If rewards are not claimed in [`Config::HistoryDepth`] eras, they are lost.
2199		#[pallet::call_index(26)]
2200		#[pallet::weight(T::WeightInfo::payout_stakers_alive_staked(T::MaxExposurePageSize::get()))]
2201		pub fn payout_stakers_by_page(
2202			origin: OriginFor<T>,
2203			validator_stash: T::AccountId,
2204			era: EraIndex,
2205			page: Page,
2206		) -> DispatchResultWithPostInfo {
2207			ensure_signed(origin)?;
2208			Self::do_payout_stakers_by_page(validator_stash, era, page)
2209		}
2210
2211		/// Migrates an account's `RewardDestination::Controller` to
2212		/// `RewardDestination::Account(controller)`.
2213		///
2214		/// Effects will be felt instantly (as soon as this function is completed successfully).
2215		///
2216		/// This will waive the transaction fee if the `payee` is successfully migrated.
2217		#[pallet::call_index(27)]
2218		#[pallet::weight(T::WeightInfo::update_payee())]
2219		pub fn update_payee(
2220			origin: OriginFor<T>,
2221			controller: T::AccountId,
2222		) -> DispatchResultWithPostInfo {
2223			let _ = ensure_signed(origin)?;
2224			let ledger = Self::ledger(StakingAccount::Controller(controller.clone()))?;
2225
2226			ensure!(
2227				(Payee::<T>::get(&ledger.stash) == {
2228					#[allow(deprecated)]
2229					Some(RewardDestination::Controller)
2230				}),
2231				Error::<T>::NotController
2232			);
2233
2234			let _ = ledger
2235				.set_payee(RewardDestination::Account(controller))
2236				.defensive_proof("ledger should have been previously retrieved from storage.")?;
2237
2238			Ok(Pays::No.into())
2239		}
2240
2241		/// Updates a batch of controller accounts to their corresponding stash account if they are
2242		/// not the same. Ignores any controller accounts that do not exist, and does not operate if
2243		/// the stash and controller are already the same.
2244		///
2245		/// Effects will be felt instantly (as soon as this function is completed successfully).
2246		///
2247		/// The dispatch origin must be `T::AdminOrigin`.
2248		#[pallet::call_index(28)]
2249		#[pallet::weight(T::WeightInfo::deprecate_controller_batch(controllers.len() as u32))]
2250		pub fn deprecate_controller_batch(
2251			origin: OriginFor<T>,
2252			controllers: BoundedVec<T::AccountId, T::MaxControllersInDeprecationBatch>,
2253		) -> DispatchResultWithPostInfo {
2254			T::AdminOrigin::ensure_origin(origin)?;
2255
2256			// Ignore controllers that do not exist or are already the same as stash.
2257			let filtered_batch_with_ledger: Vec<_> = controllers
2258				.iter()
2259				.filter_map(|controller| {
2260					let ledger = Self::ledger(StakingAccount::Controller(controller.clone()));
2261					ledger.ok().map_or(None, |ledger| {
2262						// If the controller `RewardDestination` is still the deprecated
2263						// `Controller` variant, skip deprecating this account.
2264						let payee_deprecated = Payee::<T>::get(&ledger.stash) == {
2265							#[allow(deprecated)]
2266							Some(RewardDestination::Controller)
2267						};
2268
2269						if ledger.stash != *controller && !payee_deprecated {
2270							Some(ledger)
2271						} else {
2272							None
2273						}
2274					})
2275				})
2276				.collect();
2277
2278			// Update unique pairs.
2279			let mut failures = 0;
2280			for ledger in filtered_batch_with_ledger {
2281				let _ = ledger.clone().set_controller_to_stash().map_err(|_| failures += 1);
2282			}
2283			Self::deposit_event(Event::<T>::ControllerBatchDeprecated { failures });
2284
2285			Ok(Some(T::WeightInfo::deprecate_controller_batch(controllers.len() as u32)).into())
2286		}
2287
2288		/// Restores the state of a ledger which is in an inconsistent state.
2289		///
2290		/// The requirements to restore a ledger are the following:
2291		/// * The stash is bonded; or
2292		/// * The stash is not bonded but it has a staking lock left behind; or
2293		/// * If the stash has an associated ledger and its state is inconsistent; or
2294		/// * If the ledger is not corrupted *but* its staking lock is out of sync.
2295		///
2296		/// The `maybe_*` input parameters will overwrite the corresponding data and metadata of the
2297		/// ledger associated with the stash. If the input parameters are not set, the ledger will
2298		/// be reset values from on-chain state.
2299		#[pallet::call_index(29)]
2300		#[pallet::weight(T::WeightInfo::restore_ledger())]
2301		pub fn restore_ledger(
2302			origin: OriginFor<T>,
2303			stash: T::AccountId,
2304			maybe_controller: Option<T::AccountId>,
2305			maybe_total: Option<BalanceOf<T>>,
2306			maybe_unlocking: Option<BoundedVec<UnlockChunk<BalanceOf<T>>, T::MaxUnlockingChunks>>,
2307		) -> DispatchResult {
2308			T::AdminOrigin::ensure_origin(origin)?;
2309
2310			// cannot restore ledger for virtual stakers.
2311			ensure!(!Self::is_virtual_staker(&stash), Error::<T>::VirtualStakerNotAllowed);
2312
2313			let current_lock = asset::staked::<T>(&stash);
2314			let stash_balance = asset::stakeable_balance::<T>(&stash);
2315
2316			let (new_controller, new_total) = match Self::inspect_bond_state(&stash) {
2317				Ok(LedgerIntegrityState::Corrupted) => {
2318					let new_controller = maybe_controller.unwrap_or(stash.clone());
2319
2320					let new_total = if let Some(total) = maybe_total {
2321						let new_total = total.min(stash_balance);
2322						// enforce hold == ledger.amount.
2323						asset::update_stake::<T>(&stash, new_total)?;
2324						new_total
2325					} else {
2326						current_lock
2327					};
2328
2329					Ok((new_controller, new_total))
2330				},
2331				Ok(LedgerIntegrityState::CorruptedKilled) => {
2332					if current_lock == Zero::zero() {
2333						// this case needs to restore both lock and ledger, so the new total needs
2334						// to be given by the called since there's no way to restore the total
2335						// on-chain.
2336						ensure!(maybe_total.is_some(), Error::<T>::CannotRestoreLedger);
2337						Ok((
2338							stash.clone(),
2339							maybe_total.expect("total exists as per the check above; qed."),
2340						))
2341					} else {
2342						Ok((stash.clone(), current_lock))
2343					}
2344				},
2345				Ok(LedgerIntegrityState::LockCorrupted) => {
2346					// ledger is not corrupted but its locks are out of sync. In this case, we need
2347					// to enforce a new ledger.total and staking lock for this stash.
2348					let new_total =
2349						maybe_total.ok_or(Error::<T>::CannotRestoreLedger)?.min(stash_balance);
2350					asset::update_stake::<T>(&stash, new_total)?;
2351
2352					Ok((stash.clone(), new_total))
2353				},
2354				Err(Error::<T>::BadState) => {
2355					// the stash and ledger do not exist but lock is lingering.
2356					asset::kill_stake::<T>(&stash)?;
2357					ensure!(
2358						Self::inspect_bond_state(&stash) == Err(Error::<T>::NotStash),
2359						Error::<T>::BadState
2360					);
2361
2362					return Ok(());
2363				},
2364				Ok(LedgerIntegrityState::Ok) | Err(_) => Err(Error::<T>::CannotRestoreLedger),
2365			}?;
2366
2367			// re-bond stash and controller tuple.
2368			Bonded::<T>::insert(&stash, &new_controller);
2369
2370			// resoter ledger state.
2371			let mut ledger = StakingLedger::<T>::new(stash.clone(), new_total);
2372			ledger.controller = Some(new_controller);
2373			ledger.unlocking = maybe_unlocking.unwrap_or_default();
2374			ledger.update()?;
2375
2376			ensure!(
2377				Self::inspect_bond_state(&stash) == Ok(LedgerIntegrityState::Ok),
2378				Error::<T>::BadState
2379			);
2380			Ok(())
2381		}
2382
2383		/// Migrates permissionlessly a stash from locks to holds.
2384		///
2385		/// This removes the old lock on the stake and creates a hold on it atomically. If all
2386		/// stake cannot be held, the best effort is made to hold as much as possible. The remaining
2387		/// stake is removed from the ledger.
2388		///
2389		/// The fee is waived if the migration is successful.
2390		#[pallet::call_index(30)]
2391		#[pallet::weight(T::WeightInfo::migrate_currency())]
2392		pub fn migrate_currency(
2393			origin: OriginFor<T>,
2394			stash: T::AccountId,
2395		) -> DispatchResultWithPostInfo {
2396			let _ = ensure_signed(origin)?;
2397			Self::do_migrate_currency(&stash)?;
2398
2399			// Refund the transaction fee if successful.
2400			Ok(Pays::No.into())
2401		}
2402
2403		/// Manually applies a deferred slash for a given era.
2404		///
2405		/// Normally, slashes are automatically applied shortly after the start of the `slash_era`.
2406		/// This function exists as a **fallback mechanism** in case slashes were not applied due to
2407		/// unexpected reasons. It allows anyone to manually apply an unapplied slash.
2408		///
2409		/// ## Parameters
2410		/// - `slash_era`: The staking era in which the slash was originally scheduled.
2411		/// - `slash_key`: A unique identifier for the slash, represented as a tuple:
2412		///   - `stash`: The stash account of the validator being slashed.
2413		///   - `slash_fraction`: The fraction of the stake that was slashed.
2414		///   - `page_index`: The index of the exposure page being processed.
2415		///
2416		/// ## Behavior
2417		/// - The function is **permissionless**—anyone can call it.
2418		/// - The `slash_era` **must be the current era or a past era**. If it is in the future, the
2419		///   call fails with `EraNotStarted`.
2420		/// - The fee is waived if the slash is successfully applied.
2421		///
2422		/// ## Future Improvement
2423		/// - Implement an **off-chain worker (OCW) task** to automatically apply slashes when there
2424		///   is unused block space, improving efficiency.
2425		#[pallet::call_index(31)]
2426		#[pallet::weight(T::WeightInfo::apply_slash())]
2427		pub fn apply_slash(
2428			origin: OriginFor<T>,
2429			slash_era: EraIndex,
2430			slash_key: (T::AccountId, Perbill, u32),
2431		) -> DispatchResultWithPostInfo {
2432			let _ = ensure_signed(origin)?;
2433			let active_era = ActiveEra::<T>::get().map(|a| a.index).unwrap_or_default();
2434			ensure!(slash_era <= active_era, Error::<T>::EraNotStarted);
2435			let unapplied_slash = UnappliedSlashes::<T>::take(&slash_era, &slash_key)
2436				.ok_or(Error::<T>::InvalidSlashRecord)?;
2437			slashing::apply_slash::<T>(unapplied_slash, slash_era);
2438
2439			Ok(Pays::No.into())
2440		}
2441
2442		/// Adjusts the staking ledger by withdrawing any excess staked amount.
2443		///
2444		/// This function corrects cases where a user's recorded stake in the ledger
2445		/// exceeds their actual staked funds. This situation can arise due to cases such as
2446		/// external slashing by another pallet, leading to an inconsistency between the ledger
2447		/// and the actual stake.
2448		#[pallet::call_index(32)]
2449		#[pallet::weight(T::DbWeight::get().reads_writes(2, 1))]
2450		pub fn withdraw_overstake(origin: OriginFor<T>, stash: T::AccountId) -> DispatchResult {
2451			use sp_runtime::Saturating;
2452			let _ = ensure_signed(origin)?;
2453
2454			let ledger = Self::ledger(Stash(stash.clone()))?;
2455			let actual_stake = asset::staked::<T>(&stash);
2456			let force_withdraw_amount = ledger.total.defensive_saturating_sub(actual_stake);
2457
2458			// ensure there is something to force unstake.
2459			ensure!(!force_withdraw_amount.is_zero(), Error::<T>::BoundNotMet);
2460
2461			// we ignore if active is 0. It implies the locked amount is not actively staked. The
2462			// account can still get away from potential slash, but we can't do much better here.
2463			StakingLedger {
2464				total: actual_stake,
2465				active: ledger.active.saturating_sub(force_withdraw_amount),
2466				..ledger
2467			}
2468			.update()?;
2469
2470			Self::deposit_event(Event::<T>::Withdrawn { stash, amount: force_withdraw_amount });
2471
2472			Ok(())
2473		}
2474	}
2475}