Skip to content

Commit

Permalink
Read EpochConfig from on-chain storage. (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neopallium committed Apr 30, 2024
1 parent 8ac0b67 commit a5e4c83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
17 changes: 10 additions & 7 deletions pallets/runtime/common/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ pub const BENCHMARK_MAX_INCREASE: u32 = 0;
#[macro_export]
macro_rules! misc_pallet_impls {
() => {
/// The BABE epoch configuration at genesis.
pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
sp_consensus_babe::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryPlainSlots,
};

/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -717,18 +724,14 @@ macro_rules! runtime_apis {

impl sp_consensus_babe::BabeApi<Block> for Runtime {
fn configuration() -> sp_consensus_babe::BabeConfiguration {
// The choice of `c` parameter (where `1 - c` represents the
// probability of a slot being empty), is done in accordance to the
// slot duration and expected target block time, for safely
// resisting network delays of maximum two seconds.
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
let epoch_config = Babe::epoch_config().unwrap_or(BABE_GENESIS_EPOCH_CONFIG);
sp_consensus_babe::BabeConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: PRIMARY_PROBABILITY,
c: epoch_config.c,
authorities: Babe::authorities().to_vec(),
randomness: Babe::randomness(),
allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryPlainSlots,
allowed_slots: epoch_config.allowed_slots,
}
}

Expand Down
32 changes: 16 additions & 16 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ const DEV_TREASURY: u128 = 50_000_000 * ONE_POLY;

const INITIAL_BOND: u128 = 500 * ONE_POLY;

// 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
sp_consensus_babe::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryPlainSlots,
};
const BABE_GENESIS: pallet_babe::GenesisConfig = pallet_babe::GenesisConfig {
authorities: vec![],
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
};

/// Node `ChainSpec` extensions.
///
/// Additional parameters for some Substrate core modules,
Expand Down Expand Up @@ -633,7 +621,10 @@ pub mod general {
pips: pips!(time::MINUTES, MaybeBlock::None, 25),
im_online: Default::default(),
authority_discovery: Default::default(),
babe: BABE_GENESIS,
babe: pallet_babe::GenesisConfig {
authorities: vec![],
epoch_config: Some(rt::runtime::BABE_GENESIS_EPOCH_CONFIG),
},
grandpa: Default::default(),
/*
pallet_contracts: Some(pallet_contracts::GenesisConfig {
Expand Down Expand Up @@ -787,7 +778,10 @@ pub mod testnet {
pips: pips!(time::DAYS * 30, MaybeBlock::None, 1000),
im_online: Default::default(),
authority_discovery: Default::default(),
babe: BABE_GENESIS,
babe: pallet_babe::GenesisConfig {
authorities: vec![],
epoch_config: Some(rt::runtime::BABE_GENESIS_EPOCH_CONFIG),
},
grandpa: Default::default(),
/*
pallet_contracts: Some(pallet_contracts::GenesisConfig {
Expand Down Expand Up @@ -970,7 +964,10 @@ pub mod mainnet {
pips: pips!(time::DAYS * 30, MaybeBlock::Some(time::DAYS * 90), 1000),
im_online: Default::default(),
authority_discovery: Default::default(),
babe: BABE_GENESIS,
babe: pallet_babe::GenesisConfig {
authorities: vec![],
epoch_config: Some(rt::runtime::BABE_GENESIS_EPOCH_CONFIG),
},
grandpa: Default::default(),
/*
pallet_contracts: Some(pallet_contracts::GenesisConfig {
Expand Down Expand Up @@ -1161,7 +1158,10 @@ pub mod general {
pips: pips!(time::DAYS * 7, MaybeBlock::None, 1000),
im_online: Default::default(),
authority_discovery: Default::default(),
babe: BABE_GENESIS,
babe: pallet_babe::GenesisConfig {
authorities: vec![],
epoch_config: Some(rt::runtime::BABE_GENESIS_EPOCH_CONFIG),
},
grandpa: Default::default(),
/*
pallet_contracts: Some(pallet_contracts::GenesisConfig {
Expand Down

0 comments on commit a5e4c83

Please sign in to comment.