mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-07 11:58:01 +00:00
5b8ebf7baf
* Move Service::new to a macro * Move function calls to macros * Extract offchain_workers and start_rpc in separate function In follow-up commits, we want to be able to directly call maintain_transaction_pool, offchain_workers, and start_rpc, without having to implement the Components trait. This commit is a preliminary step: we extract the code to freestanding functions. * Introduce an AbstractService trait * Introduce NewService as an implementation detail of Service * Implement traits on NewService instead Instead of implementing AbstractService, Future, and Executor on Service, we implement them on NewService instead. The implementations of AbstractService, Future, and Executor on Service still exist, but they just wrap to the respective implementations for NewService. * Move components creation back to macro invocation Instead of having multiple $build_ parameters passed to the macro, let's group them all into one. This change is necessary for the follow-up commits, because we are going to call new_impl! only after all the components have already been built. * Add a $block parameter to new_impl This makes it possible to be explicit as what the generic parameter of the NewServiceis, without relying on type inference. * Introduce the ServiceBuilder struct Introduces a new builder-like ServiceBuilder struct that creates a NewService. * Macro-ify import_blocks, export_blocks and revert_chain Similar to the introduction of new_impl!, we extract the actual code into a macro, letting us get rid of the Components and Factory traits * Add export_blocks, import_blocks and revert_chain methods on ServiceBuilder Can be used as a replacement for the chain_ops::* methods * Add run_with_builder Instead of just run, adds run_with_builder to ParseAndPrepareExport/Import/Revert. This lets you run these operations with a ServiceBuilder instead of a ServiceFactory. * Transition node and node-template to ServiceBuilder * Transition transaction-factory to the new service factory This is technically a breaking change, but the transaction-factory crate is only ever used from within substrate-node, which this commit updates as well. * Remove old service factory * Adjust the AbstractService trait to be more usable We slightly change the trait bounds in order to make all the methods usable. * Make substrate-service-test compile * Fix the node-cli tests * Remove the old API * Remove the components module * Fix indentation on chain_ops * Line widths * Fix bad line widths commit * Line widths again 🤦 * Fix the sync test * Apply suggestions from code review Co-Authored-By: Gavin Wood <i@gavwood.com> * Address some concerns * Remove TelemetryOnConnect * Remove informant::start * Update jsonrpc * Rename factory to builder * Line widths 😩
113 lines
3.7 KiB
Rust
113 lines
3.7 KiB
Rust
use crate::service;
|
|
use futures::{future, Future, sync::oneshot};
|
|
use std::cell::RefCell;
|
|
use tokio::runtime::Runtime;
|
|
pub use substrate_cli::{VersionInfo, IntoExit, error};
|
|
use substrate_cli::{informant, parse_and_prepare, ParseAndPrepare, NoCustom};
|
|
use substrate_service::{AbstractService, Roles as ServiceRoles};
|
|
use crate::chain_spec;
|
|
use log::info;
|
|
|
|
/// Parse command line arguments into service configuration.
|
|
pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()> where
|
|
I: IntoIterator<Item = T>,
|
|
T: Into<std::ffi::OsString> + Clone,
|
|
E: IntoExit,
|
|
{
|
|
match parse_and_prepare::<NoCustom, NoCustom, _>(&version, "substrate-node", args) {
|
|
ParseAndPrepare::Run(cmd) => cmd.run::<(), _, _, _, _>(load_spec, exit,
|
|
|exit, _cli_args, _custom_args, config| {
|
|
info!("{}", version.name);
|
|
info!(" version {}", config.full_version());
|
|
info!(" by {}, 2017, 2018", version.author);
|
|
info!("Chain specification: {}", config.chain_spec.name());
|
|
info!("Node name: {}", config.name);
|
|
info!("Roles: {:?}", config.roles);
|
|
let runtime = Runtime::new().map_err(|e| format!("{:?}", e))?;
|
|
match config.roles {
|
|
ServiceRoles::LIGHT => run_until_exit(
|
|
runtime,
|
|
service::new_light(config).map_err(|e| format!("{:?}", e))?,
|
|
exit
|
|
),
|
|
_ => run_until_exit(
|
|
runtime,
|
|
service::new_full(config).map_err(|e| format!("{:?}", e))?,
|
|
exit
|
|
),
|
|
}.map_err(|e| format!("{:?}", e))
|
|
}),
|
|
ParseAndPrepare::BuildSpec(cmd) => cmd.run(load_spec),
|
|
ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder::<(), _, _, _, _, _>(|config|
|
|
Ok(new_full_start!(config).0), load_spec, exit),
|
|
ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder::<(), _, _, _, _, _>(|config|
|
|
Ok(new_full_start!(config).0), load_spec, exit),
|
|
ParseAndPrepare::PurgeChain(cmd) => cmd.run(load_spec),
|
|
ParseAndPrepare::RevertChain(cmd) => cmd.run_with_builder::<(), _, _, _, _>(|config|
|
|
Ok(new_full_start!(config).0), load_spec),
|
|
ParseAndPrepare::CustomCommand(_) => Ok(())
|
|
}?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn load_spec(id: &str) -> Result<Option<chain_spec::ChainSpec>, String> {
|
|
Ok(match chain_spec::Alternative::from(id) {
|
|
Some(spec) => Some(spec.load()?),
|
|
None => None,
|
|
})
|
|
}
|
|
|
|
fn run_until_exit<T, E>(
|
|
mut runtime: Runtime,
|
|
service: T,
|
|
e: E,
|
|
) -> error::Result<()>
|
|
where
|
|
T: AbstractService,
|
|
E: IntoExit,
|
|
{
|
|
let (exit_send, exit) = exit_future::signal();
|
|
|
|
let informant = informant::build(&service);
|
|
runtime.executor().spawn(exit.until(informant).map(|_| ()));
|
|
|
|
// we eagerly drop the service so that the internal exit future is fired,
|
|
// but we need to keep holding a reference to the global telemetry guard
|
|
let _telemetry = service.telemetry();
|
|
|
|
let service_res = {
|
|
let exit = e.into_exit().map_err(|_| error::Error::Other("Exit future failed.".into()));
|
|
let service = service.map_err(|err| error::Error::Service(err));
|
|
let select = service.select(exit).map(|_| ()).map_err(|(err, _)| err);
|
|
runtime.block_on(select)
|
|
};
|
|
|
|
exit_send.fire();
|
|
|
|
// TODO [andre]: timeout this future #1318
|
|
let _ = runtime.shutdown_on_idle().wait();
|
|
|
|
service_res
|
|
}
|
|
|
|
// handles ctrl-c
|
|
pub struct Exit;
|
|
impl IntoExit for Exit {
|
|
type Exit = future::MapErr<oneshot::Receiver<()>, fn(oneshot::Canceled) -> ()>;
|
|
fn into_exit(self) -> Self::Exit {
|
|
// can't use signal directly here because CtrlC takes only `Fn`.
|
|
let (exit_send, exit) = oneshot::channel();
|
|
|
|
let exit_send_cell = RefCell::new(Some(exit_send));
|
|
ctrlc::set_handler(move || {
|
|
let exit_send = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take();
|
|
if let Some(exit_send) = exit_send {
|
|
exit_send.send(()).expect("Error sending exit notification");
|
|
}
|
|
}).expect("Error setting Ctrl-C handler");
|
|
|
|
exit.map_err(drop)
|
|
}
|
|
}
|