Skip to content
New issue

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

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

Already on GitHub? # to your account

expectRevert cheat code doesn't work for internal calls #864

Closed
2 tasks done
cruzdanilo opened this issue Mar 8, 2022 · 5 comments
Closed
2 tasks done

expectRevert cheat code doesn't work for internal calls #864

cruzdanilo opened this issue Mar 8, 2022 · 5 comments
Assignees
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test D-hard Difficulty: hard P-normal Priority: normal T-bug Type: bug

Comments

@cruzdanilo
Copy link
Contributor

cruzdanilo commented Mar 8, 2022

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

0.1.0 (39b6e39 2022-03-08T00:08:31.449073+00:00)

What command(s) is the bug in?

forge test

Operating System

macOS (M1)

Describe the bug

when trying to test internal calls (like libraries), expectRevert cheat code doesn't work.

example code:

pragma solidity ^0.8.4;

import { DSTest } from "ds-test/test.sol";
import { CheatCodes } from "./utils/CheatCodes.sol";

library Library {
    function willRevert() internal pure {
        revert("reason");
    }
}

contract RevertTest is DSTest {
    CheatCodes private constant VM = CheatCodes(HEVM_ADDRESS);

    function testExternal() external {
        VM.expectRevert("reason");
        this.willRevert();
    }

    function testInternal() external {
        VM.expectRevert("reason");
        willRevert();
    }

    function testLibrary() external {
        VM.expectRevert("reason");
        Library.willRevert();
    }

    function willRevert() public pure {
        revert("reason");
    }
}

forge test output:

Running 3 tests for RevertTest.json:RevertTest
[PASS] testExternal() (gas: 3762)
Traces:

  [3762] RevertTest::testExternal()
    ├─ [0] VM::expectRevert(reason)
    │   └─ ← ()
    ├─ [326] RevertTest::willRevert()
    │   └─ ← "reason"
    └─ ← ()

[FAIL. Reason: reason] testInternal() (gas: 3299)
Traces:

  [3299] RevertTest::testInternal()
    ├─ [0] VM::expectRevert(reason)
    │   └─ ← ()
    └─ ← "reason"

[FAIL. Reason: reason] testLibrary() (gas: 3233)
Traces:

  [3233] RevertTest::testLibrary()
    ├─ [0] VM::expectRevert(reason)
    │   └─ ← ()
    └─ ← "reason"


Failed tests:
[FAIL. Reason: reason] testInternal() (gas: 3299)
[FAIL. Reason: reason] testLibrary() (gas: 3233)
@cruzdanilo cruzdanilo added the T-bug Type: bug label Mar 8, 2022
@cruzdanilo cruzdanilo changed the title expectRevert cheat code doesn't work for library calls expectRevert cheat code doesn't work for internal calls Mar 8, 2022
@gakonst
Copy link
Member

gakonst commented Mar 8, 2022

Yep- this is because the vm.expectRevert call hooks on CALL, when we need it to hook on jumps etc. @onbjerg and I talked about this juts now, allowing this to work would require some deeper hooking into solc internals

@onbjerg
Copy link
Member

onbjerg commented Mar 9, 2022

Ref #432

@onbjerg onbjerg added A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test D-hard Difficulty: hard P-normal Priority: normal labels Mar 9, 2022
@onbjerg onbjerg self-assigned this Mar 9, 2022
@onbjerg
Copy link
Member

onbjerg commented Mar 9, 2022

Assigning myself to this as I want to work on it, but it is going to be a while until it lands since it also requires some changes in ethers-solc.

@onbjerg
Copy link
Member

onbjerg commented Jul 1, 2022

Tentatively added to stable milestone but it may prove infeasible in which case I will report back here

@mds1
Copy link
Collaborator

mds1 commented Mar 9, 2023

Closing in favor of generic JUMP support for a range of cheats, that will be tracked in #4439

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test D-hard Difficulty: hard P-normal Priority: normal T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

4 participants