Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
10 changes: 6 additions & 4 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sc_cli::{IntoExit, NoCustom, SharedParams, ImportParams, error};
use sc_service::{AbstractService, Roles as ServiceRoles, Configuration};
use log::info;
use structopt::{StructOpt, clap::App};
use sc_cli::{display_role, parse_and_prepare, AugmentClap, GetLogFilter, ParseAndPrepare};
use sc_cli::{display_role, parse_and_prepare, AugmentClap, GetSharedParams, ParseAndPrepare};
use crate::{service, ChainSpec, load_spec};
use crate::factory_impl::FactoryState;
use node_transaction_factory::RuntimeAdapter;
Expand All @@ -38,9 +38,11 @@ pub enum CustomSubcommands {
Factory(FactoryCmd),
}

impl GetLogFilter for CustomSubcommands {
fn get_log_filter(&self) -> Option<String> {
None
impl GetSharedParams for CustomSubcommands {
fn shared_params(&self) -> Option<&SharedParams> {
match self {
CustomSubcommands::Factory(cmd) => Some(&cmd.shared_params),
}
}
}

Expand Down
27 changes: 22 additions & 5 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use params::{
NodeKeyParams, NodeKeyType, Cors, CheckBlockCmd,
};
pub use params::{NoCustom, CoreParams, SharedParams, ImportParams, ExecutionStrategy};
pub use traits::{GetLogFilter, AugmentClap};
pub use traits::{GetSharedParams, AugmentClap};
use app_dirs::{AppInfo, AppDataType};
use log::info;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn parse_and_prepare<'a, CC, RP, I>(
args: I,
) -> ParseAndPrepare<'a, CC, RP>
where
CC: StructOpt + Clone + GetLogFilter,
CC: StructOpt + Clone + GetSharedParams,
RP: StructOpt + Clone + AugmentClap,
I: IntoIterator,
<I as IntoIterator>::Item: Into<std::ffi::OsString> + Clone,
Expand All @@ -216,10 +216,9 @@ where
.setting(AppSettings::SubcommandsNegateReqs)
.get_matches_from(args);
let cli_args = CoreParams::<CC, RP>::from_clap(&matches);
init_logger(cli_args.get_log_filter().as_ref().map(|v| v.as_ref()).unwrap_or(""));
fdlimit::raise_fd_limit();

match cli_args {
let args = match cli_args {
params::CoreParams::Run(params) => ParseAndPrepare::Run(
ParseAndPrepareRun { params, impl_name, version }
),
Expand All @@ -242,7 +241,9 @@ where
ParseAndPrepareRevert { params, version }
),
params::CoreParams::Custom(params) => ParseAndPrepare::CustomCommand(params),
}
};
init_logger(args.shared_params().and_then(|p| p.log.as_ref()).map(|v| v.as_ref()).unwrap_or(""));
args
}

/// Returns a string displaying the node role, special casing the sentry mode
Expand Down Expand Up @@ -277,6 +278,22 @@ pub enum ParseAndPrepare<'a, CC, RP> {
CustomCommand(CC),
}

impl<'a, CC, RP> ParseAndPrepare<'a, CC, RP> where CC: GetSharedParams {
/// Return common set of parameters shared by all commands.
pub fn shared_params(&self) -> Option<&SharedParams> {
match self {
ParseAndPrepare::Run(c) => Some(&c.params.left.shared_params),
ParseAndPrepare::BuildSpec(c) => Some(&c.params.shared_params),
ParseAndPrepare::ExportBlocks(c) => Some(&c.params.shared_params),
ParseAndPrepare::ImportBlocks(c) => Some(&c.params.shared_params),
ParseAndPrepare::CheckBlock(c) => Some(&c.params.shared_params),
ParseAndPrepare::PurgeChain(c) => Some(&c.params.shared_params),
ParseAndPrepare::RevertChain(c) => Some(&c.params.shared_params),
ParseAndPrepare::CustomCommand(c) => c.shared_params(),
}
}
}

/// Command ready to run the main client.
pub struct ParseAndPrepareRun<'a, RP> {
params: MergeParameters<RunCmd, RP>,
Expand Down
53 changes: 4 additions & 49 deletions client/cli/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use crate::traits::{AugmentClap, GetLogFilter};
use crate::traits::{AugmentClap, GetSharedParams};

