foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(cheatcodes): add `vm.storeTransient` and `vm.loadTransient`

Open 0xteddybear opened this issue 1 year ago • 4 comments

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 avatar Jun 14 '24 16:06 0xteddybear

this is related to #6908

0xteddybear avatar Jun 14 '24 16:06 0xteddybear

@klkvr would this be possible with --isolate?

mattsse avatar Jun 14 '24 16:06 mattsse

@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

klkvr avatar Jun 14 '24 17:06 klkvr

It would be great to have a vm.clearTransientStorage() cheat as well, like mentioned here: https://github.com/foundry-rs/foundry/issues/6908

MathisGD avatar May 30 '25 14:05 MathisGD