Skip to content

Commit

Permalink
remove assumeNoPartialRevert; update assumeNoPartialRevert
Browse files Browse the repository at this point in the history
  • Loading branch information
emo-eth committed Nov 5, 2024
1 parent bd54dc1 commit ed112a7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 104 deletions.
48 changes: 4 additions & 44 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,30 +825,22 @@ interface Vm {
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoRevert() external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoPartialRevert`.
/// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoRevert(bytes4 revertData) external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoPartialRevert`.
/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoRevert(bytes calldata revertData) external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoRevert`.
/// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoRevert(bytes4 revertData, address reverter) external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoPartialRevert`.
/// Discard this run's fuzz inputs and generate new ones if next call reverted with the given revert data. Call more than once to add multiple reasons.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoRevert(bytes calldata revertData, address reverter) external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoRevert`.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoPartialRevert(bytes4 revertData) external pure;

/// Discard this run's fuzz inputs and generate new ones if next call reverted with data starting with the given revert data. Call more than once to add multiple reasons. Can be combined with `assumeNoRevert`.
#[cheatcode(group = Testing, safety = Safe)]
function assumeNoPartialRevert(bytes4 revertData, address reverter) external pure;

/// Writes a breakpoint to jump to in the debugger.
#[cheatcode(group = Testing, safety = Safe)]
function breakpoint(string calldata char) external pure;
Expand Down
39 changes: 5 additions & 34 deletions crates/cheatcodes/src/test/assume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{Cheatcode, Cheatcodes, CheatsCtxt, Error, Result};
use alloy_primitives::Address;
use foundry_evm_core::constants::MAGIC_ASSUME;
use spec::Vm::{
assumeCall, assumeNoPartialRevert_0Call, assumeNoPartialRevert_1Call, assumeNoRevert_0Call,
assumeNoRevert_1Call, assumeNoRevert_2Call, assumeNoRevert_3Call, assumeNoRevert_4Call,
assumeCall, assumeNoRevert_0Call, assumeNoRevert_1Call, assumeNoRevert_2Call,
assumeNoRevert_3Call, assumeNoRevert_4Call,
};
use std::fmt::Debug;

Expand Down Expand Up @@ -47,7 +47,7 @@ impl Cheatcode for assumeCall {

impl Cheatcode for assumeNoRevert_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
assume_no_revert(ccx.state, ccx.ecx.journaled_state.depth(), None, false, None)
assume_no_revert(ccx.state, ccx.ecx.journaled_state.depth(), None, None)
}
}

Expand All @@ -58,7 +58,6 @@ impl Cheatcode for assumeNoRevert_1Call {
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
false,
None,
)
}
Expand All @@ -70,7 +69,6 @@ impl Cheatcode for assumeNoRevert_2Call {
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
false,
None,
)
}
Expand All @@ -82,7 +80,6 @@ impl Cheatcode for assumeNoRevert_3Call {
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
false,
Some(*reverter),
)
}
Expand All @@ -94,33 +91,6 @@ impl Cheatcode for assumeNoRevert_4Call {
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
false,
Some(*reverter),
)
}
}

impl Cheatcode for assumeNoPartialRevert_0Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { revertData } = self;
assume_no_revert(
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
true,
None,
)
}
}

