Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

CLI API refactoring and improvement #4692

Merged
merged 102 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
5721bab
Expose a method that allows converting RunCmd to Configuration
cecton Jan 13, 2020
e533791
WIP
cecton Jan 13, 2020
ecaeb7f
WIP
cecton Jan 13, 2020
f6c249b
WIP
cecton Jan 13, 2020
9405756
WIP
cecton Jan 15, 2020
99159d2
WIP
cecton Jan 20, 2020
904b292
WIP
cecton Jan 20, 2020
5c684a0
WIP
cecton Jan 20, 2020
85b2d11
WIP
cecton Jan 20, 2020
c8581ad
WIP
cecton Jan 20, 2020
1a9c594
WIP
cecton Jan 20, 2020
dae1972
WIP
cecton Jan 20, 2020
214076d
WIP
cecton Jan 20, 2020
f98c3a4
WIP
cecton Jan 20, 2020
963d33a
WIP
cecton Jan 20, 2020
3a1bc54
WIP
cecton Jan 20, 2020
fda5a4c
WIP
cecton Jan 20, 2020
4eb3213
WIP
cecton Jan 21, 2020
b67f970
WIP
cecton Jan 21, 2020
ca93dc7
WIP
cecton Jan 21, 2020
1025061
WIP
cecton Jan 22, 2020
6f6a8fb
Update bin/node-template/src/cli.rs
cecton Jan 21, 2020
ef4dd6a
WIP
cecton Jan 22, 2020
cd4fbb5
WIP
cecton Jan 22, 2020
a7d14f4
WIP
cecton Jan 22, 2020
165631c
WIP
cecton Jan 22, 2020
d17ed47
WIP
cecton Jan 22, 2020
82b85e6
Merge commit '410ce114e8fb37e5807606ed77fec80393042435^' into cecton-…
cecton Jan 23, 2020
8c8c962
Merge commit '410ce114e8fb37e5807606ed77fec80393042435' into cecton-c…
cecton Jan 23, 2020
f310371
Merge commit 'c2b0c0f8395165f4fc5ee6b9ca8755cc9dd0d41f^' into cecton-…
cecton Jan 23, 2020
b7f066e
Merge commit 'c2b0c0f8395165f4fc5ee6b9ca8755cc9dd0d41f' into cecton-c…
cecton Jan 23, 2020
2614c31
Merge commit '4c4ee3d5b91c4c825aef4fde2b096f014a89346a^' into cecton-…
cecton Jan 23, 2020
1fe0c47
Merge commit '4c4ee3d5b91c4c825aef4fde2b096f014a89346a' into cecton-c…
cecton Jan 23, 2020
0e81c21
Merge commit '3fa5f09dd1df584c1fa9de9c870e6cd5d44854f4' into cecton-c…
cecton Jan 23, 2020
1983f96
Merge commit '47c36e6efe8285c9a6cbbcc0f588e3e7c4009de5' into cecton-c…
cecton Jan 23, 2020
ec95f22
Merge commit '879e28ae38643c028fa09c157e7b2714bcbf02a1' into cecton-c…
cecton Jan 23, 2020
6d70474
Merge commit '083ada5c01ffbce9dbe763d4757ceba3ed1027b4' into cecton-c…
cecton Jan 23, 2020
93b8467
Merge commit '8de5340c42e5e4638e63285f93cebaa1cca5ea46' into cecton-c…
cecton Jan 23, 2020
96e6e31
Merge commit 'cfbb24cbdbc84410624a84daec55bfe178c7549f' into cecton-c…
cecton Jan 23, 2020
2c70e27
Merge commit 'cb9c1818f198b946b402652d173bbd76e8fbe7d0' into cecton-c…
cecton Jan 23, 2020
457a2c6
Merge commit '6ee1244e2d018333746d82131222308e0d802e6a^' into cecton-…
cecton Jan 23, 2020
7a1fffc
Merge commit '6ee1244e2d018333746d82131222308e0d802e6a' into cecton-c…
cecton Jan 23, 2020
f25f218
Merge commit '4b2f70f8476a8c4092df952e549090268f936d1d' into cecton-c…
cecton Jan 23, 2020
1e5e187
Merge commit '6c3b86d16ad19751fbbab86e369883b5425c72df' into cecton-c…
cecton Jan 23, 2020
33e9314
fixed node-template
cecton Jan 23, 2020
c1dd237
WIP
cecton Jan 23, 2020
7c14ccf
WIP
cecton Jan 23, 2020
fee08c4
WIP
cecton Jan 23, 2020
4a0e3ee
Merge remote-tracking branch 'origin/master' into cecton-cli-refactoring
cecton Jan 23, 2020
4bf2cfb
WIP
cecton Jan 23, 2020
8c85245
WIP
cecton Jan 23, 2020
5ee6197
WIP
cecton Jan 23, 2020
c69956e
WIP
cecton Jan 23, 2020
93fd4ce
WIP
cecton Jan 23, 2020
58b8db2
Update client/cli/src/lib.rs
cecton Jan 27, 2020
4ab4bf2
WIP
cecton Jan 27, 2020
cc7e684
Update client/cli/src/lib.rs
cecton Jan 27, 2020
7ffc8e5
WIP
cecton Jan 27, 2020
63b7735
WIP
cecton Jan 27, 2020
bc28887
WIP
cecton Jan 27, 2020
891eb19
WIP
cecton Jan 27, 2020
f75e03b
WIP
cecton Jan 27, 2020
bd596df
WIP
cecton Jan 27, 2020
9eb2e4f
Merge remote-tracking branch 'origin/master' into cecton-cli-refactoring
cecton Jan 27, 2020
6dacdbf
WIP
cecton Jan 27, 2020
d2564b7
Rename tasks_executor to task_executor
cecton Jan 27, 2020
77f3db8
WIP
cecton Jan 27, 2020
8bc1914
WIP
cecton Jan 27, 2020
3e6b83a
Merge remote-tracking branch 'origin/master' into cecton-cli-refactoring
cecton Jan 28, 2020
1ad686c
Added conflict between --sentry and --light
cecton Jan 28, 2020
69072ba
WIP
cecton Jan 28, 2020
f1eae7d
WIP
cecton Jan 28, 2020
bea809d
WIP
cecton Jan 28, 2020
b6df525
WIP
cecton Jan 29, 2020
07e0310
WIP
cecton Jan 29, 2020
e99c674
WIP
cecton Jan 29, 2020
625933a
Merge commit '8f4495886a98d4a867d3f3813a8bf4dc6bca3a3f^' into cecton-…
cecton Jan 29, 2020
5a1c557
Merge commit '8f4495886a98d4a867d3f3813a8bf4dc6bca3a3f' into cecton-c…
cecton Jan 29, 2020
c3bb276
WIP
cecton Jan 29, 2020
8aedbd4
WIP
cecton Jan 29, 2020
2df875b
Merge commit '38c5ed0af7740d55993e91bdc9001cc17446180f' into cecton-c…
cecton Jan 29, 2020
35e1cd4
WIP
cecton Jan 29, 2020
031ca07
Update client/cli/src/runtime.rs
cecton Jan 29, 2020
ff986dc
WIP
cecton Jan 29, 2020
a0d4a54
Update client/cli/src/params.rs
cecton Jan 29, 2020
34cf930
Update client/cli/src/params.rs
cecton Jan 29, 2020
957a102
Update client/cli/src/params.rs
cecton Jan 29, 2020
f3d7b01
Update client/cli/src/params.rs
cecton Jan 29, 2020
292ec6e
Update client/cli/src/params.rs
cecton Jan 29, 2020
ec78b9c
Update client/cli/src/params.rs
cecton Jan 29, 2020
505f574
Update client/cli/src/params.rs
cecton Jan 29, 2020
3ffe543
Update client/cli/src/params.rs
cecton Jan 29, 2020
03cc75f
Update client/cli/src/params.rs
cecton Jan 29, 2020
d126cb5
Update client/cli/src/params.rs
cecton Jan 29, 2020
54ac33d
Update client/cli/src/params.rs
cecton Jan 29, 2020
4fd3edf
WIP
cecton Jan 29, 2020
6276b76
WIP
cecton Jan 29, 2020
6c62acc
Merge remote-tracking branch 'origin/master' into cecton-cli-refactoring
cecton Jan 29, 2020
e543ebe
Merge remote-tracking branch 'origin/master' into cecton-cli-refactoring
cecton Jan 30, 2020
6424b53
Update client/cli/src/lib.rs
cecton Jan 30, 2020
7774621
Update client/cli/src/lib.rs
cecton Jan 30, 2020
9c09d17
WIP
cecton Jan 30, 2020
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
101 changes: 84 additions & 17 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bin/node-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ path = "src/main.rs"
[dependencies]
futures = "0.3.1"
futures01 = { package = "futures", version = "0.1.29" }
ctrlc = { version = "3.1.3", features = ["termination"] }
log = "0.4.8"
tokio = "0.1.22"
parking_lot = "0.9.0"
Expand All @@ -36,6 +35,7 @@ sc-client = { version = "2.0.0", path = "../../client/" }
node-template-runtime = { version = "2.0.0", path = "runtime" }
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
sc-basic-authority = { path = "../../client/basic-authorship" }
structopt = "=0.3.8"
cecton marked this conversation as resolved.
Show resolved Hide resolved

