From 97f9d820b89207bd1f011fabd5b15a74213aafdb Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Fri, 12 Jun 2020 16:40:07 +0200 Subject: [PATCH 1/4] initial collection of fns --- runtime/parachains/src/initializer.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/runtime/parachains/src/initializer.rs b/runtime/parachains/src/initializer.rs index 2b86d19654cd..84d02f95ad83 100644 --- a/runtime/parachains/src/initializer.rs +++ b/runtime/parachains/src/initializer.rs @@ -152,6 +152,33 @@ impl session::OneSessionHandler for Module { where I: Iterator { + assert!(HasInitialized::get().is_none()); + + // @todo how to obtain parachain? + let (para_id, genesis_args) = Paras::config().unwrap().first().unwrap(); + + Paras::schedule_para_initialize(para_id, genesis_args); + + + let random_seed = { + let mut buf = [0u8; 32]; + let random_hash = T::Randomness::random(&b"paras"[..]); + let len = sp_std::cmp::min(32, random_hash.as_ref().len()); + buf[..len].copy_from_slice(&random_hash.as_ref()[..len]); + buf + }; + + + let notification = SessionChangeNotification { + validators: validators.map(|(_account, validator)| validator).collect(), + queued: validators.map(|(_account, validator)| validator).collect(), + prev_config: HostConfiguration::default(), // @todo sane? + new_config: >::config(), // @todo + random_seed, + }; + + paras::Module::::initializer_on_new_session(¬ification); + scheduler::Module::::initializer_on_new_session(¬ification); } fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I) From 1f73cc0e881addc49773bf8c502d5bb5bcf88d3f Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Tue, 16 Jun 2020 18:51:03 +0200 Subject: [PATCH 2/4] first steps --- runtime/parachains/src/initializer.rs | 72 ++++++++++++--------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/runtime/parachains/src/initializer.rs b/runtime/parachains/src/initializer.rs index 84d02f95ad83..ad57cc7c8bee 100644 --- a/runtime/parachains/src/initializer.rs +++ b/runtime/parachains/src/initializer.rs @@ -19,15 +19,14 @@ //! //! This module can throw fatal errors if session-change notifications are received after initialization. -use sp_std::prelude::*; -use frame_support::weights::Weight; -use primitives::{ - parachain::{ValidatorId}, +use crate::{ + configuration::{self, HostConfiguration}, + paras, scheduler, }; -use frame_support::{ - decl_storage, decl_module, decl_error, traits::Randomness, -}; -use crate::{configuration::{self, HostConfiguration}, paras, scheduler}; +use frame_support::weights::Weight; +use frame_support::{decl_error, decl_module, decl_storage, traits::Randomness}; +use primitives::parachain::ValidatorId; +use sp_std::prelude::*; /// Information about a session change that has just occurred. #[derive(Default, Clone)] @@ -105,7 +104,8 @@ impl Module { /// /// Panics if the modules have already been initialized. fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, queued: I) - where I: Iterator + where + I: Iterator, { assert!(HasInitialized::get().is_none()); @@ -148,46 +148,38 @@ impl sp_runtime::BoundToRuntimeAppPublic for Module { impl session::OneSessionHandler for Module { type Key = ValidatorId; - fn on_genesis_session<'a, I: 'a>(_validators: I) - where I: Iterator + fn on_genesis_session<'a, I: 'a>(validators: I) + where + I: Iterator, { + let config = >::config(); + + for id in >::parachains() { + let origin: ::Origin = system::RawOrigin::Root.into(); + let _ = >::set_validation_upgrade_frequency(origin.clone(), config.validation_upgrade_frequency); + let _ = >::set_validation_upgrade_delay(origin.clone(), config.validation_upgrade_delay); + let _ = >::set_acceptance_period(origin.clone(), config.acceptance_period); + let _ = >::set_max_code_size(origin.clone(), config.max_code_size); + let _ = >::set_max_head_data_size(origin.clone(), config.max_head_data_size); + let _ = >::set_parathread_cores(origin.clone(), config.parathread_cores); + let _ = >::set_parathread_retries(origin.clone(), config.parathread_retries); + let _ = >::set_parachain_rotation_frequency(origin.clone(), config.parachain_rotation_frequency); + let _ = >::set_chain_availability_period(origin.clone(), config.chain_availability_period); + let _ = >::set_thread_availability_period(origin.clone(), config.thread_availability_period); + let _ = >::set_scheduling_lookahead(origin.clone(), config.scheduling_lookahead); + } - assert!(HasInitialized::get().is_none()); - - // @todo how to obtain parachain? - let (para_id, genesis_args) = Paras::config().unwrap().first().unwrap(); - - Paras::schedule_para_initialize(para_id, genesis_args); - - - let random_seed = { - let mut buf = [0u8; 32]; - let random_hash = T::Randomness::random(&b"paras"[..]); - let len = sp_std::cmp::min(32, random_hash.as_ref().len()); - buf[..len].copy_from_slice(&random_hash.as_ref()[..len]); - buf - }; - - - let notification = SessionChangeNotification { - validators: validators.map(|(_account, validator)| validator).collect(), - queued: validators.map(|(_account, validator)| validator).collect(), - prev_config: HostConfiguration::default(), // @todo sane? - new_config: >::config(), // @todo - random_seed, - }; - - paras::Module::::initializer_on_new_session(¬ification); - scheduler::Module::::initializer_on_new_session(¬ification); + // parathreads } fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I) - where I: Iterator + where + I: Iterator, { >::on_new_session(changed, validators, queued); } - fn on_disabled(_i: usize) { } + fn on_disabled(_i: usize) {} } #[cfg(test)] From d7c4a01dd62f8aae7e7ea4c9afc6a4c70a4fcb6d Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 17 Jun 2020 17:16:05 +0200 Subject: [PATCH 3/4] doc/genesis: add docs on why things are needed or not --- runtime/parachains/src/initializer.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/runtime/parachains/src/initializer.rs b/runtime/parachains/src/initializer.rs index ad57cc7c8bee..871090668958 100644 --- a/runtime/parachains/src/initializer.rs +++ b/runtime/parachains/src/initializer.rs @@ -152,10 +152,13 @@ impl session::OneSessionHandler for Module { where I: Iterator, { - let config = >::config(); + // host config as provided by the client using `fn build` based on the `GenesisConfig` + let config: HostConfiguration<_> = >::config(); + // set all the values for id in >::parachains() { let origin: ::Origin = system::RawOrigin::Root.into(); + // @todo at this point, the dispatch should never fail let _ = >::set_validation_upgrade_frequency(origin.clone(), config.validation_upgrade_frequency); let _ = >::set_validation_upgrade_delay(origin.clone(), config.validation_upgrade_delay); let _ = >::set_acceptance_period(origin.clone(), config.acceptance_period); @@ -168,8 +171,13 @@ impl session::OneSessionHandler for Module { let _ = >::set_thread_availability_period(origin.clone(), config.thread_availability_period); let _ = >::set_scheduling_lookahead(origin.clone(), config.scheduling_lookahead); } + // `ValdiatorGroups` are created in `ParasScheduler::initializer_on_new_session(..)` + // `Paras` are rotated based on the block number in `Paras::initializer_on_new_session`(..) + // `Parathreads` are also removed in `Paras::initializer_on_new_session` - // parathreads + // a new session is started with session index 0 is started after the fn returns + // that code is part of the `add_extra_genesis` block in the `Session` pallet + // from there on all code fans out from `Self::on_new_session(..)` } fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I) From 50a11ada403717ba1180805e1a9915eb3c542be7 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 17 Jun 2020 18:01:58 +0200 Subject: [PATCH 4/4] chore: all logic is already impl in on_new_session --- runtime/parachains/src/initializer.rs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/runtime/parachains/src/initializer.rs b/runtime/parachains/src/initializer.rs index 871090668958..1927b29e4392 100644 --- a/runtime/parachains/src/initializer.rs +++ b/runtime/parachains/src/initializer.rs @@ -153,24 +153,7 @@ impl session::OneSessionHandler for Module { I: Iterator, { // host config as provided by the client using `fn build` based on the `GenesisConfig` - let config: HostConfiguration<_> = >::config(); - - // set all the values - for id in >::parachains() { - let origin: ::Origin = system::RawOrigin::Root.into(); - // @todo at this point, the dispatch should never fail - let _ = >::set_validation_upgrade_frequency(origin.clone(), config.validation_upgrade_frequency); - let _ = >::set_validation_upgrade_delay(origin.clone(), config.validation_upgrade_delay); - let _ = >::set_acceptance_period(origin.clone(), config.acceptance_period); - let _ = >::set_max_code_size(origin.clone(), config.max_code_size); - let _ = >::set_max_head_data_size(origin.clone(), config.max_head_data_size); - let _ = >::set_parathread_cores(origin.clone(), config.parathread_cores); - let _ = >::set_parathread_retries(origin.clone(), config.parathread_retries); - let _ = >::set_parachain_rotation_frequency(origin.clone(), config.parachain_rotation_frequency); - let _ = >::set_chain_availability_period(origin.clone(), config.chain_availability_period); - let _ = >::set_thread_availability_period(origin.clone(), config.thread_availability_period); - let _ = >::set_scheduling_lookahead(origin.clone(), config.scheduling_lookahead); - } + // `ValdiatorGroups` are created in `ParasScheduler::initializer_on_new_session(..)` // `Paras` are rotated based on the block number in `Paras::initializer_on_new_session`(..) // `Parathreads` are also removed in `Paras::initializer_on_new_session`