// This file is part of Substrate. // Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Staking FRAME Pallet. use frame_election_provider_support::SortedListProvider; use frame_support::{ pallet_prelude::*, traits::{ Currency, CurrencyToVote, EnsureOrigin, EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, UnixTime, }, weights::Weight, }; use frame_system::{ensure_root, ensure_signed, offchain::SendTransactionTypes, pallet_prelude::*}; use sp_runtime::{ traits::{CheckedSub, SaturatedConversion, StaticLookup, Zero}, DispatchError, Perbill, Percent, }; use sp_staking::SessionIndex; use sp_std::{convert::From, prelude::*, result}; mod impls; pub use impls::*; use crate::{ log, migrations, slashing, weights::WeightInfo, ActiveEraInfo, BalanceOf, EraIndex, EraPayout, EraRewardPoints, Exposure, Forcing, NegativeImbalanceOf, Nominations, PositiveImbalanceOf, Releases, RewardDestination, SessionInterface, StakingLedger, UnappliedSlash, UnlockChunk, ValidatorPrefs, }; pub const MAX_UNLOCKING_CHUNKS: usize = 32; const STAKING_ID: LockIdentifier = *b"staking "; #[frame_support::pallet] pub mod pallet { use super::*; #[pallet::pallet] #[pallet::generate_store(pub(crate) trait Store)] pub struct Pallet(_); #[pallet::config] pub trait Config: frame_system::Config + SendTransactionTypes> { /// The staking balance. type Currency: LockableCurrency; /// Time used for computing era duration. /// /// It is guaranteed to start being called from the first `on_finalize`. Thus value at /// genesis is not used. type UnixTime: UnixTime; /// Convert a balance into a number used for election calculation. This must fit into a /// `u64` but is allowed to be sensibly lossy. The `u64` is used to communicate with the /// [`sp_npos_elections`] crate which accepts u64 numbers and does operations in 128. /// Consequently, the backward convert is used convert the u128s from sp-elections back to a /// [`BalanceOf`]. type CurrencyToVote: CurrencyToVote>; /// Something that provides the election functionality. type ElectionProvider: frame_election_provider_support::ElectionProvider< Self::AccountId, Self::BlockNumber, // we only accept an election provider that has staking as data provider. DataProvider = Pallet, >; /// Something that provides the election functionality at genesis. type GenesisElectionProvider: frame_election_provider_support::ElectionProvider< Self::AccountId, Self::BlockNumber, DataProvider = Pallet, >; /// Maximum number of nominations per nominator. const MAX_NOMINATIONS: u32; /// Tokens have been minted and are unused for validator-reward. /// See [Era payout](./index.html#era-payout). type RewardRemainder: OnUnbalanced>; /// The overarching event type. type Event: From> + IsType<::Event>; /// Handler for the unbalanced reduction when slashing a staker. type Slash: OnUnbalanced>; /// Handler for the unbalanced increment when rewarding a staker. type Reward: OnUnbalanced>; /// Number of sessions per era. #[pallet::constant] type SessionsPerEra: Get; /// Number of eras that staked funds must remain bonded for. #[pallet::constant] type BondingDuration: Get; /// Number of eras that slashes are deferred by, after computation. /// /// This should be less than the bonding duration. Set to 0 if slashes /// should be applied immediately, without opportunity for intervention. #[pallet::constant] type SlashDeferDuration: Get; /// The origin which can cancel a deferred slash. Root can always do this. type SlashCancelOrigin: EnsureOrigin; /// Interface for interacting with a session pallet. type SessionInterface: SessionInterface; /// The payout for validators and the system for the current era. /// See [Era payout](./index.html#era-payout). type EraPayout: EraPayout>; /// Something that can estimate the next session change, accurately or as a best effort /// guess. type NextNewSession: EstimateNextNewSession; /// The maximum number of nominators rewarded for each validator. /// /// For each validator only the `$MaxNominatorRewardedPerValidator` biggest stakers can /// claim their reward. This used to limit the i/o cost for the nominator payout. #[pallet::constant] type MaxNominatorRewardedPerValidator: Get; /// The fraction of the validator set that is safe to be offending. /// After the threshold is reached a new era will be forced. type OffendingValidatorsThreshold: Get; /// Something that can provide a sorted list of voters in a somewhat sorted way. The /// original use case for this was designed with [`pallet_bags_list::Pallet`] in mind. If /// the bags-list is not desired, [`impls::UseNominatorsMap`] is likely the desired option. type SortedListProvider: SortedListProvider; /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } #[pallet::extra_constants] impl Pallet { // TODO: rename to snake case after https://github.com/paritytech/substrate/issues/8826 fixed. #[allow(non_snake_case)] fn MaxNominations() -> u32 { T::MAX_NOMINATIONS } } #[pallet::type_value] pub(crate) fn HistoryDepthOnEmpty() -> u32 { 84u32 } /// Number of eras to keep in history. /// /// Information is kept for eras in `[current_era - history_depth; current_era]`. /// /// Must be more than the number of eras delayed by session otherwise. I.e. active era must /// always be in history. I.e. `active_era > current_era - history_depth` must be /// guaranteed. #[pallet::storage] #[pallet::getter(fn history_depth)] pub(crate) type HistoryDepth = StorageValue<_, u32, ValueQuery, HistoryDepthOnEmpty>; /// The ideal number of staking participants. #[pallet::storage] #[pallet::getter(fn validator_count)] pub type ValidatorCount = StorageValue<_, u32, ValueQuery>; /// Minimum number of staking participants before emergency conditions are imposed. #[pallet::storage] #[pallet::getter(fn minimum_validator_count)] pub type MinimumValidatorCount = StorageValue<_, u32, ValueQuery>; /// Any validators that may never be slashed or forcibly kicked. It's a Vec since they're /// easy to initialize and the performance hit is minimal (we expect no more than four /// invulnerables) and restricted to testnets. #[pallet::storage] #[pallet::getter(fn invulnerables)] pub type Invulnerables = StorageValue<_, Vec, ValueQuery>; /// Map from all locked "stash" accounts to the controller account. #[pallet::storage] #[pallet::getter(fn bonded)] pub type Bonded = StorageMap<_, Twox64Concat, T::AccountId, T::AccountId>; /// The minimum active bond to become and maintain the role of a nominator. #[pallet::storage] pub type MinNominatorBond = StorageValue<_, BalanceOf, ValueQuery>; /// The minimum active bond to become and maintain the role of a validator. #[pallet::storage] pub type MinValidatorBond = StorageValue<_, BalanceOf, ValueQuery>; /// Map from all (unlocked) "controller" accounts to the info regarding the staking. #[pallet::storage] #[pallet::getter(fn ledger)] pub type Ledger = StorageMap<_, Blake2_128Concat, T::AccountId, StakingLedger>>; /// Where the reward payment should be made. Keyed by stash. #[pallet::storage] #[pallet::getter(fn payee)] pub type Payee = StorageMap<_, Twox64Concat, T::AccountId, RewardDestination, ValueQuery>; /// The map from (wannabe) validator stash key to the preferences of that validator. /// /// When updating this storage item, you must also update the `CounterForValidators`. #[pallet::storage] #[pallet::getter(fn validators)] pub type Validators = StorageMap<_, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery>; /// A tracker to keep count of the number of items in the `Validators` map. #[pallet::storage] pub type CounterForValidators = StorageValue<_, u32, ValueQuery>; /// The maximum validator count before we stop allowing new validators to join. /// /// When this value is not set, no limits are enforced. #[pallet::storage] pub type MaxValidatorsCount = StorageValue<_, u32, OptionQuery>; /// The map from nominator stash key to the set of stash keys of all validators to nominate. /// /// When updating this storage item, you must also update the `CounterForNominators`. #[pallet::storage] #[pallet::getter(fn nominators)] pub type Nominators = StorageMap<_, Twox64Concat, T::AccountId, Nominations>; /// A tracker to keep count of the number of items in the `Nominators` map. #[pallet::storage] pub type CounterForNominators = StorageValue<_, u32, ValueQuery>; /// The maximum nominator count before we stop allowing new validators to join. /// /// When this value is not set, no limits are enforced. #[pallet::storage] pub type MaxNominatorsCount = StorageValue<_, u32, OptionQuery>; /// The current era index. /// /// This is the latest planned era, depending on how the Session pallet queues the validator /// set, it might be active or not. #[pallet::storage] #[pallet::getter(fn current_era)] pub type CurrentEra = StorageValue<_, EraIndex>; /// The active era information, it holds index and start. /// /// The active era is the era being currently rewarded. Validator set of this era must be /// equal to [`SessionInterface::validators`]. #[pallet::storage] #[pallet::getter(fn active_era)] pub type ActiveEra = StorageValue<_, ActiveEraInfo>; /// The session index at which the era start for the last `HISTORY_DEPTH` eras. /// /// Note: This tracks the starting session (i.e. session index when era start being active) /// for the eras in `[CurrentEra - HISTORY_DEPTH, CurrentEra]`. #[pallet::storage] #[pallet::getter(fn eras_start_session_index)] pub type ErasStartSessionIndex = StorageMap<_, Twox64Concat, EraIndex, SessionIndex>; /// Exposure of validator at era. /// /// This is keyed first by the era index to allow bulk deletion and then the stash account. /// /// Is it removed after `HISTORY_DEPTH` eras. /// If stakers hasn't been set or has been removed then empty exposure is returned. #[pallet::storage] #[pallet::getter(fn eras_stakers)] pub type ErasStakers = StorageDoubleMap< _, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, Exposure>, ValueQuery, >; /// Clipped Exposure of validator at era. /// /// This is similar to [`ErasStakers`] but number of nominators exposed is reduced to the /// `T::MaxNominatorRewardedPerValidator` biggest stakers. /// (Note: the field `total` and `own` of the exposure remains unchanged). /// This is used to limit the i/o cost for the nominator payout. /// /// This is keyed fist by the era index to allow bulk deletion and then the stash account. /// /// Is it removed after `HISTORY_DEPTH` eras. /// If stakers hasn't been set or has been removed then empty exposure is returned. #[pallet::storage] #[pallet::getter(fn eras_stakers_clipped)] pub type ErasStakersClipped = StorageDoubleMap< _, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, Exposure>, ValueQuery, >; /// Similar to `ErasStakers`, this holds the preferences of validators. /// /// This is keyed first by the era index to allow bulk deletion and then the stash account. /// /// Is it removed after `HISTORY_DEPTH` eras. // If prefs hasn't been set or has been removed then 0 commission is returned. #[pallet::storage] #[pallet::getter(fn eras_validator_prefs)] pub type ErasValidatorPrefs = StorageDoubleMap< _, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, ValidatorPrefs, ValueQuery, >; /// The total validator era payout for the last `HISTORY_DEPTH` eras. /// /// Eras that haven't finished yet or has been removed doesn't have reward. #[pallet::storage] #[pallet::getter(fn eras_validator_reward)] pub type ErasValidatorReward = StorageMap<_, Twox64Concat, EraIndex, BalanceOf>; /// Rewards for the last `HISTORY_DEPTH` eras. /// If reward hasn't been set or has been removed then 0 reward is returned. #[pallet::storage] #[pallet::getter(fn eras_reward_points)] pub type ErasRewardPoints = StorageMap<_, Twox64Concat, EraIndex, EraRewardPoints, ValueQuery>; /// The total amount staked for the last `HISTORY_DEPTH` eras. /// If total hasn't been set or has been removed then 0 stake is returned. #[pallet::storage] #[pallet::getter(fn eras_total_stake)] pub type ErasTotalStake = StorageMap<_, Twox64Concat, EraIndex, BalanceOf, ValueQuery>; /// Mode of era forcing. #[pallet::storage] #[pallet::getter(fn force_era)] pub type ForceEra = StorageValue<_, Forcing, ValueQuery>; /// The percentage of the slash that is distributed to reporters. /// /// The rest of the slashed value is handled by the `Slash`. #[pallet::storage] #[pallet::getter(fn slash_reward_fraction)] pub type SlashRewardFraction = StorageValue<_, Perbill, ValueQuery>; /// The amount of currency given to reporters of a slash event which was /// canceled by extraordinary circumstances (e.g. governance). #[pallet::storage] #[pallet::getter(fn canceled_payout)] pub type CanceledSlashPayout = StorageValue<_, BalanceOf, ValueQuery>; /// All unapplied slashes that are queued for later. #[pallet::storage] pub type UnappliedSlashes = StorageMap< _, Twox64Concat, EraIndex, Vec>>, ValueQuery, >; /// A mapping from still-bonded eras to the first session index of that era. /// /// Must contains information for eras for the range: /// `[active_era - bounding_duration; active_era]` #[pallet::storage] pub(crate) type BondedEras = StorageValue<_, Vec<(EraIndex, SessionIndex)>, ValueQuery>; /// All slashing events on validators, mapped by era to the highest slash proportion /// and slash value of the era. #[pallet::storage] pub(crate) type ValidatorSlashInEra = StorageDoubleMap< _, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, (Perbill, BalanceOf), >; /// All slashing events on nominators, mapped by era to the highest slash value of the era. #[pallet::storage] pub(crate) type NominatorSlashInEra = StorageDoubleMap<_, Twox64Concat, EraIndex, Twox64Concat, T::AccountId, BalanceOf>; /// Slashing spans for stash accounts. #[pallet::storage] pub(crate) type SlashingSpans = StorageMap<_, Twox64Concat, T::AccountId, slashing::SlashingSpans>; /// Records information about the maximum slash of a stash within a slashing span, /// as well as how much reward has been paid out. #[pallet::storage] pub(crate) type SpanSlash = StorageMap< _, Twox64Concat, (T::AccountId, slashing::SpanIndex), slashing::SpanRecord>, ValueQuery, >; /// The earliest era for which we have a pending, unapplied slash. #[pallet::storage] pub(crate) type EarliestUnappliedSlash = StorageValue<_, EraIndex>; /// The last planned session scheduled by the session pallet. /// /// This is basically in sync with the call to [`pallet_session::SessionManager::new_session`]. #[pallet::storage] #[pallet::getter(fn current_planned_session)] pub type CurrentPlannedSession = StorageValue<_, SessionIndex, ValueQuery>; /// Indices of validators that have offended in the active era and whether they are currently /// disabled. /// /// This value should be a superset of disabled validators since not all offences lead to the /// validator being disabled (if there was no slash). This is needed to track the percentage of /// validators that have offended in the current era, ensuring a new era is forced if /// `OffendingValidatorsThreshold` is reached. The vec is always kept sorted so that we can find /// whether a given validator has previously offended using binary search. It gets cleared when /// the era ends. #[pallet::storage] #[pallet::getter(fn offending_validators)] pub type OffendingValidators = StorageValue<_, Vec<(u32, bool)>, ValueQuery>; /// True if network has been upgraded to this version. /// Storage version of the pallet. /// /// This is set to v7.0.0 for new networks. #[pallet::storage] pub(crate) type StorageVersion = StorageValue<_, Releases, ValueQuery>; /// The threshold for when users can start calling `chill_other` for other validators / /// nominators. The threshold is compared to the actual number of validators / nominators /// (`CountFor*`) in the system compared to the configured max (`Max*Count`). #[pallet::storage] pub(crate) type ChillThreshold = StorageValue<_, Percent, OptionQuery>; #[pallet::genesis_config] pub struct GenesisConfig { pub history_depth: u32, pub validator_count: u32, pub minimum_validator_count: u32, pub invulnerables: Vec, pub force_era: Forcing, pub slash_reward_fraction: Perbill, pub canceled_payout: BalanceOf, pub stakers: Vec<(T::AccountId, T::AccountId, BalanceOf, crate::StakerStatus)>, pub min_nominator_bond: BalanceOf, pub min_validator_bond: BalanceOf, } #[cfg(feature = "std")] impl Default for GenesisConfig { fn default() -> Self { GenesisConfig { history_depth: 84u32, validator_count: Default::default(), minimum_validator_count: Default::default(), invulnerables: Default::default(), force_era: Default::default(), slash_reward_fraction: Default::default(), canceled_payout: Default::default(), stakers: Default::default(), min_nominator_bond: Default::default(), min_validator_bond: Default::default(), } } } #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { HistoryDepth::::put(self.history_depth); ValidatorCount::::put(self.validator_count); MinimumValidatorCount::::put(self.minimum_validator_count); Invulnerables::::put(&self.invulnerables); ForceEra::::put(self.force_era); CanceledSlashPayout::::put(self.canceled_payout); SlashRewardFraction::::put(self.slash_reward_fraction); StorageVersion::::put(Releases::V7_0_0); MinNominatorBond::::put(self.min_nominator_bond); MinValidatorBond::::put(self.min_validator_bond); for &(ref stash, ref controller, balance, ref status) in &self.stakers { log!( trace, "inserting genesis staker: {:?} => {:?} => {:?}", stash, balance, status ); assert!( T::Currency::free_balance(&stash) >= balance, "Stash does not have enough balance to bond." ); frame_support::assert_ok!(>::bond( T::Origin::from(Some(stash.clone()).into()), T::Lookup::unlookup(controller.clone()), balance, RewardDestination::Staked, )); frame_support::assert_ok!(match status { crate::StakerStatus::Validator => >::validate( T::Origin::from(Some(controller.clone()).into()), Default::default(), ), crate::StakerStatus::Nominator(votes) => >::nominate( T::Origin::from(Some(controller.clone()).into()), votes.iter().map(|l| T::Lookup::unlookup(l.clone())).collect(), ), _ => Ok(()), }); } // all voters are reported to the `SortedListProvider`. assert_eq!( T::SortedListProvider::count(), CounterForNominators::::get(), "not all genesis stakers were inserted into sorted list provider, something is wrong." ); } } #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] pub enum Event { /// The era payout has been set; the first balance is the validator-payout; the second is /// the remainder from the maximum amount of reward. /// \[era_index, validator_payout, remainder\] EraPaid(EraIndex, BalanceOf, BalanceOf), /// The nominator has been rewarded by this amount. \[stash, amount\] Rewarded(T::AccountId, BalanceOf), /// One validator (and its nominators) has been slashed by the given amount. /// \[validator, amount\] Slashed(T::AccountId, BalanceOf), /// An old slashing report from a prior era was discarded because it could /// not be processed. \[session_index\] OldSlashingReportDiscarded(SessionIndex), /// A new set of stakers was elected. StakersElected, /// An account has bonded this amount. \[stash, amount\] /// /// NOTE: This event is only emitted when funds are bonded via a dispatchable. Notably, /// it will not be emitted for staking rewards when they are added to stake. Bonded(T::AccountId, BalanceOf), /// An account has unbonded this amount. \[stash, amount\] Unbonded(T::AccountId, BalanceOf), /// An account has called `withdraw_unbonded` and removed unbonding chunks worth `Balance` /// from the unlocking queue. \[stash, amount\] Withdrawn(T::AccountId, BalanceOf), /// A nominator has been kicked from a validator. \[nominator, stash\] Kicked(T::AccountId, T::AccountId), /// The election failed. No new era is planned. StakingElectionFailed, /// An account has stopped participating as either a validator or nominator. /// \[stash\] Chilled(T::AccountId), /// The stakers' rewards are getting paid. \[era_index, validator_stash\] PayoutStarted(EraIndex, T::AccountId), } #[pallet::error] pub enum Error { /// Not a controller account. NotController, /// Not a stash account. NotStash, /// Stash is already bonded. AlreadyBonded, /// Controller is already paired. AlreadyPaired, /// Targets cannot be empty. EmptyTargets, /// Duplicate index. DuplicateIndex, /// Slash record index out of bounds. InvalidSlashIndex, /// Cannot have a validator or nominator role, with value less than the minimum defined by /// governance (see `MinValidatorBond` and `MinNominatorBond`). If unbonding is the /// intention, `chill` first to remove one's role as validator/nominator. InsufficientBond, /// Can not schedule more unlock chunks. NoMoreChunks, /// Can not rebond without unlocking chunks. NoUnlockChunk, /// Attempting to target a stash that still has funds. FundedTarget, /// Invalid era to reward. InvalidEraToReward, /// Invalid number of nominations. InvalidNumberOfNominations, /// Items are not sorted and unique. NotSortedAndUnique, /// Rewards for this era have already been claimed for this validator. AlreadyClaimed, /// Incorrect previous history depth input provided. IncorrectHistoryDepth, /// Incorrect number of slashing spans provided. IncorrectSlashingSpans, /// Internal state has become somehow corrupted and the operation cannot continue. BadState, /// Too many nomination targets supplied. TooManyTargets, /// A nomination target was supplied that was blocked or otherwise not a validator. BadTarget, /// The user has enough bond and thus cannot be chilled forcefully by an external person. CannotChillOther, /// There are too many nominators in the system. Governance needs to adjust the staking /// settings to keep things safe for the runtime. TooManyNominators, /// There are too many validators in the system. Governance needs to adjust the staking /// settings to keep things safe for the runtime. TooManyValidators, } #[pallet::hooks] impl Hooks> for Pallet { fn on_runtime_upgrade() -> Weight { if StorageVersion::::get() == Releases::V6_0_0 { migrations::v7::migrate::() } else { T::DbWeight::get().reads(1) } } #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result<(), &'static str> { if StorageVersion::::get() == Releases::V6_0_0 { migrations::v7::pre_migrate::() } else { Ok(()) } } fn on_initialize(_now: BlockNumberFor) -> Weight { // just return the weight of the on_finalize. T::DbWeight::get().reads(1) } fn on_finalize(_n: BlockNumberFor) { // Set the start of the first era. if let Some(mut active_era) = Self::active_era() { if active_era.start.is_none() { let now_as_millis_u64 = T::UnixTime::now().as_millis().saturated_into::(); active_era.start = Some(now_as_millis_u64); // This write only ever happens once, we don't include it in the weight in // general ActiveEra::::put(active_era); } } // `on_finalize` weight is tracked in `on_initialize` } fn integrity_test() { sp_std::if_std! { sp_io::TestExternalities::new_empty().execute_with(|| assert!( T::SlashDeferDuration::get() < T::BondingDuration::get() || T::BondingDuration::get() == 0, "As per documentation, slash defer duration ({}) should be less than bonding duration ({}).", T::SlashDeferDuration::get(), T::BondingDuration::get(), ) ); } } } #[pallet::call] impl Pallet { /// Take the origin account as a stash and lock up `value` of its balance. `controller` will /// be the account that controls it. /// /// `value` must be more than the `minimum_balance` specified by `T::Currency`. /// /// The dispatch origin for this call must be _Signed_ by the stash account. /// /// Emits `Bonded`. /// # /// - Independent of the arguments. Moderate complexity. /// - O(1). /// - Three extra DB entries. /// /// NOTE: Two of the storage writes (`Self::bonded`, `Self::payee`) are _never_ cleaned /// unless the `origin` falls below _existential deposit_ and gets removed as dust. /// ------------------ /// # #[pallet::weight(T::WeightInfo::bond())] pub fn bond( origin: OriginFor, controller: ::Source, #[pallet::compact] value: BalanceOf, payee: RewardDestination, ) -> DispatchResult { let stash = ensure_signed(origin)?; if >::contains_key(&stash) { Err(Error::::AlreadyBonded)? } let controller = T::Lookup::lookup(controller)?; if >::contains_key(&controller) { Err(Error::::AlreadyPaired)? } // Reject a bond which is considered to be _dust_. if value < T::Currency::minimum_balance() { Err(Error::::InsufficientBond)? } frame_system::Pallet::::inc_consumers(&stash).map_err(|_| Error::::BadState)?; // You're auto-bonded forever, here. We might improve this by only bonding when // you actually validate/nominate and remove once you unbond __everything__. >::insert(&stash, &controller); >::insert(&stash, payee); let current_era = CurrentEra::::get().unwrap_or(0); let history_depth = Self::history_depth(); let last_reward_era = current_era.saturating_sub(history_depth); let stash_balance = T::Currency::free_balance(&stash); let value = value.min(stash_balance); Self::deposit_event(Event::::Bonded(stash.clone(), value)); let item = StakingLedger { stash, total: value, active: value, unlocking: vec![], claimed_rewards: (last_reward_era..current_era).collect(), }; Self::update_ledger(&controller, &item); Ok(()) } /// Add some extra amount that have appeared in the stash `free_balance` into the balance up /// for staking. /// /// The dispatch origin for this call must be _Signed_ by the stash, not the controller. /// /// Use this if there are additional funds in your stash account that you wish to bond. /// Unlike [`bond`](Self::bond) or [`unbond`](Self::unbond) this function does not impose /// any limitation on the amount that can be added. /// /// Emits `Bonded`. /// /// # /// - Independent of the arguments. Insignificant complexity. /// - O(1). /// # #[pallet::weight(T::WeightInfo::bond_extra())] pub fn bond_extra( origin: OriginFor, #[pallet::compact] max_additional: BalanceOf, ) -> DispatchResult { let stash = ensure_signed(origin)?; let controller = Self::bonded(&stash).ok_or(Error::::NotStash)?; let mut ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash_balance = T::Currency::free_balance(&stash); if let Some(extra) = stash_balance.checked_sub(&ledger.total) { let extra = extra.min(max_additional); ledger.total += extra; ledger.active += extra; // Last check: the new active amount of ledger must be more than ED. ensure!( ledger.active >= T::Currency::minimum_balance(), Error::::InsufficientBond ); // NOTE: ledger must be updated prior to calling `Self::weight_of`. Self::update_ledger(&controller, &ledger); // update this staker in the sorted list, if they exist in it. if T::SortedListProvider::contains(&stash) { T::SortedListProvider::on_update(&stash, Self::weight_of(&ledger.stash)); debug_assert_eq!(T::SortedListProvider::sanity_check(), Ok(())); } Self::deposit_event(Event::::Bonded(stash.clone(), extra)); } Ok(()) } /// Schedule a portion of the stash to be unlocked ready for transfer out after the bond /// period ends. If this leaves an amount actively bonded less than /// T::Currency::minimum_balance(), then it is increased to the full amount. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// Once the unlock period is done, you can call `withdraw_unbonded` to actually move /// the funds out of management ready for transfer. /// /// No more than a limited number of unlocking chunks (see `MAX_UNLOCKING_CHUNKS`) /// can co-exists at the same time. In that case, [`Call::withdraw_unbonded`] need /// to be called first to remove some of the chunks (if possible). /// /// If a user encounters the `InsufficientBond` error when calling this extrinsic, /// they should call `chill` first in order to free up their bonded funds. /// /// Emits `Unbonded`. /// /// See also [`Call::withdraw_unbonded`]. #[pallet::weight(T::WeightInfo::unbond())] pub fn unbond( origin: OriginFor, #[pallet::compact] value: BalanceOf, ) -> DispatchResult { let controller = ensure_signed(origin)?; let mut ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; ensure!(ledger.unlocking.len() < MAX_UNLOCKING_CHUNKS, Error::::NoMoreChunks,); let mut value = value.min(ledger.active); if !value.is_zero() { ledger.active -= value; // Avoid there being a dust balance left in the staking system. if ledger.active < T::Currency::minimum_balance() { value += ledger.active; ledger.active = Zero::zero(); } let min_active_bond = if Nominators::::contains_key(&ledger.stash) { MinNominatorBond::::get() } else if Validators::::contains_key(&ledger.stash) { MinValidatorBond::::get() } else { Zero::zero() }; // Make sure that the user maintains enough active bond for their role. // If a user runs into this error, they should chill first. ensure!(ledger.active >= min_active_bond, Error::::InsufficientBond); // Note: in case there is no current era it is fine to bond one era more. let era = Self::current_era().unwrap_or(0) + T::BondingDuration::get(); ledger.unlocking.push(UnlockChunk { value, era }); // NOTE: ledger must be updated prior to calling `Self::weight_of`. Self::update_ledger(&controller, &ledger); // update this staker in the sorted list, if they exist in it. if T::SortedListProvider::contains(&ledger.stash) { T::SortedListProvider::on_update(&ledger.stash, Self::weight_of(&ledger.stash)); } Self::deposit_event(Event::::Unbonded(ledger.stash, value)); } Ok(()) } /// Remove any unlocked chunks from the `unlocking` queue from our management. /// /// This essentially frees up that balance to be used by the stash account to do /// whatever it wants. /// /// The dispatch origin for this call must be _Signed_ by the controller. /// /// Emits `Withdrawn`. /// /// See also [`Call::unbond`]. /// /// # /// Complexity O(S) where S is the number of slashing spans to remove /// NOTE: Weight annotation is the kill scenario, we refund otherwise. /// # #[pallet::weight(T::WeightInfo::withdraw_unbonded_kill(*num_slashing_spans))] pub fn withdraw_unbonded( origin: OriginFor, num_slashing_spans: u32, ) -> DispatchResultWithPostInfo { let controller = ensure_signed(origin)?; let mut ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let (stash, old_total) = (ledger.stash.clone(), ledger.total); if let Some(current_era) = Self::current_era() { ledger = ledger.consolidate_unlocked(current_era) } let post_info_weight = if ledger.unlocking.is_empty() && ledger.active < T::Currency::minimum_balance() { // This account must have called `unbond()` with some value that caused the active // portion to fall below existential deposit + will have no more unlocking chunks // left. We can now safely remove all staking-related information. Self::kill_stash(&stash, num_slashing_spans)?; // Remove the lock. T::Currency::remove_lock(STAKING_ID, &stash); // This is worst case scenario, so we use the full weight and return None None } else { // This was the consequence of a partial unbond. just update the ledger and move on. Self::update_ledger(&controller, &ledger); // This is only an update, so we use less overall weight. Some(T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)) }; // `old_total` should never be less than the new total because // `consolidate_unlocked` strictly subtracts balance. if ledger.total < old_total { // Already checked that this won't overflow by entry condition. let value = old_total - ledger.total; Self::deposit_event(Event::::Withdrawn(stash, value)); } Ok(post_info_weight.into()) } /// Declare the desire to validate for the origin controller. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. #[pallet::weight(T::WeightInfo::validate())] pub fn validate(origin: OriginFor, prefs: ValidatorPrefs) -> DispatchResult { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; ensure!(ledger.active >= MinValidatorBond::::get(), Error::::InsufficientBond); let stash = &ledger.stash; // Only check limits if they are not already a validator. if !Validators::::contains_key(stash) { // If this error is reached, we need to adjust the `MinValidatorBond` and start // calling `chill_other`. Until then, we explicitly block new validators to protect // the runtime. if let Some(max_validators) = MaxValidatorsCount::::get() { ensure!( CounterForValidators::::get() < max_validators, Error::::TooManyValidators ); } } Self::do_remove_nominator(stash); Self::do_add_validator(stash, prefs); Ok(()) } /// Declare the desire to nominate `targets` for the origin controller. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// # /// - The transaction's complexity is proportional to the size of `targets` (N) /// which is capped at CompactAssignments::LIMIT (MAX_NOMINATIONS). /// - Both the reads and writes follow a similar pattern. /// # #[pallet::weight(T::WeightInfo::nominate(targets.len() as u32))] pub fn nominate( origin: OriginFor, targets: Vec<::Source>, ) -> DispatchResult { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; ensure!(ledger.active >= MinNominatorBond::::get(), Error::::InsufficientBond); let stash = &ledger.stash; // Only check limits if they are not already a nominator. if !Nominators::::contains_key(stash) { // If this error is reached, we need to adjust the `MinNominatorBond` and start // calling `chill_other`. Until then, we explicitly block new nominators to protect // the runtime. if let Some(max_nominators) = MaxNominatorsCount::::get() { ensure!( CounterForNominators::::get() < max_nominators, Error::::TooManyNominators ); } } ensure!(!targets.is_empty(), Error::::EmptyTargets); ensure!(targets.len() <= T::MAX_NOMINATIONS as usize, Error::::TooManyTargets); let old = Nominators::::get(stash).map_or_else(Vec::new, |x| x.targets); let targets = targets .into_iter() .map(|t| T::Lookup::lookup(t).map_err(DispatchError::from)) .map(|n| { n.and_then(|n| { if old.contains(&n) || !Validators::::get(&n).blocked { Ok(n) } else { Err(Error::::BadTarget.into()) } }) }) .collect::, _>>()?; let nominations = Nominations { targets, // Initial nominations are considered submitted at era 0. See `Nominations` doc submitted_in: Self::current_era().unwrap_or(0), suppressed: false, }; Self::do_remove_validator(stash); Self::do_add_nominator(stash, nominations); Ok(()) } /// Declare no desire to either validate or nominate. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// # /// - Independent of the arguments. Insignificant complexity. /// - Contains one read. /// - Writes are limited to the `origin` account key. /// # #[pallet::weight(T::WeightInfo::chill())] pub fn chill(origin: OriginFor) -> DispatchResult { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; Self::chill_stash(&ledger.stash); Ok(()) } /// (Re-)set the payment target for a controller. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// # /// - Independent of the arguments. Insignificant complexity. /// - Contains a limited number of reads. /// - Writes are limited to the `origin` account key. /// --------- /// - Weight: O(1) /// - DB Weight: /// - Read: Ledger /// - Write: Payee /// # #[pallet::weight(T::WeightInfo::set_payee())] pub fn set_payee( origin: OriginFor, payee: RewardDestination, ) -> DispatchResult { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash = &ledger.stash; >::insert(stash, payee); Ok(()) } /// (Re-)set the controller of a stash. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the stash, not the controller. /// /// # /// - Independent of the arguments. Insignificant complexity. /// - Contains a limited number of reads. /// - Writes are limited to the `origin` account key. /// ---------- /// Weight: O(1) /// DB Weight: /// - Read: Bonded, Ledger New Controller, Ledger Old Controller /// - Write: Bonded, Ledger New Controller, Ledger Old Controller /// # #[pallet::weight(T::WeightInfo::set_controller())] pub fn set_controller( origin: OriginFor, controller: ::Source, ) -> DispatchResult { let stash = ensure_signed(origin)?; let old_controller = Self::bonded(&stash).ok_or(Error::::NotStash)?; let controller = T::Lookup::lookup(controller)?; if >::contains_key(&controller) { Err(Error::::AlreadyPaired)? } if controller != old_controller { >::insert(&stash, &controller); if let Some(l) = >::take(&old_controller) { >::insert(&controller, l); } } Ok(()) } /// Sets the ideal number of validators. /// /// The dispatch origin must be Root. /// /// # /// Weight: O(1) /// Write: Validator Count /// # #[pallet::weight(T::WeightInfo::set_validator_count())] pub fn set_validator_count( origin: OriginFor, #[pallet::compact] new: u32, ) -> DispatchResult { ensure_root(origin)?; ValidatorCount::::put(new); Ok(()) } /// Increments the ideal number of validators. /// /// The dispatch origin must be Root. /// /// # /// Same as [`Self::set_validator_count`]. /// # #[pallet::weight(T::WeightInfo::set_validator_count())] pub fn increase_validator_count( origin: OriginFor, #[pallet::compact] additional: u32, ) -> DispatchResult { ensure_root(origin)?; ValidatorCount::::mutate(|n| *n += additional); Ok(()) } /// Scale up the ideal number of validators by a factor. /// /// The dispatch origin must be Root. /// /// # /// Same as [`Self::set_validator_count`]. /// # #[pallet::weight(T::WeightInfo::set_validator_count())] pub fn scale_validator_count(origin: OriginFor, factor: Percent) -> DispatchResult { ensure_root(origin)?; ValidatorCount::::mutate(|n| *n += factor * *n); Ok(()) } /// Force there to be no new eras indefinitely. /// /// The dispatch origin must be Root. /// /// # Warning /// /// The election process starts multiple blocks before the end of the era. /// Thus the election process may be ongoing when this is called. In this case the /// election will continue until the next era is triggered. /// /// # /// - No arguments. /// - Weight: O(1) /// - Write: ForceEra /// # #[pallet::weight(T::WeightInfo::force_no_eras())] pub fn force_no_eras(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceNone); Ok(()) } /// Force there to be a new era at the end of the next session. After this, it will be /// reset to normal (non-forced) behaviour. /// /// The dispatch origin must be Root. /// /// # Warning /// /// The election process starts multiple blocks before the end of the era. /// If this is called just before a new era is triggered, the election process may not /// have enough blocks to get a result. /// /// # /// - No arguments. /// - Weight: O(1) /// - Write ForceEra /// # #[pallet::weight(T::WeightInfo::force_new_era())] pub fn force_new_era(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceNew); Ok(()) } /// Set the validators who cannot be slashed (if any). /// /// The dispatch origin must be Root. /// /// # /// - O(V) /// - Write: Invulnerables /// # #[pallet::weight(T::WeightInfo::set_invulnerables(invulnerables.len() as u32))] pub fn set_invulnerables( origin: OriginFor, invulnerables: Vec, ) -> DispatchResult { ensure_root(origin)?; >::put(invulnerables); Ok(()) } /// Force a current staker to become completely unstaked, immediately. /// /// The dispatch origin must be Root. /// /// # /// O(S) where S is the number of slashing spans to be removed /// Reads: Bonded, Slashing Spans, Account, Locks /// Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators, /// Account, Locks Writes Each: SpanSlash * S /// # #[pallet::weight(T::WeightInfo::force_unstake(*num_slashing_spans))] pub fn force_unstake( origin: OriginFor, stash: T::AccountId, num_slashing_spans: u32, ) -> DispatchResult { ensure_root(origin)?; // Remove all staking-related information. Self::kill_stash(&stash, num_slashing_spans)?; // Remove the lock. T::Currency::remove_lock(STAKING_ID, &stash); Ok(()) } /// Force there to be a new era at the end of sessions indefinitely. /// /// The dispatch origin must be Root. /// /// # Warning /// /// The election process starts multiple blocks before the end of the era. /// If this is called just before a new era is triggered, the election process may not /// have enough blocks to get a result. /// /// # /// - Weight: O(1) /// - Write: ForceEra /// # #[pallet::weight(T::WeightInfo::force_new_era_always())] pub fn force_new_era_always(origin: OriginFor) -> DispatchResult { ensure_root(origin)?; ForceEra::::put(Forcing::ForceAlways); Ok(()) } /// Cancel enactment of a deferred slash. /// /// Can be called by the `T::SlashCancelOrigin`. /// /// Parameters: era and indices of the slashes for that era to kill. /// /// # /// Complexity: O(U + S) /// with U unapplied slashes weighted with U=1000 /// and S is the number of slash indices to be canceled. /// - Read: Unapplied Slashes /// - Write: Unapplied Slashes /// # #[pallet::weight(T::WeightInfo::cancel_deferred_slash(slash_indices.len() as u32))] pub fn cancel_deferred_slash( origin: OriginFor, era: EraIndex, slash_indices: Vec, ) -> DispatchResult { T::SlashCancelOrigin::ensure_origin(origin)?; ensure!(!slash_indices.is_empty(), Error::::EmptyTargets); ensure!(is_sorted_and_unique(&slash_indices), Error::::NotSortedAndUnique); let mut unapplied = ::UnappliedSlashes::get(&era); let last_item = slash_indices[slash_indices.len() - 1]; ensure!((last_item as usize) < unapplied.len(), Error::::InvalidSlashIndex); for (removed, index) in slash_indices.into_iter().enumerate() { let index = (index as usize) - removed; unapplied.remove(index); } ::UnappliedSlashes::insert(&era, &unapplied); Ok(()) } /// Pay out all the stakers behind a single validator for a single era. /// /// - `validator_stash` is the stash account of the validator. Their nominators, up to /// `T::MaxNominatorRewardedPerValidator`, will also receive their rewards. /// - `era` may be any era between `[current_era - history_depth; current_era]`. /// /// The origin of this call must be _Signed_. Any account can call this function, even if /// it is not one of the stakers. /// /// # /// - Time complexity: at most O(MaxNominatorRewardedPerValidator). /// - Contains a limited number of reads and writes. /// ----------- /// N is the Number of payouts for the validator (including the validator) /// Weight: /// - Reward Destination Staked: O(N) /// - Reward Destination Controller (Creating): O(N) /// /// NOTE: weights are assuming that payouts are made to alive stash account (Staked). /// Paying even a dead controller is cheaper weight-wise. We don't do any refunds here. /// # #[pallet::weight(T::WeightInfo::payout_stakers_alive_staked( T::MaxNominatorRewardedPerValidator::get() ))] pub fn payout_stakers( origin: OriginFor, validator_stash: T::AccountId, era: EraIndex, ) -> DispatchResultWithPostInfo { ensure_signed(origin)?; Self::do_payout_stakers(validator_stash, era) } /// Rebond a portion of the stash scheduled to be unlocked. /// /// The dispatch origin must be signed by the controller. /// /// # /// - Time complexity: O(L), where L is unlocking chunks /// - Bounded by `MAX_UNLOCKING_CHUNKS`. /// - Storage changes: Can't increase storage, only decrease it. /// # #[pallet::weight(T::WeightInfo::rebond(MAX_UNLOCKING_CHUNKS as u32))] pub fn rebond( origin: OriginFor, #[pallet::compact] value: BalanceOf, ) -> DispatchResultWithPostInfo { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; ensure!(!ledger.unlocking.is_empty(), Error::::NoUnlockChunk); let initial_unlocking = ledger.unlocking.len() as u32; let (ledger, rebonded_value) = ledger.rebond(value); // Last check: the new active amount of ledger must be more than ED. ensure!(ledger.active >= T::Currency::minimum_balance(), Error::::InsufficientBond); Self::deposit_event(Event::::Bonded(ledger.stash.clone(), rebonded_value)); // NOTE: ledger must be updated prior to calling `Self::weight_of`. Self::update_ledger(&controller, &ledger); if T::SortedListProvider::contains(&ledger.stash) { T::SortedListProvider::on_update(&ledger.stash, Self::weight_of(&ledger.stash)); } let removed_chunks = 1u32 // for the case where the last iterated chunk is not removed .saturating_add(initial_unlocking) .saturating_sub(ledger.unlocking.len() as u32); Ok(Some(T::WeightInfo::rebond(removed_chunks)).into()) } /// Set `HistoryDepth` value. This function will delete any history information /// when `HistoryDepth` is reduced. /// /// Parameters: /// - `new_history_depth`: The new history depth you would like to set. /// - `era_items_deleted`: The number of items that will be deleted by this dispatch. This /// should report all the storage items that will be deleted by clearing old era history. /// Needed to report an accurate weight for the dispatch. Trusted by `Root` to report an /// accurate number. /// /// Origin must be root. /// /// # /// - E: Number of history depths removed, i.e. 10 -> 7 = 3 /// - Weight: O(E) /// - DB Weight: /// - Reads: Current Era, History Depth /// - Writes: History Depth /// - Clear Prefix Each: Era Stakers, EraStakersClipped, ErasValidatorPrefs /// - Writes Each: ErasValidatorReward, ErasRewardPoints, ErasTotalStake, /// ErasStartSessionIndex /// # #[pallet::weight(T::WeightInfo::set_history_depth(*_era_items_deleted))] pub fn set_history_depth( origin: OriginFor, #[pallet::compact] new_history_depth: EraIndex, #[pallet::compact] _era_items_deleted: u32, ) -> DispatchResult { ensure_root(origin)?; if let Some(current_era) = Self::current_era() { HistoryDepth::::mutate(|history_depth| { let last_kept = current_era.checked_sub(*history_depth).unwrap_or(0); let new_last_kept = current_era.checked_sub(new_history_depth).unwrap_or(0); for era_index in last_kept..new_last_kept { Self::clear_era_information(era_index); } *history_depth = new_history_depth }) } Ok(()) } /// Remove all data structures concerning a staker/stash once it is at a state where it can /// be considered `dust` in the staking system. The requirements are: /// /// 1. the `total_balance` of the stash is below existential deposit. /// 2. or, the `ledger.total` of the stash is below existential deposit. /// /// The former can happen in cases like a slash; the latter when a fully unbonded account /// is still receiving staking rewards in `RewardDestination::Staked`. /// /// It can be called by anyone, as long as `stash` meets the above requirements. /// /// Refunds the transaction fees upon successful execution. #[pallet::weight(T::WeightInfo::reap_stash(*num_slashing_spans))] pub fn reap_stash( origin: OriginFor, stash: T::AccountId, num_slashing_spans: u32, ) -> DispatchResultWithPostInfo { let _ = ensure_signed(origin)?; let ed = T::Currency::minimum_balance(); let reapable = T::Currency::total_balance(&stash) < ed || Self::ledger(Self::bonded(stash.clone()).ok_or(Error::::NotStash)?) .map(|l| l.total) .unwrap_or_default() < ed; ensure!(reapable, Error::::FundedTarget); Self::kill_stash(&stash, num_slashing_spans)?; T::Currency::remove_lock(STAKING_ID, &stash); Ok(Pays::No.into()) } /// Remove the given nominations from the calling validator. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// - `who`: A list of nominator stash accounts who are nominating this validator which /// should no longer be nominating this validator. /// /// Note: Making this call only makes sense if you first set the validator preferences to /// block any further nominations. #[pallet::weight(T::WeightInfo::kick(who.len() as u32))] pub fn kick( origin: OriginFor, who: Vec<::Source>, ) -> DispatchResult { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash = &ledger.stash; for nom_stash in who .into_iter() .map(T::Lookup::lookup) .collect::, _>>()? .into_iter() { Nominators::::mutate(&nom_stash, |maybe_nom| { if let Some(ref mut nom) = maybe_nom { if let Some(pos) = nom.targets.iter().position(|v| v == stash) { nom.targets.swap_remove(pos); Self::deposit_event(Event::::Kicked( nom_stash.clone(), stash.clone(), )); } } }); } Ok(()) } /// Update the various staking limits this pallet. /// /// * `min_nominator_bond`: The minimum active bond needed to be a nominator. /// * `min_validator_bond`: The minimum active bond needed to be a validator. /// * `max_nominator_count`: The max number of users who can be a nominator at once. When /// set to `None`, no limit is enforced. /// * `max_validator_count`: The max number of users who can be a validator at once. When /// set to `None`, no limit is enforced. /// /// Origin must be Root to call this function. /// /// NOTE: Existing nominators and validators will not be affected by this update. /// to kick people under the new limits, `chill_other` should be called. #[pallet::weight(T::WeightInfo::set_staking_limits())] pub fn set_staking_limits( origin: OriginFor, min_nominator_bond: BalanceOf, min_validator_bond: BalanceOf, max_nominator_count: Option, max_validator_count: Option, threshold: Option, ) -> DispatchResult { ensure_root(origin)?; MinNominatorBond::::set(min_nominator_bond); MinValidatorBond::::set(min_validator_bond); MaxNominatorsCount::::set(max_nominator_count); MaxValidatorsCount::::set(max_validator_count); ChillThreshold::::set(threshold); Ok(()) } /// Declare a `controller` to stop participating as either a validator or nominator. /// /// Effects will be felt at the beginning of the next era. /// /// The dispatch origin for this call must be _Signed_, but can be called by anyone. /// /// If the caller is the same as the controller being targeted, then no further checks are /// enforced, and this function behaves just like `chill`. /// /// If the caller is different than the controller being targeted, the following conditions /// must be met: /// * A `ChillThreshold` must be set and checked which defines how close to the max /// nominators or validators we must reach before users can start chilling one-another. /// * A `MaxNominatorCount` and `MaxValidatorCount` must be set which is used to determine /// how close we are to the threshold. /// * A `MinNominatorBond` and `MinValidatorBond` must be set and checked, which determines /// if this is a person that should be chilled because they have not met the threshold /// bond required. /// /// This can be helpful if bond requirements are updated, and we need to remove old users /// who do not satisfy these requirements. #[pallet::weight(T::WeightInfo::chill_other())] pub fn chill_other(origin: OriginFor, controller: T::AccountId) -> DispatchResult { // Anyone can call this function. let caller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash = ledger.stash; // In order for one user to chill another user, the following conditions must be met: // * A `ChillThreshold` is set which defines how close to the max nominators or // validators we must reach before users can start chilling one-another. // * A `MaxNominatorCount` and `MaxValidatorCount` which is used to determine how close // we are to the threshold. // * A `MinNominatorBond` and `MinValidatorBond` which is the final condition checked to // determine this is a person that should be chilled because they have not met the // threshold bond required. // // Otherwise, if caller is the same as the controller, this is just like `chill`. if caller != controller { let threshold = ChillThreshold::::get().ok_or(Error::::CannotChillOther)?; let min_active_bond = if Nominators::::contains_key(&stash) { let max_nominator_count = MaxNominatorsCount::::get().ok_or(Error::::CannotChillOther)?; let current_nominator_count = CounterForNominators::::get(); ensure!( threshold * max_nominator_count < current_nominator_count, Error::::CannotChillOther ); MinNominatorBond::::get() } else if Validators::::contains_key(&stash) { let max_validator_count = MaxValidatorsCount::::get().ok_or(Error::::CannotChillOther)?; let current_validator_count = CounterForValidators::::get(); ensure!( threshold * max_validator_count < current_validator_count, Error::::CannotChillOther ); MinValidatorBond::::get() } else { Zero::zero() }; ensure!(ledger.active < min_active_bond, Error::::CannotChillOther); } Self::chill_stash(&stash); Ok(()) } } } /// Check that list is sorted and has no duplicates. fn is_sorted_and_unique(list: &[u32]) -> bool { list.windows(2).all(|w| w[0] < w[1]) }