use std::{str::FromStr, path::PathBuf};
use structopt::{StructOpt, clap::{arg_enum, App, AppSettings, SubCommand, Arg}};

pub use crate::execution_strategy::ExecutionStrategy;

/// Auxiliary macro to implement `GetLogFilter` for all types that have the `shared_params` field.
macro_rules! impl_get_log_filter {
( $type:ident ) => {
impl $crate::GetLogFilter for $type {
fn get_log_filter(&self) -> Option<String> {
self.shared_params.get_log_filter()
}
}
}
}

impl Into<sc_client_api::ExecutionStrategy> for ExecutionStrategy {
fn into(self) -> sc_client_api::ExecutionStrategy {
match self {
Expand Down Expand Up @@ -153,12 +142,6 @@ pub struct ImportParams {
pub state_cache_size: usize,
}

impl GetLogFilter for SharedParams {
fn get_log_filter(&self) -> Option<String> {
self.log.clone()
}
}

/// Parameters used to create the network configuration.
#[derive(Debug, StructOpt, Clone)]
pub struct NetworkConfigurationParams {
Expand Down Expand Up @@ -723,7 +706,6 @@ fn parse_cors(s: &str) -> Result<Cors, Box<dyn std::error::Error>> {
}

impl_augment_clap!(RunCmd);
impl_get_log_filter!(RunCmd);

/// The `build-spec` command used to build a specification.
#[derive(Debug, StructOpt, Clone)]
Expand All @@ -748,8 +730,6 @@ pub struct BuildSpecCmd {
pub node_key_params: NodeKeyParams,
}

impl_get_log_filter!(BuildSpecCmd);

/// Wrapper type of `String` which holds an arbitary sized unsigned integer formatted as decimal.
#[derive(Debug, Clone)]
pub struct BlockNumber(String);
Expand Down Expand Up @@ -813,8 +793,6 @@ pub struct ExportBlocksCmd {
pub shared_params: SharedParams,
}

impl_get_log_filter!(ExportBlocksCmd);

/// The `import-blocks` command used to import blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct ImportBlocksCmd {
Expand All @@ -837,8 +815,6 @@ pub struct ImportBlocksCmd {
pub import_params: ImportParams,
}

impl_get_log_filter!(ImportBlocksCmd);

/// The `check-block` command used to validate blocks.
#[derive(Debug, StructOpt, Clone)]
pub struct CheckBlockCmd {
Expand All @@ -861,8 +837,6 @@ pub struct CheckBlockCmd {
pub import_params: ImportParams,
}

impl_get_log_filter!(CheckBlockCmd);

/// The `revert` command used revert the chain to a previous state.
#[derive(Debug, StructOpt, Clone)]
pub struct RevertCmd {
Expand All @@ -875,8 +849,6 @@ pub struct RevertCmd {
pub shared_params: SharedParams,
}

impl_get_log_filter!(RevertCmd);

/// The `purge-chain` command used to remove the whole chain.
#[derive(Debug, StructOpt, Clone)]
pub struct PurgeChainCmd {
Expand All @@ -889,8 +861,6 @@ pub struct PurgeChainCmd {
pub shared_params: SharedParams,
}

impl_get_log_filter!(PurgeChainCmd);

/// All core commands that are provided by default.
///
/// The core commands are split into multiple subcommands and `Run` is the default subcommand. From
Expand Down Expand Up @@ -924,7 +894,7 @@ pub enum CoreParams<CC, RP> {
}

impl<CC, RP> StructOpt for CoreParams<CC, RP> where
CC: StructOpt + GetLogFilter,
CC: StructOpt + GetSharedParams,
RP: StructOpt + AugmentClap
{
fn clap<'a, 'b>() -> App<'a, 'b> {
Expand Down Expand Up @@ -979,21 +949,6 @@ impl<CC, RP> StructOpt for CoreParams<CC, RP> where
}
}

impl<CC, RP> GetLogFilter for CoreParams<CC, RP> where CC: GetLogFilter {
fn get_log_filter(&self) -> Option<String> {
match self {
CoreParams::Run(c) => c.left.get_log_filter(),
CoreParams::BuildSpec(c) => c.get_log_filter(),
CoreParams::ExportBlocks(c) => c.get_log_filter(),
CoreParams::ImportBlocks(c) => c.get_log_filter(),
CoreParams::CheckBlock(c) => c.get_log_filter(),
CoreParams::PurgeChain(c) => c.get_log_filter(),
CoreParams::Revert(c) => c.get_log_filter(),
CoreParams::Custom(c) => c.get_log_filter(),
}
}
}

/// A special commandline parameter that expands to nothing.
/// Should be used as custom subcommand/run arguments if no custom values are required.
#[derive(Clone, Debug, Default)]
Expand All @@ -1015,8 +970,8 @@ impl AugmentClap for NoCustom {
}
}

impl GetLogFilter for NoCustom {
fn get_log_filter(&self) -> Option<String> {
impl GetSharedParams for NoCustom {
fn shared_params(&self) -> Option<&SharedParams> {
None
}
}
Expand Down
9 changes: 5 additions & 4 deletions client/cli/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use structopt::{StructOpt, clap::App};
use crate::params::SharedParams;

/// Something that can augment a clap app with further parameters.
/// `derive(StructOpt)` is implementing this function by default, so a macro `impl_augment_clap!`
Expand All @@ -37,8 +38,8 @@ macro_rules! impl_augment_clap {
}
}

/// Returns the log filter given by the user as commandline argument.
pub trait GetLogFilter {
/// Returns the set log filter.
fn get_log_filter(&self) -> Option<String>;
/// Supports getting common params.
pub trait GetSharedParams {
/// Returns shared params if any.
fn shared_params(&self) -> Option<&SharedParams>;
}
12 changes: 6 additions & 6 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,38 @@ pub struct ServiceBuilder<TBl, TRtApi, TCfg, TGen, TCSExt, TCl, TFchr, TSc, TImp
}

/// Full client type.
type TFullClient<TBl, TRtApi, TExecDisp> = Client<
pub type TFullClient<TBl, TRtApi, TExecDisp> = Client<
TFullBackend<TBl>,
TFullCallExecutor<TBl, TExecDisp>,
TBl,
TRtApi,
>;

/// Full client backend type.
type TFullBackend<TBl> = sc_client_db::Backend<TBl>;
pub type TFullBackend<TBl> = sc_client_db::Backend<TBl>;

/// Full client call executor type.
type TFullCallExecutor<TBl, TExecDisp> = sc_client::LocalCallExecutor<
pub type TFullCallExecutor<TBl, TExecDisp> = sc_client::LocalCallExecutor<
sc_client_db::Backend<TBl>,
NativeExecutor<TExecDisp>,
>;

/// Light client type.
type TLightClient<TBl, TRtApi, TExecDisp> = Client<
pub type TLightClient<TBl, TRtApi, TExecDisp> = Client<
TLightBackend<TBl>,
TLightCallExecutor<TBl, TExecDisp>,
TBl,
TRtApi,
>;

/// Light client backend type.
type TLightBackend<TBl> = sc_client::light::backend::Backend<
pub type TLightBackend<TBl> = sc_client::light::backend::Backend<
sc_client_db::light::LightStorage<TBl>,
Blake2Hasher,
>;

/// Light call executor type.
type TLightCallExecutor<TBl, TExecDisp> = sc_client::light::call_executor::GenesisCallExecutor<
pub type TLightCallExecutor<TBl, TExecDisp> = sc_client::light::call_executor::GenesisCallExecutor<
sc_client::light::backend::Backend<
sc_client_db::light::LightStorage<TBl>,
Blake2Hasher
Expand Down
5 changes: 4 additions & 1 deletion client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ use sp_runtime::generic::BlockId;
use sp_runtime::traits::{NumberFor, Block as BlockT};

pub use self::error::Error;
pub use self::builder::{ServiceBuilder, ServiceBuilderCommand};
pub use self::builder::{
ServiceBuilder, ServiceBuilderCommand, TFullClient, TLightClient, TFullBackend, TLightBackend,
TFullCallExecutor, TLightCallExecutor,
};
pub use config::{Configuration, Roles, PruningMode};
pub use sc_chain_spec::{ChainSpec, Properties, RuntimeGenesis, Extension as ChainSpecExtension};
pub use sp_transaction_pool::{TransactionPool, TransactionPoolMaintainer, InPoolTransaction, error::IntoPoolError};
Expand Down