Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function-level assertions don't work when using invariants. #4718

Closed
2 tasks done
wirew0lf opened this issue Apr 11, 2023 · 4 comments · Fixed by #4980 or #5445
Closed
2 tasks done

Function-level assertions don't work when using invariants. #4718

wirew0lf opened this issue Apr 11, 2023 · 4 comments · Fixed by #4980 or #5445
Labels
A-testing Area: testing C-forge Command: forge Cmd-forge-test Command: forge test T-bug Type: bug
Milestone

Comments

@wirew0lf
Copy link

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (388c3c0 2023-04-10T00:15:22.461433000Z)

What command(s) is the bug in?

forge test -m invariant

Operating System

macOS (Apple Silicon)

Describe the bug

According to the invariant testing documentation in the foundry book it is possible to use function-level assertions in a handler. However, this doesn't seem to work. Minimal working example:

Counter.sol

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        if(newNumber != 100) {
            number = newNumber;
        } 
    }
}

CounterHandler.sol

contract CounterHandler is Test {
    Counter counter;

    constructor(Counter _counter) {
        counter = _counter;
    }

    function setNumber(uint256 newNumber) public {
        counter.setNumber(newNumber);
        assertEq(uint(0), uint(1));
        assertTrue(false);
    }
}

CounterInvariants

contract CounterInvariants is Test {
    Counter counter;
    CounterHandler handler;

    function setUp() public {
        counter = new Counter();
        handler = new CounterHandler(counter);
        targetContract(address(handler));
    }

    function invariant_numberNotZero() public {
        assertEq(counter.number() != 100, true);
    }
}

As shown in the CounterHandler contract I've introduced some assertions that should fail such as assertTrue(false) and assertEq(uint(0), uint(1)). However, when running the invariant test it will succeed.
image

@wirew0lf wirew0lf added the T-bug Type: bug label Apr 11, 2023
@gakonst gakonst added this to Foundry Apr 11, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 11, 2023
@mds1
Copy link
Collaborator

mds1 commented Apr 11, 2023

@joshieDo This sounds like a bug in how we check invariant failures? Since the global failure introduced in dapphub/ds-test#30 should allow this to be supported. Also cc'ing @lucas-manuel who wrote the docs

@wirew0lf in the meantime you should be able to work around this by using require statements in your handler contract instead

@mds1 mds1 added A-testing Area: testing Cmd-forge-test Command: forge test C-forge Command: forge labels Apr 11, 2023
@0xfarhaan
Copy link
Contributor

Yeah we've also noticed the same issue, using require statements across the handlers isn't desirable as we have built alot of assertion helpers to assert state across our system.

We really could use this functionality!

@lucas-manuel
Copy link

@mds1 pinging this issue again for visibility, would be a huge unlock for invariant testing.

@mds1
Copy link
Collaborator

mds1 commented Jun 30, 2023

@Evalir since #4980 was reverted by #5013 I've reopened this since I assume it's still an issue then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testing Area: testing C-forge Command: forge Cmd-forge-test Command: forge test T-bug Type: bug
Projects
Archived in project
5 participants