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

Commit

Permalink
babe+epoch_time modificaction
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoPal committed Jun 21, 2022
1 parent 0d8a06f commit 982d220
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 78 deletions.
72 changes: 9 additions & 63 deletions node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub type WestendChainSpec = DummyChainSpec;

/// The `ChainSpec` parameterized for the rococo runtime.
#[cfg(feature = "rococo-native")]
pub type RococoChainSpec = service::GenericChainSpec<RococoGenesisExt, Extensions>;
pub type RococoChainSpec = service::GenericChainSpec<rococo::GenesisConfig, Extensions>;

/// The `ChainSpec` parameterized for the `versi` runtime.
///
Expand All @@ -118,30 +118,6 @@ pub type VersiChainSpec = RococoChainSpec;
#[cfg(not(feature = "rococo-native"))]
pub type RococoChainSpec = DummyChainSpec;

/// Extension for the Rococo genesis config to support a custom changes to the genesis state.
#[derive(serde::Serialize, serde::Deserialize)]
#[cfg(feature = "rococo-native")]
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>,
}

#[cfg(feature = "rococo-native")]
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_runtime_constants::time::EpochDurationInBlocks::set(length);
}
});
self.runtime_genesis_config.assimilate_storage(storage)
}
}

pub fn polkadot_config() -> Result<PolkadotChainSpec, String> {
PolkadotChainSpec::from_json_bytes(&include_bytes!("../res/polkadot.json")[..])
}
Expand Down Expand Up @@ -1183,10 +1159,7 @@ pub fn rococo_staging_testnet_config() -> Result<RococoChainSpec, String> {
"Rococo Staging Testnet",
"rococo_staging_testnet",
ChainType::Live,
move || RococoGenesisExt {
runtime_genesis_config: rococo_staging_testnet_config_genesis(wasm_binary),
session_length_in_blocks: None,
},
move || rococo_staging_testnet_config_genesis(wasm_binary),
boot_nodes,
Some(
TelemetryEndpoints::new(vec![(ROCOCO_STAGING_TELEMETRY_URL.to_string(), 0)])
Expand Down Expand Up @@ -1220,10 +1193,7 @@ pub fn versi_staging_testnet_config() -> Result<RococoChainSpec, String> {
"Versi Staging Testnet",
"versi_staging_testnet",
ChainType::Live,
move || RococoGenesisExt {
runtime_genesis_config: rococo_staging_testnet_config_genesis(wasm_binary),
session_length_in_blocks: Some(100),
},
move || rococo_staging_testnet_config_genesis(wasm_binary),
boot_nodes,
Some(
TelemetryEndpoints::new(vec![(VERSI_STAGING_TELEMETRY_URL.to_string(), 0)])
Expand Down Expand Up @@ -1772,11 +1742,7 @@ pub fn rococo_development_config() -> Result<RococoChainSpec, String> {
"Development",
"rococo_dev",
ChainType::Development,
move || RococoGenesisExt {
runtime_genesis_config: rococo_development_config_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || rococo_development_config_genesis(wasm_binary),
vec![],
None,
Some(DEFAULT_PROTOCOL_ID),
Expand All @@ -1795,11 +1761,7 @@ pub fn versi_development_config() -> Result<RococoChainSpec, String> {
"Development",
"versi_dev",
ChainType::Development,
move || RococoGenesisExt {
runtime_genesis_config: rococo_development_config_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || rococo_development_config_genesis(wasm_binary),
vec![],
None,
Some("versi"),
Expand All @@ -1819,11 +1781,7 @@ pub fn wococo_development_config() -> Result<RococoChainSpec, String> {
"Development",
"wococo_dev",
ChainType::Development,
move || RococoGenesisExt {
runtime_genesis_config: rococo_development_config_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || rococo_development_config_genesis(wasm_binary),
vec![],
None,
Some(WOCOCO_DEV_PROTOCOL_ID),
Expand Down Expand Up @@ -1948,11 +1906,7 @@ pub fn rococo_local_testnet_config() -> Result<RococoChainSpec, String> {
"Rococo Local Testnet",
"rococo_local_testnet",
ChainType::Local,
move || RococoGenesisExt {
runtime_genesis_config: rococo_local_testnet_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || rococo_local_testnet_genesis(wasm_binary),
vec![],
None,
Some(DEFAULT_PROTOCOL_ID),
Expand Down Expand Up @@ -1987,11 +1941,7 @@ pub fn wococo_local_testnet_config() -> Result<RococoChainSpec, String> {
"Wococo Local Testnet",
"wococo_local_testnet",
ChainType::Local,
move || RococoGenesisExt {
runtime_genesis_config: wococo_local_testnet_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || wococo_local_testnet_genesis(wasm_binary),
vec![],
None,
Some(DEFAULT_PROTOCOL_ID),
Expand Down Expand Up @@ -2026,11 +1976,7 @@ pub fn versi_local_testnet_config() -> Result<RococoChainSpec, String> {
"Versi Local Testnet",
"versi_local_testnet",
ChainType::Local,
move || RococoGenesisExt {
runtime_genesis_config: versi_local_testnet_genesis(wasm_binary),
// Use 1 minute session length.
session_length_in_blocks: Some(10),
},
move || versi_local_testnet_genesis(wasm_binary),
vec![],
None,
Some("versi"),
Expand Down
5 changes: 1 addition & 4 deletions runtime/rococo/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ pub mod time {
use runtime_common::prod_or_fast;
pub const MILLISECS_PER_BLOCK: Moment = 6000;
pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;
pub const DEFAULT_EPOCH_DURATION: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES);
frame_support::parameter_types! {
pub storage EpochDurationInBlocks: BlockNumber = DEFAULT_EPOCH_DURATION;
}
pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = prod_or_fast!(1 * HOURS, 1 * MINUTES);

// These time units are defined in number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
Expand Down
20 changes: 9 additions & 11 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,18 @@ impl pallet_preimage::Config for Runtime {
}

parameter_types! {
// pub EpochDuration: u64 = prod_or_fast!(
// EPOCH_DURATION_IN_SLOTS as u64,
// 2 * MINUTES as u64,
// "ROC_EPOCH_DURATION"
// );
pub EpochDuration: u64 = prod_or_fast!(
EPOCH_DURATION_IN_SLOTS as u64,
2 * MINUTES as u64,
"ROC_EPOCH_DURATION"
);
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
// TODO: Staking
// pub ReportLongevity: u64 =
// BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get();
pub ReportLongevity: u64 = EpochDurationInBlocks::get() as u64 * 10;
pub ReportLongevity: u64 =
BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get();
}

impl pallet_babe::Config for Runtime {
type EpochDuration = EpochDurationInBlocks; // TODO: Babe -> type EpochDuration = EpochDuration;
type EpochDuration = EpochDuration;
type ExpectedBlockTime = ExpectedBlockTime;

// session module is the trigger
Expand Down Expand Up @@ -2280,7 +2278,7 @@ sp_api::impl_runtime_apis! {
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
babe_primitives::BabeGenesisConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDurationInBlocks::get().into(), // TODO: Babe -> epoch_length: EpochDuration::get().into()
epoch_length: EpochDuration::get().into(),
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities().to_vec(),
randomness: Babe::randomness(),
Expand Down

0 comments on commit 982d220

Please sign in to comment.