Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
Forked at: 53e8ec2
Parent branch: origin/cecton-the-revenge-of-the-cli
  • Loading branch information
cecton committed Apr 7, 2020
commit dca7deef98706db4d6cd0ded15471b3f976e0d6c
21 changes: 11 additions & 10 deletions client/cli/src/commands/check_block_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ use crate::error;
use crate::params::ImportParams;
use crate::params::SharedParams;
use crate::CliConfiguration;
use sc_service::{Configuration, ServiceBuilderCommand};
//use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;
use std::str::FromStr;
use structopt::StructOpt;
use sc_service::check_block;

/// The `check-block` command used to validate blocks.
#[derive(Debug, StructOpt, Clone)]
Expand All @@ -49,17 +50,17 @@ pub struct CheckBlockCmd {

impl CheckBlockCmd {
/// Run the check-block command
pub async fn run<B, BC, BB>(
pub async fn run<B, BA, CE, IQ>(
&self,
config: Configuration,
builder: B,
client: std::sync::Arc<sc_service::Client<BA, CE, B, ()>>,
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,
B: BlockT,
BA: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
IQ: sc_service::ImportQueue<B> + Sync + 'static,
<B as sp_runtime::traits::Block>::Hash: std::str::FromStr,
{
let input = if self.input.starts_with("0x") {
&self.input[2..]
Expand All @@ -79,7 +80,7 @@ impl CheckBlockCmd {
};

let start = std::time::Instant::now();
builder(config)?.check_block(block_id).await?;
check_block(client, import_queue, block_id).await?;
println!("Completed in {} ms.", start.elapsed().as_millis());

Ok(())
Expand Down
23 changes: 12 additions & 11 deletions client/cli/src/commands/export_blocks_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ use crate::params::{BlockNumber, PruningParams, SharedParams};
use crate::CliConfiguration;
use log::info;
use sc_service::{
config::DatabaseConfig, Configuration, ServiceBuilderCommand,
config::DatabaseConfig, //Configuration, ServiceBuilderCommand,
};
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 structopt::StructOpt;
use sc_service::export_blocks;

/// The `export-blocks` command used to export blocks.
#[derive(Debug, StructOpt, Clone)]
Expand Down Expand Up @@ -62,21 +63,22 @@ pub struct ExportBlocksCmd {

impl ExportBlocksCmd {
/// Run the export-blocks command
pub async fn run<B, BC, BB>(
pub async fn run<B, BA, CE>(
&self,
config: Configuration,
builder: B,
client: std::sync::Arc<sc_service::Client<BA, CE, B, ()>>,
) -> 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: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
<<<B as sp_runtime::traits::Block>::Header as sp_runtime::traits::Header>::Number as std::str::FromStr>::Err: std::fmt::Debug,
{
// TODO: should probably not be here
/*
if let DatabaseConfig::Path { ref path, .. } = &config.database {
info!("DB path: {}", path.display());
}
*/

let from = self.from.as_ref().and_then(|f| f.parse().ok()).unwrap_or(1);
let to = self.to.as_ref().and_then(|t| t.parse().ok());
Expand All @@ -88,8 +90,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)
}
Expand Down
21 changes: 10 additions & 11 deletions client/cli/src/commands/import_blocks_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use crate::error;
use crate::params::ImportParams;
use crate::params::SharedParams;
use crate::CliConfiguration;
use sc_service::{Configuration, ServiceBuilderCommand};
//use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;
use std::fs;
use std::io::{self, Read, Seek};
use std::path::PathBuf;
use structopt::StructOpt;
use sc_service::import_blocks;

/// The `import-blocks` command used to import blocks.
#[derive(Debug, StructOpt, Clone)]
Expand Down Expand Up @@ -55,17 +56,16 @@ 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, BA, CE, IQ>(
&self,
config: Configuration,
builder: B,
client: std::sync::Arc<sc_service::Client<BA, CE, B, ()>>,
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,
B: BlockT,
BA: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
IQ: sc_service::ImportQueue<B> + Sync + 'static,
{
let file: Box<dyn ReadPlusSeek + Send> = match &self.input {
Some(filename) => Box::new(fs::File::open(filename)?),
Expand All @@ -76,8 +76,7 @@ impl ImportBlocksCmd {
}
};

builder(config)?
.import_blocks(file, false)
import_blocks(client, import_queue, file, false)
.await
.map_err(Into::into)
}
Expand Down
16 changes: 8 additions & 8 deletions client/cli/src/commands/revert_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
use crate::error;
use crate::params::{BlockNumber, PruningParams, SharedParams};
use crate::CliConfiguration;
use sc_service::{Configuration, ServiceBuilderCommand};
//use sc_service::{Configuration, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::fmt::Debug;
use structopt::StructOpt;
use sc_service::revert_chain;

/// The `revert` command used revert the chain to a previous state.
#[derive(Debug, StructOpt, Clone)]
Expand All @@ -40,16 +41,15 @@ pub struct RevertCmd {

impl RevertCmd {
/// Run the revert command
pub fn run<B, BC, BB>(&self, config: Configuration, builder: B) -> error::Result<()>
pub fn run<B, BA, CE>(&self, client: std::sync::Arc<sc_service::Client<BA, CE, B, ()>>) -> 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: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
<<<B as sp_runtime::traits::Block>::Header as sp_runtime::traits::Header>::Number as std::str::FromStr>::Err: std::fmt::Debug,
{
let blocks = self.num.parse()?;
builder(config)?.revert_chain(blocks)?;
revert_chain(client, blocks)?;

Ok(())
}
Expand Down
31 changes: 18 additions & 13 deletions client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use futures::pin_mut;
use futures::select;
use futures::{future, future::FutureExt, Future};
use log::info;
use sc_service::{AbstractService, Configuration, Role, ServiceBuilderCommand};
use sc_service::{AbstractService, Configuration, Role}; //, ServiceBuilderCommand};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL};
use std::fmt::Debug;
Expand Down Expand Up @@ -161,27 +161,32 @@ 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, CE, IQ>(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 std::str::FromStr>::Err: Debug,
<BB as BlockT>::Hash: std::str::FromStr,
BU: FnOnce(Configuration) -> sc_service::error::Result<(std::sync::Arc<sc_service::Client<BA, CE, B, ()>>, IQ)>,
B: BlockT,
BA: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
IQ: sc_service::ImportQueue<B> + Sync + 'static,
<B as sp_runtime::traits::Block>::Hash: std::str::FromStr,
<<<B as sp_runtime::traits::Block>::Header as sp_runtime::traits::Header>::Number as std::str::FromStr>::Err: std::fmt::Debug,
{
let (client, mut import_queue) = builder(self.config)?;

match subcommand {
Subcommand::BuildSpec(cmd) => cmd.run(self.config),
//Subcommand::BuildSpec(cmd) => cmd.run(client),
Subcommand::ExportBlocks(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
run_until_exit(self.tokio_runtime, cmd.run(client))
}
Subcommand::ImportBlocks(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
run_until_exit(self.tokio_runtime, cmd.run(client, import_queue))
}
Subcommand::CheckBlock(cmd) => {
run_until_exit(self.tokio_runtime, cmd.run(self.config, builder))
run_until_exit(self.tokio_runtime, cmd.run(client, import_queue))
}
Subcommand::Revert(cmd) => cmd.run(self.config, builder),
Subcommand::PurgeChain(cmd) => cmd.run(self.config),
Subcommand::Revert(cmd) => cmd.run(client),
//Subcommand::PurgeChain(cmd) => cmd.run(client),
_ => todo!(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/service/src/check_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::import_blocks::import_blocks;

pub fn check_block<B, BA, CE, IQ>(
client: Arc<Client<BA, CE, B, ()>>,
import_queue: &'static mut IQ,
import_queue: IQ,
block_id: BlockId<B>
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
where
Expand Down
3 changes: 1 addition & 2 deletions client/service/src/export_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::{io::{Read, Write, Seek}, pin::Pin};
use sc_client_api::BlockBackend;
use std::sync::Arc;

pub fn export_blocks<B, BA, CE, IQ>(
pub fn export_blocks<B, BA, CE>(
client: Arc<Client<BA, CE, B, ()>>,
mut output: impl Write + 'static,
from: NumberFor<B>,
Expand All @@ -51,7 +51,6 @@ where
B: BlockT,
BA: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
IQ: ImportQueue<B> + Sync + 'static,
{
let client = client;
let mut block = from;
Expand Down
2 changes: 1 addition & 1 deletion client/service/src/import_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::sync::Arc;

pub fn import_blocks<B, BA, CE, IQ>(
client: Arc<Client<BA, CE, B, ()>>,
import_queue: &'static mut IQ,
import_queue: IQ,
input: impl Read + Seek + Send + 'static,
force: bool,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
Expand Down
7 changes: 6 additions & 1 deletion client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ mod check_block;
mod import_blocks;
mod export_blocks;

pub use revert_chain::*;
pub use check_block::*;
pub use import_blocks::*;
pub use export_blocks::*;
pub use sp_consensus::import_queue::ImportQueue;
use std::{borrow::Cow, io, pin::Pin};
use std::marker::PhantomData;
use std::net::SocketAddr;
Expand All @@ -42,7 +47,7 @@ use wasm_timer::Instant;
use std::task::{Poll, Context};
use parking_lot::Mutex;

use sc_client::Client;
pub use sc_client::Client;
use futures::{
Future, FutureExt, Stream, StreamExt,
compat::*,
Expand Down
3 changes: 1 addition & 2 deletions client/service/src/revert_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ use std::{io::{Read, Write, Seek}, pin::Pin};
use sc_client_api::BlockBackend;
use std::sync::Arc;

pub fn revert_chain<B, BA, CE, IQ>(
pub fn revert_chain<B, BA, CE>(
client: Arc<Client<BA, CE, B, ()>>,
blocks: NumberFor<B>
) -> Result<(), Error>
where
B: BlockT,
BA: sc_client_api::backend::Backend<B> + 'static,
CE: sc_client_api::call_executor::CallExecutor<B> + Send + Sync + 'static,
IQ: ImportQueue<B> + Sync + 'static,
{
let reverted = client.revert(blocks)?;
let info = client.chain_info();
Expand Down