git.delta.rocks / unique-network / refs/commits / 5cd3d42aeddb

difftreelog

source

src/cli.rs3.7 KiBsourcehistory
1use crate::service;2use futures::{future, Future, sync::oneshot};3use std::cell::RefCell;4use tokio::runtime::Runtime;5pub use substrate_cli::{VersionInfo, IntoExit, error};6use substrate_cli::{informant, parse_and_prepare, ParseAndPrepare, NoCustom};7use substrate_service::{AbstractService, Roles as ServiceRoles};8use crate::chain_spec;9use log::info;1011/// Parse command line arguments into service configuration.12pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()> where13	I: IntoIterator<Item = T>,14	T: Into<std::ffi::OsString> + Clone,15	E: IntoExit,16{17	match parse_and_prepare::<NoCustom, NoCustom, _>(&version, "substrate-node", args) {18		ParseAndPrepare::Run(cmd) => cmd.run::<(), _, _, _, _>(load_spec, exit,19		|exit, _cli_args, _custom_args, config| {20			info!("{}", version.name);21			info!("  version {}", config.full_version());22			info!("  by {}, 2017, 2018", version.author);23			info!("Chain specification: {}", config.chain_spec.name());24			info!("Node name: {}", config.name);25			info!("Roles: {:?}", config.roles);26			let runtime = Runtime::new().map_err(|e| format!("{:?}", e))?;27			match config.roles {28				ServiceRoles::LIGHT => run_until_exit(29					runtime,30				 	service::new_light(config).map_err(|e| format!("{:?}", e))?,31					exit32				),33				_ => run_until_exit(34					runtime,35					service::new_full(config).map_err(|e| format!("{:?}", e))?,36					exit37				),38			}.map_err(|e| format!("{:?}", e))39		}),40		ParseAndPrepare::BuildSpec(cmd) => cmd.run(load_spec),41		ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder::<(), _, _, _, _, _>(|config|42			Ok(new_full_start!(config).0), load_spec, exit),43		ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder::<(), _, _, _, _, _>(|config|44			Ok(new_full_start!(config).0), load_spec, exit),45		ParseAndPrepare::PurgeChain(cmd) => cmd.run(load_spec),46		ParseAndPrepare::RevertChain(cmd) => cmd.run_with_builder::<(), _, _, _, _>(|config|47			Ok(new_full_start!(config).0), load_spec),48		ParseAndPrepare::CustomCommand(_) => Ok(())49	}?;5051	Ok(())52}5354fn load_spec(id: &str) -> Result<Option<chain_spec::ChainSpec>, String> {55	Ok(match chain_spec::Alternative::from(id) {56		Some(spec) => Some(spec.load()?),57		None => None,58	})59}6061fn run_until_exit<T, E>(62	mut runtime: Runtime,63	service: T,64	e: E,65) -> error::Result<()>66where67	T: AbstractService,68	E: IntoExit,69{70	let (exit_send, exit) = exit_future::signal();7172	let informant = informant::build(&service);73	runtime.executor().spawn(exit.until(informant).map(|_| ()));7475	// we eagerly drop the service so that the internal exit future is fired,76	// but we need to keep holding a reference to the global telemetry guard77	let _telemetry = service.telemetry();7879	let service_res = {80		let exit = e.into_exit().map_err(|_| error::Error::Other("Exit future failed.".into()));81		let service = service.map_err(|err| error::Error::Service(err));82		let select = service.select(exit).map(|_| ()).map_err(|(err, _)| err);83		runtime.block_on(select)84	};8586	exit_send.fire();8788	// TODO [andre]: timeout this future #131889	let _ = runtime.shutdown_on_idle().wait();9091	service_res92}9394// handles ctrl-c95pub struct Exit;96impl IntoExit for Exit {97	type Exit = future::MapErr<oneshot::Receiver<()>, fn(oneshot::Canceled) -> ()>;98	fn into_exit(self) -> Self::Exit {99		// can't use signal directly here because CtrlC takes only `Fn`.100		let (exit_send, exit) = oneshot::channel();101102		let exit_send_cell = RefCell::new(Some(exit_send));103		ctrlc::set_handler(move || {104			let exit_send = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take();105			if let Some(exit_send) = exit_send {106				exit_send.send(()).expect("Error sending exit notification");107			}108		}).expect("Error setting Ctrl-C handler");109110		exit.map_err(drop)111	}112}