Skip to content

Conversation

w1tcher
Copy link

@w1tcher w1tcher commented Sep 18, 2025

Motivation

This commit introduces support for the PreStateTracer in anvil's
debug_traceTransaction RPC method. With this change, users can now
obtain detailed pre-state information (accounts, storage, and balances)
when tracing transactions, enabling more advanced debugging and state
analysis workflows.

Solution

By storing the state changes generated during transaction execution in the exec_state variable, the PreStateTracer can later retrieve this data and return the relevant information.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@w1tcher thank you! please rebase and add a test, here are some samples

async fn test_call_tracer_debug_trace_call() {

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this works, this is mega extra overhead on top of the overhead of recorded traces that we already have

imo we should instead cleanup the existing replay functionality that we use for the js tracer and make this general purpose replay

gas_used: u64,
logs: Vec<Log>,
traces: Vec<CallTraceNode>,
exec_state: ResultAndState<OpHaltReason>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always storing this is quite expensive, even storing the calltracenode is kind of excessive

we now have a way to replay+trace the transactions, although it isnt super pretty (yet)

/// Traces the transaction with the js tracer
#[cfg(feature = "js-tracer")]
pub async fn trace_tx_with_js_tracer(

instead of storing all state outputs like this we should invest a bit more into making this function more resuable for any kind of tracer

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced a new function, replay_tx_with_inspector, whose main purpose is to replay a transaction by its hash and retrieve its execution result, trace, and other related data for generating the corresponding response. Additionally, I removed the previously added extra data.

    fn replay_tx_with_inspector<I, F, T>(
        &self,
        hash: B256,
        mut inspector: I,
        f: F,
    ) -> Result<T, BlockchainError>
    where
        for<'a> I: Inspector<EthEvmContext<WrapDatabaseRef<&'a CacheDB<Box<&'a StateDb>>>>>
            + Inspector<OpContext<WrapDatabaseRef<&'a CacheDB<Box<&'a StateDb>>>>>
            + 'a,
        for<'a> F:
            FnOnce(ResultAndState<OpHaltReason>, CacheDB<Box<&'a StateDb>>, I, TxEnv, Env) -> T,
    {

@mattsse mattsse self-assigned this Sep 19, 2025
@mattsse mattsse added the C-anvil Command: anvil label Sep 19, 2025
@w1tcher
Copy link
Author

w1tcher commented Sep 19, 2025

@grandizzy I have added test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-anvil Command: anvil
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants