From 60337aa76551856d7ea9fc897887aa65a17376b6 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Jun 2020 17:50:29 +0200 Subject: [PATCH] Add run_full_node, run_light_node and print_node_infos to the API (#6233) * Initial commit Forked at: e77490714d050df9bc5793e0287e0133385ed7a0 Parent branch: origin/master * Add run_full_node, run_light_node and print_node_infos to the API * Update runner.rs --- substrate/client/cli/src/runner.rs | 93 ++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/substrate/client/cli/src/runner.rs b/substrate/client/cli/src/runner.rs index 2d27743163..9286031589 100644 --- a/substrate/client/cli/src/runner.rs +++ b/substrate/client/cli/src/runner.rs @@ -18,8 +18,8 @@ use crate::CliConfiguration; use crate::Result; -use crate::SubstrateCli; use crate::Subcommand; +use crate::SubstrateCli; use chrono::prelude::*; use futures::pin_mut; use futures::select; @@ -28,7 +28,8 @@ use log::info; use sc_service::{AbstractService, Configuration, Role, ServiceBuilderCommand, TaskType}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL}; -use std::{str::FromStr, fmt::Debug, marker::PhantomData, sync::Arc}; +use sp_version::RuntimeVersion; +use std::{fmt::Debug, marker::PhantomData, str::FromStr, sync::Arc}; #[cfg(target_family = "unix")] async fn main(func: F) -> std::result::Result<(), Box> @@ -81,11 +82,11 @@ where pub fn build_runtime() -> std::result::Result { tokio::runtime::Builder::new() .threaded_scheduler() - .on_thread_start(||{ + .on_thread_start(|| { TOKIO_THREADS_ALIVE.inc(); TOKIO_THREADS_TOTAL.inc(); }) - .on_thread_stop(||{ + .on_thread_stop(|| { TOKIO_THREADS_ALIVE.dec(); }) .enable_all() @@ -140,19 +141,21 @@ impl Runner { }) } - /// A helper function that runs an `AbstractService` with tokio and stops if the process receives - /// the signal `SIGTERM` or `SIGINT`. - pub fn run_node( - self, - new_light: FNL, - new_full: FNF, - runtime_version: sp_version::RuntimeVersion, - ) -> Result<()> where - FNL: FnOnce(Configuration) -> sc_service::error::Result, - FNF: FnOnce(Configuration) -> sc_service::error::Result, - SL: AbstractService + Unpin, - SF: AbstractService + Unpin, - { + /// Log information about the node itself. + /// + /// # Example: + /// + /// ```text + /// 2020-06-03 16:14:21 Substrate Node + /// 2020-06-03 16:14:21 ✌️ version 2.0.0-rc2-f4940588c-x86_64-linux-gnu + /// 2020-06-03 16:14:21 ❤️ by Parity Technologies , 2017-2020 + /// 2020-06-03 16:14:21 📋 Chain specification: Flaming Fir + /// 2020-06-03 16:14:21 🏷 Node name: jolly-rod-7462 + /// 2020-06-03 16:14:21 👤 Role: FULL + /// 2020-06-03 16:14:21 💾 Database: RocksDb at /tmp/c/chains/flamingfir7/db + /// 2020-06-03 16:14:21 ⛓ Native runtime: node-251 (substrate-node-1.tx1.au10) + /// ``` + pub fn print_node_infos(&self, runtime_version: RuntimeVersion) { info!("{}", C::impl_name()); info!("✌️ version {}", C::impl_version()); info!( @@ -169,13 +172,65 @@ impl Runner { self.config.database.path().map_or_else(|| "".to_owned(), |p| p.display().to_string()) ); info!("⛓ Native runtime: {}", runtime_version); + } + /// A helper function that runs an `AbstractService` with tokio and stops if the process + /// receives the signal `SIGTERM` or `SIGINT`. It can run a full or a light node depending on + /// the node's configuration. + pub fn run_node( + self, + new_light: impl FnOnce(Configuration) -> sc_service::error::Result, + new_full: impl FnOnce(Configuration) -> sc_service::error::Result, + runtime_version: RuntimeVersion, + ) -> Result<()> + where + SL: AbstractService + Unpin, + SF: AbstractService + Unpin, + { match self.config.role { - Role::Light => self.run_service_until_exit(new_light), - _ => self.run_service_until_exit(new_full), + Role::Light => self.run_light_node(new_light, runtime_version), + _ => self.run_full_node(new_full, runtime_version), } } + /// A helper function that runs an `AbstractService` with tokio and stops if the process + /// receives the signal `SIGTERM` or `SIGINT`. It can only run a "full" node and will fail if + /// the node's configuration uses a "light" role. + pub fn run_full_node( + self, + new_full: impl FnOnce(Configuration) -> sc_service::error::Result, + runtime_version: RuntimeVersion, + ) -> Result<()> + where + S: AbstractService + Unpin, + { + if matches!(self.config.role, Role::Light) { + return Err("Light node has been requested but this is not implemented".into()); + } + + self.print_node_infos(runtime_version); + self.run_service_until_exit(new_full) + } + + /// A helper function that runs an `AbstractService` with tokio and stops if the process + /// receives the signal `SIGTERM` or `SIGINT`. It can only run a "light" node and will fail if + /// the node's configuration uses a "full" role. + pub fn run_light_node( + self, + new_light: impl FnOnce(Configuration) -> sc_service::error::Result, + runtime_version: RuntimeVersion, + ) -> Result<()> + where + S: AbstractService + Unpin, + { + if !matches!(self.config.role, Role::Light) { + return Err("Full node has been requested but this is not implemented".into()); + } + + self.print_node_infos(runtime_version); + self.run_service_until_exit(new_light) + } + /// A helper function that runs a future with tokio and stops if the process receives the signal /// `SIGTERM` or `SIGINT`. pub fn run_subcommand(self, subcommand: &Subcommand, builder: B) -> Result<()>