referrerpolicy=no-referrer-when-downgrade

pallet_collator_selection/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Collator Selection pallet.
17//!
18//! A pallet to manage collators in a parachain.
19//!
20//! ## Overview
21//!
22//! The Collator Selection pallet manages the collators of a parachain. **Collation is _not_ a
23//! secure activity** and this pallet does not implement any game-theoretic mechanisms to meet BFT
24//! safety assumptions of the chosen set.
25//!
26//! ## Terminology
27//!
28//! - Collator: A parachain block producer.
29//! - Bond: An amount of `Balance` _reserved_ for candidate registration.
30//! - Invulnerable: An account guaranteed to be in the collator set.
31//!
32//! ## Implementation
33//!
34//! The final `Collators` are aggregated from two individual lists:
35//!
36//! 1. [`Invulnerables`]: a set of collators appointed by governance. These accounts will always be
37//!    collators.
38//! 2. [`CandidateList`]: these are *candidates to the collation task* and may or may not be elected
39//!    as a final collator.
40//!
41//! The current implementation resolves congestion of [`CandidateList`] through a simple auction
42//! mechanism. Candidates bid for the collator slots and at the end of the session, the auction ends
43//! and the top candidates are selected to become collators. The number of selected candidates is
44//! determined by the value of `DesiredCandidates`.
45//!
46//! Before the list reaches full capacity, candidates can register by placing the minimum bond
47//! through `register_as_candidate`. Then, if an account wants to participate in the collator slot
48//! auction, they have to replace an existing candidate by placing a greater deposit through
49//! `take_candidate_slot`. Existing candidates can increase their bids through `update_bond`.
50//!
51//! At any point, an account can take the place of another account in the candidate list if they put
52//! up a greater deposit than the target. While new joiners would like to deposit as little as
53//! possible to participate in the auction, the replacement threat incentivizes candidates to bid as
54//! close to their budget as possible in order to avoid being replaced.
55//!
56//! Candidates which are not on "winning" slots in the list can also decrease their deposits through
57//! `update_bond`, but candidates who are on top slots and try to decrease their deposits will fail
58//! in order to enforce auction mechanics and have meaningful bids.
59//!
60//! Candidates will not be allowed to get kicked or `leave_intent` if the total number of collators
61//! would fall below `MinEligibleCollators`. This is to ensure that some collators will always
62//! exist, i.e. someone is eligible to produce a block.
63//!
64//! When a new session starts, candidates with the highest deposits will be selected in order until
65//! the desired number of collators is reached. Candidates can increase or decrease their deposits
66//! between sessions in order to ensure they receive a slot in the collator list.
67//!
68//! ### Rewards
69//!
70//! The Collator Selection pallet maintains an on-chain account (the "Pot"). In each block, the
71//! collator who authored it receives:
72//!
73//! - Half the value of the Pot.
74//! - Half the value of the transaction fees within the block. The other half of the transaction
75//!   fees are deposited into the Pot.
76//!
77//! To initiate rewards, an ED needs to be transferred to the pot address.
78//!
79//! Note: Eventually the Pot distribution may be modified as discussed in [this
80//! issue](https://github.com/paritytech/statemint/issues/21#issuecomment-810481073).
81
82#![cfg_attr(not(feature = "std"), no_std)]
83
84extern crate alloc;
85
86use core::marker::PhantomData;
87use frame_support::traits::TypedGet;
88pub use pallet::*;
89
90#[cfg(test)]
91mod mock;
92
93#[cfg(test)]
94mod tests;
95
96#[cfg(feature = "runtime-benchmarks")]
97mod benchmarking;
98pub mod migration;
99pub mod weights;
100
101const LOG_TARGET: &str = "runtime::collator-selection";
102
103#[frame_support::pallet]
104pub mod pallet {
105	pub use crate::weights::WeightInfo;
106	use alloc::vec::Vec;
107	use core::ops::Div;
108	use frame_support::{
109		dispatch::{DispatchClass, DispatchResultWithPostInfo},
110		pallet_prelude::*,
111		traits::{
112			Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency,
113			ValidatorRegistration,
114		},
115		BoundedVec, DefaultNoBound, PalletId,
116	};
117	use frame_system::{pallet_prelude::*, Config as SystemConfig};
118	use pallet_session::SessionManager;
119	use sp_runtime::{
120		traits::{AccountIdConversion, CheckedSub, Convert, Saturating, Zero},
121		Debug,
122	};
123	use sp_staking::SessionIndex;
124
125	/// The in-code storage version.
126	const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
127
128	type BalanceOf<T> =
129		<<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
130
131	/// A convertor from collators id. Since this pallet does not have stash/controller, this is
132	/// just identity.
133	pub struct IdentityCollator;
134	impl<T> sp_runtime::traits::Convert<T, Option<T>> for IdentityCollator {
135		fn convert(t: T) -> Option<T> {
136			Some(t)
137		}
138	}
139
140	/// Configure the pallet by specifying the parameters and types on which it depends.
141	#[pallet::config]
142	pub trait Config: frame_system::Config {
143		/// Overarching event type.
144		#[allow(deprecated)]
145		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
146
147		/// The currency mechanism.
148		type Currency: ReservableCurrency<Self::AccountId>;
149
150		/// Origin that can dictate updating parameters of this pallet.
151		type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
152
153		/// Account Identifier from which the internal Pot is generated.
154		#[pallet::constant]
155		type PotId: Get<PalletId>;
156
157		/// Maximum number of candidates that we should have.
158		///
159		/// This does not take into account the invulnerables.
160		#[pallet::constant]
161		type MaxCandidates: Get<u32>;
162
163		/// Minimum number eligible collators. Should always be greater than zero. This includes
164		/// Invulnerable collators. This ensures that there will always be one collator who can
165		/// produce a block.
166		#[pallet::constant]
167		type MinEligibleCollators: Get<u32>;
168
169		/// Maximum number of invulnerables.
170		#[pallet::constant]
171		type MaxInvulnerables: Get<u32>;
172
173		// Will be kicked if block is not produced in threshold.
174		#[pallet::constant]
175		type KickThreshold: Get<BlockNumberFor<Self>>;
176
177		/// A stable ID for a validator.
178		type ValidatorId: Member + Parameter;
179
180		/// A conversion from account ID to validator ID.
181		///
182		/// Its cost must be at most one storage read.
183		type ValidatorIdOf: Convert<Self::AccountId, Option<Self::ValidatorId>>;
184
185		/// Validate a user is registered
186		type ValidatorRegistration: ValidatorRegistration<Self::ValidatorId>;
187
188		/// The weight information of this pallet.
189		type WeightInfo: WeightInfo;
190	}
191
192	#[pallet::extra_constants]
193	impl<T: Config> Pallet<T> {
194		/// Gets this pallet's derived pot account.
195		fn pot_account() -> T::AccountId {
196			Self::account_id()
197		}
198	}
199
200	/// Basic information about a collation candidate.
201	#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, scale_info::TypeInfo, MaxEncodedLen)]
202	pub struct CandidateInfo<AccountId, Balance> {
203		/// Account identifier.
204		pub who: AccountId,
205		/// Reserved deposit.
206		pub deposit: Balance,
207	}
208
209	#[pallet::pallet]
210	#[pallet::storage_version(STORAGE_VERSION)]
211	pub struct Pallet<T>(_);
212
213	/// The invulnerable, permissioned collators. This list must be sorted.
214	#[pallet::storage]
215	pub type Invulnerables<T: Config> =
216		StorageValue<_, BoundedVec<T::AccountId, T::MaxInvulnerables>, ValueQuery>;
217
218	/// The (community, limited) collation candidates. `Candidates` and `Invulnerables` should be
219	/// mutually exclusive.
220	///
221	/// This list is sorted in ascending order by deposit and when the deposits are equal, the least
222	/// recently updated is considered greater.
223	#[pallet::storage]
224	pub type CandidateList<T: Config> = StorageValue<
225		_,
226		BoundedVec<CandidateInfo<T::AccountId, BalanceOf<T>>, T::MaxCandidates>,
227		ValueQuery,
228	>;
229
230	/// Last block authored by collator.
231	#[pallet::storage]
232	pub type LastAuthoredBlock<T: Config> =
233		StorageMap<_, Twox64Concat, T::AccountId, BlockNumberFor<T>, ValueQuery>;
234
235	/// Desired number of candidates.
236	///
237	/// This should ideally always be less than [`Config::MaxCandidates`] for weights to be correct.
238	#[pallet::storage]
239	pub type DesiredCandidates<T> = StorageValue<_, u32, ValueQuery>;
240
241	/// Fixed amount to deposit to become a collator.
242	///
243	/// When a collator calls `leave_intent` they immediately receive the deposit back.
244	#[pallet::storage]
245	pub type CandidacyBond<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;
246
247	#[pallet::genesis_config]
248	#[derive(DefaultNoBound)]
249	pub struct GenesisConfig<T: Config> {
250		pub invulnerables: Vec<T::AccountId>,
251		pub candidacy_bond: BalanceOf<T>,
252		pub desired_candidates: u32,
253	}
254
255	#[pallet::genesis_build]
256	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
257		fn build(&self) {
258			let duplicate_invulnerables = self
259				.invulnerables
260				.iter()
261				.collect::<alloc::collections::btree_set::BTreeSet<_>>();
262			assert!(
263				duplicate_invulnerables.len() == self.invulnerables.len(),
264				"duplicate invulnerables in genesis."
265			);
266
267			let mut bounded_invulnerables =
268				BoundedVec::<_, T::MaxInvulnerables>::try_from(self.invulnerables.clone())
269					.expect("genesis invulnerables are more than T::MaxInvulnerables");
270			assert!(
271				T::MaxCandidates::get() >= self.desired_candidates,
272				"genesis desired_candidates are more than T::MaxCandidates",
273			);
274
275			bounded_invulnerables.sort();
276
277			DesiredCandidates::<T>::put(self.desired_candidates);
278			CandidacyBond::<T>::put(self.candidacy_bond);
279			Invulnerables::<T>::put(bounded_invulnerables);
280		}
281	}
282
283	#[pallet::event]
284	#[pallet::generate_deposit(pub(super) fn deposit_event)]
285	pub enum Event<T: Config> {
286		/// New Invulnerables were set.
287		NewInvulnerables { invulnerables: Vec<T::AccountId> },
288		/// A new Invulnerable was added.
289		InvulnerableAdded { account_id: T::AccountId },
290		/// An Invulnerable was removed.
291		InvulnerableRemoved { account_id: T::AccountId },
292		/// The number of desired candidates was set.
293		NewDesiredCandidates { desired_candidates: u32 },
294		/// The candidacy bond was set.
295		NewCandidacyBond { bond_amount: BalanceOf<T> },
296		/// A new candidate joined.
297		CandidateAdded { account_id: T::AccountId, deposit: BalanceOf<T> },
298		/// Bond of a candidate updated.
299		CandidateBondUpdated { account_id: T::AccountId, deposit: BalanceOf<T> },
300		/// A candidate was removed.
301		CandidateRemoved { account_id: T::AccountId },
302		/// An account was replaced in the candidate list by another one.
303		CandidateReplaced { old: T::AccountId, new: T::AccountId, deposit: BalanceOf<T> },
304		/// An account was unable to be added to the Invulnerables because they did not have keys
305		/// registered. Other Invulnerables may have been set.
306		InvalidInvulnerableSkipped { account_id: T::AccountId },
307	}
308
309	#[pallet::error]
310	pub enum Error<T> {
311		/// The pallet has too many candidates.
312		TooManyCandidates,
313		/// Leaving would result in too few candidates.
314		TooFewEligibleCollators,
315		/// Account is already a candidate.
316		AlreadyCandidate,
317		/// Account is not a candidate.
318		NotCandidate,
319		/// There are too many Invulnerables.
320		TooManyInvulnerables,
321		/// Account is already an Invulnerable.
322		AlreadyInvulnerable,
323		/// Account is not an Invulnerable.
324		NotInvulnerable,
325		/// Account has no associated validator ID.
326		NoAssociatedValidatorId,
327		/// Validator ID is not yet registered.
328		ValidatorNotRegistered,
329		/// Could not insert in the candidate list.
330		InsertToCandidateListFailed,
331		/// Could not remove from the candidate list.
332		RemoveFromCandidateListFailed,
333		/// New deposit amount would be below the minimum candidacy bond.
334		DepositTooLow,
335		/// Could not update the candidate list.
336		UpdateCandidateListFailed,
337		/// Deposit amount is too low to take the target's slot in the candidate list.
338		InsufficientBond,
339		/// The target account to be replaced in the candidate list is not a candidate.
340		TargetIsNotCandidate,
341		/// The updated deposit amount is equal to the amount already reserved.
342		IdenticalDeposit,
343		/// Cannot lower candidacy bond while occupying a future collator slot in the list.
344		InvalidUnreserve,
345	}
346
347	#[pallet::hooks]
348	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
349		fn integrity_test() {
350			assert!(T::MinEligibleCollators::get() > 0, "chain must require at least one collator");
351			assert!(
352				T::MaxInvulnerables::get().saturating_add(T::MaxCandidates::get()) >=
353					T::MinEligibleCollators::get(),
354				"invulnerables and candidates must be able to satisfy collator demand"
355			);
356		}
357
358		#[cfg(feature = "try-runtime")]
359		fn try_state(_: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
360			Self::do_try_state()
361		}
362	}
363
364	#[pallet::call]
365	impl<T: Config> Pallet<T> {
366		/// Set the list of invulnerable (fixed) collators. These collators must do some
367		/// preparation, namely to have registered session keys.
368		///
369		/// The call will remove any accounts that have not registered keys from the set. That is,
370		/// it is non-atomic; the caller accepts all `AccountId`s passed in `new` _individually_ as
371		/// acceptable Invulnerables, and is not proposing a _set_ of new Invulnerables.
372		///
373		/// This call does not maintain mutual exclusivity of `Invulnerables` and `Candidates`. It
374		/// is recommended to use a batch of `add_invulnerable` and `remove_invulnerable` instead. A
375		/// `batch_all` can also be used to enforce atomicity. If any candidates are included in
376		/// `new`, they should be removed with `remove_invulnerable_candidate` after execution.
377		///
378		/// Must be called by the `UpdateOrigin`.
379		#[pallet::call_index(0)]
380		#[pallet::weight(T::WeightInfo::set_invulnerables(new.len() as u32))]
381		pub fn set_invulnerables(origin: OriginFor<T>, new: Vec<T::AccountId>) -> DispatchResult {
382			T::UpdateOrigin::ensure_origin(origin)?;
383
384			// don't wipe out the collator set
385			if new.is_empty() {
386				// Casting `u32` to `usize` should be safe on all machines running this.
387				ensure!(
388					CandidateList::<T>::decode_len().unwrap_or_default() >=
389						T::MinEligibleCollators::get() as usize,
390					Error::<T>::TooFewEligibleCollators
391				);
392			}
393
394			// Will need to check the length again when putting into a bounded vec, but this
395			// prevents the iterator from having too many elements.
396			ensure!(
397				new.len() as u32 <= T::MaxInvulnerables::get(),
398				Error::<T>::TooManyInvulnerables
399			);
400
401			let mut new_with_keys = Vec::new();
402
403			// check if the invulnerables have associated validator keys before they are set
404			for account_id in &new {
405				// don't let one unprepared collator ruin things for everyone.
406				let validator_key = T::ValidatorIdOf::convert(account_id.clone());
407				match validator_key {
408					Some(key) => {
409						// key is not registered
410						if !T::ValidatorRegistration::is_registered(&key) {
411							Self::deposit_event(Event::InvalidInvulnerableSkipped {
412								account_id: account_id.clone(),
413							});
414							continue
415						}
416						// else condition passes; key is registered
417					},
418					// key does not exist
419					None => {
420						Self::deposit_event(Event::InvalidInvulnerableSkipped {
421							account_id: account_id.clone(),
422						});
423						continue
424					},
425				}
426
427				new_with_keys.push(account_id.clone());
428			}
429
430			// should never fail since `new_with_keys` must be equal to or shorter than `new`
431			let mut bounded_invulnerables =
432				BoundedVec::<_, T::MaxInvulnerables>::try_from(new_with_keys)
433					.map_err(|_| Error::<T>::TooManyInvulnerables)?;
434
435			// Invulnerables must be sorted for removal.
436			bounded_invulnerables.sort();
437
438			Invulnerables::<T>::put(&bounded_invulnerables);
439			Self::deposit_event(Event::NewInvulnerables {
440				invulnerables: bounded_invulnerables.to_vec(),
441			});
442
443			Ok(())
444		}
445
446		/// Set the ideal number of non-invulnerable collators. If lowering this number, then the
447		/// number of running collators could be higher than this figure. Aside from that edge case,
448		/// there should be no other way to have more candidates than the desired number.
449		///
450		/// The origin for this call must be the `UpdateOrigin`.
451		#[pallet::call_index(1)]
452		#[pallet::weight(T::WeightInfo::set_desired_candidates())]
453		pub fn set_desired_candidates(
454			origin: OriginFor<T>,
455			max: u32,
456		) -> DispatchResultWithPostInfo {
457			T::UpdateOrigin::ensure_origin(origin)?;
458			// we trust origin calls, this is just a for more accurate benchmarking
459			if max > T::MaxCandidates::get() {
460				log::warn!("max > T::MaxCandidates; you might need to run benchmarks again");
461			}
462			DesiredCandidates::<T>::put(max);
463			Self::deposit_event(Event::NewDesiredCandidates { desired_candidates: max });
464			Ok(().into())
465		}
466
467		/// Set the candidacy bond amount.
468		///
469		/// If the candidacy bond is increased by this call, all current candidates which have a
470		/// deposit lower than the new bond will be kicked from the list and get their deposits
471		/// back.
472		///
473		/// The origin for this call must be the `UpdateOrigin`.
474		#[pallet::call_index(2)]
475		#[pallet::weight(T::WeightInfo::set_candidacy_bond(
476			T::MaxCandidates::get(),
477			T::MaxCandidates::get()
478		))]
479		pub fn set_candidacy_bond(
480			origin: OriginFor<T>,
481			bond: BalanceOf<T>,
482		) -> DispatchResultWithPostInfo {
483			T::UpdateOrigin::ensure_origin(origin)?;
484			let bond_increased = CandidacyBond::<T>::mutate(|old_bond| -> bool {
485				let bond_increased = *old_bond < bond;
486				*old_bond = bond;
487				bond_increased
488			});
489			let initial_len = CandidateList::<T>::decode_len().unwrap_or_default();
490			let kicked = (bond_increased && initial_len > 0)
491				.then(|| {
492					// Closure below returns the number of candidates which were kicked because
493					// their deposits were lower than the new candidacy bond.
494					CandidateList::<T>::mutate(|candidates| -> usize {
495						let first_safe_candidate = candidates
496							.iter()
497							.position(|candidate| candidate.deposit >= bond)
498							.unwrap_or(initial_len);
499						let kicked_candidates = candidates.drain(..first_safe_candidate);
500						for candidate in kicked_candidates {
501							T::Currency::unreserve(&candidate.who, candidate.deposit);
502							LastAuthoredBlock::<T>::remove(candidate.who);
503						}
504						first_safe_candidate
505					})
506				})
507				.unwrap_or_default();
508			Self::deposit_event(Event::NewCandidacyBond { bond_amount: bond });
509			Ok(Some(T::WeightInfo::set_candidacy_bond(
510				bond_increased.then(|| initial_len as u32).unwrap_or_default(),
511				kicked as u32,
512			))
513			.into())
514		}
515
516		/// Register this account as a collator candidate. The account must (a) already have
517		/// registered session keys and (b) be able to reserve the `CandidacyBond`.
518		///
519		/// This call is not available to `Invulnerable` collators.
520		#[pallet::call_index(3)]
521		#[pallet::weight(T::WeightInfo::register_as_candidate(T::MaxCandidates::get()))]
522		pub fn register_as_candidate(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
523			let who = ensure_signed(origin)?;
524
525			// ensure we are below limit.
526			let length: u32 = CandidateList::<T>::decode_len()
527				.unwrap_or_default()
528				.try_into()
529				.unwrap_or_default();
530			ensure!(length < T::MaxCandidates::get(), Error::<T>::TooManyCandidates);
531			ensure!(!Invulnerables::<T>::get().contains(&who), Error::<T>::AlreadyInvulnerable);
532
533			let validator_key = T::ValidatorIdOf::convert(who.clone())
534				.ok_or(Error::<T>::NoAssociatedValidatorId)?;
535			ensure!(
536				T::ValidatorRegistration::is_registered(&validator_key),
537				Error::<T>::ValidatorNotRegistered
538			);
539
540			let deposit = CandidacyBond::<T>::get();
541			// First authored block is current block plus kick threshold to handle session delay
542			CandidateList::<T>::try_mutate(|candidates| -> Result<(), DispatchError> {
543				ensure!(
544					!candidates.iter().any(|candidate_info| candidate_info.who == who),
545					Error::<T>::AlreadyCandidate
546				);
547				T::Currency::reserve(&who, deposit)?;
548				LastAuthoredBlock::<T>::insert(
549					who.clone(),
550					frame_system::Pallet::<T>::block_number() + T::KickThreshold::get(),
551				);
552				candidates
553					.try_insert(0, CandidateInfo { who: who.clone(), deposit })
554					.map_err(|_| Error::<T>::InsertToCandidateListFailed)?;
555				Ok(())
556			})?;
557
558			Self::deposit_event(Event::CandidateAdded { account_id: who, deposit });
559			// Safe to do unchecked add here because we ensure above that `length <
560			// T::MaxCandidates::get()`, and since `T::MaxCandidates` is `u32` it can be at most
561			// `u32::MAX`, therefore `length + 1` cannot overflow.
562			Ok(Some(T::WeightInfo::register_as_candidate(length + 1)).into())
563		}
564
565		/// Deregister `origin` as a collator candidate. Note that the collator can only leave on
566		/// session change. The `CandidacyBond` will be unreserved immediately.
567		///
568		/// This call will fail if the total number of candidates would drop below
569		/// `MinEligibleCollators`.
570		#[pallet::call_index(4)]
571		#[pallet::weight(T::WeightInfo::leave_intent(T::MaxCandidates::get()))]
572		pub fn leave_intent(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
573			let who = ensure_signed(origin)?;
574			ensure!(
575				Self::eligible_collators() > T::MinEligibleCollators::get(),
576				Error::<T>::TooFewEligibleCollators
577			);
578			let length = CandidateList::<T>::decode_len().unwrap_or_default();
579			// Do remove their last authored block.
580			Self::try_remove_candidate(&who, true)?;
581
582			Ok(Some(T::WeightInfo::leave_intent(length.saturating_sub(1) as u32)).into())
583		}
584
585		/// Add a new account `who` to the list of `Invulnerables` collators. `who` must have
586		/// registered session keys. If `who` is a candidate, they will be removed.
587		///
588		/// The origin for this call must be the `UpdateOrigin`.
589		#[pallet::call_index(5)]
590		#[pallet::weight(T::WeightInfo::add_invulnerable(
591			T::MaxInvulnerables::get().saturating_sub(1),
592			T::MaxCandidates::get()
593		))]
594		pub fn add_invulnerable(
595			origin: OriginFor<T>,
596			who: T::AccountId,
597		) -> DispatchResultWithPostInfo {
598			T::UpdateOrigin::ensure_origin(origin)?;
599
600			// ensure `who` has registered a validator key
601			let validator_key = T::ValidatorIdOf::convert(who.clone())
602				.ok_or(Error::<T>::NoAssociatedValidatorId)?;
603			ensure!(
604				T::ValidatorRegistration::is_registered(&validator_key),
605				Error::<T>::ValidatorNotRegistered
606			);
607
608			Invulnerables::<T>::try_mutate(|invulnerables| -> DispatchResult {
609				match invulnerables.binary_search(&who) {
610					Ok(_) => return Err(Error::<T>::AlreadyInvulnerable)?,
611					Err(pos) => invulnerables
612						.try_insert(pos, who.clone())
613						.map_err(|_| Error::<T>::TooManyInvulnerables)?,
614				}
615				Ok(())
616			})?;
617
618			// Error just means `who` wasn't a candidate, which is the state we want anyway. Don't
619			// remove their last authored block, as they are still a collator.
620			let _ = Self::try_remove_candidate(&who, false);
621
622			Self::deposit_event(Event::InvulnerableAdded { account_id: who });
623
624			let weight_used = T::WeightInfo::add_invulnerable(
625				Invulnerables::<T>::decode_len()
626					.unwrap_or_default()
627					.try_into()
628					.unwrap_or(T::MaxInvulnerables::get().saturating_sub(1)),
629				CandidateList::<T>::decode_len()
630					.unwrap_or_default()
631					.try_into()
632					.unwrap_or(T::MaxCandidates::get()),
633			);
634
635			Ok(Some(weight_used).into())
636		}
637
638		/// Remove an account `who` from the list of `Invulnerables` collators. `Invulnerables` must
639		/// be sorted.
640		///
641		/// The origin for this call must be the `UpdateOrigin`.
642		#[pallet::call_index(6)]
643		#[pallet::weight(T::WeightInfo::remove_invulnerable(T::MaxInvulnerables::get()))]
644		pub fn remove_invulnerable(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
645			T::UpdateOrigin::ensure_origin(origin)?;
646
647			ensure!(
648				Self::eligible_collators() > T::MinEligibleCollators::get(),
649				Error::<T>::TooFewEligibleCollators
650			);
651
652			Invulnerables::<T>::try_mutate(|invulnerables| -> DispatchResult {
653				let pos =
654					invulnerables.binary_search(&who).map_err(|_| Error::<T>::NotInvulnerable)?;
655				invulnerables.remove(pos);
656				Ok(())
657			})?;
658
659			Self::deposit_event(Event::InvulnerableRemoved { account_id: who });
660			Ok(())
661		}
662
663		/// Update the candidacy bond of collator candidate `origin` to a new amount `new_deposit`.
664		///
665		/// Setting a `new_deposit` that is lower than the current deposit while `origin` is
666		/// occupying a top-`DesiredCandidates` slot is not allowed.
667		///
668		/// This call will fail if `origin` is not a collator candidate, the updated bond is lower
669		/// than the minimum candidacy bond, and/or the amount cannot be reserved.
670		#[pallet::call_index(7)]
671		#[pallet::weight(T::WeightInfo::update_bond(T::MaxCandidates::get()))]
672		pub fn update_bond(
673			origin: OriginFor<T>,
674			new_deposit: BalanceOf<T>,
675		) -> DispatchResultWithPostInfo {
676			let who = ensure_signed(origin)?;
677			ensure!(new_deposit >= CandidacyBond::<T>::get(), Error::<T>::DepositTooLow);
678			// The function below will try to mutate the `CandidateList` entry for the caller to
679			// update their deposit to the new value of `new_deposit`. The return value is the
680			// position of the entry in the list, used for weight calculation.
681			let length =
682				CandidateList::<T>::try_mutate(|candidates| -> Result<usize, DispatchError> {
683					let idx = candidates
684						.iter()
685						.position(|candidate_info| candidate_info.who == who)
686						.ok_or_else(|| Error::<T>::NotCandidate)?;
687					let candidate_count = candidates.len();
688					// Remove the candidate from the list.
689					let mut info = candidates.remove(idx);
690					let old_deposit = info.deposit;
691					if new_deposit > old_deposit {
692						T::Currency::reserve(&who, new_deposit - old_deposit)?;
693					} else if new_deposit < old_deposit {
694						// Casting `u32` to `usize` should be safe on all machines running this.
695						ensure!(
696							idx.saturating_add(DesiredCandidates::<T>::get() as usize) <
697								candidate_count,
698							Error::<T>::InvalidUnreserve
699						);
700						T::Currency::unreserve(&who, old_deposit - new_deposit);
701					} else {
702						return Err(Error::<T>::IdenticalDeposit.into())
703					}
704
705					// Update the deposit and insert the candidate in the correct spot in the list.
706					info.deposit = new_deposit;
707					let new_pos = candidates
708						.iter()
709						.position(|candidate| candidate.deposit >= new_deposit)
710						.unwrap_or_else(|| candidates.len());
711					candidates
712						.try_insert(new_pos, info)
713						.map_err(|_| Error::<T>::InsertToCandidateListFailed)?;
714
715					Ok(candidate_count)
716				})?;
717
718			Self::deposit_event(Event::CandidateBondUpdated {
719				account_id: who,
720				deposit: new_deposit,
721			});
722			Ok(Some(T::WeightInfo::update_bond(length as u32)).into())
723		}
724
725		/// The caller `origin` replaces a candidate `target` in the collator candidate list by
726		/// reserving `deposit`. The amount `deposit` reserved by the caller must be greater than
727		/// the existing bond of the target it is trying to replace.
728		///
729		/// This call will fail if the caller is already a collator candidate or invulnerable, the
730		/// caller does not have registered session keys, the target is not a collator candidate,
731		/// and/or the `deposit` amount cannot be reserved.
732		#[pallet::call_index(8)]
733		#[pallet::weight(T::WeightInfo::take_candidate_slot(T::MaxCandidates::get()))]
734		pub fn take_candidate_slot(
735			origin: OriginFor<T>,
736			deposit: BalanceOf<T>,
737			target: T::AccountId,
738		) -> DispatchResultWithPostInfo {
739			let who = ensure_signed(origin)?;
740
741			ensure!(!Invulnerables::<T>::get().contains(&who), Error::<T>::AlreadyInvulnerable);
742			ensure!(deposit >= CandidacyBond::<T>::get(), Error::<T>::InsufficientBond);
743
744			let validator_key = T::ValidatorIdOf::convert(who.clone())
745				.ok_or(Error::<T>::NoAssociatedValidatorId)?;
746			ensure!(
747				T::ValidatorRegistration::is_registered(&validator_key),
748				Error::<T>::ValidatorNotRegistered
749			);
750
751			let length = CandidateList::<T>::decode_len().unwrap_or_default();
752			// The closure below iterates through all elements of the candidate list to ensure that
753			// the caller isn't already a candidate and to find the target it's trying to replace in
754			// the list. The return value is a tuple of the position of the candidate to be replaced
755			// in the list along with its candidate information.
756			let target_info = CandidateList::<T>::try_mutate(
757				|candidates| -> Result<CandidateInfo<T::AccountId, BalanceOf<T>>, DispatchError> {
758					// Find the position in the list of the candidate that is being replaced.
759					let mut target_info_idx = None;
760					let mut new_info_idx = None;
761					for (idx, candidate_info) in candidates.iter().enumerate() {
762						// While iterating through the candidates trying to find the target,
763						// also ensure on the same pass that our caller isn't already a
764						// candidate.
765						ensure!(candidate_info.who != who, Error::<T>::AlreadyCandidate);
766						// If we find our target, update the position but do not stop the
767						// iteration since we're also checking that the caller isn't already a
768						// candidate.
769						if candidate_info.who == target {
770							target_info_idx = Some(idx);
771						}
772						// Find the spot where the new candidate would be inserted in the current
773						// version of the list.
774						if new_info_idx.is_none() && candidate_info.deposit >= deposit {
775							new_info_idx = Some(idx);
776						}
777					}
778					let target_info_idx =
779						target_info_idx.ok_or(Error::<T>::TargetIsNotCandidate)?;
780
781					// Remove the old candidate from the list.
782					let target_info = candidates.remove(target_info_idx);
783					ensure!(deposit > target_info.deposit, Error::<T>::InsufficientBond);
784
785					// We have removed one element before `new_info_idx`, so the position we have to
786					// insert to is reduced by 1.
787					let new_pos = new_info_idx
788						.map(|i| i.saturating_sub(1))
789						.unwrap_or_else(|| candidates.len());
790					let new_info = CandidateInfo { who: who.clone(), deposit };
791					// Insert the new candidate in the correct spot in the list.
792					candidates
793						.try_insert(new_pos, new_info)
794						.expect("candidate count previously decremented; qed");
795
796					Ok(target_info)
797				},
798			)?;
799			T::Currency::reserve(&who, deposit)?;
800			T::Currency::unreserve(&target_info.who, target_info.deposit);
801			LastAuthoredBlock::<T>::remove(target_info.who.clone());
802			LastAuthoredBlock::<T>::insert(
803				who.clone(),
804				frame_system::Pallet::<T>::block_number() + T::KickThreshold::get(),
805			);
806
807			Self::deposit_event(Event::CandidateReplaced { old: target, new: who, deposit });
808			Ok(Some(T::WeightInfo::take_candidate_slot(length as u32)).into())
809		}
810	}
811
812	impl<T: Config> Pallet<T> {
813		/// Get a unique, inaccessible account ID from the `PotId`.
814		pub fn account_id() -> T::AccountId {
815			T::PotId::get().into_account_truncating()
816		}
817
818		/// Return the total number of accounts that are eligible collators (candidates and
819		/// invulnerables).
820		fn eligible_collators() -> u32 {
821			CandidateList::<T>::decode_len()
822				.unwrap_or_default()
823				.saturating_add(Invulnerables::<T>::decode_len().unwrap_or_default())
824				.try_into()
825				.unwrap_or(u32::MAX)
826		}
827
828		/// Removes a candidate if they exist and sends them back their deposit.
829		fn try_remove_candidate(
830			who: &T::AccountId,
831			remove_last_authored: bool,
832		) -> Result<(), DispatchError> {
833			CandidateList::<T>::try_mutate(|candidates| -> Result<(), DispatchError> {
834				let idx = candidates
835					.iter()
836					.position(|candidate_info| candidate_info.who == *who)
837					.ok_or(Error::<T>::NotCandidate)?;
838				let deposit = candidates[idx].deposit;
839				T::Currency::unreserve(who, deposit);
840				candidates.remove(idx);
841				if remove_last_authored {
842					LastAuthoredBlock::<T>::remove(who.clone())
843				};
844				Ok(())
845			})?;
846			Self::deposit_event(Event::CandidateRemoved { account_id: who.clone() });
847			Ok(())
848		}
849
850		/// Assemble the current set of candidates and invulnerables into the next collator set.
851		///
852		/// This is done on the fly, as frequent as we are told to do so, as the session manager.
853		pub fn assemble_collators() -> Vec<T::AccountId> {
854			// Casting `u32` to `usize` should be safe on all machines running this.
855			let desired_candidates = DesiredCandidates::<T>::get() as usize;
856			let mut collators = Invulnerables::<T>::get().to_vec();
857			collators.extend(
858				CandidateList::<T>::get()
859					.iter()
860					.rev()
861					.cloned()
862					.take(desired_candidates)
863					.map(|candidate_info| candidate_info.who),
864			);
865			collators
866		}
867
868		/// Kicks out candidates that did not produce a block in the kick threshold and refunds
869		/// their deposits.
870		///
871		/// Return value is the number of candidates left in the list.
872		pub fn kick_stale_candidates(candidates: impl IntoIterator<Item = T::AccountId>) -> u32 {
873			let now = frame_system::Pallet::<T>::block_number();
874			let kick_threshold = T::KickThreshold::get();
875			let min_collators = T::MinEligibleCollators::get();
876			candidates
877				.into_iter()
878				.filter_map(|c| {
879					let last_block = LastAuthoredBlock::<T>::get(c.clone());
880					let since_last = now.saturating_sub(last_block);
881
882					let is_invulnerable = Invulnerables::<T>::get().contains(&c);
883					let is_lazy = since_last >= kick_threshold;
884
885					if is_invulnerable {
886						// They are invulnerable. No reason for them to be in `CandidateList` also.
887						// We don't even care about the min collators here, because an Account
888						// should not be a collator twice.
889						let _ = Self::try_remove_candidate(&c, false);
890						None
891					} else {
892						if Self::eligible_collators() <= min_collators || !is_lazy {
893							// Either this is a good collator (not lazy) or we are at the minimum
894							// that the system needs. They get to stay.
895							Some(c)
896						} else {
897							// This collator has not produced a block recently enough. Bye bye.
898							let _ = Self::try_remove_candidate(&c, true);
899							None
900						}
901					}
902				})
903				.count()
904				.try_into()
905				.expect("filter_map operation can't result in a bounded vec larger than its original; qed")
906		}
907
908		/// Ensure the correctness of the state of this pallet.
909		///
910		/// This should be valid before or after each state transition of this pallet.
911		///
912		/// # Invariants
913		///
914		/// ## `DesiredCandidates`
915		///
916		/// * The current desired candidate count should not exceed the candidate list capacity.
917		/// * The number of selected candidates together with the invulnerables must be greater than
918		///   or equal to the minimum number of eligible collators.
919		#[cfg(any(test, feature = "try-runtime"))]
920		pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
921			let desired_candidates = DesiredCandidates::<T>::get();
922
923			frame_support::ensure!(
924				desired_candidates <= T::MaxCandidates::get(),
925				"Shouldn't demand more candidates than the pallet config allows."
926			);
927
928			frame_support::ensure!(
929				desired_candidates.saturating_add(T::MaxInvulnerables::get()) >=
930					T::MinEligibleCollators::get(),
931				"Invulnerable set together with desired candidates should be able to meet the collator quota."
932			);
933
934			Ok(())
935		}
936	}
937
938	/// Keep track of number of authored blocks per authority, uncles are counted as well since
939	/// they're a valid proof of being online.
940	impl<T: Config + pallet_authorship::Config>
941		pallet_authorship::EventHandler<T::AccountId, BlockNumberFor<T>> for Pallet<T>
942	{
943		fn note_author(author: T::AccountId) {
944			let pot = Self::account_id();
945			// assumes an ED will be sent to pot.
946			let reward = T::Currency::free_balance(&pot)
947				.checked_sub(&T::Currency::minimum_balance())
948				.unwrap_or_else(Zero::zero)
949				.div(2u32.into());
950			// `reward` is half of pot account minus ED, this should never fail.
951			let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive);
952			debug_assert!(_success.is_ok());
953			LastAuthoredBlock::<T>::insert(author, frame_system::Pallet::<T>::block_number());
954
955			frame_system::Pallet::<T>::register_extra_weight_unchecked(
956				T::WeightInfo::note_author(),
957				DispatchClass::Mandatory,
958			);
959		}
960	}
961
962	/// Play the role of the session manager.
963	impl<T: Config> SessionManager<T::AccountId> for Pallet<T> {
964		fn new_session(index: SessionIndex) -> Option<Vec<T::AccountId>> {
965			log::info!(
966				"assembling new collators for new session {} at #{:?}",
967				index,
968				<frame_system::Pallet<T>>::block_number(),
969			);
970
971			// The `expect` below is safe because the list is a `BoundedVec` with a max size of
972			// `T::MaxCandidates`, which is a `u32`. When `decode_len` returns `Some(len)`, `len`
973			// must be valid and at most `u32::MAX`, which must always be able to convert to `u32`.
974			let candidates_len_before: u32 = CandidateList::<T>::decode_len()
975				.unwrap_or_default()
976				.try_into()
977				.expect("length is at most `T::MaxCandidates`, so it must fit in `u32`; qed");
978			let active_candidates_count = Self::kick_stale_candidates(
979				CandidateList::<T>::get()
980					.iter()
981					.map(|candidate_info| candidate_info.who.clone()),
982			);
983			let removed = candidates_len_before.saturating_sub(active_candidates_count);
984			let result = Self::assemble_collators();
985
986			frame_system::Pallet::<T>::register_extra_weight_unchecked(
987				T::WeightInfo::new_session(removed, candidates_len_before),
988				DispatchClass::Mandatory,
989			);
990			Some(result)
991		}
992		fn start_session(_: SessionIndex) {
993			// we don't care.
994		}
995		fn end_session(_: SessionIndex) {
996			// we don't care.
997		}
998	}
999}
1000
1001/// [`TypedGet`] implementation to get the AccountId of the StakingPot.
1002pub struct StakingPotAccountId<R>(PhantomData<R>);
1003impl<R> TypedGet for StakingPotAccountId<R>
1004where
1005	R: crate::Config,
1006{
1007	type Type = <R as frame_system::Config>::AccountId;
1008	fn get() -> Self::Type {
1009		<crate::Pallet<R>>::account_id()
1010	}
1011}