Skip to content

Fixed expectRevert being applied to calls to cheatcodes #1012

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

lisandrasilva
Copy link
Contributor

This PR fixes the expectRevert cheatcode implementation. According to the Foundry documentation:

After calling expectRevert, calls to other cheatcodes before the reverting call are ignored.
This means, for example, we can call prank immediately before the reverting call.

In the previous implementation, the call to the cheatcode was being ignored in the sense that was not being executed. This PR still executes cheatcode calls but does not check if these calls revert or not.

Fixes #993.

@lisandrasilva lisandrasilva self-assigned this Apr 10, 2025
@lisandrasilva lisandrasilva added the bug Something isn't working label Apr 10, 2025
<expectedRevert>
<isRevertExpected> true </isRevertExpected>
<expectedDepth> CD </expectedDepth>
...
</expectedRevert>
requires ACCTTO =/=Int #address(FoundryCheat)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this check also have to be added to the rule [foundry.set.expectrevert.2] below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's possible/likely to delegate a call to the cheatcode address?

Copy link
Contributor

@anvacaru anvacaru Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into this and wrote two tests, one for delegatecall and one for staticcall calling a cheatcode.

The assertion at the end fails for delegatecall, whereas it does not fail for staticcall.

    function testDelegatecallCheatcodeReverts() external {
        (bool success, ) = VM_ADDRESS.delegatecall(
            abi.encodeWithSignature("roll(uint256)", 1337)
        );
        assertTrue(success);
        assertEq(block.number, 1337);
    }

    function testStaticcallcallCheatcode() external view {
        (bool success, ) = VM_ADDRESS.staticcall(
            abi.encodeWithSignature("roll(uint256)", 1337)
        );
        assertTrue(success);
        assertEq(block.number, 1337);
    }

So maybe it's worth adding the rule to cover staticcall for now. Next, in another issue/PR, we should check that Kontrol has the same behavior and ensure that delegating into VM_ADDRESS does not execute the cheat code.

@palinatolmach what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it sounds good! Let's add a rule for staticcall here, and ensure that Kontrol behaves similarly wrt delegatecall as a follow-up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do it!
I also tried the following tests with Foundry:

function testIncrementAsNotOwner_2() public {
        vm.expectRevert(Unauthorized.selector);
        (bool success, ) = VM_ADDRESS.delegatecall(
            abi.encodeWithSignature("roll(uint256)", 1337)
        );
        upOnly.increment();
    }

And this test is passing when it shouldn't, since the function does not revert as expected. On the other hand, the following test fails:

function testIncrementAsNotOwner_3() public {
        vm.expectRevert(Unauthorized.selector);
        (bool success, ) = VM_ADDRESS.delegatecall(
            abi.encodeWithSignature("roll(uint256)", 1337)
        );
        vm.prank(address(0));
        upOnly.increment();
    }

It seems that delegatecall to VM_ADDRESS clears the expectRevert cheatcode.

<expectedRevert>
<isRevertExpected> true </isRevertExpected>
<expectedDepth> CD </expectedDepth>
...
</expectedRevert>
requires ACCTTO ==Int #address(FoundryCheat)
[priority(32)]
Copy link
Contributor Author

@lisandrasilva lisandrasilva Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anvacaru and @palinatolmach, I added this rule, and now Kontrol mimics the same behavior as Foundry for the expectRevert cheatcode.
We still have to address cheatcodes being ignored when called through DELEGATECALL in another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Align expectRevert and prank behavior with Foundry
3 participants