0

I'm trying to simulate a transaction to observe the state change and events that will be emitted, before sending the transaction to the blockchain. I'm interested in all the internal calls and state changes. I've looked into eth_call but it only returns the contract return value and not events or changed states. What are the ways in which I can achieve this? I understand that there are no straightforward ways so I'm open to any possible implementation ideas.

1
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Ethereum Meta, or in Ethereum Chat. Comments continuing discussion may be removed.
    – eth
    Commented Aug 22, 2023 at 11:19

2 Answers 2

2

This is what I would do: I would run a local fork of mainnet using hardhat node provider_url and do the transaction there, while capturing the events and reading the state changes that resulted afterwards.

3
  • 1
    Thanks for the suggestion Julissa, it worked like a charm! However, I wish to reset the fork and sync with the mainnet periodically. As per documentation, I created a task - task("reset_fork", "Reset", async (taskArgs, hre) => { await hre.network.provider.request({ method: "hardhat_reset", params: [{forking: { jsonRpcUrl: "<URL>",},},],})}) But when I do npx hardhat reset_fork, it doesn't seem to reset. Local chain is still in its previous forked state. What am I missing?
    – pbsh
    Commented Jan 11, 2022 at 13:07
  • Happy it worked. As per the second question I think you should create a new question with that, I don't see whats wrong.
    – Julissa DC
    Commented Jan 11, 2022 at 13:24
  • 1
    I put together a working prototype, which can simulate transactions and sync with the mainnet periodically. But each transaction simulation takes around 15-30 seconds (eg, token swap on uniswap). Is there any kind of optimisation possible here?
    – pbsh
    Commented Jan 12, 2022 at 11:16
0

You are wanting the RPC method eth_simulateTransaction[^1]. Tenderly [^2] offers such an RPC method.

Some additional repositories that may help you develop a solution:

https://github.com/manifoldfinance/bundle-simulator/tree/feat-blockreplay https://github.com/rmeissner/safe-simulator-gh-action
https://github.com/rmeissner/safe-simulator

EVM Performance

As for performance, certain "interpreter abort" conditions within the EVM interpreter are currently checked for on every interpretation step, such as Out-of-Gas, Stack Overflow, etc. When one of these conditions is found to apply, the currently-executing call frame is immediately escaped, with any state modifications made by that call frame and its descendants being reverted, just as if a user-triggered REVERT instruction were executed.[^3]

Now, EVM formal semantics make no requirement to perform these checks for each interpretation step. This implicit pseudo-requirement makes applying runtime optimizations to EVM interpretation, such as Just-In-Time compilation or threaded-code interpretation, mostly pointless, as either every instruction implementation needs to contain/generate redundant copies of these same checks, or trampolines to the interpreter to perform these checks need to be inserted between successive instructions. [^3]

Adapting a new constraint-based approach for speculative execution for the EVM is the only real way to speed up such simulations, as the interpreter check is orthogonal to all EVM throughput issues. In contrast to the traditional approach of predicting a single future and demanding it to be perfectly accurate, a speculative EVM can, on multiple futures, leverage speculative results based on imperfect predictions whenever certain constraints are satisfied.

Under these constraints, transaction execution can be substantially accelerated providing a faster response time.

Additionally, securerpc.com will be introducing an RPC method with such functionality. Note, I am the co-founder of Manifold Finance which operates SecureRpc.

References:
[1]: https://github.com/ethereum/EIPs/pull/2560
[2]: https://tenderly.co/
[3]: https://github.com/tsutsu
quoted from tsutsu, covalent-hq and erigion contributor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.