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

Related to #4776 
Related to https://github.com/paritytech/polkadot/pull/832

To summarize the changes:
1. I did not manage to validate with types the service's Configuration. But I did reduce the possibility of errors by moving all the "fill" functions to their respective structopts
2. I split params.rs to multiple modules: one module params for just CLI parameters and one module commands for CLI subcommands (and RunCmd). Every command and params are in their own file so things are grouped better together and easier to remove
3. 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.
4. I added tests for all subcommands.
5. [deleted]

Overall the aim is to improve the situation with the Configuration and the optional parameters, add tests, make the API more consistent and simpler.
This commit is contained in:
Cecile Tonglet
2020-02-21 13:53:01 +01:00
committed by GitHub
parent 1e3e6a75f9
commit e8000e7429
43 changed files with 3040 additions and 2400 deletions
+1
View File
@@ -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" }
+2 -4
View File
@@ -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"),
-5
View File
@@ -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)]
+35 -27
View File
@@ -14,13 +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 sc_cli::{VersionInfo, error};
use sc_cli::VersionInfo;
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,
@@ -28,19 +28,22 @@ where
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, _, _,
@@ -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()
),
}
// Setup tracing.
@@ -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()
);
}
}
@@ -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),
)
},
}
}
@@ -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())
.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();
}
@@ -0,0 +1,38 @@
// 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;
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 -1
View File
@@ -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)]
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`.
///
@@ -32,3 +39,26 @@ pub fn wait_for(child: &mut Child, secs: usize) -> Option<ExitStatus> {
None
}
/// Run the node for a while (30 seconds)
pub fn run_command_for_a_while(base_path: &Path, dev: bool) {
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
View File
@@ -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());
}
@@ -0,0 +1,59 @@
// 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, fs};
use tempfile::tempdir;
mod common;
#[test]
fn import_export_and_revert_work() {
let base_path = tempdir().expect("could not create a temp dir");
let exported_blocks = base_path.path().join("exported_blocks");
common::run_command_for_a_while(base_path.path(), false);
let status = Command::new(cargo_bin("substrate"))
.args(&["export-blocks", "-d"])
.arg(base_path.path())
.arg(&exported_blocks)
.status()
.unwrap();
assert!(status.success());
let metadata = fs::metadata(&exported_blocks).unwrap();
assert!(metadata.len() > 0, "file exported_blocks should not be empty");
let _ = fs::remove_dir_all(base_path.path().join("db"));
let status = Command::new(cargo_bin("substrate"))
.args(&["import-blocks", "-d"])
.arg(base_path.path())
.arg(&exported_blocks)
.status()
.unwrap();
assert!(status.success());
let status = Command::new(cargo_bin("substrate"))
.args(&["revert", "-d"])
.arg(base_path.path())
.status()
.unwrap();
assert!(status.success());
}
@@ -0,0 +1,38 @@
// 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;
use tempfile::tempdir;
mod common;
#[test]
fn inspect_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(&["inspect", "-d"])
.arg(base_path.path())
.args(&["block", "1"])
.status()
.unwrap();
assert!(status.success());
}
@@ -15,39 +15,27 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use assert_cmd::cargo::cargo_bin;
use std::{convert::TryInto, process::Command, thread, time::Duration, fs, path::PathBuf};
use std::process::Command;
use tempfile::tempdir;
mod common;
#[test]
#[cfg(unix)]
fn purge_chain_works() {
use nix::sys::signal::{kill, Signal::SIGINT};
use nix::unistd::Pid;
let base_path = tempdir().expect("could not create a temp dir");
let base_path = "purge_chain_test";
let _ = fs::remove_dir_all(base_path);
let mut cmd = Command::new(cargo_bin("substrate"))
.args(&["--dev", "-d", 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!(common::wait_for(&mut cmd, 30).map(|x| x.success()).unwrap_or_default());
common::run_command_for_a_while(base_path.path(), true);
let status = Command::new(cargo_bin("substrate"))
.args(&["purge-chain", "--dev", "-d", base_path, "-y"])
.args(&["purge-chain", "--dev", "-d"])
.arg(base_path.path())
.arg("-y")
.status()
.unwrap();
assert!(status.success());
// Make sure that the `dev` chain folder exists, but the `db` is deleted.
assert!(PathBuf::from(base_path).join("chains/dev/").exists());
assert!(!PathBuf::from(base_path).join("chains/dev/db").exists());
assert!(base_path.path().join("chains/dev/").exists());
assert!(!base_path.path().join("chains/dev/db").exists());
}
@@ -15,7 +15,8 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use assert_cmd::cargo::cargo_bin;
use std::{convert::TryInto, process::Command, thread, time::Duration, fs};
use std::{convert::TryInto, process::Command, thread, time::Duration};
use tempfile::tempdir;
mod common;
@@ -26,13 +27,14 @@ fn running_the_node_works_and_can_be_interrupted() {
use nix::unistd::Pid;
fn run_command_and_kill(signal: Signal) {
let _ = fs::remove_dir_all("interrupt_test");
let base_path = tempdir().expect("could not create a temp dir");
let mut cmd = Command::new(cargo_bin("substrate"))
.args(&["--dev", "-d", "interrupt_test"])
.args(&["--dev", "-d"])
.arg(base_path.path())
.spawn()
.unwrap();
thread::sleep(Duration::from_secs(30));
thread::sleep(Duration::from_secs(20));
assert!(cmd.try_wait().unwrap().is_none(), "the process should still be running");
kill(Pid::from_raw(cmd.id().try_into().unwrap()), signal).unwrap();
assert_eq!(
+2 -180
View File
@@ -16,12 +16,8 @@
//! Structs to easily compose inspect sub-command for CLI.
use std::{
fmt::Debug,
str::FromStr,
};
use crate::{Inspector, PrettyPrinter};
use sc_cli::{ImportParams, SharedParams, error};
use std::fmt::Debug;
use sc_cli::{ImportParams, SharedParams};
use structopt::StructOpt;
/// The `inspect` command used to print decoded chain data.
@@ -64,177 +60,3 @@ pub enum InspectSubCmd {
input: String,
},
}
impl InspectCmd {
/// Parse CLI arguments and initialize given config.
pub fn init<G, E>(
&self,
config: &mut sc_service::config::Configuration<G, E>,
spec_factory: impl FnOnce(&str) -> Result<Option<sc_service::ChainSpec<G, E>>, String>,
version: &sc_cli::VersionInfo,
) -> error::Result<()> where
G: sc_service::RuntimeGenesis,
E: sc_service::ChainSpecExtension,
{
sc_cli::init_config(config, &self.shared_params, version, spec_factory)?;
// make sure to configure keystore
sc_cli::fill_config_keystore_in_memory(config)?;
// and all import params (especially pruning that has to match db meta)
sc_cli::fill_import_params(
config,
&self.import_params,
sc_service::Roles::FULL,
self.shared_params.dev,
)?;
Ok(())
}
/// Run the inspect command, passing the inspector.
pub fn run<B, P>(
self,
inspect: Inspector<B, P>,
) -> error::Result<()> where
B: sp_runtime::traits::Block,
B::Hash: FromStr,
P: PrettyPrinter<B>,
{
match self.command {
InspectSubCmd::Block { input } => {
let input = input.parse()?;
let res = inspect.block(input)
.map_err(|e| format!("{}", e))?;
println!("{}", res);
Ok(())
},
InspectSubCmd::Extrinsic { input } => {
let input = input.parse()?;
let res = inspect.extrinsic(input)
.map_err(|e| format!("{}", e))?;
println!("{}", res);
Ok(())
},
}
}
}
/// A block to retrieve.
#[derive(Debug, Clone, PartialEq)]
pub enum BlockAddress<Hash, Number> {
/// Get block by hash.
Hash(Hash),
/// Get block by number.
Number(Number),
/// Raw SCALE-encoded bytes.
Bytes(Vec<u8>),
}
impl<Hash: FromStr, Number: FromStr> FromStr for BlockAddress<Hash, Number> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// try to parse hash first
if let Ok(hash) = s.parse() {
return Ok(Self::Hash(hash))
}
// then number
if let Ok(number) = s.parse() {
return Ok(Self::Number(number))
}
// then assume it's bytes (hex-encoded)
sp_core::bytes::from_hex(s)
.map(Self::Bytes)
.map_err(|e| format!(
"Given string does not look like hash or number. It could not be parsed as bytes either: {}",
e
))
}
}
/// An extrinsic address to decode and print out.
#[derive(Debug, Clone, PartialEq)]
pub enum ExtrinsicAddress<Hash, Number> {
/// Extrinsic as part of existing block.
Block(BlockAddress<Hash, Number>, usize),
/// Raw SCALE-encoded extrinsic bytes.
Bytes(Vec<u8>),
}
impl<Hash: FromStr + Debug, Number: FromStr + Debug> FromStr for ExtrinsicAddress<Hash, Number> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// first try raw bytes
if let Ok(bytes) = sp_core::bytes::from_hex(s).map(Self::Bytes) {
return Ok(bytes)
}
// split by a bunch of different characters
let mut it = s.split(|c| c == '.' || c == ':' || c == ' ');
let block = it.next()
.expect("First element of split iterator is never empty; qed")
.parse()?;
let index = it.next()
.ok_or_else(|| format!("Extrinsic index missing: example \"5:0\""))?
.parse()
.map_err(|e| format!("Invalid index format: {}", e))?;
Ok(Self::Block(block, index))
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::hash::H160 as Hash;
#[test]
fn should_parse_block_strings() {
type BlockAddress = super::BlockAddress<Hash, u64>;
let b0 = BlockAddress::from_str("3BfC20f0B9aFcAcE800D73D2191166FF16540258");
let b1 = BlockAddress::from_str("1234");
let b2 = BlockAddress::from_str("0");
let b3 = BlockAddress::from_str("0x0012345f");
assert_eq!(b0, Ok(BlockAddress::Hash(
"3BfC20f0B9aFcAcE800D73D2191166FF16540258".parse().unwrap()
)));
assert_eq!(b1, Ok(BlockAddress::Number(1234)));
assert_eq!(b2, Ok(BlockAddress::Number(0)));
assert_eq!(b3, Ok(BlockAddress::Bytes(vec![0, 0x12, 0x34, 0x5f])));
}
#[test]
fn should_parse_extrinsic_address() {
type BlockAddress = super::BlockAddress<Hash, u64>;
type ExtrinsicAddress = super::ExtrinsicAddress<Hash, u64>;
let e0 = ExtrinsicAddress::from_str("1234");
let b0 = ExtrinsicAddress::from_str("3BfC20f0B9aFcAcE800D73D2191166FF16540258:5");
let b1 = ExtrinsicAddress::from_str("1234:0");
let b2 = ExtrinsicAddress::from_str("0 0");
let b3 = ExtrinsicAddress::from_str("0x0012345f");
assert_eq!(e0, Err("Extrinsic index missing: example \"5:0\"".into()));
assert_eq!(b0, Ok(ExtrinsicAddress::Block(
BlockAddress::Hash("3BfC20f0B9aFcAcE800D73D2191166FF16540258".parse().unwrap()),
5
)));
assert_eq!(b1, Ok(ExtrinsicAddress::Block(
BlockAddress::Number(1234),
0
)));
assert_eq!(b2, Ok(ExtrinsicAddress::Block(
BlockAddress::Number(0),
0
)));
assert_eq!(b3, Ok(ExtrinsicAddress::Bytes(vec![0, 0x12, 0x34, 0x5f])));
}
}
+204
View File
@@ -0,0 +1,204 @@
// 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/>.
//! Command ran by the CLI
use std::{
fmt::Debug,
str::FromStr,
};
use crate::cli::{InspectCmd, InspectSubCmd};
use crate::{Inspector, PrettyPrinter};
impl InspectCmd {
/// Initialize
pub fn init(&self, version: &sc_cli::VersionInfo) -> sc_cli::Result<()> {
self.shared_params.init(version)
}
/// Parse CLI arguments and initialize given config.
pub fn update_config<G, E>(
&self,
mut config: &mut sc_service::config::Configuration<G, E>,
spec_factory: impl FnOnce(&str) -> Result<Option<sc_service::ChainSpec<G, E>>, String>,
version: &sc_cli::VersionInfo,
) -> sc_cli::Result<()> where
G: sc_service::RuntimeGenesis,
E: sc_service::ChainSpecExtension,
{
self.shared_params.update_config(config, spec_factory, version)?;
// make sure to configure keystore
config.use_in_memory_keystore()?;
// and all import params (especially pruning that has to match db meta)
self.import_params.update_config(
&mut config,
sc_service::Roles::FULL,
self.shared_params.dev,
)?;
Ok(())
}
/// Run the inspect command, passing the inspector.
pub fn run<B, P>(
self,
inspect: Inspector<B, P>,
) -> sc_cli::Result<()> where
B: sp_runtime::traits::Block,
B::Hash: FromStr,
P: PrettyPrinter<B>,
{
match self.command {
InspectSubCmd::Block { input } => {
let input = input.parse()?;
let res = inspect.block(input)
.map_err(|e| format!("{}", e))?;
println!("{}", res);
Ok(())
},
InspectSubCmd::Extrinsic { input } => {
let input = input.parse()?;
let res = inspect.extrinsic(input)
.map_err(|e| format!("{}", e))?;
println!("{}", res);
Ok(())
},
}
}
}
/// A block to retrieve.
#[derive(Debug, Clone, PartialEq)]
pub enum BlockAddress<Hash, Number> {
/// Get block by hash.
Hash(Hash),
/// Get block by number.
Number(Number),
/// Raw SCALE-encoded bytes.
Bytes(Vec<u8>),
}
impl<Hash: FromStr, Number: FromStr> FromStr for BlockAddress<Hash, Number> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// try to parse hash first
if let Ok(hash) = s.parse() {
return Ok(Self::Hash(hash))
}
// then number
if let Ok(number) = s.parse() {
return Ok(Self::Number(number))
}
// then assume it's bytes (hex-encoded)
sp_core::bytes::from_hex(s)
.map(Self::Bytes)
.map_err(|e| format!(
"Given string does not look like hash or number. It could not be parsed as bytes either: {}",
e
))
}
}
/// An extrinsic address to decode and print out.
#[derive(Debug, Clone, PartialEq)]
pub enum ExtrinsicAddress<Hash, Number> {
/// Extrinsic as part of existing block.
Block(BlockAddress<Hash, Number>, usize),
/// Raw SCALE-encoded extrinsic bytes.
Bytes(Vec<u8>),
}
impl<Hash: FromStr + Debug, Number: FromStr + Debug> FromStr for ExtrinsicAddress<Hash, Number> {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// first try raw bytes
if let Ok(bytes) = sp_core::bytes::from_hex(s).map(Self::Bytes) {
return Ok(bytes)
}
// split by a bunch of different characters
let mut it = s.split(|c| c == '.' || c == ':' || c == ' ');
let block = it.next()
.expect("First element of split iterator is never empty; qed")
.parse()?;
let index = it.next()
.ok_or_else(|| format!("Extrinsic index missing: example \"5:0\""))?
.parse()
.map_err(|e| format!("Invalid index format: {}", e))?;
Ok(Self::Block(block, index))
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_core::hash::H160 as Hash;
#[test]
fn should_parse_block_strings() {
type BlockAddress = super::BlockAddress<Hash, u64>;
let b0 = BlockAddress::from_str("3BfC20f0B9aFcAcE800D73D2191166FF16540258");
let b1 = BlockAddress::from_str("1234");
let b2 = BlockAddress::from_str("0");
let b3 = BlockAddress::from_str("0x0012345f");
assert_eq!(b0, Ok(BlockAddress::Hash(
"3BfC20f0B9aFcAcE800D73D2191166FF16540258".parse().unwrap()
)));
assert_eq!(b1, Ok(BlockAddress::Number(1234)));
assert_eq!(b2, Ok(BlockAddress::Number(0)));
assert_eq!(b3, Ok(BlockAddress::Bytes(vec![0, 0x12, 0x34, 0x5f])));
}
#[test]
fn should_parse_extrinsic_address() {
type BlockAddress = super::BlockAddress<Hash, u64>;
type ExtrinsicAddress = super::ExtrinsicAddress<Hash, u64>;
let e0 = ExtrinsicAddress::from_str("1234");
let b0 = ExtrinsicAddress::from_str("3BfC20f0B9aFcAcE800D73D2191166FF16540258:5");
let b1 = ExtrinsicAddress::from_str("1234:0");
let b2 = ExtrinsicAddress::from_str("0 0");
let b3 = ExtrinsicAddress::from_str("0x0012345f");
assert_eq!(e0, Err("Extrinsic index missing: example \"5:0\"".into()));
assert_eq!(b0, Ok(ExtrinsicAddress::Block(
BlockAddress::Hash("3BfC20f0B9aFcAcE800D73D2191166FF16540258".parse().unwrap()),
5
)));
assert_eq!(b1, Ok(ExtrinsicAddress::Block(
BlockAddress::Number(1234),
0
)));
assert_eq!(b2, Ok(ExtrinsicAddress::Block(
BlockAddress::Number(0),
0
)));
assert_eq!(b3, Ok(ExtrinsicAddress::Bytes(vec![0, 0x12, 0x34, 0x5f])));
}
}
+10 -7
View File
@@ -23,6 +23,7 @@
#![warn(missing_docs)]
pub mod cli;
pub mod command;
use std::{
fmt,
@@ -37,8 +38,10 @@ use sp_runtime::{
traits::{Block, HashFor, NumberFor, Hash}
};
use command::{BlockAddress, ExtrinsicAddress};
/// A helper type for a generic block input.
pub type BlockAddressFor<TBlock> = cli::BlockAddress<
pub type BlockAddressFor<TBlock> = BlockAddress<
<HashFor<TBlock> as Hash>::Output,
NumberFor<TBlock>
>;
@@ -148,10 +151,10 @@ impl<TBlock: Block, TPrinter: PrettyPrinter<TBlock>> Inspector<TBlock, TPrinter>
fn get_block(&self, input: BlockAddressFor<TBlock>) -> Result<TBlock, Error> {
Ok(match input {
cli::BlockAddress::Bytes(bytes) => {
BlockAddress::Bytes(bytes) => {
TBlock::decode(&mut &*bytes)?
},
cli::BlockAddress::Number(number) => {
BlockAddress::Number(number) => {
let id = BlockId::number(number);
let not_found = format!("Could not find block {:?}", id);
let body = self.chain.block_body(&id)?
@@ -160,7 +163,7 @@ impl<TBlock: Block, TPrinter: PrettyPrinter<TBlock>> Inspector<TBlock, TPrinter>
.ok_or_else(|| Error::NotFound(not_found.clone()))?;
TBlock::new(header, body)
},
cli::BlockAddress::Hash(hash) => {
BlockAddress::Hash(hash) => {
let id = BlockId::hash(hash);
let not_found = format!("Could not find block {:?}", id);
let body = self.chain.block_body(&id)?
@@ -175,7 +178,7 @@ impl<TBlock: Block, TPrinter: PrettyPrinter<TBlock>> Inspector<TBlock, TPrinter>
/// Get a pretty-printed extrinsic.
pub fn extrinsic(
&self,
input: cli::ExtrinsicAddress<<HashFor<TBlock> as Hash>::Output, NumberFor<TBlock>>,
input: ExtrinsicAddress<<HashFor<TBlock> as Hash>::Output, NumberFor<TBlock>>,
) -> Result<String, Error> {
struct ExtrinsicPrinter<'a, A: Block, B>(A::Extrinsic, &'a B);
impl<'a, A: Block, B: PrettyPrinter<A>> fmt::Display for ExtrinsicPrinter<'a, A, B> {
@@ -185,7 +188,7 @@ impl<TBlock: Block, TPrinter: PrettyPrinter<TBlock>> Inspector<TBlock, TPrinter>
}
let ext = match input {
cli::ExtrinsicAddress::Block(block, index) => {
ExtrinsicAddress::Block(block, index) => {
let block = self.get_block(block)?;
block.extrinsics()
.get(index)
@@ -194,7 +197,7 @@ impl<TBlock: Block, TPrinter: PrettyPrinter<TBlock>> Inspector<TBlock, TPrinter>
"Could not find extrinsic {} in block {:?}", index, block
)))?
},
cli::ExtrinsicAddress::Bytes(bytes) => {
ExtrinsicAddress::Bytes(bytes) => {
TBlock::Extrinsic::decode(&mut &*bytes)?
}
};
@@ -83,7 +83,7 @@ pub fn factory<RA, Backend, Exec, Block, RtApi, Sc>(
mut factory_state: RA,
client: &Arc<Client<Backend, Exec, Block, RtApi>>,
select_chain: &Sc,
) -> sc_cli::error::Result<()>
) -> sc_cli::Result<()>
where
Block: BlockT,
Exec: sc_client::CallExecutor<Block, Backend = Backend> + Send + Sync + Clone,
@@ -97,7 +97,7 @@ where
RA: RuntimeAdapter<Block = Block>,
Block::Hash: From<sp_core::H256>,
{
let best_header: Result<<Block as BlockT>::Header, sc_cli::error::Error> =
let best_header: Result<<Block as BlockT>::Header, sc_cli::Error> =
select_chain.best_chain().map_err(|e| format!("{:?}", e).into());
let mut best_hash = best_header?.hash();
let mut best_block_id = BlockId::<Block>::hash(best_hash);