@@ -64,6 +64,8 @@ pub use equivocation::{BabeEquivocationOffence, EquivocationHandler, HandleEquiv
6464
6565pub trait Config : pallet_timestamp:: Config {
6666 /// The amount of time, in slots, that each epoch should last.
67+ /// NOTE: Currently it is not possible to change the epoch duration after
68+ /// the chain has started. Attempting to do so will brick block production.
6769 type EpochDuration : Get < SlotNumber > ;
6870
6971 /// The expected average block time at which BABE should be creating
@@ -192,6 +194,9 @@ decl_storage! {
192194 /// Next epoch randomness.
193195 NextRandomness : schnorrkel:: Randomness ;
194196
197+ /// Next epoch authorities.
198+ NextAuthorities : Vec <( AuthorityId , BabeAuthorityWeight ) >;
199+
195200 /// Randomness under construction.
196201 ///
197202 /// We make a tradeoff between storage accesses and list length.
@@ -233,6 +238,9 @@ decl_module! {
233238 pub struct Module <T : Config > for enum Call where origin: T :: Origin {
234239 /// The number of **slots** that an epoch takes. We couple sessions to
235240 /// epochs, i.e. we start a new session once the new epoch begins.
241+ /// NOTE: Currently it is not possible to change the epoch duration
242+ /// after the chain has started. Attempting to do so will brick block
243+ /// production.
236244 const EpochDuration : u64 = T :: EpochDuration :: get( ) ;
237245
238246 /// The expected average block time at which BABE should be creating
@@ -464,6 +472,9 @@ impl<T: Config> Module<T> {
464472 let randomness = Self :: randomness_change_epoch ( next_epoch_index) ;
465473 Randomness :: put ( randomness) ;
466474
475+ // Update the next epoch authorities.
476+ NextAuthorities :: put ( & next_authorities) ;
477+
467478 // After we update the current epoch, we signal the *next* epoch change
468479 // so that nodes can track changes.
469480 let next_randomness = NextRandomness :: get ( ) ;
@@ -483,7 +494,7 @@ impl<T: Config> Module<T> {
483494 // give correct results after `do_initialize` of the first block
484495 // in the chain (as its result is based off of `GenesisSlot`).
485496 pub fn current_epoch_start ( ) -> SlotNumber {
486- ( EpochIndex :: get ( ) * T :: EpochDuration :: get ( ) ) + GenesisSlot :: get ( )
497+ Self :: epoch_start ( EpochIndex :: get ( ) )
487498 }
488499
489500 /// Produces information about the current epoch.
@@ -497,6 +508,36 @@ impl<T: Config> Module<T> {
497508 }
498509 }
499510
511+ /// Produces information about the next epoch (which was already previously
512+ /// announced).
513+ pub fn next_epoch ( ) -> Epoch {
514+ let next_epoch_index = EpochIndex :: get ( ) . checked_add ( 1 ) . expect (
515+ "epoch index is u64; it is always only incremented by one; \
516+ if u64 is not enough we should crash for safety; qed.",
517+ ) ;
518+
519+ Epoch {
520+ epoch_index : next_epoch_index,
521+ start_slot : Self :: epoch_start ( next_epoch_index) ,
522+ duration : T :: EpochDuration :: get ( ) ,
523+ authorities : NextAuthorities :: get ( ) ,
524+ randomness : NextRandomness :: get ( ) ,
525+ }
526+ }
527+
528+ fn epoch_start ( epoch_index : u64 ) -> SlotNumber {
529+ // (epoch_index * epoch_duration) + genesis_slot
530+
531+ const PROOF : & str = "slot number is u64; it should relate in some way to wall clock time; \
532+ if u64 is not enough we should crash for safety; qed.";
533+
534+ let epoch_start = epoch_index
535+ . checked_mul ( T :: EpochDuration :: get ( ) )
536+ . expect ( PROOF ) ;
537+
538+ epoch_start. checked_add ( GenesisSlot :: get ( ) ) . expect ( PROOF )
539+ }
540+
500541 fn deposit_consensus < U : Encode > ( new : U ) {
501542 let log: DigestItem < T :: Hash > = DigestItem :: Consensus ( BABE_ENGINE_ID , new. encode ( ) ) ;
502543 <frame_system:: Module < T > >:: deposit_log ( log. into ( ) )
0 commit comments