From 9793945fb05634f49f8adee7c94cc4595e238b45 Mon Sep 17 00:00:00 2001 From: Jon-Becker Date: Wed, 13 Dec 2023 18:34:01 -0600 Subject: [PATCH] fix(inspect): fix panics within `wei_to_ether`, `decode` --- core/src/decode/mod.rs | 4 +- core/src/inspect/core/tracing.rs | 13 +++++-- core/tests/test_inspect.rs | 65 +++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/core/src/decode/mod.rs b/core/src/decode/mod.rs index afd9a1ed..bcfda696 100644 --- a/core/src/decode/mod.rs +++ b/core/src/decode/mod.rs @@ -127,7 +127,9 @@ pub async fn decode(args: DecodeArgs) -> Result, Error> { if TRANSACTION_HASH_REGEX.is_match(&args.target).unwrap() { // We are decoding a transaction hash, so we need to fetch the calldata from the RPC // provider. - raw_transaction = get_transaction(&args.target, &args.rpc_url).await.unwrap(); + raw_transaction = get_transaction(&args.target, &args.rpc_url).await.map_err(|_| { + Error::RpcError("failed to fetch transaction from RPC provider.".to_string()) + })?; calldata = raw_transaction.input.to_string().replacen("0x", "", 1); } else if CALLDATA_REGEX.is_match(&args.target).unwrap() { diff --git a/core/src/inspect/core/tracing.rs b/core/src/inspect/core/tracing.rs index 3ea3364e..a61c3da4 100644 --- a/core/src/inspect/core/tracing.rs +++ b/core/src/inspect/core/tracing.rs @@ -12,6 +12,7 @@ use ethers::{ }, }; use heimdall_common::{ + debug_max, ether::signatures::ResolvedFunction, utils::{ env::get_env, @@ -526,7 +527,13 @@ impl DecodedTransactionTrace { } fn wei_to_ether(wei: U256) -> f64 { - // convert U256 to u64 - let wei = wei.as_u64() as f64; - wei / 10f64.powf(18.0) + // convert U256 to u64 safely + let wei_f64 = wei.min(U256::from(u64::MAX)).as_u64() as f64; + + // if wei = u64::MAX, log that it was truncated + if wei_f64 == u64::MAX as f64 { + debug_max!("WARNING: wei value was truncated to u64::MAX. Original value: {}", wei); + } + + wei_f64 / 10f64.powf(18.0) } diff --git a/core/tests/test_inspect.rs b/core/tests/test_inspect.rs index c3bca568..44bb8279 100644 --- a/core/tests/test_inspect.rs +++ b/core/tests/test_inspect.rs @@ -1,7 +1,13 @@ #[cfg(test)] mod integration_tests { + use std::{ + sync::{Arc, Mutex}, + thread, + time::{Duration, Instant}, + }; + use clap_verbosity_flag::Verbosity; - use heimdall_common::utils::{sync::blocking_await, threading::task_pool}; + use heimdall_common::utils::threading::task_pool; use heimdall_core::inspect::{InspectArgs, InspectArgsBuilder}; use serde_json::Value; @@ -61,31 +67,44 @@ mod integration_tests { // task_pool(items, num_threads, f) let results = task_pool(txids, 10, |txid: String| { - let args = InspectArgsBuilder::new() - .target(txid.to_string()) - .verbose(Verbosity::new(-1, 0)) - .rpc_url("https://eth.llamarpc.com".to_string()) - .skip_resolving(true) - .build() - .unwrap(); - - blocking_await(move || { - // get new blocking runtime + let txid_for_thread = txid.clone(); // Clone txid for use in the thread + let finished = Arc::new(Mutex::new(false)); // Shared state to communicate between threads + let finished_for_thread = finished.clone(); + + let handle = thread::spawn(move || { + let args = InspectArgsBuilder::new() + .target(txid_for_thread.clone()) + .verbose(Verbosity::new(-1, 0)) + .rpc_url("https://eth.llamarpc.com".to_string()) + .skip_resolving(true) + .build() + .unwrap(); + let rt = tokio::runtime::Runtime::new().unwrap(); + let result = rt.block_on(heimdall_core::inspect::inspect(args)); + + *finished_for_thread.lock().unwrap() = true; // Signal that processing is finished + + result + }); - // get the storage diff for this transaction - println!("inspecting txid: {}", txid); - match rt.block_on(heimdall_core::inspect::inspect(args)) { - Ok(_) => { - println!("inspecting txid: {} ... succeeded", txid); - 1 - } - Err(_) => { - println!("inspecting txid: {} ... failed", txid); - 0 - } + let start_time = Instant::now(); + loop { + if *finished.lock().unwrap() { + break; // Exit loop if processing is finished } - }) + + if start_time.elapsed() > Duration::from_secs(60) { + println!("inspecting txid: {} ... slow", txid); + } + + thread::sleep(Duration::from_millis(100)); + } + + match handle.join().unwrap() { + Ok(_) => 1, + Err(_) => 0, + } }); let success_count = results.iter().filter(|r| **r == 1).count();