mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 22:47:56 +00:00
Adds export-state subcommand (#5842)
* Export state cli * More work * Fix tests * Make it work * Fix compilation * Apply suggestions from code review
This commit is contained in:
@@ -24,8 +24,8 @@ mod pruning_params;
|
||||
mod shared_params;
|
||||
mod transaction_pool_params;
|
||||
|
||||
use std::fmt::Debug;
|
||||
use std::str::FromStr;
|
||||
use std::{fmt::Debug, str::FromStr};
|
||||
use sp_runtime::{generic::BlockId, traits::{Block as BlockT, NumberFor}};
|
||||
|
||||
pub use crate::params::database_params::*;
|
||||
pub use crate::params::import_params::*;
|
||||
@@ -45,10 +45,10 @@ 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)) {
|
||||
if let Some(pos) = block_number.chars().position(|d| !d.is_digit(10)) {
|
||||
Err(format!(
|
||||
"Invalid block number: {}, expected decimal formatted unsigned integer",
|
||||
block_number,
|
||||
"Expected block number, found illegal digit at position: {}",
|
||||
pos,
|
||||
))
|
||||
} else {
|
||||
Ok(Self(block_number.to_owned()))
|
||||
@@ -66,8 +66,89 @@ impl BlockNumber {
|
||||
N: FromStr,
|
||||
N::Err: std::fmt::Debug,
|
||||
{
|
||||
self.0
|
||||
.parse()
|
||||
.map_err(|e| format!("BlockNumber: {} parsing failed because of {:?}", self.0, e))
|
||||
FromStr::from_str(&self.0).map_err(|e| format!("Failed to parse block number: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper type that is either a `Hash` or the number of a `Block`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockNumberOrHash(String);
|
||||
|
||||
impl FromStr for BlockNumberOrHash {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(block_number: &str) -> Result<Self, Self::Err> {
|
||||
if block_number.starts_with("0x") {
|
||||
if let Some(pos) = &block_number[2..].chars().position(|c| !c.is_ascii_hexdigit()) {
|
||||
Err(format!(
|
||||
"Expected block hash, found illegal hex character at position: {}",
|
||||
2 + pos,
|
||||
))
|
||||
} else {
|
||||
Ok(Self(block_number.into()))
|
||||
}
|
||||
} else {
|
||||
BlockNumber::from_str(block_number).map(|v| Self(v.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockNumberOrHash {
|
||||
/// Parse the inner value as `BlockId`.
|
||||
pub fn parse<B: BlockT>(&self) -> Result<BlockId<B>, String>
|
||||
where
|
||||
B::Hash: FromStr,
|
||||
<B::Hash as FromStr>::Err: std::fmt::Debug,
|
||||
NumberFor<B>: FromStr,
|
||||
<NumberFor<B> as FromStr>::Err: std::fmt::Debug,
|
||||
{
|
||||
if self.0.starts_with("0x") {
|
||||
Ok(BlockId::Hash(
|
||||
FromStr::from_str(&self.0[2..])
|
||||
.map_err(|e| format!("Failed to parse block hash: {:?}", e))?
|
||||
))
|
||||
} else {
|
||||
BlockNumber(self.0.clone()).parse().map(BlockId::Number)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
|
||||
type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
|
||||
|
||||
#[test]
|
||||
fn parse_block_number() {
|
||||
let block_number_or_hash = BlockNumberOrHash::from_str("1234").unwrap();
|
||||
let parsed = block_number_or_hash.parse::<Block>().unwrap();
|
||||
assert_eq!(BlockId::Number(1234), parsed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_block_hash() {
|
||||
let hash = sp_core::H256::default();
|
||||
let hash_str = format!("{:?}", hash);
|
||||
let block_number_or_hash = BlockNumberOrHash::from_str(&hash_str).unwrap();
|
||||
let parsed = block_number_or_hash.parse::<Block>().unwrap();
|
||||
assert_eq!(BlockId::Hash(hash), parsed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_block_hash_fails() {
|
||||
assert_eq!(
|
||||
"Expected block hash, found illegal hex character at position: 2",
|
||||
BlockNumberOrHash::from_str("0xHello").unwrap_err(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_block_number_fails() {
|
||||
assert_eq!(
|
||||
"Expected block number, found illegal digit at position: 3",
|
||||
BlockNumberOrHash::from_str("345Hello").unwrap_err(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user