impl Cheatcode for assumeNoPartialRevert_1Call {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { revertData, reverter } = self;
assume_no_revert(
ccx.state,
ccx.ecx.journaled_state.depth(),
Some(revertData.to_vec()),
true,
Some(*reverter),
)
}
Expand All @@ -130,11 +100,12 @@ fn assume_no_revert(
state: &mut Cheatcodes,
depth: u64,
reason: Option<Vec<u8>>,
partial_match: bool,
reverter: Option<Address>,
) -> Result {
ensure!(state.expected_revert.is_none(), ASSUME_EXPECT_REJECT_MAGIC);

// if reason is not none, check if it is 4 bytes; if so, allow partial matches
let partial_match = reason.is_some() && reason.as_ref().unwrap().len() == 4;
// if assume_no_revert is not set, set it
if state.assume_no_revert.is_none() {
state.assume_no_revert = Some(AssumeNoRevert { depth, reasons: vec![], reverted_by: None });
Expand Down
1 change: 1 addition & 0 deletions crates/cheatcodes/src/test/revert_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) trait RevertParameters {
fn reason(&self) -> Option<&[u8]>;
fn partial_match(&self) -> bool;
}

impl RevertParameters for AcceptableRevertParameters {
fn reverter(&self) -> Option<Address> {
self.reverter
Expand Down
17 changes: 9 additions & 8 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,15 @@ forgetest_init!(test_assume_no_revert_with_data, |prj, cmd| {
_vm.assumeNoRevert(abi.encodeWithSelector(Reverter.RevertWithData.selector, 4), address(reverter));
reverter.twoPossibleReverts(x);
}
/// @dev Test that `assumeNoRevert` assumptions are cleared after the first non-cheatcode external call
function testMultipleAssumesClearAfterCall_fails(uint256 x) public view {
_vm.assumeNoRevert(Reverter.MyRevert.selector);
_vm.assumeNoRevert(Reverter.RevertWithData.selector, address(reverter));
reverter.twoPossibleReverts(x);
reverter.twoPossibleReverts(2);
}
/// @dev Test that `assumeNoRevert` correctly rejects a generic assumeNoRevert call after any specific reason is provided
function testMultipleAssumes_ThrowOnGenericNoRevert_AfterSpecific_fails(bytes4 selector) public view {
Expand Down Expand Up @@ -2560,14 +2569,6 @@ forgetest_init!(test_assume_no_revert_with_data, |prj, cmd| {
"testMultipleAssumesClearAfterCall_fails",
"FAIL: MyRevert(); counterexample:",
);
// need a better way to handle cheatcodes reverting; currently their messages get abi-encoded
// and treated like normal evm data, which makes them hard (and inefficient) to match in the
// handler
// assert_failure_contains(
// &mut cmd,
// "testMultipleAssumes_ThrowOnGenericNoRevert_fails",
// "FAIL: vm.assumeNoRevert: cannot combine a generic assumeNoRevert with specific
// assumeNoRevert reasons;", );
assert_failure_contains(
&mut cmd,
"testMultipleAssumes_ThrowOnGenericNoRevert_AfterSpecific_fails",
Expand Down
2 changes: 0 additions & 2 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions testdata/default/cheats/AssumeNoRevert.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ contract ReverterTest is Test {
reverter.revertWithDataIf2(x);
}

/// @dev Test that `assumeNoPartialRevert` anticipates and correctly rejects a specific error selector with any extra data
/// @dev Test that `assumeNoRevert` anticipates and correctly rejects a specific error selector with any extra data (ie providing selector allows for arbitrary extra data)
function testAssumeWithDataPartial(uint256 x) public view {
_vm.assumeNoPartialRevert(Reverter.RevertWithData.selector);
_vm.assumeNoRevert(Reverter.RevertWithData.selector);
reverter.revertWithDataIf2(x);
}

Expand All @@ -100,9 +100,9 @@ contract ReverterTest is Test {
reverter.twoPossibleReverts(x);
}

/// @dev Test that `assumeNoPartialRevert` correctly interacts with `assumeNoRevert`
/// @dev Test that `assumeNoRevert` correctly interacts with itself when partially matching on the error selector
function testMultipleAssumes_Partial(uint256 x) public view {
_vm.assumeNoPartialRevert(Reverter.RevertWithData.selector);
_vm.assumeNoRevert(Reverter.RevertWithData.selector);
_vm.assumeNoRevert(Reverter.MyRevert.selector);
reverter.twoPossibleReverts(x);
}
Expand Down

0 comments on commit ed112a7

Please sign in to comment.