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

Add tests & Service's Configuration has optional fields that shouldn't be optional #4842

Merged
merged 91 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
ff20e5e
Initial commit
cecton Feb 6, 2020
d0fc553
Moving out pub functions from sc_cli
cecton Feb 6, 2020
6683ebe
Update branch 'cecton-cli-service-configurations' from parent 'origin…
cecton Feb 7, 2020
1b6a9e8
Update branch 'cecton-cli-service-configurations' from parent 'origin…
cecton Feb 7, 2020
057d0fc
WIP
cecton Feb 7, 2020
b1dc8ae
WIP
cecton Feb 7, 2020
de4bd11
WIP
cecton Feb 7, 2020
2b70a8a
WIP
cecton Feb 7, 2020
562c8be
WIP
cecton Feb 7, 2020
8218473
WIP
cecton Feb 7, 2020
025b88f
WIP
cecton Feb 10, 2020
f4290e2
WIP
cecton Feb 10, 2020
f84cbc5
WIP
cecton Feb 10, 2020
58521d1
WIP
cecton Feb 10, 2020
8b618ac
Update branch 'cecton-cli-service-configurations' from parent 'origin…
cecton Feb 10, 2020
76185d4
Update branch 'cecton-cli-service-configurations' from parent 'origin…
cecton Feb 10, 2020
ce4bcbf
Update branch 'cecton-cli-service-configurations' from parent 'origin…
cecton Feb 10, 2020
c0d27a5
WIP
cecton Feb 11, 2020
0a6b34f
WIP
cecton Feb 11, 2020
7ccbf6d
WIP
cecton Feb 11, 2020
c6acedd
WIP
cecton Feb 11, 2020
d488431
WIP
cecton Feb 11, 2020
7a42b48
WIP
cecton Feb 11, 2020
0b46330
WIP
cecton Feb 11, 2020
faaf28d
WIP
cecton Feb 11, 2020
0c0d979
WIP
cecton Feb 11, 2020
4b12bfc
WIP
cecton Feb 11, 2020
a780282
Update from parent 'origin/master' (no conflict)
cecton Feb 11, 2020
9546034
Update from parent 'origin/master' (conflicts)
cecton Feb 11, 2020
af8e3ff
WIP
cecton Feb 11, 2020
462e7e1
WIP
cecton Feb 11, 2020
6c072c2
WIP
cecton Feb 11, 2020
9cef94b
WIP
cecton Feb 11, 2020
df98c7d
WIP
cecton Feb 11, 2020
9f25ef9
WIP
cecton Feb 11, 2020
c3c23c7
WIP
cecton Feb 11, 2020
9ee3e58
WIP
cecton Feb 11, 2020
e42db52
WIP
cecton Feb 11, 2020
3779128
WIP
cecton Feb 11, 2020
65d5604
WIP
cecton Feb 12, 2020
520480d
WIP
cecton Feb 12, 2020
603b145
Revert "WIP"
cecton Feb 12, 2020
310e535
WIP
cecton Feb 12, 2020
5e79434
Update from parent 'origin/master' (no conflict)
cecton Feb 12, 2020
d89b8fe
Update from parent 'origin/master' (conflicts)
cecton Feb 12, 2020
51cea65
WIP
cecton Feb 12, 2020
daf8dbe
WIP
cecton Feb 12, 2020
01003d3
WIP
cecton Feb 12, 2020
a2b243a
WIP
cecton Feb 12, 2020
4699a5b
Wait for client to be dropped before continuing
cecton Feb 12, 2020
1aa4ebb
WIP
cecton Feb 12, 2020
c6eb086
Revert "WIP"
cecton Feb 13, 2020
8d5f16e
Revert "Wait for client to be dropped before continuing"
cecton Feb 13, 2020
19cbc2d
WIP
cecton Feb 13, 2020
0a39b8b
WIP
cecton Feb 13, 2020
e6c3f2c
WIP
cecton Feb 13, 2020
0143192
WIP
cecton Feb 13, 2020
3a5a9d0
WIP
cecton Feb 13, 2020
77a282d
WIP
cecton Feb 13, 2020
7be6d6c
WIP
cecton Feb 17, 2020
c5201de
Update from parent 'origin/master' (no conflict)
cecton Feb 17, 2020
b4aab6e
Update from parent 'origin/master' (conflicts)
cecton Feb 17, 2020
e961052
Update from parent 'origin/master' (no conflict)
cecton Feb 18, 2020
6d53bf4
WIP
cecton Feb 18, 2020
96cd4ef
Update from parent 'origin/master' (no conflict)
cecton Feb 19, 2020
24a5475
Update from parent 'origin/master' (conflicts)
cecton Feb 19, 2020
c46a21b
Added a test for inspect command
cecton Feb 19, 2020
06261d0
Update from parent 'origin/master' (conflicts)
cecton Feb 19, 2020
c502eb3
Fixes according to suggestions
cecton Feb 19, 2020
6a41c5b
Update from parent 'origin/master' (no conflict)
cecton Feb 19, 2020
2baa706
Update from parent 'origin/master' (conflicts)
cecton Feb 19, 2020
46a1ac8
WIP
cecton Feb 19, 2020
3907e30
Wait for RPC server to cleanup on shutdown
arkpar Feb 20, 2020
42b0cf9
Update from parent 'origin/master' (no conflict)
cecton Feb 20, 2020
b661624
Update from parent 'origin/master' (conflicts)
cecton Feb 20, 2020
1c48788
Include test on Windows
cecton Feb 21, 2020
be53825
Update client/cli/src/commands/runcmd.rs
cecton Feb 21, 2020
8841dfb
Update utils/frame/benchmarking-cli/src/lib.rs
cecton Feb 21, 2020
c61f433
Update utils/frame/benchmarking-cli/src/lib.rs
cecton Feb 21, 2020
399a7ca
Update client/service/src/config.rs
cecton Feb 21, 2020
7734c2b
Update client/service/src/config.rs
cecton Feb 21, 2020
861a854
Removed extra module
cecton Feb 21, 2020
1e54938
Update client/cli/src/params/transaction_pool_params.rs
cecton Feb 21, 2020
8a289e2
Update client/cli/src/params/transaction_pool_params.rs
cecton Feb 21, 2020
5780c90
Update client/cli/src/params/mod.rs
cecton Feb 21, 2020
eb49376
Removed another extra module
cecton Feb 21, 2020
2173d22
Update client/cli/src/params/mod.rs
cecton Feb 21, 2020
c57fd85
Added comment and removed unused import
cecton Feb 21, 2020
df0522f
Update bin/node/cli/src/command.rs
cecton Feb 21, 2020
8e3118a
Update bin/node/cli/tests/build_spec_works.rs
cecton Feb 21, 2020
f11a6d5
Fixed missing import
cecton Feb 21, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 21 additions & 18 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_cli::{VersionInfo, error};
use sc_cli::VersionInfo;
use crate::service;
use crate::chain_spec;
use crate::cli::Cli;

