1#![cfg_attr(not(feature = "std"), no_std)]
22#![warn(unused_must_use, unsafe_code, unused_variables, unused_must_use)]
23
24extern crate alloc;
25
26use alloc::{boxed::Box, vec, vec::Vec};
27use codec::{Decode, Encode};
28use frame_support::{
29 dispatch::{DispatchResultWithPostInfo, Pays},
30 ensure,
31 traits::{ConstU32, DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler},
32 weights::Weight,
33 BoundedVec, WeakBoundedVec,
34};
35use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
36use sp_consensus_babe::{
37 digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
38 AllowedSlots, BabeAuthorityWeight, BabeEpochConfiguration, ConsensusLog, Epoch,
39 EquivocationProof, Randomness as BabeRandomness, Slot, BABE_ENGINE_ID, RANDOMNESS_LENGTH,
40 RANDOMNESS_VRF_CONTEXT,
41};
42use sp_core::crypto::Wraps;
43use sp_runtime::{
44 generic::DigestItem,
45 traits::{IsMember, One, SaturatedConversion, Saturating, Zero},
46 ConsensusEngineId, Permill,
47};
48use sp_session::{GetSessionNumber, GetValidatorCount};
49use sp_staking::{offence::OffenceReportSystem, SessionIndex};
50
51pub use sp_consensus_babe::AuthorityId;
52
53const LOG_TARGET: &str = "runtime::babe";
54
55mod default_weights;
56mod equivocation;
57mod randomness;
58
59#[cfg(any(feature = "runtime-benchmarks", test))]
60mod benchmarking;
61#[cfg(all(feature = "std", test))]
62mod mock;
63#[cfg(all(feature = "std", test))]
64mod tests;
65
66pub use equivocation::{EquivocationOffence, EquivocationReportSystem};
67#[allow(deprecated)]
68pub use randomness::CurrentBlockRandomness;
69pub use randomness::{
70 ParentBlockRandomness, RandomnessFromOneEpochAgo, RandomnessFromTwoEpochsAgo,
71};
72
73pub use pallet::*;
74
75pub trait WeightInfo {
76 fn plan_config_change() -> Weight;
77 fn report_equivocation(validator_count: u32, max_nominators_per_validator: u32) -> Weight;
78}
79
80pub trait EpochChangeTrigger {
82 fn trigger<T: Config>(now: BlockNumberFor<T>);
85}
86
87pub struct ExternalTrigger;
90
91impl EpochChangeTrigger for ExternalTrigger {
92 fn trigger<T: Config>(_: BlockNumberFor<T>) {} }
94
95pub struct SameAuthoritiesForever;
98
99impl EpochChangeTrigger for SameAuthoritiesForever {
100 fn trigger<T: Config>(now: BlockNumberFor<T>) {
101 if Pallet::<T>::should_epoch_change(now) {
102 let authorities = Authorities::<T>::get();
103 let next_authorities = authorities.clone();
104
105 Pallet::<T>::enact_epoch_change(authorities, next_authorities, None);
106 }
107 }
108}
109
110const UNDER_CONSTRUCTION_SEGMENT_LENGTH: u32 = 256;
111
112#[frame_support::pallet]
113pub mod pallet {
114 use super::*;
115 use frame_support::pallet_prelude::*;
116 use frame_system::pallet_prelude::*;
117
118 #[pallet::pallet]
120 pub struct Pallet<T>(_);
121
122 #[pallet::config]
123 #[pallet::disable_frame_system_supertrait_check]
124 pub trait Config: pallet_timestamp::Config {
125 #[pallet::constant]
129 type EpochDuration: Get<u64>;
130
131 #[pallet::constant]
137 type ExpectedBlockTime: Get<Self::Moment>;
138
139 type EpochChangeTrigger: EpochChangeTrigger;
145
146 type DisabledValidators: DisabledValidators;
150
151 type WeightInfo: WeightInfo;
153
154 #[pallet::constant]
156 type MaxAuthorities: Get<u32>;
157
158 #[pallet::constant]
160 type MaxNominators: Get<u32>;
161
162 type KeyOwnerProof: Parameter + GetSessionNumber + GetValidatorCount;
166
167 type EquivocationReportSystem: OffenceReportSystem<
171 Option<Self::AccountId>,
172 (EquivocationProof<HeaderFor<Self>>, Self::KeyOwnerProof),
173 >;
174 }
175
176 #[pallet::error]
177 pub enum Error<T> {
178 InvalidEquivocationProof,
180 InvalidKeyOwnershipProof,
182 DuplicateOffenceReport,
184 InvalidConfiguration,
186 }
187
188 #[pallet::storage]
190 pub type EpochIndex<T> = StorageValue<_, u64, ValueQuery>;
191
192 #[pallet::storage]
194 pub type Authorities<T: Config> = StorageValue<
195 _,
196 WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
197 ValueQuery,
198 >;
199
200 #[pallet::storage]
203 pub type GenesisSlot<T> = StorageValue<_, Slot, ValueQuery>;
204
205 #[pallet::storage]
207 pub type CurrentSlot<T> = StorageValue<_, Slot, ValueQuery>;
208
209 #[pallet::storage]
223 pub type Randomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
224
225 #[pallet::storage]
227 pub type PendingEpochConfigChange<T> = StorageValue<_, NextConfigDescriptor>;
228
229 #[pallet::storage]
231 pub type NextRandomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
232
233 #[pallet::storage]
235 pub type NextAuthorities<T: Config> = StorageValue<
236 _,
237 WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
238 ValueQuery,
239 >;
240
241 #[pallet::storage]
251 pub type SegmentIndex<T> = StorageValue<_, u32, ValueQuery>;
252
253 #[pallet::storage]
255 pub type UnderConstruction<T: Config> = StorageMap<
256 _,
257 Twox64Concat,
258 u32,
259 BoundedVec<BabeRandomness, ConstU32<UNDER_CONSTRUCTION_SEGMENT_LENGTH>>,
260 ValueQuery,
261 >;
262
263 #[pallet::storage]
266 pub type Initialized<T> = StorageValue<_, Option<PreDigest>>;
267
268 #[pallet::storage]
273 pub type AuthorVrfRandomness<T> = StorageValue<_, Option<BabeRandomness>, ValueQuery>;
274
275 #[pallet::storage]
281 pub type EpochStart<T: Config> =
282 StorageValue<_, (BlockNumberFor<T>, BlockNumberFor<T>), ValueQuery>;
283
284 #[pallet::storage]
290 pub type Lateness<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
291
292 #[pallet::storage]
295 pub type EpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
296
297 #[pallet::storage]
300 pub type NextEpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
301
302 #[pallet::storage]
311 pub type SkippedEpochs<T> =
312 StorageValue<_, BoundedVec<(u64, SessionIndex), ConstU32<100>>, ValueQuery>;
313
314 #[derive(frame_support::DefaultNoBound)]
315 #[pallet::genesis_config]
316 pub struct GenesisConfig<T: Config> {
317 pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
318 pub epoch_config: BabeEpochConfiguration,
319 #[serde(skip)]
320 pub _config: core::marker::PhantomData<T>,
321 }
322
323 #[pallet::genesis_build]
324 impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
325 fn build(&self) {
326 SegmentIndex::<T>::put(0);
327 Pallet::<T>::initialize_genesis_authorities(&self.authorities);
328 EpochConfig::<T>::put(&self.epoch_config);
329 }
330 }
331
332 #[pallet::hooks]
333 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
334 fn on_initialize(now: BlockNumberFor<T>) -> Weight {
336 Self::initialize(now);
337 Weight::zero()
338 }
339
340 #[cfg(feature = "try-runtime")]
341 fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
342 Self::do_try_state()
343 }
344
345 fn on_finalize(_now: BlockNumberFor<T>) {
347 if let Some(pre_digest) = Initialized::<T>::take().flatten() {
353 let authority_index = pre_digest.authority_index();
354
355 if T::DisabledValidators::is_disabled(authority_index) {
356 panic!(
357 "Validator with index {:?} is disabled and should not be attempting to author blocks.",
358 authority_index,
359 );
360 }
361
362 if let Some(signature) = pre_digest.vrf_signature() {
363 let randomness: Option<BabeRandomness> = Authorities::<T>::get()
364 .get(authority_index as usize)
365 .and_then(|(authority, _)| {
366 let public = authority.as_inner_ref();
367 let transcript = sp_consensus_babe::make_vrf_transcript(
368 &Randomness::<T>::get(),
369 CurrentSlot::<T>::get(),
370 EpochIndex::<T>::get(),
371 );
372
373 debug_assert!({
377 use sp_core::crypto::VrfPublic;
378 public.vrf_verify(&transcript.clone().into_sign_data(), &signature)
379 });
380
381 public
382 .make_bytes(
383 RANDOMNESS_VRF_CONTEXT,
384 &transcript,
385 &signature.pre_output,
386 )
387 .ok()
388 });
389
390 if let Some(randomness) = pre_digest.is_primary().then(|| randomness).flatten()
391 {
392 Self::deposit_randomness(&randomness);
393 }
394
395 AuthorVrfRandomness::<T>::put(randomness);
396 }
397 }
398
399 Lateness::<T>::kill();
401 }
402 }
403
404 #[pallet::call]
405 impl<T: Config> Pallet<T> {
406 #[pallet::call_index(0)]
411 #[pallet::weight(<T as Config>::WeightInfo::report_equivocation(
412 key_owner_proof.validator_count(),
413 T::MaxNominators::get(),
414 ))]
415 pub fn report_equivocation(
416 origin: OriginFor<T>,
417 equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
418 key_owner_proof: T::KeyOwnerProof,
419 ) -> DispatchResultWithPostInfo {
420 let reporter = ensure_signed(origin)?;
421 T::EquivocationReportSystem::process_evidence(
422 Some(reporter),
423 (*equivocation_proof, key_owner_proof),
424 )?;
425 Ok(Pays::No.into())
427 }
428
429 #[pallet::call_index(1)]
438 #[pallet::weight(<T as Config>::WeightInfo::report_equivocation(
439 key_owner_proof.validator_count(),
440 T::MaxNominators::get(),
441 ))]
442 pub fn report_equivocation_unsigned(
443 origin: OriginFor<T>,
444 equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
445 key_owner_proof: T::KeyOwnerProof,
446 ) -> DispatchResultWithPostInfo {
447 ensure_none(origin)?;
448 T::EquivocationReportSystem::process_evidence(
449 None,
450 (*equivocation_proof, key_owner_proof),
451 )?;
452 Ok(Pays::No.into())
453 }
454
455 #[pallet::call_index(2)]
460 #[pallet::weight(<T as Config>::WeightInfo::plan_config_change())]
461 pub fn plan_config_change(
462 origin: OriginFor<T>,
463 config: NextConfigDescriptor,
464 ) -> DispatchResult {
465 ensure_root(origin)?;
466 match config {
467 NextConfigDescriptor::V1 { c, allowed_slots } => {
468 ensure!(
469 (c.0 != 0 || allowed_slots != AllowedSlots::PrimarySlots) && c.1 != 0,
470 Error::<T>::InvalidConfiguration
471 );
472 },
473 }
474 PendingEpochConfigChange::<T>::put(config);
475 Ok(())
476 }
477 }
478
479 #[allow(deprecated)]
480 #[pallet::validate_unsigned]
481 impl<T: Config> ValidateUnsigned for Pallet<T> {
482 type Call = Call<T>;
483 fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity {
484 Self::validate_unsigned(source, call)
485 }
486
487 fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
488 Self::pre_dispatch(call)
489 }
490 }
491}
492
493impl<T: Config> FindAuthor<u32> for Pallet<T> {
494 fn find_author<'a, I>(digests: I) -> Option<u32>
495 where
496 I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
497 {
498 for (id, mut data) in digests.into_iter() {
499 if id == BABE_ENGINE_ID {
500 let pre_digest: PreDigest = PreDigest::decode(&mut data).ok()?;
501 return Some(pre_digest.authority_index());
502 }
503 }
504
505 None
506 }
507}
508
509impl<T: Config> IsMember<AuthorityId> for Pallet<T> {
510 fn is_member(authority_id: &AuthorityId) -> bool {
511 Authorities::<T>::get().iter().any(|id| &id.0 == authority_id)
512 }
513}
514
515impl<T: Config> pallet_session::ShouldEndSession<BlockNumberFor<T>> for Pallet<T> {
516 fn should_end_session(now: BlockNumberFor<T>) -> bool {
517 Self::initialize(now);
522 Self::should_epoch_change(now)
523 }
524}
525
526impl<T: Config> Pallet<T> {
527 pub fn epoch_index() -> u64 {
529 EpochIndex::<T>::get()
530 }
531 pub fn authorities() -> WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities> {
533 Authorities::<T>::get()
534 }
535 pub fn genesis_slot() -> Slot {
537 GenesisSlot::<T>::get()
538 }
539 pub fn current_slot() -> Slot {
541 CurrentSlot::<T>::get()
542 }
543 pub fn randomness() -> BabeRandomness {
545 Randomness::<T>::get()
546 }
547 pub fn initialized() -> Option<Option<PreDigest>> {
549 Initialized::<T>::get()
550 }
551 pub fn author_vrf_randomness() -> Option<BabeRandomness> {
553 AuthorVrfRandomness::<T>::get()
554 }
555 pub fn lateness() -> BlockNumberFor<T> {
557 Lateness::<T>::get()
558 }
559 pub fn epoch_config() -> Option<BabeEpochConfiguration> {
561 EpochConfig::<T>::get()
562 }
563 pub fn skipped_epochs() -> BoundedVec<(u64, SessionIndex), ConstU32<100>> {
565 SkippedEpochs::<T>::get()
566 }
567
568 pub fn slot_duration() -> T::Moment {
570 <T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
573 }
574
575 pub fn should_epoch_change(now: BlockNumberFor<T>) -> bool {
578 now != One::one() && {
587 let diff = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start());
588 *diff >= T::EpochDuration::get()
589 }
590 }
591
592 pub fn next_expected_epoch_change(now: BlockNumberFor<T>) -> Option<BlockNumberFor<T>> {
608 let next_slot = Self::current_epoch_start().saturating_add(T::EpochDuration::get());
609 next_slot.checked_sub(*CurrentSlot::<T>::get()).map(|slots_remaining| {
610 let blocks_remaining: BlockNumberFor<T> = slots_remaining.saturated_into();
612 now.saturating_add(blocks_remaining)
613 })
614 }
615
616 pub fn enact_epoch_change(
624 authorities: WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
625 next_authorities: WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
626 session_index: Option<SessionIndex>,
627 ) {
628 debug_assert!(Initialized::<T>::get().is_some());
631
632 if authorities.is_empty() {
633 log::warn!(target: LOG_TARGET, "Ignoring empty epoch change.");
634 return;
635 }
636
637 let epoch_index = sp_consensus_babe::epoch_index(
646 CurrentSlot::<T>::get(),
647 GenesisSlot::<T>::get(),
648 T::EpochDuration::get(),
649 );
650
651 let current_epoch_index = EpochIndex::<T>::get();
652 if current_epoch_index.saturating_add(1) != epoch_index {
653 if let Some(session_index) = session_index {
656 SkippedEpochs::<T>::mutate(|skipped_epochs| {
657 if epoch_index < session_index as u64 {
658 log::warn!(
659 target: LOG_TARGET,
660 "Current epoch index {} is lower than session index {}.",
661 epoch_index,
662 session_index,
663 );
664
665 return;
666 }
667
668 if skipped_epochs.is_full() {
669 skipped_epochs.remove(0);
673 }
674
675 skipped_epochs.force_push((epoch_index, session_index));
676 })
677 }
678 }
679
680 EpochIndex::<T>::put(epoch_index);
681 Authorities::<T>::put(authorities);
682
683 let next_epoch_index = epoch_index
685 .checked_add(1)
686 .expect("epoch indices will never reach 2^64 before the death of the universe; qed");
687
688 let randomness = Self::randomness_change_epoch(next_epoch_index);
691 Randomness::<T>::put(randomness);
692
693 NextAuthorities::<T>::put(&next_authorities);
695
696 EpochStart::<T>::mutate(|(previous_epoch_start_block, current_epoch_start_block)| {
698 *previous_epoch_start_block = core::mem::take(current_epoch_start_block);
699 *current_epoch_start_block = <frame_system::Pallet<T>>::block_number();
700 });
701
702 let next_randomness = NextRandomness::<T>::get();
705
706 let next_epoch = NextEpochDescriptor {
707 authorities: next_authorities.into_inner(),
708 randomness: next_randomness,
709 };
710 Self::deposit_consensus(ConsensusLog::NextEpochData(next_epoch));
711
712 if let Some(next_config) = NextEpochConfig::<T>::get() {
713 EpochConfig::<T>::put(next_config);
714 }
715
716 if let Some(pending_epoch_config_change) = PendingEpochConfigChange::<T>::take() {
717 let next_epoch_config: BabeEpochConfiguration =
718 pending_epoch_config_change.clone().into();
719 NextEpochConfig::<T>::put(next_epoch_config);
720
721 Self::deposit_consensus(ConsensusLog::NextConfigData(pending_epoch_config_change));
722 }
723 }
724
725 pub fn current_epoch_start() -> Slot {
730 sp_consensus_babe::epoch_start_slot(
731 EpochIndex::<T>::get(),
732 GenesisSlot::<T>::get(),
733 T::EpochDuration::get(),
734 )
735 }
736
737 pub fn current_epoch() -> Epoch {
739 Epoch {
740 epoch_index: EpochIndex::<T>::get(),
741 start_slot: Self::current_epoch_start(),
742 duration: T::EpochDuration::get(),
743 authorities: Authorities::<T>::get().into_inner(),
744 randomness: Randomness::<T>::get(),
745 config: EpochConfig::<T>::get()
746 .expect("EpochConfig is initialized in genesis; we never `take` or `kill` it; qed"),
747 }
748 }
749
750 pub fn next_epoch() -> Epoch {
753 let next_epoch_index = EpochIndex::<T>::get().checked_add(1).expect(
754 "epoch index is u64; it is always only incremented by one; \
755 if u64 is not enough we should crash for safety; qed.",
756 );
757
758 let start_slot = sp_consensus_babe::epoch_start_slot(
759 next_epoch_index,
760 GenesisSlot::<T>::get(),
761 T::EpochDuration::get(),
762 );
763
764 Epoch {
765 epoch_index: next_epoch_index,
766 start_slot,
767 duration: T::EpochDuration::get(),
768 authorities: NextAuthorities::<T>::get().into_inner(),
769 randomness: NextRandomness::<T>::get(),
770 config: NextEpochConfig::<T>::get().unwrap_or_else(|| {
771 EpochConfig::<T>::get().expect(
772 "EpochConfig is initialized in genesis; we never `take` or `kill` it; qed",
773 )
774 }),
775 }
776 }
777
778 fn deposit_consensus<U: Encode>(new: U) {
779 let log = DigestItem::Consensus(BABE_ENGINE_ID, new.encode());
780 <frame_system::Pallet<T>>::deposit_log(log)
781 }
782
783 fn deposit_randomness(randomness: &BabeRandomness) {
784 let segment_idx = SegmentIndex::<T>::get();
785 let mut segment = UnderConstruction::<T>::get(&segment_idx);
786 if segment.try_push(*randomness).is_ok() {
787 UnderConstruction::<T>::insert(&segment_idx, &segment);
789 } else {
790 let segment_idx = segment_idx + 1;
792 let bounded_randomness =
793 BoundedVec::<_, ConstU32<UNDER_CONSTRUCTION_SEGMENT_LENGTH>>::try_from(vec![
794 *randomness,
795 ])
796 .expect("UNDER_CONSTRUCTION_SEGMENT_LENGTH >= 1");
797 UnderConstruction::<T>::insert(&segment_idx, bounded_randomness);
798 SegmentIndex::<T>::put(&segment_idx);
799 }
800 }
801
802 fn initialize_genesis_authorities(authorities: &[(AuthorityId, BabeAuthorityWeight)]) {
803 if !authorities.is_empty() {
804 assert!(Authorities::<T>::get().is_empty(), "Authorities are already initialized!");
805 let bounded_authorities =
806 WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.to_vec())
807 .expect("Initial number of authorities should be lower than T::MaxAuthorities");
808 Authorities::<T>::put(&bounded_authorities);
809 NextAuthorities::<T>::put(&bounded_authorities);
810 }
811 }
812
813 fn initialize_genesis_epoch(genesis_slot: Slot) {
814 GenesisSlot::<T>::put(genesis_slot);
815 debug_assert_ne!(*GenesisSlot::<T>::get(), 0);
816
817 let next = NextEpochDescriptor {
821 authorities: Authorities::<T>::get().into_inner(),
822 randomness: Randomness::<T>::get(),
823 };
824
825 Self::deposit_consensus(ConsensusLog::NextEpochData(next));
826 }
827
828 fn initialize(now: BlockNumberFor<T>) {
829 let initialized = Initialized::<T>::get().is_some();
832 if initialized {
833 return;
834 }
835
836 let pre_digest =
837 <frame_system::Pallet<T>>::digest()
838 .logs
839 .iter()
840 .filter_map(|s| s.as_pre_runtime())
841 .filter_map(|(id, mut data)| {
842 if id == BABE_ENGINE_ID {
843 PreDigest::decode(&mut data).ok()
844 } else {
845 None
846 }
847 })
848 .next();
849
850 if let Some(ref pre_digest) = pre_digest {
851 let current_slot = pre_digest.slot();
853
854 if *GenesisSlot::<T>::get() == 0 {
858 Self::initialize_genesis_epoch(current_slot)
859 }
860
861 let lateness = current_slot.saturating_sub(CurrentSlot::<T>::get() + 1);
863 let lateness = BlockNumberFor::<T>::from(*lateness as u32);
864
865 Lateness::<T>::put(lateness);
866 CurrentSlot::<T>::put(current_slot);
867 }
868
869 Initialized::<T>::put(pre_digest);
870
871 T::EpochChangeTrigger::trigger::<T>(now);
873 }
874
875 fn randomness_change_epoch(next_epoch_index: u64) -> BabeRandomness {
878 let this_randomness = NextRandomness::<T>::get();
879 let segment_idx: u32 = SegmentIndex::<T>::mutate(|s| core::mem::replace(s, 0));
880
881 let rho_size = (segment_idx.saturating_add(1) * UNDER_CONSTRUCTION_SEGMENT_LENGTH) as usize;
883
884 let next_randomness = compute_randomness(
885 this_randomness,
886 next_epoch_index,
887 (0..segment_idx).flat_map(|i| UnderConstruction::<T>::take(&i)),
888 Some(rho_size),
889 );
890 NextRandomness::<T>::put(&next_randomness);
891 this_randomness
892 }
893
894 pub(crate) fn session_index_for_epoch(epoch_index: u64) -> SessionIndex {
901 let skipped_epochs = SkippedEpochs::<T>::get();
902 match skipped_epochs.binary_search_by_key(&epoch_index, |(epoch_index, _)| *epoch_index) {
903 Ok(index) => skipped_epochs[index].1,
905 Err(0) => epoch_index.saturated_into::<u32>(),
908 Err(index) => {
910 let closest_skipped_epoch = skipped_epochs[index - 1];
913
914 let skipped_epochs = closest_skipped_epoch.0 - closest_skipped_epoch.1 as u64;
919 epoch_index.saturating_sub(skipped_epochs).saturated_into::<u32>()
920 },
921 }
922 }
923
924 pub fn submit_unsigned_equivocation_report(
929 equivocation_proof: EquivocationProof<HeaderFor<T>>,
930 key_owner_proof: T::KeyOwnerProof,
931 ) -> Option<()> {
932 T::EquivocationReportSystem::publish_evidence((equivocation_proof, key_owner_proof)).ok()
933 }
934}
935
936#[cfg(any(feature = "try-runtime", test))]
937impl<T: Config> Pallet<T> {
938 pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
952 use frame_support::ensure;
953
954 ensure!(
955 Authorities::<T>::decode_len().unwrap_or(0) as u32 <= T::MaxAuthorities::get(),
956 "Authorities length exceeds MaxAuthorities"
957 );
958
959 ensure!(
960 NextAuthorities::<T>::decode_len().unwrap_or(0) as u32 <= T::MaxAuthorities::get(),
961 "NextAuthorities length exceeds MaxAuthorities"
962 );
963
964 ensure!(
965 EpochConfig::<T>::get().is_some(),
966 "EpochConfig must be initialized and never be None"
967 );
968
969 let genesis_slot = *GenesisSlot::<T>::get();
970 ensure!(*CurrentSlot::<T>::get() >= genesis_slot, "CurrentSlot must be >= GenesisSlot");
971
972 let skipped_epochs = SkippedEpochs::<T>::get();
973 ensure!(
974 skipped_epochs.windows(2).all(|w| w[0].0 < w[1].0),
975 "SkippedEpochs must be sorted in ascending order by epoch index"
976 );
977 ensure!(
978 skipped_epochs
979 .iter()
980 .all(|(epoch_index, session_index)| *epoch_index >= *session_index as u64),
981 "SkippedEpochs entry has epoch_index < session_index"
982 );
983
984 let (previous_epoch_start, current_epoch_start) = EpochStart::<T>::get();
985 ensure!(
986 previous_epoch_start <= current_epoch_start,
987 "EpochStart previous epoch start must be <= current epoch start"
988 );
989
990 Ok(())
991 }
992}
993
994impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
995 fn on_timestamp_set(moment: T::Moment) {
996 let slot_duration = Self::slot_duration();
997 assert!(!slot_duration.is_zero(), "Babe slot duration cannot be zero.");
998
999 let timestamp_slot = moment / slot_duration;
1000 let timestamp_slot = Slot::from(timestamp_slot.saturated_into::<u64>());
1001
1002 assert_eq!(
1003 CurrentSlot::<T>::get(),
1004 timestamp_slot,
1005 "Timestamp slot must match `CurrentSlot`"
1006 );
1007 }
1008}
1009
1010impl<T: Config> frame_support::traits::EstimateNextSessionRotation<BlockNumberFor<T>>
1011 for Pallet<T>
1012{
1013 fn average_session_length() -> BlockNumberFor<T> {
1014 T::EpochDuration::get().saturated_into()
1015 }
1016
1017 fn estimate_current_session_progress(_now: BlockNumberFor<T>) -> (Option<Permill>, Weight) {
1018 let elapsed = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start()) + 1;
1019
1020 (
1021 Some(Permill::from_rational(*elapsed, T::EpochDuration::get())),
1022 T::DbWeight::get().reads(3),
1024 )
1025 }
1026
1027 fn estimate_next_session_rotation(
1028 now: BlockNumberFor<T>,
1029 ) -> (Option<BlockNumberFor<T>>, Weight) {
1030 (
1031 Self::next_expected_epoch_change(now),
1032 T::DbWeight::get().reads(3),
1034 )
1035 }
1036}
1037
1038impl<T: Config> frame_support::traits::Lateness<BlockNumberFor<T>> for Pallet<T> {
1039 fn lateness(&self) -> BlockNumberFor<T> {
1040 Lateness::<T>::get()
1041 }
1042}
1043
1044impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
1045 type Public = AuthorityId;
1046}
1047
1048impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T>
1049where
1050 T: pallet_session::Config,
1051{
1052 type Key = AuthorityId;
1053
1054 fn on_genesis_session<'a, I: 'a>(validators: I)
1055 where
1056 I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
1057 {
1058 let authorities = validators.map(|(_, k)| (k, 1)).collect::<Vec<_>>();
1059 Self::initialize_genesis_authorities(&authorities);
1060 }
1061
1062 fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, queued_validators: I)
1063 where
1064 I: Iterator<Item = (&'a T::AccountId, AuthorityId)>,
1065 {
1066 let authorities = validators.map(|(_account, k)| (k, 1)).collect::<Vec<_>>();
1067 let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::force_from(
1068 authorities,
1069 Some(
1070 "Warning: The session has more validators than expected. \
1071 A runtime configuration adjustment may be needed.",
1072 ),
1073 );
1074
1075 let next_authorities = queued_validators.map(|(_account, k)| (k, 1)).collect::<Vec<_>>();
1076 let next_bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::force_from(
1077 next_authorities,
1078 Some(
1079 "Warning: The session has more queued validators than expected. \
1080 A runtime configuration adjustment may be needed.",
1081 ),
1082 );
1083
1084 let session_index = <pallet_session::Pallet<T>>::current_index();
1085
1086 Self::enact_epoch_change(bounded_authorities, next_bounded_authorities, Some(session_index))
1087 }
1088
1089 fn on_disabled(i: u32) {
1090 Self::deposit_consensus(ConsensusLog::OnDisabled(i))
1091 }
1092}
1093
1094fn compute_randomness(
1099 last_epoch_randomness: BabeRandomness,
1100 epoch_index: u64,
1101 rho: impl Iterator<Item = BabeRandomness>,
1102 rho_size_hint: Option<usize>,
1103) -> BabeRandomness {
1104 let mut s = Vec::with_capacity(40 + rho_size_hint.unwrap_or(0) * RANDOMNESS_LENGTH);
1105 s.extend_from_slice(&last_epoch_randomness);
1106 s.extend_from_slice(&epoch_index.to_le_bytes());
1107
1108 for vrf_output in rho {
1109 s.extend_from_slice(&vrf_output[..]);
1110 }
1111
1112 sp_io::hashing::blake2_256(&s)
1113}
1114
1115pub mod migrations {
1116 use super::*;
1117 use frame_support::pallet_prelude::{StorageValue, ValueQuery};
1118
1119 pub trait BabePalletPrefix: Config {
1121 fn pallet_prefix() -> &'static str;
1122 }
1123
1124 struct __OldNextEpochConfig<T>(core::marker::PhantomData<T>);
1125 impl<T: BabePalletPrefix> frame_support::traits::StorageInstance for __OldNextEpochConfig<T> {
1126 fn pallet_prefix() -> &'static str {
1127 T::pallet_prefix()
1128 }
1129 const STORAGE_PREFIX: &'static str = "NextEpochConfig";
1130 }
1131
1132 type OldNextEpochConfig<T> =
1133 StorageValue<__OldNextEpochConfig<T>, Option<NextConfigDescriptor>, ValueQuery>;
1134
1135 pub fn add_epoch_configuration<T: BabePalletPrefix>(
1138 epoch_config: BabeEpochConfiguration,
1139 ) -> Weight {
1140 let mut writes = 0;
1141 let mut reads = 0;
1142
1143 if let Some(pending_change) = OldNextEpochConfig::<T>::get() {
1144 PendingEpochConfigChange::<T>::put(pending_change);
1145
1146 writes += 1;
1147 }
1148
1149 reads += 1;
1150
1151 OldNextEpochConfig::<T>::kill();
1152
1153 EpochConfig::<T>::put(epoch_config.clone());
1154 NextEpochConfig::<T>::put(epoch_config);
1155
1156 writes += 3;
1157
1158 T::DbWeight::get().reads_writes(reads, writes)
1159 }
1160}