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

Commit

Permalink
Make changes
Browse files Browse the repository at this point in the history
  • Loading branch information
expenses committed Feb 17, 2021
1 parent 743accb commit 120fc30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
32 changes: 25 additions & 7 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ use sp_timestamp::OnTimestampSet;

use sp_consensus_babe::{
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
BabeAuthorityWeight, ConsensusLog, Epoch, EquivocationProof, Slot, BABE_ENGINE_ID,
BabeAuthorityWeight, BabeEpochConfiguration, ConsensusLog, Epoch,
EquivocationProof, Slot, BABE_ENGINE_ID,
};
use sp_consensus_vrf::schnorrkel;

Expand Down Expand Up @@ -187,7 +188,7 @@ decl_storage! {
pub Randomness get(fn randomness): schnorrkel::Randomness;

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

/// Next epoch randomness.
NextRandomness: schnorrkel::Randomness;
Expand Down Expand Up @@ -224,10 +225,16 @@ decl_storage! {
/// on block finalization. Querying this storage entry outside of block
/// execution context should always yield zero.
Lateness get(fn lateness): T::BlockNumber;

EpochConfig: BabeEpochConfiguration;
NextEpochConfig: Option<BabeEpochConfiguration>;
}
add_extra_genesis {
config(authorities): Vec<(AuthorityId, BabeAuthorityWeight)>;
build(|config| Module::<T>::initialize_authorities(&config.authorities))
config(epoch_config): BabeEpochConfiguration;
build(|config| Module::<T>::initialize_authorities_and_epoch_config(
&config.authorities, &config.epoch_config
))
}
}

Expand Down Expand Up @@ -436,7 +443,7 @@ impl<T: Config> Module<T> {
pub fn plan_config_change(
config: NextConfigDescriptor,
) {
NextEpochConfig::put(config);
PendingEpochConfigChange::put(config);
}

/// DANGEROUS: Enact an epoch change. Should be done on every block where `should_epoch_change` has returned `true`,
Expand Down Expand Up @@ -483,8 +490,16 @@ impl<T: Config> Module<T> {
};
Self::deposit_consensus(ConsensusLog::NextEpochData(next_epoch));

if let Some(next_config) = NextEpochConfig::take() {
Self::deposit_consensus(ConsensusLog::NextConfigData(next_config));
if let Some(pending_epoch_config_change) = PendingEpochConfigChange::take() {
if let Some(next_config) = NextEpochConfig::get() {
EpochConfig::put(next_config);
}

let next_epoch_config: BabeEpochConfiguration =
pending_epoch_config_change.clone().into();
NextEpochConfig::put(next_epoch_config);

Self::deposit_consensus(ConsensusLog::NextConfigData(pending_epoch_config_change));
}
}

Expand All @@ -503,6 +518,7 @@ impl<T: Config> Module<T> {
duration: T::EpochDuration::get(),
authorities: Self::authorities(),
randomness: Self::randomness(),
config: EpochConfig::get(),
}
}

Expand All @@ -520,6 +536,7 @@ impl<T: Config> Module<T> {
duration: T::EpochDuration::get(),
authorities: NextAuthorities::get(),
randomness: NextRandomness::get(),
config: NextEpochConfig::get().unwrap_or_else(EpochConfig::get),
}
}

Expand Down Expand Up @@ -668,12 +685,13 @@ impl<T: Config> Module<T> {
this_randomness
}

fn initialize_authorities(authorities: &[(AuthorityId, BabeAuthorityWeight)]) {
fn initialize_authorities_and_epoch_config(authorities: &[(AuthorityId, BabeAuthorityWeight)], epoch_config: &BabeEpochConfiguration) {
if !authorities.is_empty() {
assert!(Authorities::get().is_empty(), "Authorities are already initialized!");
Authorities::put(authorities);
NextAuthorities::put(authorities);
}
EpochConfig::put(epoch_config);
}

fn do_report_equivocation(
Expand Down
10 changes: 9 additions & 1 deletion primitives/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ pub enum AllowedSlots {
PrimaryAndSecondaryVRFSlots,
}

impl Default for AllowedSlots {
fn default() -> Self {
Self::PrimarySlots
}
}

impl AllowedSlots {
/// Whether plain secondary slots are allowed.
pub fn is_secondary_plain_slots_allowed(&self) -> bool {
Expand All @@ -247,7 +253,7 @@ impl sp_consensus::SlotData for BabeGenesisConfiguration {
}

/// Configuration data used by the BABE consensus engine.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, Default)]
pub struct BabeEpochConfiguration {
/// A constant value that is used in the threshold calculation formula.
/// Expressed as a rational where the first member of the tuple is the
Expand Down Expand Up @@ -362,6 +368,8 @@ pub struct Epoch {
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
/// Randomness for this epoch.
pub randomness: [u8; VRF_OUTPUT_LENGTH],
/// Configuration of the epoch.
pub config: BabeEpochConfiguration,
}

sp_api::decl_runtime_apis! {
Expand Down

0 comments on commit 120fc30

Please sign in to comment.