Unverified Commit 14da83d5 authored by Bastian Köcher's avatar Bastian Köcher Committed by GitHub
Browse files

Support variable session length for Rococo chains at genesis (#2167)

This pr adds support to change the session length of a Rococo chain at
genesis. This is rather useful because Rococo has a session length of
1 hour, while on rococo-local you will now get 1 minute. This improves
the dev experience, because a parachain is only going live at the
start of a new session.
parent 23f6d849
Pipeline #118059 passed with stages
in 17 minutes and 24 seconds
...@@ -5672,6 +5672,7 @@ dependencies = [ ...@@ -5672,6 +5672,7 @@ dependencies = [
"sp-offchain", "sp-offchain",
"sp-runtime", "sp-runtime",
"sp-session", "sp-session",
"sp-state-machine",
"sp-storage", "sp-storage",
"sp-transaction-pool", "sp-transaction-pool",
"sp-trie", "sp-trie",
......
...@@ -39,6 +39,7 @@ sp-session = { git = "https://github.com/paritytech/substrate", branch = "master ...@@ -39,6 +39,7 @@ sp-session = { git = "https://github.com/paritytech/substrate", branch = "master
sp-storage = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-storage = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
# Substrate Pallets # Substrate Pallets
pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" } pallet-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
......
...@@ -66,7 +66,32 @@ pub type KusamaChainSpec = service::GenericChainSpec<kusama::GenesisConfig, Exte ...@@ -66,7 +66,32 @@ pub type KusamaChainSpec = service::GenericChainSpec<kusama::GenesisConfig, Exte
pub type WestendChainSpec = service::GenericChainSpec<westend::GenesisConfig, Extensions>; pub type WestendChainSpec = service::GenericChainSpec<westend::GenesisConfig, Extensions>;
/// The `ChainSpec` parametrized for the rococo runtime. /// The `ChainSpec` parametrized for the rococo runtime.
pub type RococoChainSpec = service::GenericChainSpec<rococo::GenesisConfig, Extensions>; pub type RococoChainSpec = service::GenericChainSpec<RococoGenesisExt, Extensions>;
/// Extension for the Rococo genesis config to support a custom changes to the genesis state.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct RococoGenesisExt {
/// The runtime genesis config.
runtime_genesis_config: rococo::GenesisConfig,
/// The session length in blocks.
///
/// If `None` is supplied, the default value is used.
session_length_in_blocks: Option<u32>,
}
impl sp_runtime::BuildStorage for RococoGenesisExt {
fn assimilate_storage(
&self,
storage: &mut sp_core::storage::Storage,
) -> Result<(), String> {
sp_state_machine::BasicExternalities::execute_with_storage(storage, || {
if let Some(length) = self.session_length_in_blocks.as_ref() {
rococo::constants::time::EpochDurationInBlocks::set(length);
}
});
self.runtime_genesis_config.assimilate_storage(storage)
}
}
pub fn polkadot_config() -> Result<PolkadotChainSpec, String> { pub fn polkadot_config() -> Result<PolkadotChainSpec, String> {
PolkadotChainSpec::from_json_bytes(&include_bytes!("../res/polkadot.json")[..]) PolkadotChainSpec::from_json_bytes(&include_bytes!("../res/polkadot.json")[..])
...@@ -925,7 +950,10 @@ pub fn rococo_staging_testnet_config() -> Result<RococoChainSpec, String> { ...@@ -925,7 +950,10 @@ pub fn rococo_staging_testnet_config() -> Result<RococoChainSpec, String> {
"Rococo Staging Testnet", "Rococo Staging Testnet",
"rococo_staging_testnet", "rococo_staging_testnet",
ChainType::Live, ChainType::Live,
move || rococo_staging_testnet_config_genesis(wasm_binary), move || RococoGenesisExt {
runtime_genesis_config: rococo_staging_testnet_config_genesis(wasm_binary),
session_length_in_blocks: None,
},
boot_nodes, boot_nodes,
Some( Some(
TelemetryEndpoints::new(vec![(ROCOCO_STAGING_TELEMETRY_URL.to_string(), 0)]) TelemetryEndpoints::new(vec![(ROCOCO_STAGING_TELEMETRY_URL.to_string(), 0)])
...@@ -1542,7 +1570,11 @@ pub fn rococo_local_testnet_config() -> Result<RococoChainSpec, String> { ...@@ -1542,7 +1570,11 @@ pub fn rococo_local_testnet_config() -> Result<RococoChainSpec, String> {
"Rococo Local Testnet", "Rococo Local Testnet",
"rococo_local_testnet", "rococo_local_testnet",
ChainType::Local, ChainType::Local,
move || rococo_local_testnet_genesis(wasm_binary), move || RococoGenesisExt {
runtime_genesis_config: rococo_local_testnet_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
vec![], vec![],
None, None,
Some(DEFAULT_PROTOCOL_ID), Some(DEFAULT_PROTOCOL_ID),
......
...@@ -33,7 +33,9 @@ pub mod time { ...@@ -33,7 +33,9 @@ pub mod time {
use primitives::v0::{Moment, BlockNumber}; use primitives::v0::{Moment, BlockNumber};
pub const MILLISECS_PER_BLOCK: Moment = 6000; pub const MILLISECS_PER_BLOCK: Moment = 6000;
pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 1 * HOURS; frame_support::parameter_types! {
pub storage EpochDurationInBlocks: BlockNumber = 1 * HOURS;
}
// These time units are defined in number of blocks. // These time units are defined in number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
......
...@@ -328,13 +328,13 @@ parameter_types! { ...@@ -328,13 +328,13 @@ parameter_types! {
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const MaxNominatorRewardedPerValidator: u32 = 64; pub const MaxNominatorRewardedPerValidator: u32 = 64;
// quarter of the last session will be for election. // quarter of the last session will be for election.
pub const ElectionLookahead: BlockNumber = EPOCH_DURATION_IN_BLOCKS / 4; pub ElectionLookahead: BlockNumber = EpochDurationInBlocks::get() / 4;
pub const MaxIterations: u32 = 10; pub const MaxIterations: u32 = 10;
pub MinSolutionScoreBump: Perbill = Perbill::from_rational_approximation(5u32, 10_000); pub MinSolutionScoreBump: Perbill = Perbill::from_rational_approximation(5u32, 10_000);
} }
parameter_types! { parameter_types! {
pub const SessionDuration: BlockNumber = EPOCH_DURATION_IN_BLOCKS as _; pub SessionDuration: BlockNumber = EpochDurationInBlocks::get() as _;
} }
parameter_types! { parameter_types! {
...@@ -457,12 +457,11 @@ impl pallet_session::Config for Runtime { ...@@ -457,12 +457,11 @@ impl pallet_session::Config for Runtime {
} }
parameter_types! { parameter_types! {
pub const EpochDuration: u64 = EPOCH_DURATION_IN_BLOCKS as u64;
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK; pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
} }
impl pallet_babe::Config for Runtime { impl pallet_babe::Config for Runtime {
type EpochDuration = EpochDuration; type EpochDuration = EpochDurationInBlocks;
type ExpectedBlockTime = ExpectedBlockTime; type ExpectedBlockTime = ExpectedBlockTime;
// session module is the trigger // session module is the trigger
...@@ -798,7 +797,7 @@ sp_api::impl_runtime_apis! { ...@@ -798,7 +797,7 @@ sp_api::impl_runtime_apis! {
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results> // <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
babe_primitives::BabeGenesisConfiguration { babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(), slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(), epoch_length: EpochDurationInBlocks::get().into(),
c: PRIMARY_PROBABILITY, c: PRIMARY_PROBABILITY,
genesis_authorities: Babe::authorities(), genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(), randomness: Babe::randomness(),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment