bluealloy / revm

Rust implementation of the Ethereum Virtual Machine.
https://bluealloy.github.io/revm/
MIT License
1.59k stars 518 forks source link

feat(testing): support symbolic accounts with fresh storage #1747

Closed grandizzy closed 1 week ago

grandizzy commented 1 week ago

(opening this draft PR to get initial thoughts and if OK to have this impl in revm, thank you!)

Motivation

Re symbolic testing (atm with Kontrol) in foundry https://github.com/foundry-rs/foundry/issues/4072#issuecomment-1456844543

There's a need for a new freshStorage(address target) cheatcode where storage of target contract should follow rules below:

For example, for a contract like:

contract Counter {
    address[] public owners;

    function getOwner(uint256 pos) public view returns (address) {
        return owners[pos];
    }

    function setOwner(uint256 pos, address owner) public {
        owners[pos] = owner;
    }
}

a test like the one bellow should result in PASS

function test_fresh_storage(uint256 a) public {
    a = bound(a, 0, 100);

    Counter counter = new Counter();
    vm.freshStorage(address(counter));

    // Next call would fail with array out of bounds without freshStorage
    address owner = counter.getOwner(a);
    // Subsequent calls should retrieve same value
    assertEq(counter.getOwner(a), owner);
    // Change slot and make sure new value retrieved
    counter.setOwner(a, address(111));
    assertEq(counter.getOwner(a), address(111));
}

Solution

grandizzy commented 1 week ago

@rakita @mattsse @DaniPopes could you please have a look at let me know if the right way to go, if so will follow up on working the random related failures. thank you!

rakita commented 1 week ago

This does not look like a good fit for Revm. Couldn't you add a logic in step_end that would override the sload value?

grandizzy commented 1 week ago

This does not look like a good fit for Revm. Couldn't you add a logic in step_end that would override the sload value?

yeah, can do that, just wanted to check if somehow needed here too. thank you, going to close this then.