use alloc::vec::Vec;
use sp_staking::SessionIndex;
pub use pallet::*;
type Session<T> = pallet_session::Pallet<T>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin};
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config + pallet_session::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type PrivilegedOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
ValidatorsRegistered(Vec<T::ValidatorId>),
ValidatorsDeregistered(Vec<T::ValidatorId>),
}
#[pallet::storage]
pub(crate) type ValidatorsToRetire<T: Config> =
StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
#[pallet::storage]
pub(crate) type ValidatorsToAdd<T: Config> = StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({100_000})]
pub fn register_validators(
origin: OriginFor<T>,
validators: Vec<T::ValidatorId>,
) -> DispatchResult {
T::PrivilegedOrigin::ensure_origin(origin)?;
validators.clone().into_iter().for_each(|v| ValidatorsToAdd::<T>::append(v));
Self::deposit_event(Event::ValidatorsRegistered(validators));
Ok(())
}
#[pallet::call_index(1)]
#[pallet::weight({100_000})]
pub fn deregister_validators(
origin: OriginFor<T>,
validators: Vec<T::ValidatorId>,
) -> DispatchResult {
T::PrivilegedOrigin::ensure_origin(origin)?;
validators.clone().into_iter().for_each(|v| ValidatorsToRetire::<T>::append(v));
Self::deposit_event(Event::ValidatorsDeregistered(validators));
Ok(())
}
}
}
impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> {
fn new_session(new_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {
if new_index <= 1 {
return None
}
let mut validators = Session::<T>::validators();
ValidatorsToRetire::<T>::take().iter().for_each(|v| {
if let Some(pos) = validators.iter().position(|r| r == v) {
validators.swap_remove(pos);
}
});
ValidatorsToAdd::<T>::take().into_iter().for_each(|v| {
if !validators.contains(&v) {
validators.push(v);
}
});
Some(validators)
}
fn end_session(_: SessionIndex) {}
fn start_session(_start_index: SessionIndex) {}
}
impl<T: Config> pallet_session::historical::SessionManager<T::ValidatorId, ()> for Pallet<T> {
fn new_session(new_index: SessionIndex) -> Option<Vec<(T::ValidatorId, ())>> {
<Self as pallet_session::SessionManager<_>>::new_session(new_index)
.map(|r| r.into_iter().map(|v| (v, Default::default())).collect())
}
fn start_session(start_index: SessionIndex) {
<Self as pallet_session::SessionManager<_>>::start_session(start_index)
}
fn end_session(end_index: SessionIndex) {
<Self as pallet_session::SessionManager<_>>::end_session(end_index)
}
}