Skip to content

Commit

Permalink
fix(anvil): Apply state overrides in debug_traceCall
Browse files Browse the repository at this point in the history
Co-authored-by: mixy1 <[email protected]>
  • Loading branch information
iliastsa and mixy1 committed Oct 23, 2024
1 parent 4d7435e commit 1cc97a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,13 +1430,19 @@ impl Backend {
block_request: Option<BlockRequest>,
opts: GethDebugTracingCallOptions,
) -> Result<GethTrace, BlockchainError> {
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<dyn MaybeFullDatabase>
} else {
state
};

if let Some(tracer) = tracer {
return match tracer {
GethDebugTracerType::BuiltInTracer(tracer) => match tracer {
Expand Down
50 changes: 49 additions & 1 deletion crates/anvil/tests/it/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<Vec<_>>();

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!()
}
}
}

// <https://github.com/foundry-rs/foundry/issues/2656>
#[tokio::test(flavor = "multi_thread")]
async fn test_trace_address_fork() {
Expand Down

0 comments on commit 1cc97a4

Please sign in to comment.