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
@@ -22,7 +22,7 @@ use crate::params::SharedParams;
use crate::CliConfiguration;
use log::info;
use sc_network::config::build_multiaddr;
use sc_service::{config::MultiaddrWithPeerId, Configuration};
use sc_service::{config::{MultiaddrWithPeerId, NetworkConfiguration}, ChainSpec};
use structopt::StructOpt;
use std::io::Write;
@@ -51,13 +51,16 @@ pub struct BuildSpecCmd {
impl BuildSpecCmd {
/// Run the build-spec command
pub fn run(&self, config: Configuration) -> error::Result<()> {
pub fn run(
&self,
mut spec: Box<dyn ChainSpec>,
network_config: NetworkConfiguration,
) -> error::Result<()> {
info!("Building chain spec");
let mut spec = config.chain_spec;
let raw_output = self.raw;
if spec.boot_nodes().is_empty() && !self.disable_default_bootnode {
let keys = config.network.node_key.into_keypair()?;
let keys = network_config.node_key.into_keypair()?;
let peer_id = keys.public().into_peer_id();
let addr = MultiaddrWithPeerId {
multiaddr: build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(30333u16)],
@@ -19,9 +19,9 @@
use crate::{
CliConfiguration, error, params::{ImportParams, SharedParams, BlockNumberOrHash},
};
use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt::Debug, str::FromStr};
use sc_client_api::{BlockBackend, UsageProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::{fmt::Debug, str::FromStr, sync::Arc};
use structopt::StructOpt;
/// The `check-block` command used to validate blocks.
@@ -48,21 +48,21 @@ pub struct CheckBlockCmd {
impl CheckBlockCmd {
/// Run the check-block command
pub async fn run<B, BC, BB>(
pub async fn run<B, C, IQ>(
&self,
config: Configuration,
builder: B,
client: Arc<C>,
import_queue: IQ,
) -> error::Result<()>
where
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: BlockT + Debug,
<NumberFor<BB> as FromStr>::Err: std::fmt::Debug,
BB::Hash: FromStr,
<BB::Hash as FromStr>::Err: std::fmt::Debug,
B: BlockT + for<'de> serde::Deserialize<'de>,
C: BlockBackend<B> + UsageProvider<B> + Send + Sync + 'static,
IQ: sc_service::ImportQueue<B> + 'static,
B::Hash: FromStr,
<B::Hash as FromStr>::Err: Debug,
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
let start = std::time::Instant::now();
builder(config)?.check_block(self.input.parse()?).await?;
sc_service::chain_ops::check_block(client, import_queue, self.input.parse()?).await?;
println!("Completed in {} ms.", start.elapsed().as_millis());
Ok(())
@@ -21,13 +21,16 @@ use crate::params::{BlockNumber, DatabaseParams, PruningParams, SharedParams};
use crate::CliConfiguration;
use log::info;
use sc_service::{
config::DatabaseConfig, Configuration, ServiceBuilderCommand,
config::DatabaseConfig, chain_ops::export_blocks,
};
use sc_client_api::{BlockBackend, UsageProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use structopt::StructOpt;
/// The `export-blocks` command used to export blocks.
@@ -68,19 +71,17 @@ pub struct ExportBlocksCmd {
impl ExportBlocksCmd {
/// Run the export-blocks command
pub async fn run<B, BC, BB>(
pub async fn run<B, C>(
&self,
config: Configuration,
builder: B,
client: Arc<C>,
database_config: DatabaseConfig,
) -> error::Result<()>
where
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: sp_runtime::traits::Block + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
<BB as BlockT>::Hash: std::str::FromStr,
B: BlockT,
C: BlockBackend<B> + UsageProvider<B> + 'static,
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
if let DatabaseConfig::RocksDb { ref path, .. } = &config.database {
if let DatabaseConfig::RocksDb { ref path, .. } = database_config {
info!("DB path: {}", path.display());
}
@@ -94,8 +95,7 @@ impl ExportBlocksCmd {
None => Box::new(io::stdout()),
};
builder(config)?
.export_blocks(file, from.into(), to, binary)
export_blocks(client, file, from.into(), to, binary)
.await
.map_err(Into::into)
}
@@ -20,10 +20,10 @@ use crate::{
CliConfiguration, error, params::{PruningParams, SharedParams, BlockNumberOrHash},
};
use log::info;
use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt::Debug, str::FromStr, io::Write};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::{fmt::Debug, str::FromStr, io::Write, sync::Arc};
use structopt::StructOpt;
use sc_client_api::{StorageProvider, UsageProvider};
/// The `export-state` command used to export the state of a given block into
/// a chain spec.
@@ -44,23 +44,22 @@ pub struct ExportStateCmd {
impl ExportStateCmd {
/// Run the `export-state` command
pub fn run<B, BC, BB>(
pub async fn run<B, BA, C>(
&self,
config: Configuration,
builder: B,
client: Arc<C>,
mut input_spec: Box<dyn sc_service::ChainSpec>,
) -> error::Result<()>
where
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: BlockT + Debug,
<NumberFor<BB> as FromStr>::Err: std::fmt::Debug,
BB::Hash: FromStr,
<BB::Hash as FromStr>::Err: std::fmt::Debug,
B: BlockT,
C: UsageProvider<B> + StorageProvider<B, BA>,
BA: sc_client_api::backend::Backend<B>,
B::Hash: FromStr,
<B::Hash as FromStr>::Err: Debug,
<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
info!("Exporting raw state...");
let mut input_spec = config.chain_spec.cloned_box();
let block_id = self.input.as_ref().map(|b| b.parse()).transpose()?;
let raw_state = builder(config)?.export_raw_state(block_id)?;
let raw_state = sc_service::chain_ops::export_raw_state(client, block_id)?;
input_spec.set_storage(raw_state);
info!("Generating new chain spec...");
@@ -20,13 +20,15 @@ use crate::error;
use crate::params::ImportParams;
use crate::params::SharedParams;
use crate::CliConfiguration;
use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sc_service::chain_ops::import_blocks;
use sp_runtime::traits::Block as BlockT;
use std::fmt::Debug;
use std::fs;
use std::io::{self, Read, Seek};
use std::path::PathBuf;
use std::sync::Arc;
use structopt::StructOpt;
use sc_client_api::UsageProvider;
/// The `import-blocks` command used to import blocks.
#[derive(Debug, StructOpt)]
@@ -61,17 +63,15 @@ impl<T: Read + Seek> ReadPlusSeek for T {}
impl ImportBlocksCmd {
/// Run the import-blocks command
pub async fn run<B, BC, BB>(
pub async fn run<B, C, IQ>(
&self,
config: Configuration,
builder: B,
client: Arc<C>,
import_queue: IQ,
) -> error::Result<()>
where
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: sp_runtime::traits::Block + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
<BB as BlockT>::Hash: std::str::FromStr,
C: UsageProvider<B> + Send + Sync + 'static,
B: BlockT + for<'de> serde::Deserialize<'de>,
IQ: sc_service::ImportQueue<B> + 'static,
{
let file: Box<dyn ReadPlusSeek + Send> = match &self.input {
Some(filename) => Box::new(fs::File::open(filename)?),
@@ -82,8 +82,7 @@ impl ImportBlocksCmd {
}
};
builder(config)?
.import_blocks(file, false, self.binary)
import_blocks(client, import_queue, file, false, self.binary)
.await
.map_err(Into::into)
}
@@ -19,7 +19,7 @@
use crate::error;
use crate::params::{DatabaseParams, SharedParams};
use crate::CliConfiguration;
use sc_service::Configuration;
use sc_service::DatabaseConfig;
use std::fmt::Debug;
use std::fs;
use std::io::{self, Write};
@@ -43,8 +43,8 @@ pub struct PurgeChainCmd {
impl PurgeChainCmd {
/// Run the purge command
pub fn run(&self, config: Configuration) -> error::Result<()> {
let db_path = config.database.path()
pub fn run(&self, database_config: DatabaseConfig) -> error::Result<()> {
let db_path = database_config.path()
.ok_or_else(||
error::Error::Input("Cannot purge custom database implementation".into())
)?;
@@ -19,10 +19,13 @@
use crate::error;
use crate::params::{BlockNumber, PruningParams, SharedParams};
use crate::CliConfiguration;
use sc_service::{Configuration, ServiceBuilderCommand};
use sc_service::chain_ops::revert_chain;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;
use std::str::FromStr;
use std::sync::Arc;
use structopt::StructOpt;
use sc_client_api::{Backend, UsageProvider};
/// The `revert` command used revert the chain to a previous state.
#[derive(Debug, StructOpt)]
@@ -42,16 +45,19 @@ pub struct RevertCmd {
impl RevertCmd {
/// Run the revert command
pub fn run<B, BC, BB>(&self, config: Configuration, builder: B) -> error::Result<()>
pub async fn run<B, BA, C>(
&self,
client: Arc<C>,
backend: Arc<BA>,
) -> error::Result<()>
where
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
BC: ServiceBuilderCommand<Block = BB> + Unpin,
BB: sp_runtime::traits::Block + Debug,
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
<BB as BlockT>::Hash: std::str::FromStr,
B: BlockT,
BA: Backend<B>,
C: UsageProvider<B>,
<<<B as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
{
let blocks = self.num.parse()?;
builder(config)?.revert_chain(blocks)?;
revert_chain(client, backend, blocks)?;
Ok(())
}