fix(core): block FromStr implementation (#2155)

This commit is contained in:
DaniPopes 2023-02-14 22:15:58 +01:00 committed by GitHub
parent d8597202ed
commit a69036f07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 11 deletions

View File

@ -535,8 +535,7 @@ impl FromStr for BlockId {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.strip_prefix("0x").unwrap_or(s); if s.starts_with("0x") && s.len() == 66 {
if s.len() == 64 {
let hash = s.parse::<H256>().map_err(|e| e.to_string()); let hash = s.parse::<H256>().map_err(|e| e.to_string());
hash.map(Self::Hash) hash.map(Self::Hash)
} else { } else {
@ -645,15 +644,10 @@ impl FromStr for BlockNumber {
"safe" => Ok(Self::Safe), "safe" => Ok(Self::Safe),
"earliest" => Ok(Self::Earliest), "earliest" => Ok(Self::Earliest),
"pending" => Ok(Self::Pending), "pending" => Ok(Self::Pending),
n => { // hex
if let Ok(n) = n.parse::<U64>() { n if n.starts_with("0x") => n.parse().map(Self::Number).map_err(|e| e.to_string()),
Ok(Self::Number(n)) // decimal
} else if let Ok(n) = n.parse::<u64>() { n => n.parse::<u64>().map(|n| Self::Number(n.into())).map_err(|e| e.to_string()),
Ok(Self::Number(n.into()))
} else {
Err("Invalid block number".into())
}
}
} }
} }
} }