/// Parse and run command line arguments
pub fn run(version: VersionInfo) -> error::Result<()> {
pub fn run(version: VersionInfo) -> sc_cli::Result<()> {
let opt = sc_cli::from_args::<Cli>(&version);

let config = sc_service::Configuration::new(&version);
let mut config = sc_service::Configuration::from_version(&version);

match opt.subcommand {
Some(subcommand) => sc_cli::run_subcommand(
config,
subcommand,
chain_spec::load_spec,
|config: _| Ok(new_full_start!(config).0),
&version,
),
None => sc_cli::run(
config,
opt.run,
service::new_light,
service::new_full,
chain_spec::load_spec,
&version,
)
Some(subcommand) => {
subcommand.init(&version)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't init and update_config not done inside of run()? Someone could just call run() without calling the other methods before and it would be incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because for cumulus we want to be able to modify the config before running the node.

We could have another "run" command that would do all 3 commands but we need to keep the existing 3.

Since a helper would simply call init & update_config & run, I thought it wouldn't be super helpful and I have not added it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the run and run_subcommand helpers as they are not helping much anymore. Running a command is always a set of 3 commands: 1. init 2. update config 3. run. This still allow the user to change the config before arguments get parsed or right after.

😱 you didn't read the PR description

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is law :P

Yeah it is okay for now. However it could happen that the user just calls run() (because nothing prevents them) and it will break.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this "pattern" is a let's repeat some code "pattern" :D

subcommand.update_config(&mut config, chain_spec::load_spec, &version)?;
subcommand.run(
config,
|config: _| Ok(new_full_start!(config).0),
)
},
None => {
opt.run.init(&version)?;
opt.run.update_config(&mut config, chain_spec::load_spec, &version)?;
opt.run.run(
config,
service::new_light,
service::new_full,
&version,
)
},
Comment on lines +30 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be way less error prone than before.

Before: I had put opt.run.shared_params in the subcommand's init and it was a bug.

}
}
6 changes: 2 additions & 4 deletions bin/node-template/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ mod service;
mod cli;
mod command;

pub use sc_cli::{VersionInfo, error};

fn main() -> Result<(), error::Error> {
let version = VersionInfo {
fn main() -> sc_cli::Result<()> {
let version = sc_cli::VersionInfo {
name: "Substrate Node",
commit: env!("VERGEN_SHA_SHORT"),
version: env!("CARGO_PKG_VERSION"),
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ futures = "0.3.1"
tempfile = "3.1.0"
assert_cmd = "0.12"
nix = "0.17"
serde_json = "1.0"

[build-dependencies]
build-script-utils = { version = "2.0.0", package = "substrate-build-script-utils", path = "../../../utils/build-script-utils" }
Expand Down
6 changes: 2 additions & 4 deletions bin/node/cli/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

#![warn(missing_docs)]

use sc_cli::VersionInfo;

fn main() -> Result<(), sc_cli::error::Error> {
let version = VersionInfo {
fn main() -> sc_cli::Result<()> {
let version = sc_cli::VersionInfo {
name: "Substrate Node",
commit: env!("VERGEN_SHA_SHORT"),
version: env!("CARGO_PKG_VERSION"),
Expand Down
5 changes: 0 additions & 5 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ use structopt::StructOpt;

/// An overarching CLI command definition.
#[derive(Clone, Debug, StructOpt)]
#[structopt(settings = &[
structopt::clap::AppSettings::GlobalVersion,
structopt::clap::AppSettings::ArgsNegateSubcommands,
structopt::clap::AppSettings::SubcommandsNegateReqs,
])]
pub struct Cli {
/// Possible subcommand with parameters.
#[structopt(subcommand)]
Expand Down
62 changes: 35 additions & 27 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,36 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use sc_cli::{VersionInfo, error};
use sc_cli::{VersionInfo, self};
cecton marked this conversation as resolved.
Show resolved Hide resolved
use sc_service::{Roles as ServiceRoles};
use node_transaction_factory::RuntimeAdapter;
use crate::{Cli, service, ChainSpec, load_spec, Subcommand, factory_impl::FactoryState};

/// Parse command line arguments into service configuration.
pub fn run<I, T>(args: I, version: VersionInfo) -> error::Result<()>
pub fn run<I, T>(args: I, version: VersionInfo) -> sc_cli::Result<()>
where
I: Iterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
let args: Vec<_> = args.collect();
let opt = sc_cli::from_iter::<Cli, _>(args.clone(), &version);

let mut config = sc_service::Configuration::new(&version);
let mut config = sc_service::Configuration::from_version(&version);

match opt.subcommand {
None => sc_cli::run(
config,
opt.run,
service::new_light,
service::new_full,
load_spec,
&version,
),
None => {
opt.run.init(&version)?;
opt.run.update_config(&mut config, load_spec, &version)?;
opt.run.run(
config,
service::new_light,
service::new_full,
&version,
)
},
Some(Subcommand::Inspect(cmd)) => {
cmd.init(&mut config, load_spec, &version)?;
cmd.init(&version)?;
cmd.update_config(&mut config, load_spec, &version)?;

let client = sc_service::new_full_client::<
node_runtime::Block, node_runtime::RuntimeApi, node_executor::Executor, _, _,
Expand All @@ -50,25 +53,27 @@ where
cmd.run(inspect)
},
Some(Subcommand::Benchmark(cmd)) => {
cmd.init(&mut config, load_spec, &version)?;
cmd.init(&version)?;
cmd.update_config(&mut config, load_spec, &version)?;

cmd.run::<_, _, node_runtime::Block, node_executor::Executor>(config)
},
Some(Subcommand::Factory(cli_args)) => {
sc_cli::init(&cli_args.shared_params, &version)?;
sc_cli::init_config(&mut config, &cli_args.shared_params, &version, load_spec)?;
sc_cli::fill_import_params(
cli_args.shared_params.init(&version)?;
cli_args.shared_params.update_config(&mut config, load_spec, &version)?;
cli_args.import_params.update_config(
&mut config,
&cli_args.import_params,
ServiceRoles::FULL,
cli_args.shared_params.dev,
)?;

sc_cli::fill_config_keystore_in_memory(&mut config)?;
config.use_in_memory_keystore()?;

match ChainSpec::from(config.expect_chain_spec().id()) {
Some(ref c) if c == &ChainSpec::Development || c == &ChainSpec::LocalTestnet => {},
_ => panic!("Factory is only supported for development and local testnet."),
_ => return Err(
"Factory is only supported for development and local testnet.".into()
),
Comment on lines +74 to +76
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was panicking with a nice stacktrace in the console. I don't know why. It seems to me it's a normal user error so I changed it. No more stacktrace but the error message is still explicit enough imho.

}

// Setup tracing.
Expand All @@ -77,7 +82,9 @@ where
cli_args.import_params.tracing_receiver.into(), tracing_targets
);
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
panic!("Unable to set global default subscriber {}", e);
return Err(
format!("Unable to set global default subscriber {}", e).into()
);
}
}

Expand All @@ -96,12 +103,13 @@ where

Ok(())
},
Some(Subcommand::Base(subcommand)) => sc_cli::run_subcommand(
config,
subcommand,
load_spec,
|config: service::NodeConfiguration| Ok(new_full_start!(config).0),
&version,
),
Some(Subcommand::Base(subcommand)) => {
subcommand.init(&version)?;
subcommand.update_config(&mut config, load_spec, &version)?;
subcommand.run(
config,
|config: service::NodeConfiguration| Ok(new_full_start!(config).0),
)
},
}
}
37 changes: 37 additions & 0 deletions bin/node/cli/tests/build_spec_works.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use assert_cmd::cargo::cargo_bin;
use std::process::Command;
use tempfile::tempdir;

#[test]
fn build_spec_works() {
let base_path = tempdir().expect("could not create a temp dir");

let output = Command::new(cargo_bin("substrate"))
.args(&["build-spec", "--dev", "-d"])
.arg(base_path.path().as_os_str())
cecton marked this conversation as resolved.
Show resolved Hide resolved
.output()
.unwrap();
assert!(output.status.success());

// Make sure that the `dev` chain folder exists, but the `db` doesn't
assert!(base_path.path().join("chains/dev/").exists());
assert!(!base_path.path().join("chains/dev/db").exists());

let _value: serde_json::Value = serde_json::from_slice(output.stdout.as_slice()).unwrap();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
Expand All @@ -14,10 +14,25 @@
// 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::params::SharedParams;
#![cfg(unix)]

/// Supports getting common params.
pub trait GetSharedParams {
/// Returns shared params if any.
fn shared_params(&self) -> Option<&SharedParams>;
use assert_cmd::cargo::cargo_bin;
use std::process::Command;
use tempfile::tempdir;

mod common;

#[test]
fn check_block_works() {
let base_path = tempdir().expect("could not create a temp dir");

common::run_command_for_a_while(base_path.path(), false);

let status = Command::new(cargo_bin("substrate"))
.args(&["check-block", "-d"])
.arg(base_path.path())
.arg("1")
.status()
.unwrap();
assert!(status.success());
}
31 changes: 30 additions & 1 deletion bin/node/cli/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use std::{process::{Child, ExitStatus}, thread, time::Duration};
#![cfg(unix)]
#![allow(dead_code)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is dead inside here? :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. I think common.rs is executed on its own...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So everything looks like dead code when it's compiled


use std::{process::{Child, ExitStatus}, thread, time::Duration, path::Path};
use assert_cmd::cargo::cargo_bin;
use std::{convert::TryInto, process::Command};
use nix::sys::signal::{kill, Signal::SIGINT};
use nix::unistd::Pid;

/// Wait for the given `child` the given number of `secs`.
///
Expand All @@ -32,3 +39,25 @@ pub fn wait_for(child: &mut Child, secs: usize) -> Option<ExitStatus> {

None
}

pub fn run_command_for_a_while(base_path: &Path, dev: bool) {
cecton marked this conversation as resolved.
Show resolved Hide resolved
let mut cmd = Command::new(cargo_bin("substrate"));

if dev {
cmd.arg("--dev");
}

let mut cmd = cmd
.arg("-d")
.arg(base_path)
.spawn()
.unwrap();

// Let it produce some blocks.
thread::sleep(Duration::from_secs(30));
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running");

// Stop the process
kill(Pid::from_raw(cmd.id().try_into().unwrap()), SIGINT).unwrap();
assert!(wait_for(&mut cmd, 20).map(|x| x.success()).unwrap_or_default());
}
40 changes: 40 additions & 0 deletions bin/node/cli/tests/factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

#![cfg(unix)]

use assert_cmd::cargo::cargo_bin;
use std::process::{Command, Stdio};
use tempfile::tempdir;

mod common;

#[test]
fn factory_works() {
let base_path = tempdir().expect("could not create a temp dir");

let status = Command::new(cargo_bin("substrate"))
.stdout(Stdio::null())
.args(&["factory", "--dev", "-d"])
.arg(base_path.path())
.status()
.unwrap();
assert!(status.success());

// Make sure that the `dev` chain folder exists & `db`
assert!(base_path.path().join("chains/dev/").exists());
assert!(base_path.path().join("chains/dev/db").exists());
}
Loading