#![cfg_attr(not(feature = "std"), no_std)]
#![warn(unused_must_use, unsafe_code, unused_variables, unused_must_use)]
extern crate alloc;
use alloc::{boxed::Box, vec, vec::Vec};
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays},
ensure,
traits::{ConstU32, DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler},
weights::Weight,
BoundedVec, WeakBoundedVec,
};
use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
use sp_consensus_babe::{
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
AllowedSlots, BabeAuthorityWeight, BabeEpochConfiguration, ConsensusLog, Epoch,
EquivocationProof, Randomness as BabeRandomness, Slot, BABE_ENGINE_ID, RANDOMNESS_LENGTH,
RANDOMNESS_VRF_CONTEXT,
};
use sp_core::crypto::Wraps;
use sp_runtime::{
generic::DigestItem,
traits::{IsMember, One, SaturatedConversion, Saturating, Zero},
ConsensusEngineId, Permill,
};
use sp_session::{GetSessionNumber, GetValidatorCount};
use sp_staking::{offence::OffenceReportSystem, SessionIndex};
pub use sp_consensus_babe::AuthorityId;
const LOG_TARGET: &str = "runtime::babe";
mod default_weights;
mod equivocation;
mod randomness;
#[cfg(any(feature = "runtime-benchmarks", test))]
mod benchmarking;
#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;
pub use equivocation::{EquivocationOffence, EquivocationReportSystem};
#[allow(deprecated)]
pub use randomness::CurrentBlockRandomness;
pub use randomness::{
ParentBlockRandomness, RandomnessFromOneEpochAgo, RandomnessFromTwoEpochsAgo,
};
pub use pallet::*;
pub trait WeightInfo {
fn plan_config_change() -> Weight;
fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight;
}
pub trait EpochChangeTrigger {
fn trigger<T: Config>(now: BlockNumberFor<T>);
}
pub struct ExternalTrigger;
impl EpochChangeTrigger for ExternalTrigger {
fn trigger<T: Config>(_: BlockNumberFor<T>) {} }
pub struct SameAuthoritiesForever;
impl EpochChangeTrigger for SameAuthoritiesForever {
fn trigger<T: Config>(now: BlockNumberFor<T>) {
if Pallet::<T>::should_epoch_change(now) {
let authorities = Authorities::<T>::get();
let next_authorities = authorities.clone();
Pallet::<T>::enact_epoch_change(authorities, next_authorities, None);
}
}
}
const UNDER_CONSTRUCTION_SEGMENT_LENGTH: u32 = 256;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: pallet_timestamp::Config {
#[pallet::constant]
type EpochDuration: Get<u64>;
#[pallet::constant]
type ExpectedBlockTime: Get<Self::Moment>;
type EpochChangeTrigger: EpochChangeTrigger;
type DisabledValidators: DisabledValidators;
type WeightInfo: WeightInfo;
#[pallet::constant]
type MaxAuthorities: Get<u32>;
#[pallet::constant]
type MaxNominators: Get<u32>;
type KeyOwnerProof: Parameter + GetSessionNumber + GetValidatorCount;
type EquivocationReportSystem: OffenceReportSystem<
Option<Self::AccountId>,
(EquivocationProof<HeaderFor<Self>>, Self::KeyOwnerProof),
>;
}
#[pallet::error]
pub enum Error<T> {
InvalidEquivocationProof,
InvalidKeyOwnershipProof,
DuplicateOffenceReport,
InvalidConfiguration,
}
#[pallet::storage]
pub type EpochIndex<T> = StorageValue<_, u64, ValueQuery>;
#[pallet::storage]
pub type Authorities<T: Config> = StorageValue<
_,
WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
ValueQuery,
>;
#[pallet::storage]
pub type GenesisSlot<T> = StorageValue<_, Slot, ValueQuery>;
#[pallet::storage]
pub type CurrentSlot<T> = StorageValue<_, Slot, ValueQuery>;
#[pallet::storage]
pub type Randomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
#[pallet::storage]
pub type PendingEpochConfigChange<T> = StorageValue<_, NextConfigDescriptor>;
#[pallet::storage]
pub type NextRandomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
#[pallet::storage]
pub type NextAuthorities<T: Config> = StorageValue<
_,
WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
ValueQuery,
>;
#[pallet::storage]
pub type SegmentIndex<T> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
pub type UnderConstruction<T: Config> = StorageMap<
_,
Twox64Concat,
u32,
BoundedVec<BabeRandomness, ConstU32<UNDER_CONSTRUCTION_SEGMENT_LENGTH>>,
ValueQuery,
>;
#[pallet::storage]
pub type Initialized<T> = StorageValue<_, Option<PreDigest>>;
#[pallet::storage]
pub type AuthorVrfRandomness<T> = StorageValue<_, Option<BabeRandomness>, ValueQuery>;
#[pallet::storage]
pub type EpochStart<T: Config> =
StorageValue<_, (BlockNumberFor<T>, BlockNumberFor<T>), ValueQuery>;
#[pallet::storage]
pub type Lateness<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
#[pallet::storage]
pub type EpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
#[pallet::storage]
pub type NextEpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
#[pallet::storage]
pub type SkippedEpochs<T> =
StorageValue<_, BoundedVec<(u64, SessionIndex), ConstU32<100>>, ValueQuery>;
#[derive(frame_support::DefaultNoBound)]
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
pub epoch_config: BabeEpochConfiguration,
#[serde(skip)]
pub _config: core::marker::PhantomData<T>,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
SegmentIndex::<T>::put(0);
Pallet::<T>::initialize_genesis_authorities(&self.authorities);
EpochConfig::<T>::put(&self.epoch_config);
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
Self::initialize(now);
Weight::zero()
}
fn on_finalize(_now: BlockNumberFor<T>) {
if let Some(pre_digest) = Initialized::<T>::take().flatten() {
let authority_index = pre_digest.authority_index();
if T::DisabledValidators::is_disabled(authority_index) {
panic!(
"Validator with index {:?} is disabled and should not be attempting to author blocks.",
authority_index,
);
}
if let Some(signature) = pre_digest.vrf_signature() {
let randomness: Option<BabeRandomness> = Authorities::<T>::get()
.get(authority_index as usize)
.and_then(|(authority, _)| {
let public = authority.as_inner_ref();
let transcript = sp_consensus_babe::make_vrf_transcript(
&Randomness::<T>::get(),
CurrentSlot::<T>::get(),
EpochIndex::<T>::get(),
);
debug_assert!({
use sp_core::crypto::VrfPublic;
public.vrf_verify(&transcript.clone().into_sign_data(), &signature)
});
public
.make_bytes(
RANDOMNESS_VRF_CONTEXT,
&transcript,
&signature.pre_output,
)
.ok()
});
if let Some(randomness) = pre_digest.is_primary().then(|| randomness).flatten()
{
Self::deposit_randomness(&randomness);
}
AuthorVrfRandomness::<T>::put(randomness);
}
}
Lateness::<T>::kill();
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::report_equivocation(
key_owner_proof.validator_count(),
T::MaxNominators::get(),
))]
pub fn report_equivocation(
origin: OriginFor<T>,
equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
key_owner_proof: T::KeyOwnerProof,
) -> DispatchResultWithPostInfo {
let reporter = ensure_signed(origin)?;
T::EquivocationReportSystem::process_evidence(
Some(reporter),
(*equivocation_proof, key_owner_proof),
)?;
Ok(Pays::No.into())
}
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::report_equivocation(
key_owner_proof.validator_count(),
T::MaxNominators::get(),
))]
pub fn report_equivocation_unsigned(
origin: OriginFor<T>,
equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
key_owner_proof: T::KeyOwnerProof,
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
T::EquivocationReportSystem::process_evidence(
None,
(*equivocation_proof, key_owner_proof),
)?;
Ok(Pays::No.into())
}
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::plan_config_change())]
pub fn plan_config_change(
origin: OriginFor<T>,
config: NextConfigDescriptor,
) -> DispatchResult {
ensure_root(origin)?;
match config {
NextConfigDescriptor::V1 { c, allowed_slots } => {
ensure!(
(c.0 != 0 || allowed_slots != AllowedSlots::PrimarySlots) && c.1 != 0,
Error::<T>::InvalidConfiguration
);
},
}
PendingEpochConfigChange::<T>::put(config);
Ok(())
}
}
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity {
Self::validate_unsigned(source, call)
}
fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
Self::pre_dispatch(call)
}
}
}
impl<T: Config> FindAuthor<u32> for Pallet<T> {
fn find_author<'a, I>(digests: I) -> Option<u32>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
for (id, mut data) in digests.into_iter() {
if id == BABE_ENGINE_ID {
let pre_digest: PreDigest = PreDigest::decode(&mut data).ok()?;
return Some(pre_digest.authority_index())
}
}
None
}
}
impl<T: Config> IsMember<AuthorityId> for Pallet<T> {
fn is_member(authority_id: &AuthorityId) -> bool {
Authorities::<T>::get().iter().any(|id| &id.0 == authority_id)
}
}
impl<T: Config> pallet_session::ShouldEndSession<BlockNumberFor<T>> for Pallet<T> {
fn should_end_session(now: BlockNumberFor<T>) -> bool {
Self::initialize(now);
Self::should_epoch_change(now)
}
}
impl<T: Config> Pallet<T> {
pub fn epoch_index() -> u64 {
EpochIndex::<T>::get()
}
pub fn authorities() -> WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities> {
Authorities::<T>::get()
}
pub fn genesis_slot() -> Slot {
GenesisSlot::<T>::get()
}
pub fn current_slot() -> Slot {
CurrentSlot::<T>::get()
}
pub fn randomness() -> BabeRandomness {
Randomness::<T>::get()
}
pub fn initialized() -> Option<Option<PreDigest>> {
Initialized::<T>::get()
}
pub fn author_vrf_randomness() -> Option<BabeRandomness> {
AuthorVrfRandomness::<T>::get()
}
pub fn lateness() -> BlockNumberFor<T> {
Lateness::<T>::get()
}
pub fn epoch_config() -> Option<BabeEpochConfiguration> {
EpochConfig::<T>::get()
}
pub fn skipped_epochs() -> BoundedVec<(u64, SessionIndex), ConstU32<100>> {
SkippedEpochs::<T>::get()
}
pub fn slot_duration() -> T::Moment {
<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
}
pub fn should_epoch_change(now: BlockNumberFor<T>) -> bool {
now != One::one() && {
let diff = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start());
*diff >= T::EpochDuration::get()
}
}
pub fn next_expected_epoch_change(now: BlockNumberFor<T>) -> Option<BlockNumberFor<T>> {
let next_slot = Self::current_epoch_start().saturating_add(T::EpochDuration::get());
next_slot.checked_sub(*CurrentSlot::<T>::get()).map(|slots_remaining| {
let blocks_remaining: BlockNumberFor<T> = slots_remaining.saturated_into();
now.saturating_add(blocks_remaining)
})
}
pub fn enact_epoch_change(
authorities: WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
next_authorities: WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
session_index: Option<SessionIndex>,
) {
debug_assert!(Initialized::<T>::get().is_some());
if authorities.is_empty() {
log::warn!(target: LOG_TARGET, "Ignoring empty epoch change.");
return
}
let epoch_index = sp_consensus_babe::epoch_index(
CurrentSlot::<T>::get(),
GenesisSlot::<T>::get(),
T::EpochDuration::get(),
);
let current_epoch_index = EpochIndex::<T>::get();
if current_epoch_index.saturating_add(1) != epoch_index {
if let Some(session_index) = session_index {
SkippedEpochs::<T>::mutate(|skipped_epochs| {
if epoch_index < session_index as u64 {
log::warn!(
target: LOG_TARGET,
"Current epoch index {} is lower than session index {}.",
epoch_index,
session_index,
);
return
}
if skipped_epochs.is_full() {
skipped_epochs.remove(0);
}
skipped_epochs.force_push((epoch_index, session_index));
})
}
}
EpochIndex::<T>::put(epoch_index);
Authorities::<T>::put(authorities);
let next_epoch_index = epoch_index
.checked_add(1)
.expect("epoch indices will never reach 2^64 before the death of the universe; qed");
let randomness = Self::randomness_change_epoch(next_epoch_index);
Randomness::<T>::put(randomness);
NextAuthorities::<T>::put(&next_authorities);
EpochStart::<T>::mutate(|(previous_epoch_start_block, current_epoch_start_block)| {
*previous_epoch_start_block = core::mem::take(current_epoch_start_block);
*current_epoch_start_block = <frame_system::Pallet<T>>::block_number();
});
let next_randomness = NextRandomness::<T>::get();
let next_epoch = NextEpochDescriptor {
authorities: next_authorities.into_inner(),
randomness: next_randomness,
};
Self::deposit_consensus(ConsensusLog::NextEpochData(next_epoch));
if let Some(next_config) = NextEpochConfig::<T>::get() {
EpochConfig::<T>::put(next_config);
}
if let Some(pending_epoch_config_change) = PendingEpochConfigChange::<T>::take() {
let next_epoch_config: BabeEpochConfiguration =
pending_epoch_config_change.clone().into();
NextEpochConfig::<T>::put(next_epoch_config);
Self::deposit_consensus(ConsensusLog::NextConfigData(pending_epoch_config_change));
}
}
pub fn current_epoch_start() -> Slot {
sp_consensus_babe::epoch_start_slot(
EpochIndex::<T>::get(),
GenesisSlot::<T>::get(),
T::EpochDuration::get(),
)
}
pub fn current_epoch() -> Epoch {
Epoch {
epoch_index: EpochIndex::<T>::get(),
start_slot: Self::current_epoch_start(),
duration: T::EpochDuration::get(),
authorities: Authorities::<T>::get().into_inner(),
randomness: Randomness::<T>::get(),
config: EpochConfig::<T>::get()
.expect("EpochConfig is initialized in genesis; we never `take` or `kill` it; qed"),
}
}
pub fn next_epoch() -> Epoch {
let next_epoch_index = EpochIndex::<T>::get().checked_add(1).expect(
"epoch index is u64; it is always only incremented by one; \
if u64 is not enough we should crash for safety; qed.",
);
let start_slot = sp_consensus_babe::epoch_start_slot(
next_epoch_index,
GenesisSlot::<T>::get(),
T::EpochDuration::get(),
);
Epoch {
epoch_index: next_epoch_index,
start_slot,
duration: T::EpochDuration::get(),
authorities: NextAuthorities::<T>::get().into_inner(),
randomness: NextRandomness::<T>::get(),
config: NextEpochConfig::<T>::get().unwrap_or_else(|| {
EpochConfig::<T>::get().expect(
"EpochConfig is initialized in genesis; we never `take` or `kill` it; qed",
)
}),
}
}
fn deposit_consensus<U: Encode>(new: U) {
let log = DigestItem::Consensus(BABE_ENGINE_ID, new.encode());
<frame_system::Pallet<T>>::deposit_log(log)
}
fn deposit_randomness(randomness: &BabeRandomness) {
let segment_idx = SegmentIndex::<T>::get();
let mut segment = UnderConstruction::<T>::get(&segment_idx);
if segment.try_push(*randomness).is_ok() {
UnderConstruction::<T>::insert(&segment_idx, &segment);
} else {
let segment_idx = segment_idx + 1;
let bounded_randomness =
BoundedVec::<_, ConstU32<UNDER_CONSTRUCTION_SEGMENT_LENGTH>>::try_from(vec![
*randomness,
])
.expect("UNDER_CONSTRUCTION_SEGMENT_LENGTH >= 1");
UnderConstruction::<T>::insert(&segment_idx, bounded_randomness);
SegmentIndex::<T>::put(&segment_idx);
}
}
fn initialize_genesis_authorities(authorities: &[(AuthorityId, BabeAuthorityWeight)]) {
if !authorities.is_empty() {
assert!(Authorities::<T>::get().is_empty(), "Authorities are already initialized!");
let bounded_authorities =
WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.to_vec())
.expect("Initial number of authorities should be lower than T::MaxAuthorities");
Authorities::<T>::put(&bounded_authorities);
NextAuthorities::<T>::put(&bounded_authorities);
}
}
fn initialize_genesis_epoch(genesis_slot: Slot) {
GenesisSlot::<T>::put(genesis_slot);
debug_assert_ne!(*GenesisSlot::<T>::get(), 0);
let next = NextEpochDescriptor {
authorities: Authorities::<T>::get().into_inner(),
randomness: Randomness::<T>::get(),
};
Self::deposit_consensus(ConsensusLog::NextEpochData(next));
}
fn initialize(now: BlockNumberFor<T>) {
let initialized = Initialized::<T>::get().is_some();
if initialized {
return
}
let pre_digest =
<frame_system::Pallet<T>>::digest()
.logs
.iter()
.filter_map(|s| s.as_pre_runtime())
.filter_map(|(id, mut data)| {
if id == BABE_ENGINE_ID {
PreDigest::decode(&mut data).ok()
} else {
None
}
})
.next();
if let Some(ref pre_digest) = pre_digest {
let current_slot = pre_digest.slot();
if *GenesisSlot::<T>::get() == 0 {
Self::initialize_genesis_epoch(current_slot)
}
let lateness = current_slot.saturating_sub(CurrentSlot::<T>::get() + 1);
let lateness = BlockNumberFor::<T>::from(*lateness as u32);
Lateness::<T>::put(lateness);
CurrentSlot::<T>::put(current_slot);
}
Initialized::<T>::put(pre_digest);
T::EpochChangeTrigger::trigger::<T>(now);
}
fn randomness_change_epoch(next_epoch_index: u64) -> BabeRandomness {
let this_randomness = NextRandomness::<T>::get();
let segment_idx: u32 = SegmentIndex::<T>::mutate(|s| core::mem::replace(s, 0));
let rho_size = (segment_idx.saturating_add(1) * UNDER_CONSTRUCTION_SEGMENT_LENGTH) as usize;
let next_randomness = compute_randomness(
this_randomness,
next_epoch_index,
(0..segment_idx).flat_map(|i| UnderConstruction::<T>::take(&i)),
Some(rho_size),
);
NextRandomness::<T>::put(&next_randomness);
this_randomness
}
pub(crate) fn session_index_for_epoch(epoch_index: u64) -> SessionIndex {
let skipped_epochs = SkippedEpochs::<T>::get();
match skipped_epochs.binary_search_by_key(&epoch_index, |(epoch_index, _)| *epoch_index) {
Ok(index) => skipped_epochs[index].1,
Err(0) => epoch_index.saturated_into::<u32>(),
Err(index) => {
let closest_skipped_epoch = skipped_epochs[index - 1];
let skipped_epochs = closest_skipped_epoch.0 - closest_skipped_epoch.1 as u64;
epoch_index.saturating_sub(skipped_epochs).saturated_into::<u32>()
},
}
}
pub fn submit_unsigned_equivocation_report(
equivocation_proof: EquivocationProof<HeaderFor<T>>,
key_owner_proof: T::KeyOwnerProof,
) -> Option<()> {
T::EquivocationReportSystem::publish_evidence((equivocation_proof, key_owner_proof)).ok()
}
}
impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
fn on_timestamp_set(moment: T::Moment) {
let slot_duration = Self::slot_duration();
assert!(!slot_duration.is_zero(), "Babe slot duration cannot be zero.");
let timestamp_slot = moment / slot_duration;
let timestamp_slot = Slot::from(timestamp_slot.saturated_into::<u64>());
assert_eq!(
CurrentSlot::<T>::get(),
timestamp_slot,
"Timestamp slot must match `CurrentSlot`"
);
}
}
impl<T: Config> frame_support::traits::EstimateNextSessionRotation<BlockNumberFor<T>>
for Pallet<T>
{
fn average_session_length() -> BlockNumberFor<T> {
T::EpochDuration::get().saturated_into()
}
fn estimate_current_session_progress(_now: BlockNumberFor<T>) -> (Option<Permill>, Weight) {
let elapsed = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start()) + 1;
(
Some(Permill::from_rational(*elapsed, T::EpochDuration::get())),
T::DbWeight::get().reads(3),
)
}
fn estimate_next_session_rotation(
now: BlockNumberFor<T>,
) -> (Option<BlockNumberFor<T>>, Weight) {
(
Self::next_expected_epoch_change(now),
T::DbWeight::get().reads(3),
)
}
}
impl<T: Config> frame_support::traits::Lateness<BlockNumberFor<T>> for Pallet<T> {
fn lateness(&self) -> BlockNumberFor<T> {
Lateness::<T>::get()
}
}
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
type Public = AuthorityId;
}
impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T>
where
T: pallet_session::Config,
{
type Key = AuthorityId;
fn on_genesis_session<'a, I: 'a>(validators: I)
where
I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
{
let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>();
Self::initialize_genesis_authorities(&authorities);
}
fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, queued_validators: I)
where
I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
{
let authorities = validators.map(|(_account, k)| (k, 1)).collect::<Vec<_>>();
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::force_from(
authorities,
Some(
"Warning: The session has more validators than expected. \
A runtime configuration adjustment may be needed.",
),
);
let next_authorities = queued_validators.map(|(_account, k)| (k, 1)).collect::<Vec<_>>();
let next_bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::force_from(
next_authorities,
Some(
"Warning: The session has more queued validators than expected. \
A runtime configuration adjustment may be needed.",
),
);
let session_index = <pallet_session::Pallet<T>>::current_index();
Self::enact_epoch_change(bounded_authorities, next_bounded_authorities, Some(session_index))
}
fn on_disabled(i: u32) {
Self::deposit_consensus(ConsensusLog::OnDisabled(i))
}
}
fn compute_randomness(
last_epoch_randomness: BabeRandomness,
epoch_index: u64,
rho: impl Iterator<Item = BabeRandomness>,
rho_size_hint: Option<usize>,
) -> BabeRandomness {
let mut s = Vec::with_capacity(40 + rho_size_hint.unwrap_or(0) * RANDOMNESS_LENGTH);
s.extend_from_slice(&last_epoch_randomness);
s.extend_from_slice(&epoch_index.to_le_bytes());
for vrf_output in rho {
s.extend_from_slice(&vrf_output[..]);
}
sp_io::hashing::blake2_256(&s)
}
pub mod migrations {
use super::*;
use frame_support::pallet_prelude::{StorageValue, ValueQuery};
pub trait BabePalletPrefix: Config {
fn pallet_prefix() -> &'static str;
}
struct __OldNextEpochConfig<T>(core::marker::PhantomData<T>);
impl<T: BabePalletPrefix> frame_support::traits::StorageInstance for __OldNextEpochConfig<T> {
fn pallet_prefix() -> &'static str {
T::pallet_prefix()
}
const STORAGE_PREFIX: &'static str = "NextEpochConfig";
}
type OldNextEpochConfig<T> =
StorageValue<__OldNextEpochConfig<T>, Option<NextConfigDescriptor>, ValueQuery>;
pub fn add_epoch_configuration<T: BabePalletPrefix>(
epoch_config: BabeEpochConfiguration,
) -> Weight {
let mut writes = 0;
let mut reads = 0;
if let Some(pending_change) = OldNextEpochConfig::<T>::get() {
PendingEpochConfigChange::<T>::put(pending_change);
writes += 1;
}
reads += 1;
OldNextEpochConfig::<T>::kill();
EpochConfig::<T>::put(epoch_config.clone());
NextEpochConfig::<T>::put(epoch_config);
writes += 3;
T::DbWeight::get().reads_writes(reads, writes)
}
}