diff --git a/substrate/client/cli/src/lib.rs b/substrate/client/cli/src/lib.rs index d4387c984f..c2e11b56ca 100644 --- a/substrate/client/cli/src/lib.rs +++ b/substrate/client/cli/src/lib.rs @@ -42,7 +42,7 @@ use sc_network::{ use sp_core::H256; use std::{ - io::{Write, Read, Seek, Cursor, stdin, stdout, ErrorKind}, iter, fs::{self, File}, + io::{Write, Read, Seek, Cursor, stdin, stdout, ErrorKind}, iter, fmt::Debug, fs::{self, File}, net::{Ipv4Addr, SocketAddr}, path::{Path, PathBuf}, str::FromStr, pin::Pin, task::Poll }; @@ -64,7 +64,7 @@ use lazy_static::lazy_static; use futures::{Future, compat::Future01CompatExt, executor::block_on}; use sc_telemetry::TelemetryEndpoints; use sp_runtime::generic::BlockId; -use sp_runtime::traits::Block as BlockT; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; /// default sub directory to store network config const DEFAULT_NETWORK_CONFIG_PATH : &'static str = "network"; @@ -373,6 +373,8 @@ impl<'a> ParseAndPrepareExport<'a> { where S: FnOnce(&str) -> Result>, String>, F: FnOnce(Configuration) -> Result, B: ServiceBuilderCommand, + <<<::Block as BlockT>::Header as HeaderT> + ::Number as FromStr>::Err: Debug, C: Default, G: RuntimeGenesis, E: ChainSpecExtension, @@ -383,8 +385,9 @@ impl<'a> ParseAndPrepareExport<'a> { if let DatabaseConfig::Path { ref path, .. } = &config.database { info!("DB path: {}", path.display()); } - let from = self.params.from.unwrap_or(1); - let to = self.params.to; + let from = self.params.from.and_then(|f| f.parse().ok()).unwrap_or(1); + let to = self.params.to.and_then(|t| t.parse().ok()); + let json = self.params.json; let file: Box = match self.params.output { @@ -402,7 +405,7 @@ impl<'a> ParseAndPrepareExport<'a> { }); let mut export_fut = builder(config)? - .export_blocks(file, from.into(), to.map(Into::into), json) + .export_blocks(file, from.into(), to, json) .compat(); let fut = futures::future::poll_fn(|cx| { if exit_recv.try_recv().is_ok() { @@ -596,6 +599,8 @@ impl<'a> ParseAndPrepareRevert<'a> { S: FnOnce(&str) -> Result>, String>, F: FnOnce(Configuration) -> Result, B: ServiceBuilderCommand, + <<<::Block as BlockT>::Header as HeaderT> + ::Number as FromStr>::Err: Debug, C: Default, G: RuntimeGenesis, E: ChainSpecExtension, @@ -603,8 +608,8 @@ impl<'a> ParseAndPrepareRevert<'a> { 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())?; + let blocks = self.params.num.parse()?; + builder(config)?.revert_chain(blocks)?; Ok(()) } } diff --git a/substrate/client/cli/src/params.rs b/substrate/client/cli/src/params.rs index 7121c53858..10be3f0c1b 100644 --- a/substrate/client/cli/src/params.rs +++ b/substrate/client/cli/src/params.rs @@ -16,7 +16,7 @@ use crate::traits::{AugmentClap, GetLogFilter}; -use std::path::PathBuf; +use std::{str::FromStr, path::PathBuf}; use structopt::{StructOpt, clap::{arg_enum, App, AppSettings, SubCommand, Arg}}; pub use crate::execution_strategy::ExecutionStrategy; @@ -734,6 +734,41 @@ pub struct BuildSpecCmd { 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); + +impl FromStr for BlockNumber { + type Err = String; + + fn from_str(block_number: &str) -> Result { + if block_number.chars().any(|d| !d.is_digit(10)) { + Err(format!( + "Invalid block number: {}, expected decimal formatted unsigned integer", + block_number + )) + } else { + Ok(Self(block_number.to_owned())) + } + } +} + +impl BlockNumber { + /// Wrapper on top of `std::str::parse` but with `Error` as a `String` + /// + /// See `https://doc.rust-lang.org/std/primitive.str.html#method.parse` for more elaborate + /// documentation. + pub fn parse(&self) -> Result + where + N: FromStr, + N::Err: std::fmt::Debug, + { + self.0 + .parse() + .map_err(|e| format!("BlockNumber: {} parsing failed because of {:?}", self.0, e)) + } +} + /// The `export-blocks` command used to export blocks. #[derive(Debug, StructOpt, Clone)] pub struct ExportBlocksCmd { @@ -745,13 +780,13 @@ pub struct ExportBlocksCmd { /// /// Default is 1. #[structopt(long = "from", value_name = "BLOCK")] - pub from: Option, + pub from: Option, /// Specify last block number. /// /// Default is best block. #[structopt(long = "to", value_name = "BLOCK")] - pub to: Option, + pub to: Option, /// Use JSON output rather than binary. #[structopt(long = "json")] @@ -817,7 +852,7 @@ impl_get_log_filter!(CheckBlockCmd); pub struct RevertCmd { /// Number of blocks to revert. #[structopt(default_value = "256")] - pub num: u32, + pub num: BlockNumber, #[allow(missing_docs)] #[structopt(flatten)] diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index cf008101ed..1acdc30570 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -172,7 +172,7 @@ pub trait Trait: 'static + Eq + Clone { /// The block number type used by the runtime. type BlockNumber: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + SimpleArithmetic - + Default + Bounded + Copy + sp_std::hash::Hash; + + Default + Bounded + Copy + sp_std::hash::Hash + sp_std::str::FromStr; /// The output of the `Hashing` function. type Hash: diff --git a/substrate/primitives/runtime/src/generic/header.rs b/substrate/primitives/runtime/src/generic/header.rs index c095490bc9..35f2e91afc 100644 --- a/substrate/primitives/runtime/src/generic/header.rs +++ b/substrate/primitives/runtime/src/generic/header.rs @@ -105,7 +105,7 @@ impl codec::EncodeLike for Header where impl traits::Header for Header where Number: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash + MaybeDisplay + - SimpleArithmetic + Codec + Copy + Into + TryFrom, + SimpleArithmetic + Codec + Copy + Into + TryFrom + sp_std::str::FromStr, Hash: HashT, Hash::Output: Default + sp_std::hash::Hash + Copy + Member + MaybeSerialize + Debug + MaybeDisplay + SimpleBitOps + Codec, diff --git a/substrate/primitives/runtime/src/traits.rs b/substrate/primitives/runtime/src/traits.rs index 0001690b38..97ff85c986 100644 --- a/substrate/primitives/runtime/src/traits.rs +++ b/substrate/primitives/runtime/src/traits.rs @@ -524,7 +524,7 @@ pub trait IsMember { pub trait Header: Clone + Send + Sync + Codec + Eq + MaybeSerialize + Debug + 'static { /// Header number. type Number: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash - + Copy + MaybeDisplay + SimpleArithmetic + Codec; + + Copy + MaybeDisplay + SimpleArithmetic + Codec + sp_std::str::FromStr; /// Header hash type type Hash: Member + MaybeSerializeDeserialize + Debug + sp_std::hash::Hash + Copy + MaybeDisplay + Default + SimpleBitOps + Codec + AsRef<[u8]> + AsMut<[u8]>;