From 7b121b2988aeb162d82d453e9b66a624be819958 Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 27 Jul 2018 11:47:45 +0200 Subject: [PATCH 1/3] Exit signal gets its own trait --- polkadot/cli/src/lib.rs | 14 ++++---------- polkadot/collator/src/lib.rs | 17 +++++++++++------ polkadot/src/main.rs | 14 ++++++++------ substrate/cli/src/lib.rs | 22 +++++++++++++++------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/polkadot/cli/src/lib.rs b/polkadot/cli/src/lib.rs index ba29a39eccec4..b2da24ee10394 100644 --- a/polkadot/cli/src/lib.rs +++ b/polkadot/cli/src/lib.rs @@ -38,7 +38,7 @@ use chain_spec::ChainSpec; use futures::Future; use tokio::runtime::Runtime; pub use service::{Components as ServiceComponents, Service, CustomConfiguration}; -pub use cli::VersionInfo; +pub use cli::{VersionInfo, IntoExit}; fn load_spec(id: &str) -> Result, String> { Ok(match ChainSpec::from(id) { @@ -51,22 +51,16 @@ fn load_spec(id: &str) -> Result, String> { /// /// This will be invoked with the service and spawn a future that resolves /// when complete. -pub trait Worker { +pub trait Worker: IntoExit { /// A future that resolves when the work is done or the node should exit. /// This will be run on a tokio runtime. type Work: Future + Send + 'static; - /// An exit scheduled for the future. - type Exit: Future + Send + 'static; - /// Return configuration for the polkadot node. // TODO: make this the full configuration, so embedded nodes don't need // string CLI args fn configuration(&self) -> service::CustomConfiguration { Default::default() } - /// Don't work, but schedule an exit. - fn exit_only(&self) -> Self::Exit; - /// Do work and schedule exit. fn work(self, service: &service::Service) -> Self::Work; } @@ -85,9 +79,9 @@ pub fn run(args: I, worker: W, version: cli::VersionInfo) -> error::Res W: Worker, { - match cli::prepare_execution::(args, worker.exit_only(), version, load_spec, "parity-polkadot")? { + match cli::prepare_execution::(args, worker, version, load_spec, "parity-polkadot")? { cli::Action::ExecutedInternally => (), - cli::Action::RunService(mut config) => { + cli::Action::RunService((mut config, worker)) => { info!("Parity ·:· Polkadot"); info!(" version {}", config.full_version()); info!(" by Parity Technologies, 2017, 2018"); diff --git a/polkadot/collator/src/lib.rs b/polkadot/collator/src/lib.rs index 0acee408b68a8..41ea617c99967 100644 --- a/polkadot/collator/src/lib.rs +++ b/polkadot/collator/src/lib.rs @@ -69,7 +69,7 @@ use polkadot_api::PolkadotApi; use polkadot_primitives::{AccountId, BlockId, SessionKey}; use polkadot_primitives::parachain::{self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId}; use polkadot_cli::{ServiceComponents, Service, CustomConfiguration, VersionInfo}; -use polkadot_cli::Worker; +use polkadot_cli::{Worker, IntoExit}; use tokio::timer::Deadline; const COLLATION_TIMEOUT: Duration = Duration::from_secs(30); @@ -211,12 +211,21 @@ struct CollationNode { key: Arc, } +impl IntoExit for CollationNode where + P: ParachainContext + Send + 'static, + E: Future + Send + Clone + 'static +{ + type Exit = E; + fn into_exit(self) -> Self::Exit { + self.exit + } +} + impl Worker for CollationNode where P: ParachainContext + Send + 'static, E: Future + Send + Clone + 'static { type Work = Box + Send>; - type Exit = E; fn configuration(&self) -> CustomConfiguration { let mut config = CustomConfiguration::default(); @@ -227,10 +236,6 @@ impl Worker for CollationNode where config } - fn exit_only(&self) -> Self::Exit { - self.exit.clone() - } - fn work(self, service: &Service) -> Self::Work { let CollationNode { parachain_context, exit, para_id, key } = self; let client = service.client(); diff --git a/polkadot/src/main.rs b/polkadot/src/main.rs index 4e3fba054f962..d3e951596e200 100644 --- a/polkadot/src/main.rs +++ b/polkadot/src/main.rs @@ -38,11 +38,9 @@ mod vergen { // the regular polkadot worker simply does nothing until ctrl-c struct Worker; -impl cli::Worker for Worker { - type Work = Self::Exit; +impl cli::IntoExit for Worker { type Exit = future::MapErr, fn(oneshot::Canceled) -> ()>; - - fn exit_only(&self) -> Self::Exit { + fn into_exit(self) -> Self::Exit { // can't use signal directly here because CtrlC takes only `Fn`. let (exit_send, exit) = oneshot::channel(); @@ -55,9 +53,13 @@ impl cli::Worker for Worker { exit.map_err(drop) } +} - fn work(self, _service: &Service) -> Self::Exit { - self.exit_only() +impl cli::Worker for Worker { + type Work = ::Exit; + fn work(self, _service: &Service) -> Self::Work { + use cli::IntoExit; + self.into_exit() } } diff --git a/substrate/cli/src/lib.rs b/substrate/cli/src/lib.rs index 814a4994e191a..b17ac289edd3b 100644 --- a/substrate/cli/src/lib.rs +++ b/substrate/cli/src/lib.rs @@ -85,11 +85,19 @@ pub struct VersionInfo { } /// CLI Action -pub enum Action { +pub enum Action { /// Substrate handled the command. No need to do anything. ExecutedInternally, /// Service mode requested. Caller should start the service. - RunService(FactoryFullConfiguration), + RunService((FactoryFullConfiguration, E)), +} + +/// Something that can be converted into an exit signal. +pub trait IntoExit { + /// Exit signal type. + type Exit: Future + Send + 'static; + /// Convet into exit signal. + fn into_exit(self) -> Self::Exit; } fn load_spec(matches: &clap::ArgMatches, factory: F) -> Result, String> @@ -146,11 +154,11 @@ pub fn prepare_execution( version: VersionInfo, spec_factory: S, impl_name: &'static str, -) -> error::Result> +) -> error::Result> where I: IntoIterator, T: Into + Clone, - E: Future + Send + 'static, + E: IntoExit, F: ServiceFactory, S: FnOnce(&str) -> Result>>, String>, { @@ -182,13 +190,13 @@ where if let Some(matches) = matches.subcommand_matches("export-blocks") { let spec = load_spec(&matches, spec_factory)?; - export_blocks::(matches, spec, exit)?; + export_blocks::(matches, spec, exit.into_exit())?; return Ok(Action::ExecutedInternally); } if let Some(matches) = matches.subcommand_matches("import-blocks") { let spec = load_spec(&matches, spec_factory)?; - import_blocks::(matches, spec, exit)?; + import_blocks::(matches, spec, exit.into_exit())?; return Ok(Action::ExecutedInternally); } @@ -298,7 +306,7 @@ where config.telemetry_url = Some(url.to_owned()); } - Ok(Action::RunService(config)) + Ok(Action::RunService((config, exit))) } fn build_spec(matches: &clap::ArgMatches, spec: ChainSpec>) -> error::Result<()> From 2d874b2c4a88452eaec85d299abd6dafe9bc2668 Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 27 Jul 2018 14:19:06 +0200 Subject: [PATCH 2/3] Typo --- substrate/cli/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/cli/src/lib.rs b/substrate/cli/src/lib.rs index b17ac289edd3b..38922dd3d5c83 100644 --- a/substrate/cli/src/lib.rs +++ b/substrate/cli/src/lib.rs @@ -96,7 +96,7 @@ pub enum Action { pub trait IntoExit { /// Exit signal type. type Exit: Future + Send + 'static; - /// Convet into exit signal. + /// Convert into exit signal. fn into_exit(self) -> Self::Exit; } From 68f505324de0430656341ae778794ca0af54f32c Mon Sep 17 00:00:00 2001 From: arkpar Date: Fri, 27 Jul 2018 16:03:43 +0200 Subject: [PATCH 3/3] Removed clone bounds --- polkadot/collator/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/polkadot/collator/src/lib.rs b/polkadot/collator/src/lib.rs index 41ea617c99967..37428495d9d38 100644 --- a/polkadot/collator/src/lib.rs +++ b/polkadot/collator/src/lib.rs @@ -213,7 +213,7 @@ struct CollationNode { impl IntoExit for CollationNode where P: ParachainContext + Send + 'static, - E: Future + Send + Clone + 'static + E: Future + Send + 'static { type Exit = E; fn into_exit(self) -> Self::Exit { @@ -223,7 +223,7 @@ impl IntoExit for CollationNode where impl Worker for CollationNode where P: ParachainContext + Send + 'static, - E: Future + Send + Clone + 'static + E: Future + Send + 'static { type Work = Box + Send>;