Skip to content

Commit

Permalink
fix(inspect): fix panics within wei_to_ether, decode (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Becker authored Dec 14, 2023
1 parent 33ff781 commit 7c7ca04
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
4 changes: 3 additions & 1 deletion core/src/decode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ pub async fn decode(args: DecodeArgs) -> Result<Vec<ResolvedFunction>, 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() {
Expand Down
13 changes: 10 additions & 3 deletions core/src/inspect/core/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ethers::{
},
};
use heimdall_common::{
debug_max,
ether::signatures::ResolvedFunction,
utils::{
env::get_env,
Expand Down Expand Up @@ -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)
}
65 changes: 42 additions & 23 deletions core/tests/test_inspect.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 7c7ca04

Please sign in to comment.