Skip to content

feat(forge): native support for simulating the passage of time when running invariants #4994

@PaulRBerg

Description

@PaulRBerg

Component

Forge

Describe the feature you would like

Because most smart contract systems expect time to pass, it is a common need to simulate the passage of time (with vm.warp) when running invariant test campaigns with Foundry.

But this is difficult to do today because state is not preserved during invariants runs. Just look at Maple Finance's solution - they have to apply the useCurrentTimestamp modifier to all invariant tests, as well as all functions in the handler contracts.

Now, regardless of the state-preserving limitation and if and when it will be lifted, I posit that it would be helpful to offer native support for time warps in invariants.

Here's what I have in mind :

[profile.default.invariant]
  time_jump_min = 50
  time_jump_max = 1000

Nevermind the names - we can think about what would fit best later. What matters is the idea - to introduce two new config options which would be used for bounding a time jump that gets applied after every invariant test run, i.e. have Foundry do something like this:

modifier jump() {
    uint256 timeWarp = block.timestamp;
    timeWarp = _bound(timeWarp, 50 seconds, 1000 seconds);
    timestampStore.increaseCurrentTimestamp(timeWarp);
    vm.warp(timestampStore.currentTimestamp());
    _;
}

This way, an invariant test campaign would more accurately simulate the real world, where time passes between function calls.

Additional context

Related discussions:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Completed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions