Skip to content

feat: expectCall and mockCall #382

@tarrencev

Description

@tarrencev

following up on a telegram convo here to track:

Would it be possible to implement a cheat code like this, that reverts if the condition is not met before the end of the test:

// Asserts that a call was made, doesn't modify the execution
function expectCall(address contract, bytes memory call) external;

expectCall(bridge, abi.encodeWithSelector(
    IRollCallBridge.queue.selector,
    governor,
    ...
))

// Mocks a call, asserting it was called and returning the provided return value
function mockCall(address contract, bytes memory call, bytes memory return) external;

mockCall(bridge, abi.encodeWithSelector(
    IRollCallBridge.queue.selector,
    governor,
    ...
), abi.encode(uint256(420)))

Re: mock

My thought was I often have fakes like:


contract OVM_FakeL1BlockNumber is iOVM_L1BlockNumber {
    uint256 private blocknumber;

    function setL1BlockNumber(uint256 n) external {
        blocknumber = n;
    }

    function getL1BlockNumber() external view override returns (uint256) {
        return blocknumber;
    }
}


Used like


// setup
blocknumber = OVM_FakeL1BlockNumber(
    Lib_PredeployAddresses.L1_BLOCK_NUMBER
);
vm.etch(
    Lib_PredeployAddresses.L1_BLOCK_NUMBER,
    type(OVM_FakeL1BlockNumber).runtimeCode
);
...
blocknumber.setL1BlockNumber(5);
// call that uses `getL1BlockNumber()


Which could be replaced with:


vm.mockCall(Lib_PredeployAddresses.L1_BLOCK_NUMBER, abi.encodeWithSelector(iOVM_L1BlockNumber.getL1BlockNumber.selector), abi.encode(5));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions