foundry icon indicating copy to clipboard operation
foundry copied to clipboard

fix: nonce correction logic

Open klkvr opened this issue 9 months ago • 0 comments

Motivation

Current approach with nonce correction is not really straightforward as nonce is getting corrected only when invoking certain cheats.

Currently this results in issues when executing the following example:

contract SimpleScript is Script {
    function setUp() external {
        uint256 initialFork = vm.activeFork();
        vm.createSelectFork("<url>");
        vm.selectFork(initialFork);
    }

    function run() external {
        assert(vm.getNonce(msg.sender) == 0);
    }
}

Specifically, the issue is in this line: https://github.com/foundry-rs/foundry/blob/503792a1dbadd901a4c02f6fcd1de1caff1573ff/crates/cheatcodes/src/evm/fork.rs#L119-L122

We assume here that we've either corrected nonce on the current fork or will not come back to it. This could've been fixed by adding correct_sender_nonce invocation but imo having invocations of it in a lot of random places is not great and clean.

What we are trying to do here is to prevent run() and setUp() from increasing --sender nonce. Technically this is as simple as avoiding call with depth 0 increasing tx.origin nonce.

Solution

This PR changes flow for nonce correction by always applying it in call hook for top-level call seen by cheatcodes inspector.

This results in sender nonce being corrected regardless of the script logic and allows us to remove the corrected_nonce flag.

klkvr avatar May 10 '24 21:05 klkvr