Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

babe: expose next epoch data #7829

Merged
6 commits merged into from Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/node/runtime/src/lib.rs
Expand Up @@ -1160,6 +1160,10 @@ impl_runtime_apis! {
Babe::current_epoch()
}

fn next_epoch() -> sp_consensus_babe::Epoch {
Babe::next_epoch()
}

fn generate_key_ownership_proof(
_slot_number: sp_consensus_babe::SlotNumber,
authority_id: sp_consensus_babe::AuthorityId,
Expand Down
27 changes: 26 additions & 1 deletion frame/babe/src/lib.rs
Expand Up @@ -192,6 +192,9 @@ decl_storage! {
/// Next epoch randomness.
NextRandomness: schnorrkel::Randomness;

/// Next epoch authorities.
NextAuthorities: Vec<(AuthorityId, BabeAuthorityWeight)>;

/// Randomness under construction.
///
/// We make a tradeoff between storage accesses and list length.
Expand Down Expand Up @@ -464,9 +467,13 @@ impl<T: Config> Module<T> {
let randomness = Self::randomness_change_epoch(next_epoch_index);
Randomness::put(randomness);

// Update the next epoch authorities.
NextAuthorities::put(next_authorities);

// After we update the current epoch, we signal the *next* epoch change
// so that nodes can track changes.
let next_randomness = NextRandomness::get();
let next_authorities = NextAuthorities::get();
andresilva marked this conversation as resolved.
Show resolved Hide resolved

let next_epoch = NextEpochDescriptor {
authorities: next_authorities,
Expand All @@ -483,7 +490,7 @@ impl<T: Config> Module<T> {
// give correct results after `do_initialize` of the first block
// in the chain (as its result is based off of `GenesisSlot`).
pub fn current_epoch_start() -> SlotNumber {
(EpochIndex::get() * T::EpochDuration::get()) + GenesisSlot::get()
Self::epoch_start(EpochIndex::get())
}

/// Produces information about the current epoch.
Expand All @@ -497,6 +504,24 @@ impl<T: Config> Module<T> {
}
}

/// Produces information about the next epoch (which was already previously
/// announced).
pub fn next_epoch() -> Epoch {
let next_epoch_index = EpochIndex::get() + 1;
andresilva marked this conversation as resolved.
Show resolved Hide resolved

Epoch {
epoch_index: next_epoch_index,
start_slot: Self::epoch_start(next_epoch_index),
duration: T::EpochDuration::get(),
authorities: NextAuthorities::get(),
randomness: NextRandomness::get(),
}
}

fn epoch_start(epoch_index: u64) -> SlotNumber {
(epoch_index * T::EpochDuration::get()) + GenesisSlot::get()
andresilva marked this conversation as resolved.
Show resolved Hide resolved
}

fn deposit_consensus<U: Encode>(new: U) {
let log: DigestItem<T::Hash> = DigestItem::Consensus(BABE_ENGINE_ID, new.encode());
<frame_system::Module<T>>::deposit_log(log.into())
Expand Down
4 changes: 4 additions & 0 deletions primitives/consensus/babe/src/lib.rs
Expand Up @@ -382,6 +382,10 @@ sp_api::decl_runtime_apis! {
/// Returns information regarding the current epoch.
fn current_epoch() -> Epoch;

/// Returns information regarding the next epoch (which was already
/// previously announced).
fn next_epoch() -> Epoch;

/// Generates a proof of key ownership for the given authority in the
/// current epoch. An example usage of this module is coupled with the
/// session historical module to prove that a given authority key is
Expand Down
8 changes: 8 additions & 0 deletions test-utils/runtime/src/lib.rs
Expand Up @@ -741,6 +741,10 @@ cfg_if! {
<pallet_babe::Module<Runtime>>::current_epoch()
}

fn next_epoch() -> sp_consensus_babe::Epoch {
<pallet_babe::Module<Runtime>>::next_epoch()
}

fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: sp_consensus_babe::EquivocationProof<
<Block as BlockT>::Header,
Expand Down Expand Up @@ -996,6 +1000,10 @@ cfg_if! {
<pallet_babe::Module<Runtime>>::current_epoch()
}

fn next_epoch() -> sp_consensus_babe::Epoch {
<pallet_babe::Module<Runtime>>::next_epoch()
}

fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: sp_consensus_babe::EquivocationProof<
<Block as BlockT>::Header,
Expand Down