Skip to main content

pallet_babe/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Consensus extension module for BABE consensus. Collects on-chain randomness
19//! from VRF outputs and manages epoch transitions.
20
21#![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
80/// Trigger an epoch change, if any should take place.
81pub trait EpochChangeTrigger {
82	/// Trigger an epoch change, if any should take place. This should be called
83	/// during every block, after initialization is done.
84	fn trigger<T: Config>(now: BlockNumberFor<T>);
85}
86
87/// A type signifying to BABE that an external trigger
88/// for epoch changes (e.g. pallet-session) is used.
89pub struct ExternalTrigger;
90
91impl EpochChangeTrigger for ExternalTrigger {
92	fn trigger<T: Config>(_: BlockNumberFor<T>) {} // nothing - trigger is external.
93}
94
95/// A type signifying to BABE that it should perform epoch changes
96/// with an internal trigger, recycling the same authorities forever.
97pub 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	/// The BABE Pallet
119	#[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		/// The amount of time, in slots, that each epoch should last.
126		/// NOTE: Currently it is not possible to change the epoch duration after
127		/// the chain has started. Attempting to do so will brick block production.
128		#[pallet::constant]
129		type EpochDuration: Get<u64>;
130
131		/// The expected average block time at which BABE should be creating
132		/// blocks. Since BABE is probabilistic it is not trivial to figure out
133		/// what the expected average block time should be based on the slot
134		/// duration and the security parameter `c` (where `1 - c` represents
135		/// the probability of a slot being empty).
136		#[pallet::constant]
137		type ExpectedBlockTime: Get<Self::Moment>;
138
139		/// BABE requires some logic to be triggered on every block to query for whether an epoch
140		/// has ended and to perform the transition to the next epoch.
141		///
142		/// Typically, the `ExternalTrigger` type should be used. An internal trigger should only be
143		/// used when no other module is responsible for changing authority set.
144		type EpochChangeTrigger: EpochChangeTrigger;
145
146		/// A way to check whether a given validator is disabled and should not be authoring blocks.
147		/// Blocks authored by a disabled validator will lead to a panic as part of this module's
148		/// initialization.
149		type DisabledValidators: DisabledValidators;
150
151		/// Helper for weights computations
152		type WeightInfo: WeightInfo;
153
154		/// Max number of authorities allowed
155		#[pallet::constant]
156		type MaxAuthorities: Get<u32>;
157
158		/// The maximum number of nominators for each validator.
159		#[pallet::constant]
160		type MaxNominators: Get<u32>;
161
162		/// The proof of key ownership, used for validating equivocation reports.
163		/// The proof must include the session index and validator count of the
164		/// session at which the equivocation occurred.
165		type KeyOwnerProof: Parameter + GetSessionNumber + GetValidatorCount;
166
167		/// The equivocation handling subsystem, defines methods to check/report an
168		/// offence and for submitting a transaction to report an equivocation
169		/// (from an offchain context).
170		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		/// An equivocation proof provided as part of an equivocation report is invalid.
179		InvalidEquivocationProof,
180		/// A key ownership proof provided as part of an equivocation report is invalid.
181		InvalidKeyOwnershipProof,
182		/// A given equivocation report is valid but already previously reported.
183		DuplicateOffenceReport,
184		/// Submitted configuration is invalid.
185		InvalidConfiguration,
186	}
187
188	/// Current epoch index.
189	#[pallet::storage]
190	pub type EpochIndex<T> = StorageValue<_, u64, ValueQuery>;
191
192	/// Current epoch authorities.
193	#[pallet::storage]
194	pub type Authorities<T: Config> = StorageValue<
195		_,
196		WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
197		ValueQuery,
198	>;
199
200	/// The slot at which the first epoch actually started. This is 0
201	/// until the first block of the chain.
202	#[pallet::storage]
203	pub type GenesisSlot<T> = StorageValue<_, Slot, ValueQuery>;
204
205	/// Current slot number.
206	#[pallet::storage]
207	pub type CurrentSlot<T> = StorageValue<_, Slot, ValueQuery>;
208
209	/// The epoch randomness for the *current* epoch.
210	///
211	/// # Security
212	///
213	/// This MUST NOT be used for gambling, as it can be influenced by a
214	/// malicious validator in the short term. It MAY be used in many
215	/// cryptographic protocols, however, so long as one remembers that this
216	/// (like everything else on-chain) it is public. For example, it can be
217	/// used where a number is needed that cannot have been chosen by an
218	/// adversary, for purposes such as public-coin zero-knowledge proofs.
219	// NOTE: the following fields don't use the constants to define the
220	// array size because the metadata API currently doesn't resolve the
221	// variable to its underlying value.
222	#[pallet::storage]
223	pub type Randomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
224
225	/// Pending epoch configuration change that will be applied when the next epoch is enacted.
226	#[pallet::storage]
227	pub type PendingEpochConfigChange<T> = StorageValue<_, NextConfigDescriptor>;
228
229	/// Next epoch randomness.
230	#[pallet::storage]
231	pub type NextRandomness<T> = StorageValue<_, BabeRandomness, ValueQuery>;
232
233	/// Next epoch authorities.
234	#[pallet::storage]
235	pub type NextAuthorities<T: Config> = StorageValue<
236		_,
237		WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities>,
238		ValueQuery,
239	>;
240
241	/// Randomness under construction.
242	///
243	/// We make a trade-off between storage accesses and list length.
244	/// We store the under-construction randomness in segments of up to
245	/// `UNDER_CONSTRUCTION_SEGMENT_LENGTH`.
246	///
247	/// Once a segment reaches this length, we begin the next one.
248	/// We reset all segments and return to `0` at the beginning of every
249	/// epoch.
250	#[pallet::storage]
251	pub type SegmentIndex<T> = StorageValue<_, u32, ValueQuery>;
252
253	/// TWOX-NOTE: `SegmentIndex` is an increasing integer, so this is okay.
254	#[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	/// Temporary value (cleared at block finalization) which is `Some`
264	/// if per-block initialization has already been called for current block.
265	#[pallet::storage]
266	pub type Initialized<T> = StorageValue<_, Option<PreDigest>>;
267
268	/// This field should always be populated during block processing unless
269	/// secondary plain slots are enabled (which don't contain a VRF output).
270	///
271	/// It is set in `on_finalize`, before it will contain the value from the last block.
272	#[pallet::storage]
273	pub type AuthorVrfRandomness<T> = StorageValue<_, Option<BabeRandomness>, ValueQuery>;
274
275	/// The block numbers when the last and current epoch have started, respectively `N-1` and
276	/// `N`.
277	/// NOTE: We track this is in order to annotate the block number when a given pool of
278	/// entropy was fixed (i.e. it was known to chain observers). Since epochs are defined in
279	/// slots, which may be skipped, the block numbers may not line up with the slot numbers.
280	#[pallet::storage]
281	pub type EpochStart<T: Config> =
282		StorageValue<_, (BlockNumberFor<T>, BlockNumberFor<T>), ValueQuery>;
283
284	/// How late the current block is compared to its parent.
285	///
286	/// This entry is populated as part of block execution and is cleaned up
287	/// on block finalization. Querying this storage entry outside of block
288	/// execution context should always yield zero.
289	#[pallet::storage]
290	pub type Lateness<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
291
292	/// The configuration for the current epoch. Should never be `None` as it is initialized in
293	/// genesis.
294	#[pallet::storage]
295	pub type EpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
296
297	/// The configuration for the next epoch, `None` if the config will not change
298	/// (you can fallback to `EpochConfig` instead in that case).
299	#[pallet::storage]
300	pub type NextEpochConfig<T> = StorageValue<_, BabeEpochConfiguration>;
301
302	/// A list of the last 100 skipped epochs and the corresponding session index
303	/// when the epoch was skipped.
304	///
305	/// This is only used for validating equivocation proofs. An equivocation proof
306	/// must contains a key-ownership proof for a given session, therefore we need a
307	/// way to tie together sessions and epoch indices, i.e. we need to validate that
308	/// a validator was the owner of a given key on a given session, and what the
309	/// active epoch index was during that session.
310	#[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		/// Initialization
335		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		/// Block finalization
346		fn on_finalize(_now: BlockNumberFor<T>) {
347			// at the end of the block, we can safely include the new VRF output
348			// from this block into the under-construction randomness. If we've determined
349			// that this block was the first in a new epoch, the changeover logic has
350			// already occurred at this point, so the under-construction randomness
351			// will only contain outputs from the right epoch.
352			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							// NOTE: this is verified by the client when importing the block, before
374							// execution. We don't run the verification again here to avoid slowing
375							// down the runtime.
376							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			// remove temporary "environment" entry from storage
400			Lateness::<T>::kill();
401		}
402	}
403
404	#[pallet::call]
405	impl<T: Config> Pallet<T> {
406		/// Report authority equivocation/misbehavior. This method will verify
407		/// the equivocation proof and validate the given key ownership proof
408		/// against the extracted offender. If both are valid, the offence will
409		/// be reported.
410		#[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			// Waive the fee since the report is valid and beneficial
426			Ok(Pays::No.into())
427		}
428
429		/// Report authority equivocation/misbehavior. This method will verify
430		/// the equivocation proof and validate the given key ownership proof
431		/// against the extracted offender. If both are valid, the offence will
432		/// be reported.
433		/// This extrinsic must be called unsigned and it is expected that only
434		/// block authors will call it (validated in `ValidateUnsigned`), as such
435		/// if the block author is defined it will be defined as the equivocation
436		/// reporter.
437		#[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		/// Plan an epoch config change. The epoch config change is recorded and will be enacted on
456		/// the next call to `enact_epoch_change`. The config will be activated one epoch after.
457		/// Multiple calls to this method will replace any existing planned config change that had
458		/// not been enacted yet.
459		#[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		// it might be (and it is in current implementation) that session module is calling
518		// `should_end_session` from it's own `on_initialize` handler, in which case it's
519		// possible that babe's own `on_initialize` has not run yet, so let's ensure that we
520		// have initialized the pallet and updated the current slot.
521		Self::initialize(now);
522		Self::should_epoch_change(now)
523	}
524}
525
526impl<T: Config> Pallet<T> {
527	/// Public function to access epoch_index storage.
528	pub fn epoch_index() -> u64 {
529		EpochIndex::<T>::get()
530	}
531	/// Public function to access authorities storage.
532	pub fn authorities() -> WeakBoundedVec<(AuthorityId, BabeAuthorityWeight), T::MaxAuthorities> {
533		Authorities::<T>::get()
534	}
535	/// Public function to access genesis_slot storage.
536	pub fn genesis_slot() -> Slot {
537		GenesisSlot::<T>::get()
538	}
539	/// Public function to access current_slot storage.
540	pub fn current_slot() -> Slot {
541		CurrentSlot::<T>::get()
542	}
543	/// Public function to access randomness storage.
544	pub fn randomness() -> BabeRandomness {
545		Randomness::<T>::get()
546	}
547	/// Public function to access initialized storage.
548	pub fn initialized() -> Option<Option<PreDigest>> {
549		Initialized::<T>::get()
550	}
551	/// Public function to access author_vrf_randomness storage.
552	pub fn author_vrf_randomness() -> Option<BabeRandomness> {
553		AuthorVrfRandomness::<T>::get()
554	}
555	/// Public function to access lateness storage.
556	pub fn lateness() -> BlockNumberFor<T> {
557		Lateness::<T>::get()
558	}
559	/// Public function to access epoch_config storage.
560	pub fn epoch_config() -> Option<BabeEpochConfiguration> {
561		EpochConfig::<T>::get()
562	}
563	/// Public function to access skipped_epochs storage.
564	pub fn skipped_epochs() -> BoundedVec<(u64, SessionIndex), ConstU32<100>> {
565		SkippedEpochs::<T>::get()
566	}
567
568	/// Determine the BABE slot duration based on the Timestamp module configuration.
569	pub fn slot_duration() -> T::Moment {
570		// we double the minimum block-period so each author can always propose within
571		// the majority of their slot.
572		<T as pallet_timestamp::Config>::MinimumPeriod::get().saturating_mul(2u32.into())
573	}
574
575	/// Determine whether an epoch change should take place at this block.
576	/// Assumes that initialization has already taken place.
577	pub fn should_epoch_change(now: BlockNumberFor<T>) -> bool {
578		// The epoch has technically ended during the passage of time
579		// between this block and the last, but we have to "end" the epoch now,
580		// since there is no earlier possible block we could have done it.
581		//
582		// The exception is for block 1: the genesis has slot 0, so we treat
583		// epoch 0 as having started at the slot of block 1. We want to use
584		// the same randomness and validator set as signalled in the genesis,
585		// so we don't rotate the epoch.
586		now != One::one() && {
587			let diff = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start());
588			*diff >= T::EpochDuration::get()
589		}
590	}
591
592	/// Return the _best guess_ block number, at which the next epoch change is predicted to happen.
593	///
594	/// Returns None if the prediction is in the past; This implies an error internally in the Babe
595	/// and should not happen under normal circumstances.
596	///
597	/// In other word, this is only accurate if no slots are missed. Given missed slots, the slot
598	/// number will grow while the block number will not. Hence, the result can be interpreted as an
599	/// upper bound.
600	// ## IMPORTANT NOTE
601	//
602	// This implementation is linked to how [`should_epoch_change`] is working. This might need to
603	// be updated accordingly, if the underlying mechanics of slot and epochs change.
604	//
605	// WEIGHT NOTE: This function is tied to the weight of `EstimateNextSessionRotation`. If you
606	// update this function, you must also update the corresponding weight.
607	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			// This is a best effort guess. Drifts in the slot/block ratio will cause errors here.
611			let blocks_remaining: BlockNumberFor<T> = slots_remaining.saturated_into();
612			now.saturating_add(blocks_remaining)
613		})
614	}
615
616	/// DANGEROUS: Enact an epoch change. Should be done on every block where `should_epoch_change`
617	/// has returned `true`, and the caller is the only caller of this function.
618	///
619	/// Typically, this is not handled directly by the user, but by higher-level validator-set
620	/// manager logic like `pallet-session`.
621	///
622	/// This doesn't do anything if `authorities` is empty.
623	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		// PRECONDITION: caller has done initialization and is guaranteed
629		// by the session module to be called before this.
630		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		// Update epoch index.
638		//
639		// NOTE: we figure out the epoch index from the slot, which may not
640		// necessarily be contiguous if the chain was offline for more than
641		// `T::EpochDuration` slots. When skipping from epoch N to e.g. N+4, we
642		// will be using the randomness and authorities for that epoch that had
643		// been previously announced for epoch N+1, and the randomness collected
644		// during the current epoch (N) will be used for epoch N+5.
645		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			// we are skipping epochs therefore we need to update the mapping
654			// of epochs to session
655			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						// NOTE: this is O(n) but we currently don't have a bounded `VecDeque`.
670						// this vector is bounded to a small number of elements so performance
671						// shouldn't be an issue.
672						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		// Update epoch randomness.
684		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		// Returns randomness for the current epoch and computes the *next*
689		// epoch randomness.
690		let randomness = Self::randomness_change_epoch(next_epoch_index);
691		Randomness::<T>::put(randomness);
692
693		// Update the next epoch authorities.
694		NextAuthorities::<T>::put(&next_authorities);
695
696		// Update the start blocks of the previous and new current epoch.
697		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		// After we update the current epoch, we signal the *next* epoch change
703		// so that nodes can track changes.
704		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	/// Finds the start slot of the current epoch.
726	///
727	/// Only guaranteed to give correct results after `initialize` of the first
728	/// block in the chain (as its result is based off of `GenesisSlot`).
729	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	/// Produces information about the current epoch.
738	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	/// Produces information about the next epoch (which was already previously
751	/// announced).
752	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			// push onto current segment: not full.
788			UnderConstruction::<T>::insert(&segment_idx, &segment);
789		} else {
790			// move onto the next segment and update the index.
791			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		// deposit a log because this is the first block in epoch #0
818		// we use the same values as genesis because we haven't collected any
819		// randomness yet.
820		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		// since `initialize` can be called twice (e.g. if session module is present)
830		// let's ensure that we only do the initialization once per block
831		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			// the slot number of the current block being initialized
852			let current_slot = pre_digest.slot();
853
854			// on the first non-zero block (i.e. block #1)
855			// this is where the first epoch (epoch #0) actually starts.
856			// we need to adjust internal storage accordingly.
857			if *GenesisSlot::<T>::get() == 0 {
858				Self::initialize_genesis_epoch(current_slot)
859			}
860
861			// how many slots were skipped between current and last block
862			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		// enact epoch change, if necessary.
872		T::EpochChangeTrigger::trigger::<T>(now);
873	}
874
875	/// Call this function exactly once when an epoch changes, to update the
876	/// randomness. Returns the new randomness.
877	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		// overestimate to the segment being full.
882		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	/// Returns the session index that was live when the given epoch happened,
895	/// taking into account any skipped epochs.
896	///
897	/// This function is only well defined for epochs that actually existed,
898	/// e.g. if we skipped from epoch 10 to 20 then a call for epoch 15 (which
899	/// didn't exist) will return an incorrect session index.
900	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			// we have an exact match so we just return the given session index
904			Ok(index) => skipped_epochs[index].1,
905			// we haven't found any skipped epoch before the given epoch,
906			// so the epoch index and session index should match
907			Err(0) => epoch_index.saturated_into::<u32>(),
908			// we have found a skipped epoch before the given epoch
909			Err(index) => {
910				// the element before the given index should give us the skipped epoch
911				// that's closest to the one we're trying to find the session index for
912				let closest_skipped_epoch = skipped_epochs[index - 1];
913
914				// calculate the number of skipped epochs at this point by checking the difference
915				// between the epoch and session indices. epoch index should always be greater or
916				// equal to session index, this is because epochs can be skipped whereas sessions
917				// can't (this is enforced when pushing into `SkippedEpochs`)
918				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	/// Submits an extrinsic to report an equivocation. This method will create
925	/// an unsigned extrinsic with a call to `report_equivocation_unsigned` and
926	/// will push the transaction to the pool. Only useful in an offchain
927	/// context.
928	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	/// Ensure the correctness of the state of this pallet.
939	///
940	/// # Invariants
941	///
942	/// * `Authorities` length must not exceed `MaxAuthorities`.
943	/// * `NextAuthorities` length must not exceed `MaxAuthorities`.
944	/// * `EpochConfig` must always be `Some` (initialized in genesis, never killed).
945	/// * `CurrentSlot` must be greater than or equal to `GenesisSlot` (after genesis
946	///   initialization).
947	/// * `SkippedEpochs` must be sorted by epoch index in ascending order.
948	/// * Each `SkippedEpochs` entry must have `epoch_index >= session_index`.
949	/// * `EpochStart` previous epoch start block must be less than or equal to the current epoch
950	///   start block.
951	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			// Read: Current Slot, Epoch Index, Genesis Slot
1023			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			// Read: Current Slot, Epoch Index, Genesis Slot
1033			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
1094// compute randomness for a new epoch. rho is the concatenation of all
1095// VRF outputs in the prior epoch.
1096//
1097// an optional size hint as to how many VRF outputs there were may be provided.
1098fn 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	/// Something that can return the storage prefix of the `Babe` pallet.
1120	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	/// A storage migration that adds the current epoch configuration for Babe
1136	/// to storage.
1137	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}