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

feat(cheatcodes): disallow usage of expectRevert with expectCall and expectEmit #5144

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions evm/src/executor/inspector/cheatcodes/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ fn expect_revert(state: &mut Cheatcodes, reason: Option<Bytes>, depth: u64) -> R
Ok(Bytes::new())
}

fn expect_emit(
Copy link
Member

Choose a reason for hiding this comment

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

I like having this as standalone fn

Copy link
Member Author

Choose a reason for hiding this comment

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

yeh my objective over time is to make handling cheatcodes a bit more state-machine like, with the ability to compose error messages and add hints and whatnot. This is a small part of it (breaking everything down into functions instead of inlining everything)

state: &mut Cheatcodes,
address: Option<H160>,
depth: u64,
checks: [bool; 4],
) -> Result {
state.expected_emits.push_back(ExpectedEmit { depth, address, checks, ..Default::default() });
Ok(Bytes::new())
}

#[instrument(skip_all, fields(expected_revert, status, retdata = hex::encode(&retdata)))]
pub fn handle_expect_revert(
is_create: bool,
Expand Down Expand Up @@ -347,39 +357,26 @@ pub fn apply<DB: DatabaseExt>(
expect_revert(state, Some(inner.0.into()), data.journaled_state.depth())
}
HEVMCalls::ExpectEmit0(_) => {
state.expected_emits.push_back(ExpectedEmit {
depth: data.journaled_state.depth(),
checks: [true, true, true, true],
..Default::default()
});
Ok(Bytes::new())
}
HEVMCalls::ExpectEmit1(inner) => {
state.expected_emits.push_back(ExpectedEmit {
depth: data.journaled_state.depth(),
checks: [true, true, true, true],
address: Some(inner.0),
..Default::default()
});
Ok(Bytes::new())
}
HEVMCalls::ExpectEmit2(inner) => {
state.expected_emits.push_back(ExpectedEmit {
depth: data.journaled_state.depth(),
checks: [inner.0, inner.1, inner.2, inner.3],
..Default::default()
});
Ok(Bytes::new())
}
HEVMCalls::ExpectEmit3(inner) => {
state.expected_emits.push_back(ExpectedEmit {
depth: data.journaled_state.depth(),
checks: [inner.0, inner.1, inner.2, inner.3],
address: Some(inner.4),
..Default::default()
});
Ok(Bytes::new())
expect_emit(state, None, data.journaled_state.depth(), [true, true, true, true])
}
HEVMCalls::ExpectEmit1(inner) => expect_emit(
state,
Some(inner.0),
data.journaled_state.depth(),
[true, true, true, true],
),
HEVMCalls::ExpectEmit2(inner) => expect_emit(
state,
None,
data.journaled_state.depth(),
[inner.0, inner.1, inner.2, inner.3],
),
HEVMCalls::ExpectEmit3(inner) => expect_emit(
state,
Some(inner.4),
data.journaled_state.depth(),
[inner.0, inner.1, inner.2, inner.3],
),
HEVMCalls::ExpectCall0(inner) => expect_call(
state,
inner.0,
Expand Down
9 changes: 9 additions & 0 deletions evm/src/executor/inspector/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,15 @@ where

// Handle expected reverts
if let Some(expected_revert) = &self.expected_revert {
// Irrespective of whether a revert will be matched or not, disallow having expected
// reverts alongside expected emits or calls.
if !self.expected_calls.is_empty() || !self.expected_emits.is_empty() {
return (
InstructionResult::Revert,
remaining_gas,
"Cannot expect a function to revert while trying to match expected calls or events.".to_string().encode().into(),
)
}
if data.journaled_state.depth() == expected_revert.depth {
let expected_revert = std::mem::take(&mut self.expected_revert).unwrap();
return match handle_expect_revert(
Expand Down
8 changes: 8 additions & 0 deletions testdata/cheats/ExpectCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ contract ExpectCallTest is DSTest {
cheats.expectCallMinGas(address(inner), 0, 50_001, abi.encodeWithSelector(inner.add.selector, 1, 1));
this.exposed_addHardGasLimit(target);
}

/// Ensure that you cannot use expectCall with an expectRevert.
function testFailExpectCallWithRevertDisallowed() public {
Contract target = new Contract();
cheats.expectRevert();
cheats.expectCall(address(target), abi.encodeWithSelector(target.add.selector));
this.exposed_callTargetNTimes(target, 5, 5, 1);
}
}

contract ExpectCallCountTest is DSTest {
Expand Down
9 changes: 9 additions & 0 deletions testdata/cheats/ExpectEmit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,15 @@ contract ExpectEmitTest is DSTest {
emitter.emitWindowAndOnTest(this);
}

/// We should not be able to expect emits if we're expecting the function reverts, no matter
/// if the function reverts or not.
function testFailEmitWindowWithRevertDisallowed() public {
cheats.expectRevert();
cheats.expectEmit(true, false, false, true);
emit A(1);
emitter.emitWindow();
}

/// This test will fail if we check that all expected logs were emitted
/// after every call from the same depth as the call that invoked the cheatcode.
///
Expand Down