Remove ServiceBuilderCommand and implement the chain ops as standalone functions instead. (#6543)

* :)

* Slight tidy

* Remove ServiceBuilderCommand

* Remove whitespace

* Keep task manager alive for check_block/import_blocks

* Pass task_manager to run_until_exit

* Make task_manager in run_until_exit and make subcommands async

* Change the async_run fn to return a future and task manager

* async_run should take a result fn

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Fix spaces in export_raw_state

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Ashley
2020-07-02 12:57:56 +02:00
committed by GitHub
parent 424d5c722d
commit 5f751e4472
21 changed files with 960 additions and 805 deletions
+48 -19
View File
@@ -25,10 +25,11 @@ use futures::pin_mut;
use futures::select;
use futures::{future, future::FutureExt, Future};
use log::info;
use sc_service::{Configuration, ServiceBuilderCommand, TaskType, TaskManager};
use sc_service::{Configuration, TaskType, TaskManager};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL};
use std::{fmt::Debug, marker::PhantomData, str::FromStr};
use std::{fmt::Debug, marker::PhantomData, str::FromStr, sync::Arc};
use sc_client_api::{UsageProvider, BlockBackend, StorageProvider};
#[cfg(target_family = "unix")]
async fn main<F, E>(func: F) -> std::result::Result<(), Box<dyn std::error::Error>>
@@ -92,7 +93,11 @@ pub fn build_runtime() -> std::result::Result<tokio::runtime::Runtime, std::io::
.build()
}
fn run_until_exit<FUT, ERR>(mut tokio_runtime: tokio::runtime::Runtime, future: FUT) -> Result<()>
fn run_until_exit<FUT, ERR>(
mut tokio_runtime: tokio::runtime::Runtime,
future: FUT,
mut task_manager: TaskManager,
) -> Result<()>
where
FUT: Future<Output = std::result::Result<(), ERR>> + future::Future,
ERR: 'static + std::error::Error,
@@ -102,6 +107,9 @@ where
tokio_runtime.block_on(main(f)).map_err(|e| e.to_string())?;
task_manager.terminate();
drop(tokio_runtime);
Ok(())
}
@@ -173,29 +181,47 @@ impl<C: SubstrateCli> Runner<C> {
/// A helper function that runs a future with tokio and stops if the process receives the signal
/// `SIGTERM` or `SIGINT`.
pub fn run_subcommand<B, BC, BB>(self, subcommand: &Subcommand, builder: B) -> Result<()>
pub fn run_subcommand<BU, B, BA, IQ, CL>(self, subcommand: &Subcommand, builder: BU)
-> Result<()>
where
B: FnOnce(Configuration) -> sc_service::error::Result<BC>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: sp_runtime::traits::Block + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
<BB as BlockT>::Hash: FromStr,
<<BB as BlockT>::Hash as FromStr>::Err: Debug,
BU: FnOnce(Configuration)
-> sc_service::error::Result<(Arc<CL>, Arc<BA>, IQ, TaskManager)>,
B: BlockT + for<'de> serde::Deserialize<'de>,
BA: sc_client_api::backend::Backend<B> + 'static,
IQ: sc_service::ImportQueue<B> + 'static,
<B as BlockT>::Hash: FromStr,
<<B as BlockT>::Hash as FromStr>::Err: Debug,
<<<B as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
CL: UsageProvider<B> + BlockBackend<B> + StorageProvider<B, BA> + Send + Sync +
'static,
{
let chain_spec = self.config.chain_spec.cloned_box();
let network_config = self.config.network.clone();
let db_config = self.config.database.clone();
match subcommand {
Subcommand::BuildSpec(cmd) => cmd.run(self.config),
Subcommand::BuildSpec(cmd) => cmd.run(chain_spec, network_config),
Subcommand::ExportBlocks(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
let (client, _, _, task_manager) = builder(self.config)?;
run_until_exit(self.tokio_runtime, cmd.run(client, db_config), task_manager)
}
Subcommand::ImportBlocks(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
let (client, _, import_queue, task_manager) = builder(self.config)?;
run_until_exit(self.tokio_runtime, cmd.run(client, import_queue), task_manager)
}
Subcommand::CheckBlock(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
let (client, _, import_queue, task_manager) = builder(self.config)?;
run_until_exit(self.tokio_runtime, cmd.run(client, import_queue), task_manager)
}
Subcommand::Revert(cmd) => cmd.run(self.config, builder),
Subcommand::PurgeChain(cmd) => cmd.run(self.config),
Subcommand::ExportState(cmd) => cmd.run(self.config, builder),
Subcommand::Revert(cmd) => {
let (client, backend, _, task_manager) = builder(self.config)?;
run_until_exit(self.tokio_runtime, cmd.run(client, backend), task_manager)
},
Subcommand::PurgeChain(cmd) => cmd.run(db_config),
Subcommand::ExportState(cmd) => {
let (client, _, _, task_manager) = builder(self.config)?;
run_until_exit(self.tokio_runtime, cmd.run(client, chain_spec), task_manager)
},
}
}
@@ -221,11 +247,14 @@ impl<C: SubstrateCli> Runner<C> {
/// A helper function that runs a future with tokio and stops if the process receives
/// the signal SIGTERM or SIGINT
pub fn async_run<FUT>(self, runner: impl FnOnce(Configuration) -> FUT) -> Result<()>
pub fn async_run<FUT>(
self, runner: impl FnOnce(Configuration) -> Result<(FUT, TaskManager)>,
) -> Result<()>
where
FUT: Future<Output = Result<()>>,
{
run_until_exit(self.tokio_runtime, runner(self.config))
let (future, task_manager) = runner(self.config)?;
run_until_exit(self.tokio_runtime, future, task_manager)
}
/// Get an immutable reference to the node Configuration