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

Commit

Permalink
Companion PR for substrate PR 8072 - Add a config field to babe epochs (
Browse files Browse the repository at this point in the history
#2467)

* Add a config field to babe epochs

* Fix test

* Add BABE_GENESIS_EPOCH_CONFIG consts

* Use PrimaryAndSecondaryVRFSlots and remove newlines

* Make epoch_configs Some

* Fix tests

* Fix test service tests

* Add a BabeEpochConfigMigrations OnRuntimeUpgrade

* Apply suggestions

* Use PrimaryAndSecondaryPlainSlots in kusama

* Remove migration from test runtime and rococo

* Add HasPalletPrefix

* Rename to BabePalletPrefix and change BabeApi -> Babe

* "Update Substrate"

* Update substrate

* Resolve parantheses errors

Co-authored-by: parity-processbot <>
  • Loading branch information
expenses committed Mar 10, 2021
1 parent 6b72d03 commit e6b065f
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 183 deletions.
329 changes: 170 additions & 159 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ mod tests {
use polkadot_node_subsystem::messages::AllMessages;
use sp_core::testing::TaskExecutor;
use sp_runtime::{Digest, DigestItem};
use sp_consensus_babe::Epoch as BabeEpoch;
use sp_consensus_babe::{
Epoch as BabeEpoch, BabeEpochConfiguration, AllowedSlots,
};
use sp_consensus_babe::digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest};
use sp_keyring::sr25519::Keyring as Sr25519Keyring;
use assert_matches::assert_matches;
Expand Down Expand Up @@ -1358,6 +1360,10 @@ mod tests {
duration: 200,
authorities: vec![(Sr25519Keyring::Alice.public().into(), 1)],
randomness: [0u8; 32],
config: BabeEpochConfiguration {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
},
}));
}
);
Expand Down Expand Up @@ -1463,6 +1469,10 @@ mod tests {
duration: 200,
authorities: vec![(Sr25519Keyring::Alice.public().into(), 1)],
randomness: [0u8; 32],
config: BabeEpochConfiguration {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
},
}));
}
);
Expand Down Expand Up @@ -1714,6 +1724,10 @@ mod tests {
duration: 200,
authorities: vec![(Sr25519Keyring::Alice.public().into(), 1)],
randomness: [0u8; 32],
config: BabeEpochConfiguration {
c: (1, 4),
allowed_slots: AllowedSlots::PrimarySlots,
},
}));
}
);
Expand Down
8 changes: 7 additions & 1 deletion node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ mod tests {
use sp_core::testing::TaskExecutor;
use std::{collections::{HashMap, BTreeMap}, sync::{Arc, Mutex}};
use futures::channel::oneshot;
use polkadot_node_primitives::BabeEpoch;
use polkadot_node_primitives::{
BabeEpoch, BabeEpochConfiguration, BabeAllowedSlots,
};

#[derive(Default, Clone)]
struct MockRuntimeApi {
Expand Down Expand Up @@ -1158,6 +1160,10 @@ mod tests {
duration: 10,
authorities: Vec::new(),
randomness: [1u8; 32],
config: BabeEpochConfiguration {
c: (1, 4),
allowed_slots: BabeAllowedSlots::PrimarySlots,
},
};
runtime_api.babe_epoch = Some(epoch.clone());
let runtime_api = Arc::new(runtime_api);
Expand Down
4 changes: 3 additions & 1 deletion node/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ use polkadot_primitives::v1::{
use std::pin::Pin;

pub use sp_core::traits::SpawnNamed;
pub use sp_consensus_babe::Epoch as BabeEpoch;
pub use sp_consensus_babe::{
Epoch as BabeEpoch, BabeEpochConfiguration, AllowedSlots as BabeAllowedSlots,
};

pub mod approval;

Expand Down
40 changes: 32 additions & 8 deletions node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ fn polkadot_staging_testnet_config_genesis(wasm_binary: &[u8]) -> polkadot::Gene
phantom: Default::default(),
},
pallet_membership_Instance1: Default::default(),
pallet_babe: Default::default(),
pallet_babe: polkadot::BabeConfig {
authorities: Default::default(),
epoch_config: Some(polkadot::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: polkadot::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -448,7 +451,10 @@ fn westend_staging_testnet_config_genesis(wasm_binary: &[u8]) -> westend::Genesi
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
},
pallet_babe: Default::default(),
pallet_babe: westend::BabeConfig {
authorities: Default::default(),
epoch_config: Some(westend::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: westend::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -644,7 +650,10 @@ fn kusama_staging_testnet_config_genesis(wasm_binary: &[u8]) -> kusama::GenesisC
phantom: Default::default(),
},
pallet_membership_Instance1: Default::default(),
pallet_babe: Default::default(),
pallet_babe: kusama::BabeConfig {
authorities: Default::default(),
epoch_config: Some(kusama::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: kusama::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -850,7 +859,10 @@ fn rococo_staging_testnet_config_genesis(wasm_binary: &[u8]) -> rococo_runtime::
),
)).collect::<Vec<_>>(),
},
pallet_babe: Default::default(),
pallet_babe: rococo_runtime::BabeConfig {
authorities: Default::default(),
epoch_config: Some(rococo_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: rococo_runtime::AuthorityDiscoveryConfig {
Expand Down Expand Up @@ -1130,7 +1142,10 @@ pub fn polkadot_testnet_genesis(
phantom: Default::default(),
},
pallet_membership_Instance1: Default::default(),
pallet_babe: Default::default(),
pallet_babe: polkadot::BabeConfig {
authorities: Default::default(),
epoch_config: Some(polkadot::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: polkadot::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -1225,7 +1240,10 @@ pub fn kusama_testnet_genesis(
phantom: Default::default(),
},
pallet_membership_Instance1: Default::default(),
pallet_babe: Default::default(),
pallet_babe: kusama::BabeConfig {
authorities: Default::default(),
epoch_config: Some(kusama::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: kusama::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -1309,7 +1327,10 @@ pub fn westend_testnet_genesis(
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
},
pallet_babe: Default::default(),
pallet_babe: westend::BabeConfig {
authorities: Default::default(),
epoch_config: Some(westend::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: westend::AuthorityDiscoveryConfig { keys: vec![] },
Expand Down Expand Up @@ -1363,7 +1384,10 @@ pub fn rococo_testnet_genesis(
),
)).collect::<Vec<_>>(),
},
pallet_babe: Default::default(),
pallet_babe: rococo_runtime::BabeConfig {
authorities: Default::default(),
epoch_config: Some(rococo_runtime::BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_im_online: Default::default(),
pallet_authority_discovery: rococo_runtime::AuthorityDiscoveryConfig {
Expand Down
7 changes: 5 additions & 2 deletions node/test/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use grandpa::AuthorityId as GrandpaId;
use pallet_staking::Forcing;
use polkadot_primitives::v1::{ValidatorId, AccountId, AssignmentId};
use polkadot_service::chain_spec::{get_account_id_from_seed, get_from_seed, Extensions};
use polkadot_test_runtime::constants::currency::DOTS;
use polkadot_test_runtime::{constants::currency::DOTS, BABE_GENESIS_EPOCH_CONFIG};
use sc_chain_spec::{ChainSpec, ChainType};
use sp_core::sr25519;
use sp_runtime::Perbill;
Expand Down Expand Up @@ -162,7 +162,10 @@ fn polkadot_testnet_genesis(
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
},
pallet_babe: Default::default(),
pallet_babe: runtime::BabeConfig {
authorities: vec![],
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
},
pallet_grandpa: Default::default(),
pallet_authority_discovery: runtime::AuthorityDiscoveryConfig { keys: vec![] },
claims: runtime::ClaimsConfig {
Expand Down
30 changes: 28 additions & 2 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 4,
};

/// The BABE epoch configuration at genesis.
pub const BABE_GENESIS_EPOCH_CONFIG: babe_primitives::BabeEpochConfiguration =
babe_primitives::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots
};

/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -1036,6 +1043,24 @@ construct_runtime! {
}
}

impl pallet_babe::migrations::BabePalletPrefix for Runtime {
fn pallet_prefix() -> &'static str {
"Babe"
}
}

pub struct BabeEpochConfigMigrations;
impl frame_support::traits::OnRuntimeUpgrade for BabeEpochConfigMigrations {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
pallet_babe::migrations::add_epoch_configuration::<Runtime>(
babe_primitives::BabeEpochConfiguration {
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryPlainSlots,
..BABE_GENESIS_EPOCH_CONFIG
}
)
}
}

/// The address format for describing accounts.
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
/// Block header type as expected by this runtime.
Expand Down Expand Up @@ -1067,6 +1092,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllModules,
BabeEpochConfigMigrations,
>;
/// The payload being signed in the transactions.
pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
Expand Down Expand Up @@ -1236,10 +1262,10 @@ sp_api::impl_runtime_apis! {
babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: PRIMARY_PROBABILITY,
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots,
allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
}
}

Expand Down
28 changes: 25 additions & 3 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 6,
};

/// The BABE epoch configuration at genesis.
pub const BABE_GENESIS_EPOCH_CONFIG: babe_primitives::BabeEpochConfiguration =
babe_primitives::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots
};

/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -1031,6 +1038,21 @@ construct_runtime! {
}
}

impl pallet_babe::migrations::BabePalletPrefix for Runtime {
fn pallet_prefix() -> &'static str {
"Babe"
}
}

pub struct BabeEpochConfigMigrations;
impl frame_support::traits::OnRuntimeUpgrade for BabeEpochConfigMigrations {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
pallet_babe::migrations::add_epoch_configuration::<Runtime>(
BABE_GENESIS_EPOCH_CONFIG,
)
}
}

/// The address format for describing accounts.
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
/// Block header type as expected by this runtime.
Expand Down Expand Up @@ -1063,7 +1085,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllModules,
()
BabeEpochConfigMigrations,
>;
/// The payload being signed in transactions.
pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
Expand Down Expand Up @@ -1232,10 +1254,10 @@ sp_api::impl_runtime_apis! {
babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: PRIMARY_PROBABILITY,
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots,
allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
}
}

Expand Down
11 changes: 9 additions & 2 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 0,
};

/// The BABE epoch configuration at genesis.
pub const BABE_GENESIS_EPOCH_CONFIG: babe_primitives::BabeEpochConfiguration =
babe_primitives::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots
};

/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -805,10 +812,10 @@ sp_api::impl_runtime_apis! {
babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDurationInBlocks::get().into(),
c: PRIMARY_PROBABILITY,
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots,
allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
}
}

Expand Down
11 changes: 9 additions & 2 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 1,
};

/// The BABE epoch configuration at genesis.
pub const BABE_GENESIS_EPOCH_CONFIG: babe_primitives::BabeEpochConfiguration =
babe_primitives::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots
};

/// Native version.
#[cfg(any(feature = "std", test))]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -737,10 +744,10 @@ sp_api::impl_runtime_apis! {
babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: PRIMARY_PROBABILITY,
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
allowed_slots: babe_primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots,
allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
}
}

Expand Down
Loading

0 comments on commit e6b065f

Please sign in to comment.