Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inspect): fix panics within wei_to_ether, decode #239

Merged
merged 1 commit into from
Dec 14, 2023
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
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
Loading