pezpallet_collator_selection/
lib.rs

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