use crate::{
configuration::{self, HostConfiguration},
disputes::{self, DisputesHandler as _, SlashingHandler as _},
dmp, hrmp, inclusion, paras, scheduler, session_info, shared,
};
use alloc::vec::Vec;
use codec::{Decode, Encode};
use frame_support::{
traits::{OneSessionHandler, Randomness},
weights::Weight,
};
use frame_system::limits::BlockWeights;
use polkadot_primitives::{BlockNumber, ConsensusLog, SessionIndex, ValidatorId};
use scale_info::TypeInfo;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub use pallet::*;
#[derive(Clone)]
pub struct SessionChangeNotification<BlockNumber> {
pub validators: Vec<ValidatorId>,
pub queued: Vec<ValidatorId>,
pub prev_config: HostConfiguration<BlockNumber>,
pub new_config: HostConfiguration<BlockNumber>,
pub random_seed: [u8; 32],
pub session_index: SessionIndex,
}
pub trait OnNewSession<N> {
fn on_new_session(notification: &SessionChangeNotification<N>);
}
impl<N> OnNewSession<N> for () {
fn on_new_session(_: &SessionChangeNotification<N>) {}
}
pub type ValidatorSetCount = u32;
impl<BlockNumber: Default + From<u32>> Default for SessionChangeNotification<BlockNumber> {
fn default() -> Self {
Self {
validators: Vec::new(),
queued: Vec::new(),
prev_config: HostConfiguration::default(),
new_config: HostConfiguration::default(),
random_seed: Default::default(),
session_index: Default::default(),
}
}
}
#[derive(Encode, Decode, TypeInfo)]
pub(crate) struct BufferedSessionChange {
pub validators: Vec<ValidatorId>,
pub queued: Vec<ValidatorId>,
pub session_index: SessionIndex,
}
pub trait WeightInfo {
fn force_approve(d: u32) -> Weight;
}
impl WeightInfo for () {
fn force_approve(_: u32) -> Weight {
BlockWeights::default().max_block
}
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config:
frame_system::Config
+ configuration::Config
+ shared::Config
+ paras::Config
+ scheduler::Config
+ inclusion::Config
+ session_info::Config
+ disputes::Config
+ dmp::Config
+ hrmp::Config
{
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
type ForceOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
type CoretimeOnNewSession: OnNewSession<BlockNumberFor<Self>>;
type WeightInfo: WeightInfo;
}
#[pallet::storage]
pub(super) type HasInitialized<T: Config> = StorageValue<_, ()>;
#[pallet::storage]
pub(crate) type BufferedSessionChanges<T: Config> =
StorageValue<_, Vec<BufferedSessionChange>, ValueQuery>;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
let total_weight = configuration::Pallet::<T>::initializer_initialize(now) +
shared::Pallet::<T>::initializer_initialize(now) +
paras::Pallet::<T>::initializer_initialize(now) +
scheduler::Pallet::<T>::initializer_initialize(now) +
inclusion::Pallet::<T>::initializer_initialize(now) +
session_info::Pallet::<T>::initializer_initialize(now) +
T::DisputesHandler::initializer_initialize(now) +
T::SlashingHandler::initializer_initialize(now) +
dmp::Pallet::<T>::initializer_initialize(now) +
hrmp::Pallet::<T>::initializer_initialize(now);
HasInitialized::<T>::set(Some(()));
total_weight
}
fn on_finalize(now: BlockNumberFor<T>) {
hrmp::Pallet::<T>::initializer_finalize();
dmp::Pallet::<T>::initializer_finalize();
T::SlashingHandler::initializer_finalize();
T::DisputesHandler::initializer_finalize();
session_info::Pallet::<T>::initializer_finalize();
inclusion::Pallet::<T>::initializer_finalize();
scheduler::Pallet::<T>::initializer_finalize();
paras::Pallet::<T>::initializer_finalize(now);
shared::Pallet::<T>::initializer_finalize();
configuration::Pallet::<T>::initializer_finalize();
if let Some(BufferedSessionChange { session_index, validators, queued }) =
BufferedSessionChanges::<T>::take().pop()
{
Self::apply_new_session(session_index, validators, queued);
}
HasInitialized::<T>::take();
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((
<T as Config>::WeightInfo::force_approve(
frame_system::Pallet::<T>::digest().logs.len() as u32,
),
DispatchClass::Operational,
))]
pub fn force_approve(origin: OriginFor<T>, up_to: BlockNumber) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
frame_system::Pallet::<T>::deposit_log(ConsensusLog::ForceApprove(up_to).into());
Ok(())
}
}
}
impl<T: Config> Pallet<T> {
fn apply_new_session(
session_index: SessionIndex,
all_validators: Vec<ValidatorId>,
queued: Vec<ValidatorId>,
) {
let random_seed = {
let mut buf = [0u8; 32];
let (random_hash, _) = T::Randomness::random(&b"paras"[..]);
let len = core::cmp::min(32, random_hash.as_ref().len());
buf[..len].copy_from_slice(&random_hash.as_ref()[..len]);
buf
};
let configuration::SessionChangeOutcome { prev_config, new_config } =
configuration::Pallet::<T>::initializer_on_new_session(&session_index);
let new_config = new_config.unwrap_or_else(|| prev_config.clone());
let validators = shared::Pallet::<T>::initializer_on_new_session(
session_index,
random_seed,
&new_config,
all_validators,
);
let notification = SessionChangeNotification {
validators,
queued,
prev_config,
new_config,
random_seed,
session_index,
};
let outgoing_paras = paras::Pallet::<T>::initializer_on_new_session(¬ification);
scheduler::Pallet::<T>::initializer_on_new_session(¬ification);
inclusion::Pallet::<T>::initializer_on_new_session(¬ification, &outgoing_paras);
session_info::Pallet::<T>::initializer_on_new_session(¬ification);
T::DisputesHandler::initializer_on_new_session(¬ification);
T::SlashingHandler::initializer_on_new_session(session_index);
dmp::Pallet::<T>::initializer_on_new_session(¬ification, &outgoing_paras);
hrmp::Pallet::<T>::initializer_on_new_session(¬ification, &outgoing_paras);
T::CoretimeOnNewSession::on_new_session(¬ification);
}
fn on_new_session<'a, I: 'a>(
_changed: bool,
session_index: SessionIndex,
validators: I,
queued: Option<I>,
) where
I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
{
let validators: Vec<_> = validators.map(|(_, v)| v).collect();
let queued: Vec<_> = if let Some(queued) = queued {
queued.map(|(_, v)| v).collect()
} else {
validators.clone()
};
if session_index == 0 {
Self::apply_new_session(0, validators, queued);
} else {
BufferedSessionChanges::<T>::mutate(|v| {
v.push(BufferedSessionChange { validators, queued, session_index })
});
}
}
#[cfg(any(test, feature = "runtime-benchmarks"))]
pub(crate) fn test_trigger_on_new_session<'a, I: 'a>(
changed: bool,
session_index: SessionIndex,
validators: I,
queued: Option<I>,
) where
I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
{
Self::on_new_session(changed, session_index, validators, queued)
}
pub(crate) fn upcoming_session_change() -> bool {
!BufferedSessionChanges::<T>::get().is_empty()
}
}
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
type Public = ValidatorId;
}
impl<T: pallet_session::Config + Config> OneSessionHandler<T::AccountId> for Pallet<T> {
type Key = ValidatorId;
fn on_genesis_session<'a, I: 'a>(validators: I)
where
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
{
Pallet::<T>::on_new_session(false, 0, validators, None);
}
fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I)
where
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
{
let session_index = pallet_session::Pallet::<T>::current_index();
Pallet::<T>::on_new_session(changed, session_index, validators, Some(queued));
}
fn on_disabled(_i: u32) {}
}