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

Return babe configuration information in the babe api epoch functions #8072

Merged
26 commits merged into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
120fc30
Make changes
expenses Feb 17, 2021
4cbac3e
Add serialize/deserialize, copy babe epoch config defaults from node …
expenses Feb 17, 2021
cb22034
Fix line widths and turn default features off for serde
expenses Feb 17, 2021
a85fdcd
Remove ser/deser from Epoch, fix node-cli
expenses Feb 17, 2021
2617aaf
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Feb 18, 2021
b1b589f
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Feb 22, 2021
11826f0
Apply suggestions
expenses Feb 22, 2021
f16980a
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Feb 22, 2021
6c920d6
Add comment to BABE_GENESIS_EPOCH_CONFIG in bin
expenses Feb 23, 2021
099673a
Apply suggestions
expenses Feb 24, 2021
f5d3b18
Add a sketchy migration function
expenses Feb 24, 2021
44ad4ff
Add a migration test
expenses Feb 24, 2021
8d9cecb
Check for PendingEpochConfigChange as well
expenses Feb 24, 2021
a339dfe
Make epoch_config in node-cli
expenses Feb 25, 2021
fed698d
Move updating EpochConfig out of the if
expenses Feb 25, 2021
e1e3e16
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Feb 25, 2021
5834911
Fix executor tests
expenses Feb 25, 2021
a06fb96
Calculate weight for add_epoch_configurations
expenses Mar 1, 2021
7fd659f
Fix babe test
expenses Mar 1, 2021
4bd651f
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Mar 1, 2021
bdfe039
Apply suggestions from code review
expenses Mar 3, 2021
74fbbb4
Add more asserts to tests, remove unused changes to primitives/slots
expenses Mar 3, 2021
8b9a2af
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Mar 3, 2021
a49a87f
Allow setting the migration pallet prefix
expenses Mar 9, 2021
cec8f99
Merge remote-tracking branch 'origin/master' into ashley-standalone-e…
expenses Mar 9, 2021
35ff1cf
Rename to BabePalletPrefix
expenses Mar 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
expenses marked this conversation as resolved.
Show resolved Hide resolved
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;
expenses marked this conversation as resolved.
Show resolved Hide resolved
expenses marked this conversation as resolved.
Show resolved Hide resolved
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
expenses marked this conversation as resolved.
Show resolved Hide resolved
expenses marked this conversation as resolved.
Show resolved Hide resolved
))
}
}

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);
}
expenses marked this conversation as resolved.
Show resolved Hide resolved

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