From 1cc97a4efad595cf8786e7f7d27cfa1fbdea297c Mon Sep 17 00:00:00 2001 From: Ilias Tsatiris Date: Wed, 23 Oct 2024 15:57:41 +0300 Subject: [PATCH 1/2] fix(anvil): Apply state overrides in debug_traceCall Co-authored-by: mixy1 --- crates/anvil/src/eth/backend/mem/mod.rs | 8 +++- crates/anvil/tests/it/traces.rs | 50 ++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 0414b013cbdd..a0408f568202 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1430,13 +1430,19 @@ impl Backend { block_request: Option, opts: GethDebugTracingCallOptions, ) -> Result { - let GethDebugTracingCallOptions { tracing_options, block_overrides: _, state_overrides: _ } = + let GethDebugTracingCallOptions { tracing_options, block_overrides: _, state_overrides } = opts; let GethDebugTracingOptions { config, tracer, tracer_config, .. } = tracing_options; self.with_database_at(block_request, |state, block| { let block_number = block.number; + let state = if let Some(overrides) = state_overrides { + Box::new(state::apply_state_override(overrides, state)?) as Box + } else { + state + }; + if let Some(tracer) = tracer { return match tracer { GethDebugTracerType::BuiltInTracer(tracer) => match tracer { diff --git a/crates/anvil/tests/it/traces.rs b/crates/anvil/tests/it/traces.rs index aaa2ca298d14..c3928ee38d68 100644 --- a/crates/anvil/tests/it/traces.rs +++ b/crates/anvil/tests/it/traces.rs @@ -5,12 +5,16 @@ use crate::{ }; use alloy_eips::BlockId; use alloy_network::{EthereumWallet, TransactionBuilder}; -use alloy_primitives::{hex, Address, Bytes, U256}; +use alloy_primitives::{ + hex::{self, FromHex}, + Address, Bytes, U256, +}; use alloy_provider::{ ext::{DebugApi, TraceApi}, Provider, }; use alloy_rpc_types::{ + state::StateOverride, trace::{ filter::{TraceFilter, TraceFilterMode}, geth::{ @@ -259,6 +263,50 @@ async fn test_call_tracer_debug_trace_call() { } } +#[tokio::test(flavor = "multi_thread")] +async fn test_debug_trace_call_state_override() { + let (_api, handle) = spawn(NodeConfig::test()).await; + let wallets = handle.dev_wallets().collect::>(); + + let tx = TransactionRequest::default() + .from(wallets[1].address()) + .to("0x1234567890123456789012345678901234567890".parse().unwrap()); + + let override_json = r#"{ + "0x1234567890123456789012345678901234567890": { + "balance": "0x01", + "code": "0x30315f5260205ff3" + } + }"#; + + let state_override: StateOverride = serde_json::from_str(override_json).unwrap(); + + let tx_traces = handle + .http_provider() + .debug_trace_call( + tx.clone(), + BlockId::latest(), + GethDebugTracingCallOptions::default() + .with_tracing_options(GethDebugTracingOptions::default()) + .with_state_overrides(state_override), + ) + .await + .unwrap(); + + match tx_traces { + GethTrace::Default(trace_res) => { + assert_eq!( + trace_res.return_value, + Bytes::from_hex("0000000000000000000000000000000000000000000000000000000000000001") + .unwrap() + ); + } + _ => { + unreachable!() + } + } +} + // #[tokio::test(flavor = "multi_thread")] async fn test_trace_address_fork() { From 422288b01476efccabbe1aab8522561c23d74b52 Mon Sep 17 00:00:00 2001 From: Ilias Tsatiris Date: Wed, 23 Oct 2024 17:58:14 +0300 Subject: [PATCH 2/2] chore(anvil): fix formatting --- crates/anvil/src/eth/backend/mem/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index a0408f568202..383ddc21419e 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1438,7 +1438,8 @@ impl Backend { let block_number = block.number; let state = if let Some(overrides) = state_overrides { - Box::new(state::apply_state_override(overrides, state)?) as Box + Box::new(state::apply_state_override(overrides, state)?) + as Box } else { state };