lib.rs 3.69 KB
Newer Older
Gav's avatar
Gav committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Polkadot CLI library.

#![warn(missing_docs)]
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
20
#![warn(unused_extern_crates)]
Gav's avatar
Gav committed
21

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
22
extern crate futures;
23
extern crate tokio;
24

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
25
extern crate substrate_cli as cli;
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
26
extern crate polkadot_service as service;
27
extern crate exit_future;
Gav's avatar
Gav committed
28
29
30
31

#[macro_use]
extern crate log;

Gav Wood's avatar
Gav Wood committed
32
33
mod chain_spec;

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
34
pub use cli::error;
Gav's avatar
Gav committed
35

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
36
use chain_spec::ChainSpec;
37

38
39
use futures::Future;
use tokio::runtime::Runtime;
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
40
pub use service::{Components as ServiceComponents, Service, CustomConfiguration};
41
pub use cli::{VersionInfo, IntoExit};
Gav Wood's avatar
Gav Wood committed
42

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
43
44
45
46
47
fn load_spec(id: &str) -> Result<Option<service::ChainSpec>, String> {
	Ok(match ChainSpec::from(id) {
		Some(spec) => Some(spec.load()?),
		None => None,
	})
48
49
}

50
51
52
53
/// Additional worker making use of the node, to run asynchronously before shutdown.
///
/// This will be invoked with the service and spawn a future that resolves
/// when complete.
54
pub trait Worker: IntoExit {
55
56
	/// A future that resolves when the work is done or the node should exit.
	/// This will be run on a tokio runtime.
57
	type Work: Future<Item=(),Error=()> + Send + 'static;
58

59
60
61
	/// Return configuration for the polkadot node.
	// TODO: make this the full configuration, so embedded nodes don't need
	// string CLI args
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
62
	fn configuration(&self) -> service::CustomConfiguration { Default::default() }
63

64
	/// Do work and schedule exit.
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
65
	fn work<C: service::Components>(self, service: &service::Service<C>) -> Self::Work;
66
67
}

Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
68
/// Parse command line arguments into service configuration.
Gav's avatar
Gav committed
69
70
71
72
73
74
75
///
/// IANA unassigned port ranges that we could use:
/// 6717-6766		Unassigned
/// 8504-8553		Unassigned
/// 9556-9591		Unassigned
/// 9803-9874		Unassigned
/// 9926-9949		Unassigned
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
76
pub fn run<I, T, W>(args: I, worker: W, version: cli::VersionInfo) -> error::Result<()> where
Gav's avatar
Gav committed
77
78
	I: IntoIterator<Item = T>,
	T: Into<std::ffi::OsString> + Clone,
79
	W: Worker,
Gav's avatar
Gav committed
80
{
81

82
	match cli::prepare_execution::<service::Factory, _, _, _, _>(args, worker, version, load_spec, "parity-polkadot")? {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
83
		cli::Action::ExecutedInternally => (),
84
		cli::Action::RunService((mut config, worker)) => {
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
85
86
87
88
89
90
91
92
93
94
95
96
			info!("Parity ·:· Polkadot");
			info!("  version {}", config.full_version());
			info!("  by Parity Technologies, 2017, 2018");
			info!("Chain specification: {}", config.chain_spec.name());
			info!("Node name: {}", config.name);
			info!("Roles: {:?}", config.roles);
			config.custom = worker.configuration();
			let mut runtime = Runtime::new()?;
			let executor = runtime.executor();
			match config.roles == service::Roles::LIGHT {
				true => run_until_exit(&mut runtime, service::new_light(config, executor)?, worker)?,
				false => run_until_exit(&mut runtime, service::new_full(config, executor)?, worker)?,
97
98
99
100
101
			}
		}
	}
	Ok(())
}
102
103
104
105
106
fn run_until_exit<C, W>(
	runtime: &mut Runtime,
	service: service::Service<C>,
	worker: W,
) -> error::Result<()>
107
	where
108
		C: service::Components,
109
		W: Worker,
110
{
111
	let (exit_send, exit) = exit_future::signal();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
112

113
	let executor = runtime.executor();
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
114
	cli::informant::start(&service, exit.clone(), executor.clone());
115

116
	let _ = runtime.block_on(worker.work(&service));
117
	exit_send.fire();
Gav's avatar
Gav committed
118
119
	Ok(())
}