foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat: add `vm.clearTransient`

Open andreas-blockswap opened this issue 2 years ago • 8 comments

Component

Forge

Describe the feature you would like

In solc version 0.8.24, transactions compose differently than message calls. While adding support for solc 0.8.24 (issue #6903) I think that it will be useful with a cheat code to clear the transient storage, so that we can model transactions with message calls.

For example a cheat code function clearTransientStorage(address) to zero the transient storage. This cheat code can be used to model a transaction a.f(x) with

vm.clearTransientStorage(address(a));
a.f(x);
vm.clearTransientStorage(address(a));

and transaction composition of a.f(x) and a.g(y) can be modeled with

vm.clearTransientStorage(address(a));
a.f(x);
vm.clearTransientStorage(address(a));
a.g(y);
vm.clearTransientStorage(address(a));

Additional context

No response

andreas-blockswap avatar Jan 26 '24 14:01 andreas-blockswap

I think the transient storage context should be split by default for each top-level call in the Foundry test runner.

Also, I would like transient storage to be cleared when explicitly switching the runtime context using prank etc.

kaihiroi avatar Jan 30 '24 07:01 kaihiroi

Hello, was this implemented?

cloud1992 avatar Jul 02 '24 17:07 cloud1992

Through this comment, I learned that running tests with the --isolate option allows the top-level call to be executed as a context-independent tx.

kaihiroi avatar Jul 03 '24 05:07 kaihiroi

@andreas-blockswap does running in --isolate mode meet your requirement?

grandizzy avatar Oct 03 '24 12:10 grandizzy

I think this is still useful, otherwise you are forced to use the whole test suite in isolated to get it to pass in CI? or have some hacks where transient tests get filtered with --mt --mc and both of them be separately ran instead of just a single forge test command in the CI. Is there anyway to clear transient storage atm without this new cheatcode?

0xdapper avatar Oct 14 '24 11:10 0xdapper

What about using inline config to specify specific tests that run in isolate mode, this way a single forge test invocation can be used? I am unsure if this is currently supported, but @0xdapper I think this would address your feedback without needing to add a new cheatcode, and should be safer since you don't have to remember to add calls to a cheat

mds1 avatar Oct 15 '24 03:10 mds1

Nice, yeah, I am in favour of this approach too, inline is not supported atm but can be easily added

grandizzy avatar Oct 15 '24 04:10 grandizzy

Yes, the inline config would address it. But I still think vm.clearTransient, vm.tstore, vm.tload are all useful cheats. Inline would still be more restrictive in the sense that it is all or nothing, and you would need additional hacks like making external self calls(this.performSomethingWithoutContextChange) while with vm.clearTransient you would control when you want to clear. Maybe it is not a useful general pattern but that could potentially be highlighted in the docs that --isolate might normally the preferred way to test things if you are looking to test some transient state functions?

0xdapper avatar Oct 15 '24 11:10 0xdapper

ref https://github.com/foundry-rs/foundry/issues/5371

grandizzy avatar Nov 19 '24 13:11 grandizzy