[build-dependencies]
vergen = "3.0.4"
Expand Down
148 changes: 37 additions & 111 deletions bin/node-template/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,49 @@
use crate::service;
use futures::{future::{select, Map}, FutureExt, TryFutureExt, channel::oneshot, compat::Future01CompatExt};
use std::cell::RefCell;
use tokio::runtime::Runtime;
pub use sc_cli::{VersionInfo, IntoExit, error};
use sc_cli::{display_role, informant, parse_and_prepare, ParseAndPrepare, NoCustom};
pub use sc_cli::{VersionInfo, error};
use sc_cli::{CoreParams, RunCmd};
use sc_service::{AbstractService, Roles as ServiceRoles, Configuration};
use structopt::StructOpt;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use crate::chain_spec;
use log::info;

#[derive(Clone, Debug, StructOpt)]
#[structopt(settings = &[structopt::clap::AppSettings::GlobalVersion, structopt::clap::AppSettings::ArgsNegateSubcommands, structopt::clap::AppSettings::SubcommandsNegateReqs])]
cecton marked this conversation as resolved.
Show resolved Hide resolved
struct Cli {
#[structopt(subcommand)]
subcommand: Option<CoreParams>,
}

/// Parse command line arguments into service configuration.
pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()> where
I: IntoIterator<Item = T>,
pub fn run<I, T>(args: I, version: VersionInfo) -> error::Result<()>
where
I: Iterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
{
type Config<T> = Configuration<(), T>;
match parse_and_prepare::<NoCustom, NoCustom, _>(&version, "substrate-node", args) {
ParseAndPrepare::Run(cmd) => cmd.run(load_spec, exit,
|exit, _cli_args, _custom_args, config: Config<_>| {
info!("{}", version.name);
info!(" version {}", config.full_version());
info!(" by {}, 2017, 2018", version.author);
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {}", display_role(&config));
let runtime = Runtime::new().map_err(|e| format!("{:?}", e))?;
match config.roles {
ServiceRoles::LIGHT => run_until_exit(
runtime,
service::new_light(config)?,
exit
),
_ => run_until_exit(
runtime,
service::new_full(config)?,
exit
),
}
}),
ParseAndPrepare::BuildSpec(cmd) => cmd.run::<NoCustom, _, _, _>(load_spec),
ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::CheckBlock(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::PurgeChain(cmd) => cmd.run(load_spec),
ParseAndPrepare::RevertChain(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec),
ParseAndPrepare::CustomCommand(_) => Ok(())
}?;

Ok(())
type Config<A, B> = Configuration<A, B>;

let args: Vec<_> = args.collect();
let opt = sc_cli::from_iter::<Cli, _>(args.clone(), &version);
let subcommand = opt.subcommand.unwrap_or_else(|| {
let opt = sc_cli::from_iter::<RunCmd, _>(args, &version);

eprintln!(
cecton marked this conversation as resolved.
Show resolved Hide resolved
"WARNING: running this command without the subcommand `run` is deprecated, please \
use run:\n{} run [node_arguments]",
version.executable_name,
);
CoreParams::Run(opt)
});

sc_cli::run(
subcommand,
service::new_light,
service::new_full,
load_spec,
|config: Config<_, _>| Ok(new_full_start!(config).0),
"substrate-node",
cecton marked this conversation as resolved.
Show resolved Hide resolved
&version,
)
}

fn load_spec(id: &str) -> Result<Option<chain_spec::ChainSpec>, String> {
Expand All @@ -61,68 +52,3 @@ fn load_spec(id: &str) -> Result<Option<chain_spec::ChainSpec>, String> {
None => None,
})
}

fn run_until_exit<T, E>(
mut runtime: Runtime,
service: T,
e: E,
) -> error::Result<()>
where
T: AbstractService,
E: IntoExit,
{
let (exit_send, exit) = oneshot::channel();

let informant = informant::build(&service);

let future = select(exit, informant)
.map(|_| Ok(()))
.compat();

runtime.executor().spawn(future);

// we eagerly drop the service so that the internal exit future is fired,
// but we need to keep holding a reference to the global telemetry guard
let _telemetry = service.telemetry();

let service_res = {
let exit = e.into_exit();
let service = service
.map_err(|err| error::Error::Service(err))
.compat();
let select = select(service, exit)
.map(|_| Ok(()))
.compat();
runtime.block_on(select)
};

let _ = exit_send.send(());

// TODO [andre]: timeout this future #1318

use futures01::Future;

let _ = runtime.shutdown_on_idle().wait();

service_res
}

// handles ctrl-c
pub struct Exit;
impl IntoExit for Exit {
type Exit = Map<oneshot::Receiver<()>, fn(Result<(), oneshot::Canceled>) -> ()>;
fn into_exit(self) -> Self::Exit {
// can't use signal directly here because CtrlC takes only `Fn`.
let (exit_send, exit) = oneshot::channel();

let exit_send_cell = RefCell::new(Some(exit_send));
ctrlc::set_handler(move || {
let exit_send = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take();
if let Some(exit_send) = exit_send {
exit_send.send(()).expect("Error sending exit notification");
}
}).expect("Error setting Ctrl-C handler");

exit.map(drop)
}
}
4 changes: 2 additions & 2 deletions bin/node-template/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod chain_spec;
mod service;
mod cli;

