This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[client cli] generic blocknumber #4376
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9a4826f
rewrite me
niklasad1 4c3af0a
Merge remote-tracking branch 'origin/master' into na-cli-generic-bloc…
niklasad1 962e2f4
[cli]: make `BlockNumber` generic
niklasad1 35cc16b
Merge remote-tracking branch 'origin/master' into na-cli-generic-bloc…
niklasad1 bbfb106
cleanup
niklasad1 9b825a5
Merge remote-tracking branch 'origin/master' into na-cli-generic-bloc…
niklasad1 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for printing the error which can be useful |
||
{ | ||
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)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nothing prevents parsing
BlockNumber to a signed integer thus it relies on each type's
FromStr` implementation.If the value is bigger than 2^(n-1) it will be an
Overflow
for the types incore::num
at least, where n is the number of bits of the integer.