Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn parse_block_id(block: &str) -> Result<String, TraceError> {
let num: u64 = s.parse().map_err(|_| {
TraceError::InvalidInput(format!("--block: invalid block identifier '{s}'"))
})?;
Ok(format!("0x{num:x}"))
Ok(format!("{num:#x}"))
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/cli/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::utils::disk_cache::{CacheError, DiskCache, ALL_CACHE_KINDS};
use clap::Parser;
use std::fs;
use std::path::Path;
use thiserror::Error;

#[derive(Parser, Debug)]
pub struct CleanArgs {
Expand All @@ -11,17 +10,7 @@ pub struct CleanArgs {
pub only_unknown: bool,
}

#[derive(Debug, Error)]
pub enum CleanError {
#[error("{0}")]
Cache(#[from] CacheError),

#[error("io error: {0}")]
Io(#[from] std::io::Error),
}

#[allow(clippy::needless_pass_by_value)] // clap produces owned values
pub fn run(args: CleanArgs) -> Result<(), CleanError> {
pub fn run(args: &CleanArgs) -> Result<(), CacheError> {
let mut found_any = false;

for kind in ALL_CACHE_KINDS {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
match args.command {
cli::Command::Tx(tx_args) => cli::tx::run(tx_args)?,
cli::Command::Call(call_args) => cli::call::run(call_args)?,
cli::Command::Clean(clean_args) => cli::clean::run(clean_args)?,
cli::Command::Clean(ref clean_args) => cli::clean::run(clean_args)?,
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/utils/disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,6 @@ mod tests {
cache.insert_transient_miss("0xdeadbeef".to_owned());
assert!(matches!(cache.lookup("0xdeadbeef"), CacheLookup::Miss));
assert!(!cache.entries.contains_key("0xdeadbeef"));
assert!(cache.transient_misses.contains("0xdeadbeef"));
}
}
30 changes: 14 additions & 16 deletions src/utils/hex_utils.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use alloy_primitives::U256;

pub fn strip_0x(s: &str) -> &str {
s.strip_prefix("0x")
.or_else(|| s.strip_prefix("0X"))
.unwrap_or(s)
}

pub fn require_0x(s: &str) -> Option<&str> {
s.strip_prefix("0x").or_else(|| s.strip_prefix("0X"))
}

pub fn strip_0x(s: &str) -> &str {
require_0x(s).unwrap_or(s)
}

pub fn is_valid_address(s: &str) -> bool {
require_0x(s).is_some_and(|h| h.len() == 40 && h.chars().all(|c| c.is_ascii_hexdigit()))
require_0x(s).is_some_and(|h| h.len() == 40 && h.bytes().all(|b| b.is_ascii_hexdigit()))
}

pub fn is_valid_tx_hash(s: &str) -> bool {
require_0x(s).is_some_and(|h| h.len() == 64 && h.chars().all(|c| c.is_ascii_hexdigit()))
require_0x(s).is_some_and(|h| h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit()))
}

pub fn parse_hex_u256(s: &str) -> Option<U256> {
Expand All @@ -27,6 +25,14 @@ pub fn parse_hex_u256(s: &str) -> Option<U256> {
mod tests {
use super::*;

#[test]
fn test_require_0x() {
assert_eq!(require_0x("0xabc"), Some("abc"));
assert_eq!(require_0x("0Xabc"), Some("abc"));
assert_eq!(require_0x("abc"), None);
assert_eq!(require_0x("0x"), Some(""));
}

#[test]
fn test_strip_0x() {
assert_eq!(strip_0x("0xabc"), "abc");
Expand All @@ -36,14 +42,6 @@ mod tests {
assert_eq!(strip_0x("0X"), "");
}

#[test]
fn test_require_0x() {
assert_eq!(require_0x("0xabc"), Some("abc"));
assert_eq!(require_0x("0Xabc"), Some("abc"));
assert_eq!(require_0x("abc"), None);
assert_eq!(require_0x("0x"), Some(""));
}

#[test]
fn test_is_valid_address() {
assert!(is_valid_address(
Expand Down
41 changes: 15 additions & 26 deletions src/utils/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
/// Precompiled contracts are at addresses 0x01-0x0a (and potentially higher in newer forks).
/// Returns `Some((name, signature))` if the address is a known precompile, `None` otherwise.
pub fn get_precompile_info(address: &str) -> Option<(&'static str, &'static str)> {
let addr = super::hex_utils::strip_0x(address);

// Full 40-char addresses: precompiles have 38+ leading zeros.
// Short-circuit: if the non-zero portion is beyond 0x0a, it's not a precompile.
if addr.len() == 40 && !addr[..24].chars().all(|c| c == '0') {
return None;
}

let normalized = if addr.len() == 40 { &addr[24..] } else { addr };

// Lowercase only the short suffix for matching.
let normalized_lower = normalized.to_lowercase();

match normalized_lower.as_str() {
"0000000000000001" | "01" | "1" => {
match super::hex_utils::strip_0x(address)
.trim_start_matches('0')
.to_lowercase()
.as_str()
{
"1" => {
// ecrecover takes 128 bytes: hash(32) + v(32) + r(32) + s(32).
Some(("ecrecover", "ecrecover(bytes32,uint8,uint256,uint256)"))
}
"0000000000000002" | "02" | "2" => Some(("sha256", "sha256(bytes)")),
"0000000000000003" | "03" | "3" => Some(("ripemd160", "ripemd160(bytes)")),
"0000000000000004" | "04" | "4" => Some(("identity", "identity(bytes)")),
"0000000000000005" | "05" | "5" => {
Some(("modexp", "modexp(uint256,uint256,uint256,bytes)"))
}
"0000000000000006" | "06" | "6" => Some(("ecadd", "ecadd(bytes)")),
"0000000000000007" | "07" | "7" => Some(("ecmul", "ecmul(bytes)")),
"0000000000000008" | "08" | "8" => Some(("ecpairing", "ecpairing(bytes)")),
"0000000000000009" | "09" | "9" => Some(("blake2f", "blake2f(bytes)")),
"000000000000000a" | "0a" | "a" => Some(("pointevaluation", "pointevaluation(bytes)")),
"2" => Some(("sha256", "sha256(bytes)")),
"3" => Some(("ripemd160", "ripemd160(bytes)")),
"4" => Some(("identity", "identity(bytes)")),
"5" => Some(("modexp", "modexp(uint256,uint256,uint256,bytes)")),
"6" => Some(("ecadd", "ecadd(bytes)")),
"7" => Some(("ecmul", "ecmul(bytes)")),
"8" => Some(("ecpairing", "ecpairing(bytes)")),
"9" => Some(("blake2f", "blake2f(bytes)")),
"a" => Some(("pointevaluation", "pointevaluation(bytes)")),
_ => None,
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/utils/value_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,36 @@ pub fn parse_value(s: &str) -> Result<String, String> {

if let Some(hex) = hex_utils::require_0x(s) {
let v = U256::from_str_radix(hex, 16).map_err(|_| format!("invalid hex value: {s}"))?;
return Ok(format!("0x{v:x}"));
return Ok(format!("{v:#x}"));
}

if let Some(num_str) = s.strip_suffix("ether") {
let wei = to_wei(num_str, 18)?;
return Ok(format!("0x{wei:x}"));
return Ok(format!("{wei:#x}"));
}

if let Some(num_str) = s.strip_suffix("gwei") {
let wei = to_wei(num_str, 9)?;
return Ok(format!("0x{wei:x}"));
return Ok(format!("{wei:#x}"));
}

if let Some(num_str) = s.strip_suffix("wei") {
let v = U256::from_str_radix(num_str, 10).map_err(|_| format!("invalid value: {s}"))?;
return Ok(format!("0x{v:x}"));
return Ok(format!("{v:#x}"));
}

let v = U256::from_str_radix(s, 10).map_err(|_| format!("invalid value: {s}"))?;
Ok(format!("0x{v:x}"))
Ok(format!("{v:#x}"))
}

/// Convert a possibly-decimal number string to wei given the unit's decimal count.
fn to_wei(num_str: &str, decimals: u32) -> Result<U256, String> {
let num_str = num_str.trim();

let (combined, frac_len) = match num_str.find('.') {
Some(dot) => {
let frac = &num_str[dot + 1..];
if frac.len() > decimals as usize {
fn to_wei(num_str: &str, decimals: usize) -> Result<U256, String> {
let (combined, frac_len) = match num_str.split_once('.') {
Some((int_part, frac)) => {
if frac.len() > decimals {
return Err(format!("too many decimal places: {num_str}"));
}
#[allow(clippy::cast_possible_truncation)] // frac.len() <= 18 (validated above)
(format!("{}{frac}", &num_str[..dot]), frac.len() as u32)
(format!("{int_part}{frac}"), frac.len())
}
None => (num_str.to_string(), 0),
};
Expand Down Expand Up @@ -77,11 +73,12 @@ mod tests {
#[test]
fn test_parse_value_ether() {
assert_eq!(parse_value("1ether").unwrap(), "0xde0b6b3a7640000");
assert_eq!(parse_value("1.ether").unwrap(), "0xde0b6b3a7640000");
assert_eq!(parse_value("0.000000000000000001ether").unwrap(), "0x1");

let result = parse_value("1.010101010101010101ether").unwrap();
let expected = U256::from(1_010_101_010_101_010_101u128);
assert_eq!(result, format!("0x{expected:x}"));
assert_eq!(result, format!("{expected:#x}"));
}

#[test]
Expand Down
Loading