From 188a103e203d1fa0ad8661b076bc0b47f913c36c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 27 Feb 2020 16:18:38 +0100 Subject: [PATCH] Spawn a future to start the collator (#851) * Running a node without a database set in config is a bug, not a user error --- polkadot/collator/src/lib.rs | 58 +++++++++++++++++++++++++++++++----- polkadot/service/src/lib.rs | 2 +- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/polkadot/collator/src/lib.rs b/polkadot/collator/src/lib.rs index e0f230452c..7dc4f70d20 100644 --- a/polkadot/collator/src/lib.rs +++ b/polkadot/collator/src/lib.rs @@ -217,8 +217,7 @@ pub async fn collate

( Ok(collation) } -/// Run the collator node using the given `service`. -fn run_collator_node( +fn build_collator_service( service: S, para_id: ParaId, key: Arc, @@ -381,6 +380,51 @@ fn run_collator_node( Ok(service) } +/// Async function that will run the collator node with the given `RelayChainContext` and `ParachainContext` +/// built by the given `BuildParachainContext` and arguments to the underlying polkadot node. +pub async fn start_collator

( + build_parachain_context: P, + para_id: ParaId, + key: Arc, + config: Configuration, +) -> Result<(), polkadot_service::Error> +where + P: BuildParachainContext, + P::ParachainContext: Send + 'static, + ::ProduceCandidate: Send, +{ + match (config.expect_chain_spec().is_kusama(), config.roles) { + (true, Roles::LIGHT) => + build_collator_service( + service::kusama_new_light(config, Some((key.public(), para_id)))?, + para_id, + key, + build_parachain_context, + )?.await, + (true, _) => + build_collator_service( + service::kusama_new_full(config, Some((key.public(), para_id)), None, false, 6000)?, + para_id, + key, + build_parachain_context, + )?.await, + (false, Roles::LIGHT) => + build_collator_service( + service::polkadot_new_light(config, Some((key.public(), para_id)))?, + para_id, + key, + build_parachain_context, + )?.await, + (false, _) => + build_collator_service( + service::polkadot_new_full(config, Some((key.public(), para_id)), None, false, 6000)?, + para_id, + key, + build_parachain_context, + )?.await, + } +} + fn compute_targets(para_id: ParaId, session_keys: &[ValidatorId], roster: DutyRoster) -> HashSet { use polkadot_primitives::parachain::Chain; @@ -392,7 +436,7 @@ fn compute_targets(para_id: ParaId, session_keys: &[ValidatorId], roster: DutyRo } /// Run a collator node with the given `RelayChainContext` and `ParachainContext` -/// build by the given `BuildParachainContext` and arguments to the underlying polkadot node. +/// built by the given `BuildParachainContext` and arguments to the underlying polkadot node. /// /// This function blocks until done. pub fn run_collator

( @@ -408,7 +452,7 @@ pub fn run_collator

( match (config.expect_chain_spec().is_kusama(), config.roles) { (true, Roles::LIGHT) => sc_cli::run_service_until_exit(config, |config| { - run_collator_node( + build_collator_service( service::kusama_new_light(config, Some((key.public(), para_id)))?, para_id, key, @@ -417,7 +461,7 @@ pub fn run_collator

( }), (true, _) => sc_cli::run_service_until_exit(config, |config| { - run_collator_node( + build_collator_service( service::kusama_new_full(config, Some((key.public(), para_id)), None, false, 6000)?, para_id, key, @@ -426,7 +470,7 @@ pub fn run_collator

( }), (false, Roles::LIGHT) => sc_cli::run_service_until_exit(config, |config| { - run_collator_node( + build_collator_service( service::polkadot_new_light(config, Some((key.public(), para_id)))?, para_id, key, @@ -435,7 +479,7 @@ pub fn run_collator

( }), (false, _) => sc_cli::run_service_until_exit(config, |config| { - run_collator_node( + build_collator_service( service::polkadot_new_full(config, Some((key.public(), para_id)), None, false, 6000)?, para_id, key, diff --git a/polkadot/service/src/lib.rs b/polkadot/service/src/lib.rs index 2a2db82cb4..ee64465f69 100644 --- a/polkadot/service/src/lib.rs +++ b/polkadot/service/src/lib.rs @@ -272,7 +272,7 @@ pub fn new_full( let is_authority = config.roles.is_authority() && !is_collator; let force_authoring = config.force_authoring; let max_block_data_size = max_block_data_size; - let db_path = if let Some(DatabaseConfig::Path { ref path, .. }) = config.database { + let db_path = if let DatabaseConfig::Path { ref path, .. } = config.expect_database() { path.clone() } else { return Err("Starting a Polkadot service with a custom database isn't supported".to_string().into());