pub use sc_cli::{VersionInfo, IntoExit, error};
pub use sc_cli::{VersionInfo, error};

fn main() -> Result<(), cli::error::Error> {
let version = VersionInfo {
Expand All @@ -21,5 +21,5 @@ fn main() -> Result<(), cli::error::Error> {
support_url: "support.anonymous.an",
};

cli::run(std::env::args(), cli::Exit, version)
cli::run(std::env::args(), version)
}
4 changes: 2 additions & 2 deletions bin/node-template/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ macro_rules! new_full_start {
}

/// Builds a new service for a full client.
pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisConfig>)
pub fn new_full(config: Configuration<GenesisConfig>)
-> Result<impl AbstractService, ServiceError>
{
let is_authority = config.roles.is_authority();
Expand Down Expand Up @@ -195,7 +195,7 @@ pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisCon
}

/// Builds a new service for a light client.
pub fn new_light<C: Send + Default + 'static>(config: Configuration<C, GenesisConfig>)
pub fn new_light(config: Configuration<GenesisConfig>)
-> Result<impl AbstractService, ServiceError>
{
let inherent_data_providers = InherentDataProviders::new();
Expand Down
6 changes: 2 additions & 4 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hex-literal = "0.2.1"
jsonrpc-core = "14.0.3"
log = "0.4.8"
rand = "0.7.2"
structopt = "=0.3.7"
structopt = "=0.3.8"

# primitives
sp-authority-discovery = { version = "2.0.0", path = "../../../primitives/authority-discovery" }
Expand Down Expand Up @@ -83,7 +83,6 @@ node-executor = { version = "2.0.0", path = "../executor" }
# CLI-specific dependencies
tokio = { version = "0.1.22", optional = true }
sc-cli = { version = "2.0.0", optional = true, path = "../../../client/cli" }
ctrlc = { version = "3.1.3", features = ["termination"], optional = true }
node-transaction-factory = { version = "2.0.0", optional = true, path = "../transaction-factory" }

# WASM-specific dependencies
Expand All @@ -101,7 +100,7 @@ tempfile = "3.1.0"
[build-dependencies]
sc-cli = { version = "2.0.0", package = "sc-cli", path = "../../../client/cli" }
build-script-utils = { version = "2.0.0", package = "substrate-build-script-utils", path = "../../../utils/build-script-utils" }
structopt = "=0.3.7"
structopt = "=0.3.8"
vergen = "3.0.4"

[features]
Expand All @@ -115,7 +114,6 @@ cli = [
"sc-cli",
"node-transaction-factory",
"tokio",
"ctrlc",
"sc-service/rocksdb",
"node-executor/wasmi-errno",
]
Expand Down
23 changes: 1 addition & 22 deletions bin/node/cli/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@ use futures::channel::oneshot;
use futures::{future, FutureExt};
use sc_cli::VersionInfo;

use std::cell::RefCell;

// handles ctrl-c
struct Exit;
impl sc_cli::IntoExit for Exit {
type Exit = future::Map<oneshot::Receiver<()>, fn(Result<(), oneshot::Canceled>) -> ()>;
fn into_exit(self) -> Self::Exit {
// can't use signal directly here because CtrlC takes only `Fn`.
let (exit_send, exit) = oneshot::channel();

let exit_send_cell = RefCell::new(Some(exit_send));
ctrlc::set_handler(move || {
if let Some(exit_send) = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take() {
exit_send.send(()).expect("Error sending exit notification");
}
}).expect("Error setting Ctrl-C handler");

exit.map(|_| ())
}
}

fn main() -> Result<(), sc_cli::error::Error> {
let version = VersionInfo {
name: "Substrate Node",
Expand All @@ -54,5 +33,5 @@ fn main() -> Result<(), sc_cli::error::Error> {
support_url: "https://github.com/paritytech/substrate/issues/new",
};

node_cli::run(std::env::args(), Exit, version)
node_cli::run(std::env::args(), version)
}
4 changes: 2 additions & 2 deletions bin/node/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::{fs, env, path::Path};
use structopt::{StructOpt, clap::Shell};
use sc_cli::{NoCustom, CoreParams};
use sc_cli::CoreParams;
use vergen::{ConstantsFlags, generate_cargo_keys};

fn main() {
Expand Down Expand Up @@ -48,5 +48,5 @@ fn build_completion(shell: &Shell) {

fs::create_dir(&path).ok();

CoreParams::<NoCustom, NoCustom>::clap().gen_completions("substrate-node", *shell, &path);
CoreParams::clap().gen_completions("substrate-node", *shell, &path);
}
Loading