foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
7.87k stars 1.58k forks source link

Feature request: `vm.storeTransient` and `vm.loadTransient` #8165

Open 0xteddybear opened 2 weeks ago

0xteddybear commented 2 weeks ago

Component

Forge

Describe the feature you would like

A cheatcode that sets a contract's transient storage slot to a particular value, and another one allowing to read it

Additional context

It'd be consistent to have methods to read/write transient storage similar to those that already perform those operations on regular storage

They are useful for mocks & unit testing of contracts using transient storage, and would be extra useful given there are currently no solidity variables of transient type and that makes it more cumbersome to write mocks by hand or with automated mocking tools such as smock-foundry

Setting the transient storage slot from outside the contract enables devs to do less mocking

0xteddybear commented 2 weeks ago

this is related to #6908

mattsse commented 2 weeks ago

@klkvr would this be possible with --isolate?

klkvr commented 2 weeks ago

@klkvr would this be possible with --isolate?

only if cheatcode is invoked inside of isolated subcall

contract TransientAssert {
    function assertLocked() external {
        bool locked;
        assembly { locked := tload(0) }
        assertTrue(locked);
    }
}

contract TestTransient {
    TransientAssert target = new TransientAssert();

    // passes only without --isolate
    function test_root() {
        vm.tstore(target, bytes32(0), bytes32(1));
        target.assertLocked();
    }

    // passes always
    function test_subcall() {
        this.test_root();
    }
}

we can probably allow it always, even though I think testing transient storage makes sense only with --isolate