#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
use core::marker::PhantomData;
use frame_support::traits::TypedGet;
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod migration;
pub mod weights;
const LOG_TARGET: &str = "runtime::collator-selection";
#[frame_support::pallet]
pub mod pallet {
pub use crate::weights::WeightInfo;
use alloc::vec::Vec;
use core::ops::Div;
use frame_support::{
dispatch::{DispatchClass, DispatchResultWithPostInfo},
pallet_prelude::*,
traits::{
Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency,
ValidatorRegistration,
},
BoundedVec, DefaultNoBound, PalletId,
};
use frame_system::{pallet_prelude::*, Config as SystemConfig};
use pallet_session::SessionManager;
use sp_runtime::{
traits::{AccountIdConversion, CheckedSub, Convert, Saturating, Zero},
Debug,
};
use sp_staking::SessionIndex;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
pub struct IdentityCollator;
impl<T> sp_runtime::traits::Convert<T, Option<T>> for IdentityCollator {
fn convert(t: T) -> Option<T> {
Some(t)
}
}
#[pallet::config]
pub trait Config: frame_system::Config {
#[allow(deprecated)]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: ReservableCurrency<Self::AccountId>;
type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
#[pallet::constant]
type PotId: Get<PalletId>;
#[pallet::constant]
type MaxCandidates: Get<u32>;
#[pallet::constant]
type MinEligibleCollators: Get<u32>;
#[pallet::constant]
type MaxInvulnerables: Get<u32>;
#[pallet::constant]
type KickThreshold: Get<BlockNumberFor<Self>>;
type ValidatorId: Member + Parameter;
type ValidatorIdOf: Convert<Self::AccountId, Option<Self::ValidatorId>>;
type ValidatorRegistration: ValidatorRegistration<Self::ValidatorId>;
type WeightInfo: WeightInfo;
}
#[pallet::extra_constants]
impl<T: Config> Pallet<T> {
fn pot_account() -> T::AccountId {
Self::account_id()
}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, scale_info::TypeInfo, MaxEncodedLen)]
pub struct CandidateInfo<AccountId, Balance> {
pub who: AccountId,
pub deposit: Balance,
}
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::storage]
pub type Invulnerables<T: Config> =
StorageValue<_, BoundedVec<T::AccountId, T::MaxInvulnerables>, ValueQuery>;
#[pallet::storage]
pub type CandidateList<T: Config> = StorageValue<
_,
BoundedVec<CandidateInfo<T::AccountId, BalanceOf<T>>, T::MaxCandidates>,
ValueQuery,
>;
#[pallet::storage]
pub type LastAuthoredBlock<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, BlockNumberFor<T>, ValueQuery>;
#[pallet::storage]
pub type DesiredCandidates<T> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
pub type CandidacyBond<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;
#[pallet::genesis_config]
#[derive(DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub invulnerables: Vec<T::AccountId>,
pub candidacy_bond: BalanceOf<T>,
pub desired_candidates: u32,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let duplicate_invulnerables = self
.invulnerables
.iter()
.collect::<alloc::collections::btree_set::BTreeSet<_>>();
assert!(
duplicate_invulnerables.len() == self.invulnerables.len(),
"duplicate invulnerables in genesis."
);
let mut bounded_invulnerables =
BoundedVec::<_, T::MaxInvulnerables>::try_from(self.invulnerables.clone())
.expect("genesis invulnerables are more than T::MaxInvulnerables");
assert!(
T::MaxCandidates::get() >= self.desired_candidates,
"genesis desired_candidates are more than T::MaxCandidates",
);
bounded_invulnerables.sort();
DesiredCandidates::<T>::put(self.desired_candidates);
CandidacyBond::<T>::put(self.candidacy_bond);
Invulnerables::<T>::put(bounded_invulnerables);
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
NewInvulnerables { invulnerables: Vec<T::AccountId> },
InvulnerableAdded { account_id: T::AccountId },
InvulnerableRemoved { account_id: T::AccountId },
NewDesiredCandidates { desired_candidates: u32 },
NewCandidacyBond { bond_amount: BalanceOf<T> },
CandidateAdded { account_id: T::AccountId, deposit: BalanceOf<T> },
CandidateBondUpdated { account_id: T::AccountId, deposit: BalanceOf<T> },
CandidateRemoved { account_id: T::AccountId },
CandidateReplaced { old: T::AccountId, new: T::AccountId, deposit: BalanceOf<T> },
InvalidInvulnerableSkipped { account_id: T::AccountId },
}
#[pallet::error]
pub enum Error<T> {
TooManyCandidates,
TooFewEligibleCollators,
AlreadyCandidate,
NotCandidate,
TooManyInvulnerables,
AlreadyInvulnerable,
NotInvulnerable,
NoAssociatedValidatorId,
ValidatorNotRegistered,
InsertToCandidateListFailed,
RemoveFromCandidateListFailed,
DepositTooLow,
UpdateCandidateListFailed,
InsufficientBond,
TargetIsNotCandidate,
IdenticalDeposit,
InvalidUnreserve,
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn integrity_test() {
assert!(T::MinEligibleCollators::get() > 0, "chain must require at least one collator");
assert!(
T::MaxInvulnerables::get().saturating_add(T::MaxCandidates::get()) >=
T::MinEligibleCollators::get(),
"invulnerables and candidates must be able to satisfy collator demand"
);
}
#[cfg(feature = "try-runtime")]
fn try_state(_: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
Self::do_try_state()
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::set_invulnerables(new.len() as u32))]
pub fn set_invulnerables(origin: OriginFor<T>, new: Vec<T::AccountId>) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
if new.is_empty() {
ensure!(
CandidateList::<T>::decode_len().unwrap_or_default() >=
T::MinEligibleCollators::get() as usize,
Error::<T>::TooFewEligibleCollators
);
}
ensure!(
new.len() as u32 <= T::MaxInvulnerables::get(),
Error::<T>::TooManyInvulnerables
);
let mut new_with_keys = Vec::new();
for account_id in &new {
let validator_key = T::ValidatorIdOf::convert(account_id.clone());
match validator_key {
Some(key) => {
if !T::ValidatorRegistration::is_registered(&key) {
Self::deposit_event(Event::InvalidInvulnerableSkipped {
account_id: account_id.clone(),
});
continue;
}
},
None => {
Self::deposit_event(Event::InvalidInvulnerableSkipped {
account_id: account_id.clone(),
});
continue;
},
}
new_with_keys.push(account_id.clone());
}
let mut bounded_invulnerables =
BoundedVec::<_, T::MaxInvulnerables>::try_from(new_with_keys)
.map_err(|_| Error::<T>::TooManyInvulnerables)?;
bounded_invulnerables.sort();
Invulnerables::<T>::put(&bounded_invulnerables);
Self::deposit_event(Event::NewInvulnerables {
invulnerables: bounded_invulnerables.to_vec(),
});
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::set_desired_candidates())]
pub fn set_desired_candidates(
origin: OriginFor<T>,
max: u32,
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
if max > T::MaxCandidates::get() {
log::warn!("max > T::MaxCandidates; you might need to run benchmarks again");
}
DesiredCandidates::<T>::put(max);
Self::deposit_event(Event::NewDesiredCandidates { desired_candidates: max });
Ok(().into())
}
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::set_candidacy_bond(
T::MaxCandidates::get(),
T::MaxCandidates::get()
))]
pub fn set_candidacy_bond(
origin: OriginFor<T>,
bond: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
let bond_increased = CandidacyBond::<T>::mutate(|old_bond| -> bool {
let bond_increased = *old_bond < bond;
*old_bond = bond;
bond_increased
});
let initial_len = CandidateList::<T>::decode_len().unwrap_or_default();
let kicked = (bond_increased && initial_len > 0)
.then(|| {
CandidateList::<T>::mutate(|candidates| -> usize {
let first_safe_candidate = candidates
.iter()
.position(|candidate| candidate.deposit >= bond)
.unwrap_or(initial_len);
let kicked_candidates = candidates.drain(..first_safe_candidate);
for candidate in kicked_candidates {
T::Currency::unreserve(&candidate.who, candidate.deposit);
LastAuthoredBlock::<T>::remove(candidate.who);
}
first_safe_candidate
})
})
.unwrap_or_default();
Self::deposit_event(Event::NewCandidacyBond { bond_amount: bond });
Ok(Some(T::WeightInfo::set_candidacy_bond(
bond_increased.then(|| initial_len as u32).unwrap_or_default(),
kicked as u32,
))
.into())
}
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::register_as_candidate(T::MaxCandidates::get()))]
pub fn register_as_candidate(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let length: u32 = CandidateList::<T>::decode_len()
.unwrap_or_default()
.try_into()
.unwrap_or_default();
ensure!(length < T::MaxCandidates::get(), Error::<T>::TooManyCandidates);
ensure!(!Invulnerables::<T>::get().contains(&who), Error::<T>::AlreadyInvulnerable);
let validator_key = T::ValidatorIdOf::convert(who.clone())
.ok_or(Error::<T>::NoAssociatedValidatorId)?;
ensure!(
T::ValidatorRegistration::is_registered(&validator_key),
Error::<T>::ValidatorNotRegistered
);
let deposit = CandidacyBond::<T>::get();
CandidateList::<T>::try_mutate(|candidates| -> Result<(), DispatchError> {
ensure!(
!candidates.iter().any(|candidate_info| candidate_info.who == who),
Error::<T>::AlreadyCandidate
);
T::Currency::reserve(&who, deposit)?;
LastAuthoredBlock::<T>::insert(
who.clone(),
frame_system::Pallet::<T>::block_number() + T::KickThreshold::get(),
);
candidates
.try_insert(0, CandidateInfo { who: who.clone(), deposit })
.map_err(|_| Error::<T>::InsertToCandidateListFailed)?;
Ok(())
})?;
Self::deposit_event(Event::CandidateAdded { account_id: who, deposit });
Ok(Some(T::WeightInfo::register_as_candidate(length + 1)).into())
}
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::leave_intent(T::MaxCandidates::get()))]
pub fn leave_intent(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(
Self::eligible_collators() > T::MinEligibleCollators::get(),
Error::<T>::TooFewEligibleCollators
);
let length = CandidateList::<T>::decode_len().unwrap_or_default();
Self::try_remove_candidate(&who, true)?;
Ok(Some(T::WeightInfo::leave_intent(length.saturating_sub(1) as u32)).into())
}
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::add_invulnerable(
T::MaxInvulnerables::get().saturating_sub(1),
T::MaxCandidates::get()
))]
pub fn add_invulnerable(
origin: OriginFor<T>,
who: T::AccountId,
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
let validator_key = T::ValidatorIdOf::convert(who.clone())
.ok_or(Error::<T>::NoAssociatedValidatorId)?;
ensure!(
T::ValidatorRegistration::is_registered(&validator_key),
Error::<T>::ValidatorNotRegistered
);
Invulnerables::<T>::try_mutate(|invulnerables| -> DispatchResult {
match invulnerables.binary_search(&who) {
Ok(_) => return Err(Error::<T>::AlreadyInvulnerable)?,
Err(pos) => invulnerables
.try_insert(pos, who.clone())
.map_err(|_| Error::<T>::TooManyInvulnerables)?,
}
Ok(())
})?;
let _ = Self::try_remove_candidate(&who, false);
Self::deposit_event(Event::InvulnerableAdded { account_id: who });
let weight_used = T::WeightInfo::add_invulnerable(
Invulnerables::<T>::decode_len()
.unwrap_or_default()
.try_into()
.unwrap_or(T::MaxInvulnerables::get().saturating_sub(1)),
CandidateList::<T>::decode_len()
.unwrap_or_default()
.try_into()
.unwrap_or(T::MaxCandidates::get()),
);
Ok(Some(weight_used).into())
}
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::remove_invulnerable(T::MaxInvulnerables::get()))]
pub fn remove_invulnerable(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
T::UpdateOrigin::ensure_origin(origin)?;
ensure!(
Self::eligible_collators() > T::MinEligibleCollators::get(),
Error::<T>::TooFewEligibleCollators
);
Invulnerables::<T>::try_mutate(|invulnerables| -> DispatchResult {
let pos =
invulnerables.binary_search(&who).map_err(|_| Error::<T>::NotInvulnerable)?;
invulnerables.remove(pos);
Ok(())
})?;
Self::deposit_event(Event::InvulnerableRemoved { account_id: who });
Ok(())
}
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::update_bond(T::MaxCandidates::get()))]
pub fn update_bond(
origin: OriginFor<T>,
new_deposit: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(new_deposit >= CandidacyBond::<T>::get(), Error::<T>::DepositTooLow);
let length =
CandidateList::<T>::try_mutate(|candidates| -> Result<usize, DispatchError> {
let idx = candidates
.iter()
.position(|candidate_info| candidate_info.who == who)
.ok_or_else(|| Error::<T>::NotCandidate)?;
let candidate_count = candidates.len();
let mut info = candidates.remove(idx);
let old_deposit = info.deposit;
if new_deposit > old_deposit {
T::Currency::reserve(&who, new_deposit - old_deposit)?;
} else if new_deposit < old_deposit {
ensure!(
idx.saturating_add(DesiredCandidates::<T>::get() as usize) <
candidate_count,
Error::<T>::InvalidUnreserve
);
T::Currency::unreserve(&who, old_deposit - new_deposit);
} else {
return Err(Error::<T>::IdenticalDeposit.into());
}
info.deposit = new_deposit;
let new_pos = candidates
.iter()
.position(|candidate| candidate.deposit >= new_deposit)
.unwrap_or_else(|| candidates.len());
candidates
.try_insert(new_pos, info)
.map_err(|_| Error::<T>::InsertToCandidateListFailed)?;
Ok(candidate_count)
})?;
Self::deposit_event(Event::CandidateBondUpdated {
account_id: who,
deposit: new_deposit,
});
Ok(Some(T::WeightInfo::update_bond(length as u32)).into())
}
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::take_candidate_slot(T::MaxCandidates::get()))]
pub fn take_candidate_slot(
origin: OriginFor<T>,
deposit: BalanceOf<T>,
target: T::AccountId,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(!Invulnerables::<T>::get().contains(&who), Error::<T>::AlreadyInvulnerable);
ensure!(deposit >= CandidacyBond::<T>::get(), Error::<T>::InsufficientBond);
let validator_key = T::ValidatorIdOf::convert(who.clone())
.ok_or(Error::<T>::NoAssociatedValidatorId)?;
ensure!(
T::ValidatorRegistration::is_registered(&validator_key),
Error::<T>::ValidatorNotRegistered
);
let length = CandidateList::<T>::decode_len().unwrap_or_default();
let target_info = CandidateList::<T>::try_mutate(
|candidates| -> Result<CandidateInfo<T::AccountId, BalanceOf<T>>, DispatchError> {
let mut target_info_idx = None;
let mut new_info_idx = None;
for (idx, candidate_info) in candidates.iter().enumerate() {
ensure!(candidate_info.who != who, Error::<T>::AlreadyCandidate);
if candidate_info.who == target {
target_info_idx = Some(idx);
}
if new_info_idx.is_none() && candidate_info.deposit >= deposit {
new_info_idx = Some(idx);
}
}
let target_info_idx =
target_info_idx.ok_or(Error::<T>::TargetIsNotCandidate)?;
let target_info = candidates.remove(target_info_idx);
ensure!(deposit > target_info.deposit, Error::<T>::InsufficientBond);
let new_pos = new_info_idx
.map(|i| i.saturating_sub(1))
.unwrap_or_else(|| candidates.len());
let new_info = CandidateInfo { who: who.clone(), deposit };
candidates
.try_insert(new_pos, new_info)
.expect("candidate count previously decremented; qed");
Ok(target_info)
},
)?;
T::Currency::reserve(&who, deposit)?;
T::Currency::unreserve(&target_info.who, target_info.deposit);
LastAuthoredBlock::<T>::remove(target_info.who.clone());
LastAuthoredBlock::<T>::insert(
who.clone(),
frame_system::Pallet::<T>::block_number() + T::KickThreshold::get(),
);
Self::deposit_event(Event::CandidateReplaced { old: target, new: who, deposit });
Ok(Some(T::WeightInfo::take_candidate_slot(length as u32)).into())
}
}
impl<T: Config> Pallet<T> {
pub fn account_id() -> T::AccountId {
T::PotId::get().into_account_truncating()
}
fn eligible_collators() -> u32 {
CandidateList::<T>::decode_len()
.unwrap_or_default()
.saturating_add(Invulnerables::<T>::decode_len().unwrap_or_default())
.try_into()
.unwrap_or(u32::MAX)
}
fn try_remove_candidate(
who: &T::AccountId,
remove_last_authored: bool,
) -> Result<(), DispatchError> {
CandidateList::<T>::try_mutate(|candidates| -> Result<(), DispatchError> {
let idx = candidates
.iter()
.position(|candidate_info| candidate_info.who == *who)
.ok_or(Error::<T>::NotCandidate)?;
let deposit = candidates[idx].deposit;
T::Currency::unreserve(who, deposit);
candidates.remove(idx);
if remove_last_authored {
LastAuthoredBlock::<T>::remove(who.clone())
};
Ok(())
})?;
Self::deposit_event(Event::CandidateRemoved { account_id: who.clone() });
Ok(())
}
pub fn assemble_collators() -> Vec<T::AccountId> {
let desired_candidates = DesiredCandidates::<T>::get() as usize;
let mut collators = Invulnerables::<T>::get().to_vec();
collators.extend(
CandidateList::<T>::get()
.iter()
.rev()
.cloned()
.take(desired_candidates)
.map(|candidate_info| candidate_info.who),
);
collators
}
pub fn kick_stale_candidates(candidates: impl IntoIterator<Item = T::AccountId>) -> u32 {
let now = frame_system::Pallet::<T>::block_number();
let kick_threshold = T::KickThreshold::get();
let min_collators = T::MinEligibleCollators::get();
candidates
.into_iter()
.filter_map(|c| {
let last_block = LastAuthoredBlock::<T>::get(c.clone());
let since_last = now.saturating_sub(last_block);
let is_invulnerable = Invulnerables::<T>::get().contains(&c);
let is_lazy = since_last >= kick_threshold;
if is_invulnerable {
let _ = Self::try_remove_candidate(&c, false);
None
} else {
if Self::eligible_collators() <= min_collators || !is_lazy {
Some(c)
} else {
let _ = Self::try_remove_candidate(&c, true);
None
}
}
})
.count()
.try_into()
.expect("filter_map operation can't result in a bounded vec larger than its original; qed")
}
#[cfg(any(test, feature = "try-runtime"))]
pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
let desired_candidates = DesiredCandidates::<T>::get();
frame_support::ensure!(
desired_candidates <= T::MaxCandidates::get(),
"Shouldn't demand more candidates than the pallet config allows."
);
frame_support::ensure!(
desired_candidates.saturating_add(T::MaxInvulnerables::get()) >=
T::MinEligibleCollators::get(),
"Invulnerable set together with desired candidates should be able to meet the collator quota."
);
Ok(())
}
}
impl<T: Config + pallet_authorship::Config>
pallet_authorship::EventHandler<T::AccountId, BlockNumberFor<T>> for Pallet<T>
{
fn note_author(author: T::AccountId) {
let pot = Self::account_id();
let reward = T::Currency::free_balance(&pot)
.checked_sub(&T::Currency::minimum_balance())
.unwrap_or_else(Zero::zero)
.div(2u32.into());
let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive);
debug_assert!(_success.is_ok());
LastAuthoredBlock::<T>::insert(author, frame_system::Pallet::<T>::block_number());
frame_system::Pallet::<T>::register_extra_weight_unchecked(
T::WeightInfo::note_author(),
DispatchClass::Mandatory,
);
}
}
impl<T: Config> SessionManager<T::AccountId> for Pallet<T> {
fn new_session(index: SessionIndex) -> Option<Vec<T::AccountId>> {
log::info!(
"assembling new collators for new session {} at #{:?}",
index,
<frame_system::Pallet<T>>::block_number(),
);
let candidates_len_before: u32 = CandidateList::<T>::decode_len()
.unwrap_or_default()
.try_into()
.expect("length is at most `T::MaxCandidates`, so it must fit in `u32`; qed");
let active_candidates_count = Self::kick_stale_candidates(
CandidateList::<T>::get()
.iter()
.map(|candidate_info| candidate_info.who.clone()),
);
let removed = candidates_len_before.saturating_sub(active_candidates_count);
let result = Self::assemble_collators();
frame_system::Pallet::<T>::register_extra_weight_unchecked(
T::WeightInfo::new_session(removed, candidates_len_before),
DispatchClass::Mandatory,
);
Some(result)
}
fn start_session(_: SessionIndex) {
}
fn end_session(_: SessionIndex) {
}
}
}
pub struct StakingPotAccountId<R>(PhantomData<R>);
impl<R> TypedGet for StakingPotAccountId<R>
where
R: crate::Config,
{
type Type = <R as frame_system::Config>::AccountId;
fn get() -> Self::Type {
<crate::Pallet<R>>::account_id()
}
}