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 all commits
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
9 changes: 3 additions & 6 deletions core/cli/src/informant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ use futures::{Future, Stream};
use futures03::{StreamExt as _, TryStreamExt as _};
use log::{info, warn};
use sr_primitives::{generic::BlockId, traits::Header};
use service::{Service, Components};
use service::AbstractService;
use tokio::runtime::TaskExecutor;

mod display;

/// Spawn informant on the event loop
#[deprecated(note = "Please use informant::build instead, and then create the task manually")]
pub fn start<C>(service: &Service<C>, exit: ::exit_future::Exit, handle: TaskExecutor) where
C: Components,
{
pub fn start(service: &impl AbstractService, exit: ::exit_future::Exit, handle: TaskExecutor) {
handle.spawn(exit.until(build(service)).map(|_| ()));
}

/// Creates an informant in the form of a `Future` that must be polled regularly.
pub fn build<C>(service: &Service<C>) -> impl Future<Item = (), Error = ()>
where C: Components {
pub fn build(service: &impl AbstractService) -> impl Future<Item = (), Error = ()> {
let client = service.client();

let mut display = display::InformantDisplay::new();
Expand Down
87 changes: 85 additions & 2 deletions core/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub mod informant;

use client::ExecutionStrategies;
use service::{
config::Configuration,
ServiceFactory, FactoryFullConfiguration, RuntimeGenesis,
config::Configuration, ServiceFactory,
ServiceBuilderExport, ServiceBuilderImport, ServiceBuilderRevert,
FactoryFullConfiguration, RuntimeGenesis,
FactoryGenesis, PruningMode, ChainSpec,
};
use network::{
Expand Down Expand Up @@ -342,6 +343,36 @@ impl<'a> ParseAndPrepareExport<'a> {
config, exit.into_exit(), file, from.into(), to.map(Into::into), json
).map_err(Into::into)
}

/// Runs the command and exports from the chain.
pub fn run_with_builder<C, G, F, B, S, E>(
self,
builder: F,
spec_factory: S,
exit: E,
) -> error::Result<()>
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
B: ServiceBuilderExport,
C: Default,
G: RuntimeGenesis,
E: IntoExit
{
let config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;

info!("DB path: {}", config.database_path.display());
let from = self.params.from.unwrap_or(1);
let to = self.params.to;
let json = self.params.json;

let file: Box<dyn Write> = match self.params.output {
Some(filename) => Box::new(File::create(filename)?),
None => Box::new(stdout()),
};

builder(config)?.export_blocks(exit.into_exit(), file, from.into(), to.map(Into::into), json)?;
Ok(())
}
}

/// Command ready to import the chain.
Expand Down Expand Up @@ -381,6 +412,41 @@ impl<'a> ParseAndPrepareImport<'a> {
tokio::run(fut);
Ok(())
}

/// Runs the command and imports to the chain.
pub fn run_with_builder<C, G, F, B, S, E>(
self,
builder: F,
spec_factory: S,
exit: E,
) -> error::Result<()>
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
B: ServiceBuilderImport,
C: Default,
G: RuntimeGenesis,
E: IntoExit
{
let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
config.execution_strategies = ExecutionStrategies {
importing: self.params.execution.into(),
other: self.params.execution.into(),
..Default::default()
};

let file: Box<dyn ReadPlusSeek> = match self.params.input {
Some(filename) => Box::new(File::open(filename)?),
None => {
let mut buffer = Vec::new();
stdin().read_to_end(&mut buffer)?;
Box::new(Cursor::new(buffer))
},
};

let fut = builder(config)?.import_blocks(exit.into_exit(), file)?;
tokio::run(fut);
Ok(())
}
}

/// Command ready to purge the chain.
Expand Down Expand Up @@ -450,6 +516,23 @@ impl<'a> ParseAndPrepareRevert<'a> {
let blocks = self.params.num;
Ok(service::chain_ops::revert_chain::<F>(config, blocks.into())?)
}

/// Runs the command and reverts the chain.
pub fn run_with_builder<C, G, F, B, S>(
self,
builder: F,
spec_factory: S
) -> error::Result<()>
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
B: ServiceBuilderRevert,
C: Default,
G: RuntimeGenesis {
let config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
let blocks = self.params.num;
builder(config)?.revert_chain(blocks.into())?;
Ok(())
}
}

/// Parse command line interface arguments and executes the desired command.
Expand Down
Loading