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

Commit 91c3ea9

Browse files
authored
Exit signal gets its own trait (#433)
* Exit signal gets its own trait * Typo * Removed clone bounds
1 parent 8f414ab commit 91c3ea9

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

polkadot/cli/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use chain_spec::ChainSpec;
3838
use futures::Future;
3939
use tokio::runtime::Runtime;
4040
pub use service::{Components as ServiceComponents, Service, CustomConfiguration};
41-
pub use cli::VersionInfo;
41+
pub use cli::{VersionInfo, IntoExit};
4242

4343
fn load_spec(id: &str) -> Result<Option<service::ChainSpec>, String> {
4444
Ok(match ChainSpec::from(id) {
@@ -51,22 +51,16 @@ fn load_spec(id: &str) -> Result<Option<service::ChainSpec>, String> {
5151
///
5252
/// This will be invoked with the service and spawn a future that resolves
5353
/// when complete.
54-
pub trait Worker {
54+
pub trait Worker: IntoExit {
5555
/// A future that resolves when the work is done or the node should exit.
5656
/// This will be run on a tokio runtime.
5757
type Work: Future<Item=(),Error=()> + Send + 'static;
5858

59-
/// An exit scheduled for the future.
60-
type Exit: Future<Item=(),Error=()> + Send + 'static;
61-
6259
/// Return configuration for the polkadot node.
6360
// TODO: make this the full configuration, so embedded nodes don't need
6461
// string CLI args
6562
fn configuration(&self) -> service::CustomConfiguration { Default::default() }
6663

67-
/// Don't work, but schedule an exit.
68-
fn exit_only(&self) -> Self::Exit;
69-
7064
/// Do work and schedule exit.
7165
fn work<C: service::Components>(self, service: &service::Service<C>) -> Self::Work;
7266
}
@@ -85,9 +79,9 @@ pub fn run<I, T, W>(args: I, worker: W, version: cli::VersionInfo) -> error::Res
8579
W: Worker,
8680
{
8781

88-
match cli::prepare_execution::<service::Factory, _, _, _, _>(args, worker.exit_only(), version, load_spec, "parity-polkadot")? {
82+
match cli::prepare_execution::<service::Factory, _, _, _, _>(args, worker, version, load_spec, "parity-polkadot")? {
8983
cli::Action::ExecutedInternally => (),
90-
cli::Action::RunService(mut config) => {
84+
cli::Action::RunService((mut config, worker)) => {
9185
info!("Parity ·:· Polkadot");
9286
info!(" version {}", config.full_version());
9387
info!(" by Parity Technologies, 2017, 2018");

polkadot/collator/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use polkadot_api::PolkadotApi;
6969
use polkadot_primitives::{AccountId, BlockId, SessionKey};
7070
use polkadot_primitives::parachain::{self, BlockData, DutyRoster, HeadData, ConsolidatedIngress, Message, Id as ParaId};
7171
use polkadot_cli::{ServiceComponents, Service, CustomConfiguration, VersionInfo};
72-
use polkadot_cli::Worker;
72+
use polkadot_cli::{Worker, IntoExit};
7373
use tokio::timer::Deadline;
7474

7575
const COLLATION_TIMEOUT: Duration = Duration::from_secs(30);
@@ -211,12 +211,21 @@ struct CollationNode<P, E> {
211211
key: Arc<ed25519::Pair>,
212212
}
213213

214+
impl<P, E> IntoExit for CollationNode<P, E> where
215+
P: ParachainContext + Send + 'static,
216+
E: Future<Item=(),Error=()> + Send + 'static
217+
{
218+
type Exit = E;
219+
fn into_exit(self) -> Self::Exit {
220+
self.exit
221+
}
222+
}
223+
214224
impl<P, E> Worker for CollationNode<P, E> where
215225
P: ParachainContext + Send + 'static,
216-
E: Future<Item=(),Error=()> + Send + Clone + 'static
226+
E: Future<Item=(),Error=()> + Send + 'static
217227
{
218228
type Work = Box<Future<Item=(),Error=()> + Send>;
219-
type Exit = E;
220229

221230
fn configuration(&self) -> CustomConfiguration {
222231
let mut config = CustomConfiguration::default();
@@ -227,10 +236,6 @@ impl<P, E> Worker for CollationNode<P, E> where
227236
config
228237
}
229238

230-
fn exit_only(&self) -> Self::Exit {
231-
self.exit.clone()
232-
}
233-
234239
fn work<C: ServiceComponents>(self, service: &Service<C>) -> Self::Work {
235240
let CollationNode { parachain_context, exit, para_id, key } = self;
236241
let client = service.client();

polkadot/src/main.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ mod vergen {
3838

3939
// the regular polkadot worker simply does nothing until ctrl-c
4040
struct Worker;
41-
impl cli::Worker for Worker {
42-
type Work = Self::Exit;
41+
impl cli::IntoExit for Worker {
4342
type Exit = future::MapErr<oneshot::Receiver<()>, fn(oneshot::Canceled) -> ()>;
44-
45-
fn exit_only(&self) -> Self::Exit {
43+
fn into_exit(self) -> Self::Exit {
4644
// can't use signal directly here because CtrlC takes only `Fn`.
4745
let (exit_send, exit) = oneshot::channel();
4846

@@ -55,9 +53,13 @@ impl cli::Worker for Worker {
5553

5654
exit.map_err(drop)
5755
}
56+
}
5857

59-
fn work<C: ServiceComponents>(self, _service: &Service<C>) -> Self::Exit {
60-
self.exit_only()
58+
impl cli::Worker for Worker {
59+
type Work = <Self as cli::IntoExit>::Exit;
60+
fn work<C: ServiceComponents>(self, _service: &Service<C>) -> Self::Work {
61+
use cli::IntoExit;
62+
self.into_exit()
6163
}
6264
}
6365

substrate/cli/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,19 @@ pub struct VersionInfo {
8585
}
8686

8787
/// CLI Action
88-
pub enum Action<F: ServiceFactory> {
88+
pub enum Action<F: ServiceFactory, E: IntoExit> {
8989
/// Substrate handled the command. No need to do anything.
9090
ExecutedInternally,
9191
/// Service mode requested. Caller should start the service.
92-
RunService(FactoryFullConfiguration<F>),
92+
RunService((FactoryFullConfiguration<F>, E)),
93+
}
94+
95+
/// Something that can be converted into an exit signal.
96+
pub trait IntoExit {
97+
/// Exit signal type.
98+
type Exit: Future<Item=(),Error=()> + Send + 'static;
99+
/// Convert into exit signal.
100+
fn into_exit(self) -> Self::Exit;
93101
}
94102

95103
fn load_spec<F, G>(matches: &clap::ArgMatches, factory: F) -> Result<ChainSpec<G>, String>
@@ -146,11 +154,11 @@ pub fn prepare_execution<F, I, T, E, S>(
146154
version: VersionInfo,
147155
spec_factory: S,
148156
impl_name: &'static str,
149-
) -> error::Result<Action<F>>
157+
) -> error::Result<Action<F, E>>
150158
where
151159
I: IntoIterator<Item = T>,
152160
T: Into<std::ffi::OsString> + Clone,
153-
E: Future<Item=(),Error=()> + Send + 'static,
161+
E: IntoExit,
154162
F: ServiceFactory,
155163
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
156164
{
@@ -182,13 +190,13 @@ where
182190

183191
if let Some(matches) = matches.subcommand_matches("export-blocks") {
184192
let spec = load_spec(&matches, spec_factory)?;
185-
export_blocks::<F, _>(matches, spec, exit)?;
193+
export_blocks::<F, _>(matches, spec, exit.into_exit())?;
186194
return Ok(Action::ExecutedInternally);
187195
}
188196

189197
if let Some(matches) = matches.subcommand_matches("import-blocks") {
190198
let spec = load_spec(&matches, spec_factory)?;
191-
import_blocks::<F, _>(matches, spec, exit)?;
199+
import_blocks::<F, _>(matches, spec, exit.into_exit())?;
192200
return Ok(Action::ExecutedInternally);
193201
}
194202

@@ -298,7 +306,7 @@ where
298306
config.telemetry_url = Some(url.to_owned());
299307
}
300308

301-
Ok(Action::RunService(config))
309+
Ok(Action::RunService((config, exit)))
302310
}
303311

304312
fn build_spec<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>) -> error::Result<()>

0 commit comments

Comments
 (0)