[client cli] generic blocknumber (#4376)

* rewrite me

* [cli]: make `BlockNumber` generic

* cleanup
This commit is contained in:
Niklas Adolfsson
2019-12-17 11:45:20 +01:00
committed by Bastian Köcher
parent e19f5adfb7
commit 7c6ad9dd1c
5 changed files with 54 additions and 14 deletions
+12 -7
View File
@@ -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<Option<ChainSpec<G, E>>, String>,
F: FnOnce(Configuration<C, G, E>) -> Result<B, error::Error>,
B: ServiceBuilderCommand,
<<<<B as 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<dyn Write> = 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<Option<ChainSpec<G, E>>, String>,
F: FnOnce(Configuration<C, G, E>) -> Result<B, error::Error>,
B: ServiceBuilderCommand,
<<<<B as 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(())
}
}
+39 -4
View File
@@ -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<Self, Self::Err> {
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<N>` 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<N>(&self) -> Result<N, String>
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<u32>,
pub from: Option<BlockNumber>,
/// Specify last block number.
///
/// Default is best block.
#[structopt(long = "to", value_name = "BLOCK")]
pub to: Option<u32>,
pub to: Option<BlockNumber>,
/// 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)]