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

Commit

Permalink
BabeApi functions return a StandaloneEpoch
Browse files Browse the repository at this point in the history
  • Loading branch information
expenses committed Feb 8, 2021
1 parent bbc7b0e commit b2f4332
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,11 +1205,11 @@ impl_runtime_apis! {
Babe::current_epoch_start()
}

fn current_epoch() -> sp_consensus_babe::Epoch {
fn current_epoch() -> sp_consensus_babe::StandaloneEpoch {
Babe::current_epoch()
}

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

Expand Down
24 changes: 19 additions & 5 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use sp_timestamp::OnTimestampSet;
use sp_consensus_babe::{
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
inherents::{BabeInherentData, INHERENT_IDENTIFIER},
BabeAuthorityWeight, ConsensusLog, Epoch, EquivocationProof, Slot, BABE_ENGINE_ID,
BabeAuthorityWeight, ConsensusLog, EquivocationProof, Slot, BABE_ENGINE_ID, StandaloneEpoch,
BabeEpochConfiguration,
};
use sp_consensus_vrf::schnorrkel;
use sp_inherents::{InherentData, InherentIdentifier, MakeFatalError, ProvideInherent};
Expand Down Expand Up @@ -188,6 +189,9 @@ decl_storage! {
// variable to its underlying value.
pub Randomness get(fn randomness): schnorrkel::Randomness;

/// The Current epoch configuration.
EpochConfig: Option<BabeEpochConfiguration>;

/// Next epoch configuration, if changed.
NextEpochConfig: Option<NextConfigDescriptor>;

Expand Down Expand Up @@ -486,6 +490,8 @@ impl<T: Config> Module<T> {
Self::deposit_consensus(ConsensusLog::NextEpochData(next_epoch));

if let Some(next_config) = NextEpochConfig::take() {
let config: BabeEpochConfiguration = next_config.clone().into();
EpochConfig::put(config);
Self::deposit_consensus(ConsensusLog::NextConfigData(next_config));
}
}
Expand All @@ -498,30 +504,38 @@ impl<T: Config> Module<T> {
}

/// Produces information about the current epoch.
pub fn current_epoch() -> Epoch {
Epoch {
pub fn current_epoch() -> StandaloneEpoch {
let epoch_config = EpochConfig::get().unwrap();

StandaloneEpoch {
epoch_index: EpochIndex::get(),
start_slot: Self::current_epoch_start(),
duration: T::EpochDuration::get(),
authorities: Self::authorities(),
randomness: Self::randomness(),
c: epoch_config.c,
allowed_slots: epoch_config.allowed_slots,
}
}

/// Produces information about the next epoch (which was already previously
/// announced).
pub fn next_epoch() -> Epoch {
pub fn next_epoch() -> StandaloneEpoch {
let next_epoch_index = EpochIndex::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.",
);

Epoch {
let epoch_config = EpochConfig::get().unwrap();

StandaloneEpoch {
epoch_index: next_epoch_index,
start_slot: Self::epoch_start(next_epoch_index),
duration: T::EpochDuration::get(),
authorities: NextAuthorities::get(),
randomness: NextRandomness::get(),
c: epoch_config.c,
allowed_slots: epoch_config.allowed_slots,
}
}

Expand Down
29 changes: 27 additions & 2 deletions primitives/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ pub struct Epoch {
pub randomness: [u8; VRF_OUTPUT_LENGTH],
}

/// BABE epoch information with `c` and `allowed_slots`
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct StandaloneEpoch {
/// The epoch index.
pub epoch_index: u64,
/// The starting slot of the epoch.
pub start_slot: Slot,
/// The duration of this epoch.
pub duration: u64,
/// The authorities and their weights.
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
/// Randomness for this epoch.
pub randomness: [u8; VRF_OUTPUT_LENGTH],
/// A constant value that is used in the threshold calculation formula.
/// Expressed as a rational where the first member of the tuple is the
/// numerator and the second is the denominator. The rational should
/// represent a value between 0 and 1.
/// In the threshold formula calculation, `1 - c` represents the probability
/// of a slot being empty.
pub c: (u64, u64),
/// Whether this chain should run with secondary slots, which are assigned
/// in round-robin manner.
pub allowed_slots: AllowedSlots,
}

sp_api::decl_runtime_apis! {
/// API necessary for block authorship with BABE.
#[api_version(2)]
Expand All @@ -379,11 +404,11 @@ sp_api::decl_runtime_apis! {
fn current_epoch_start() -> Slot;

/// Returns information regarding the current epoch.
fn current_epoch() -> Epoch;
fn current_epoch() -> StandaloneEpoch;

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

/// Generates a proof of key ownership for the given authority in the
/// current epoch. An example usage of this module is coupled with the
Expand Down
8 changes: 4 additions & 4 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@ cfg_if! {
<pallet_babe::Module<Runtime>>::current_epoch_start()
}

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

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

Expand Down Expand Up @@ -1002,11 +1002,11 @@ cfg_if! {
<pallet_babe::Module<Runtime>>::current_epoch_start()
}

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

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

Expand Down

0 comments on commit b2f4332

Please sign in to comment.