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

fix(forge) expectRevert for cheatcodes #6833

Closed
wants to merge 3 commits into from
Closed
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
43 changes: 24 additions & 19 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,30 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
status: InstructionResult,
retdata: Bytes,
) -> (InstructionResult, Gas, Bytes) {
// Handle expected reverts
if let Some(expected_revert) = &self.expected_revert {
if data.journaled_state.depth() <= expected_revert.depth {
if expected_revert.pending {
let expected_revert = self.expected_revert.as_mut().unwrap();
Comment on lines +925 to +926
Copy link
Member

Choose a reason for hiding this comment

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

can we add some docs here?

expected_revert.pending = false;
} else {
let expected_revert = std::mem::take(&mut self.expected_revert).unwrap();
return match expect::handle_expect_revert(
false,
expected_revert.reason.as_deref(),
status,
retdata,
) {
Err(error) => {
trace!(expected=?expected_revert, ?error, ?status, "Expected revert mismatch");
(InstructionResult::Revert, remaining_gas, error.abi_encode().into())
}
Ok((_, retdata)) => (InstructionResult::Return, remaining_gas, retdata),
};
}
}
}

if call.contract == CHEATCODE_ADDRESS || call.contract == HARDHAT_CONSOLE_ADDRESS {
return (status, remaining_gas, retdata);
}
Expand Down Expand Up @@ -955,25 +979,6 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
}
}

// Handle expected reverts
if let Some(expected_revert) = &self.expected_revert {
if data.journaled_state.depth() <= expected_revert.depth {
let expected_revert = std::mem::take(&mut self.expected_revert).unwrap();
return match expect::handle_expect_revert(
false,
expected_revert.reason.as_deref(),
status,
retdata,
) {
Err(error) => {
trace!(expected=?expected_revert, ?error, ?status, "Expected revert mismatch");
(InstructionResult::Revert, remaining_gas, error.abi_encode().into())
}
Ok((_, retdata)) => (InstructionResult::Return, remaining_gas, retdata),
};
}
}

// If `startStateDiffRecording` has been called, update the `reverted` status of the
// previous call depth's recorded accesses, if any
if let Some(recorded_account_diffs_stack) = &mut self.recorded_account_diffs_stack {
Expand Down
6 changes: 5 additions & 1 deletion crates/cheatcodes/src/test/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub struct ExpectedRevert {
pub reason: Option<Vec<u8>>,
/// The depth at which the revert is expected
pub depth: u64,
/// Flag which is being switched once we exit `expectRevert` call
/// Needed to not fail due to `expectRevert` not reverting
pub pending: bool,
Comment on lines +62 to +64
Copy link
Member

Choose a reason for hiding this comment

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

I think I understand this, but would love to have a bit more context here

}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -435,7 +438,8 @@ fn expect_revert(state: &mut Cheatcodes, reason: Option<&[u8]>, depth: u64) -> R
state.expected_revert.is_none(),
"you must call another function prior to expecting a second revert"
);
state.expected_revert = Some(ExpectedRevert { reason: reason.map(<[_]>::to_vec), depth });
state.expected_revert =
Some(ExpectedRevert { reason: reason.map(<[_]>::to_vec), depth, pending: true });
Ok(Default::default())
}

Expand Down
14 changes: 11 additions & 3 deletions testdata/cheats/Json.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,27 @@ contract ParseJsonTest is DSTest {
function test_parseJsonKeys() public {
string memory jsonString =
'{"some_key_to_value": "some_value", "some_key_to_array": [1, 2, 3], "some_key_to_object": {"key1": "value1", "key2": 2}}';

string[] memory keys = vm.parseJsonKeys(jsonString, "$");
assertEq(abi.encode(keys), abi.encode(["some_key_to_value", "some_key_to_array", "some_key_to_object"]));
string[] memory expected = new string[](3);
expected[0] = "some_key_to_value";
expected[1] = "some_key_to_array";
expected[2] = "some_key_to_object";
assertEq(abi.encode(keys), abi.encode(expected));

keys = vm.parseJsonKeys(jsonString, ".some_key_to_object");
assertEq(abi.encode(keys), abi.encode(["key1", "key2"]));
expected = new string[](2);
expected[0] = "key1";
expected[1] = "key2";
assertEq(abi.encode(keys), abi.encode(expected));

vm.expectRevert("JSON value at \".some_key_to_array\" is not an object");
vm.parseJsonKeys(jsonString, ".some_key_to_array");

vm.expectRevert("JSON value at \".some_key_to_value\" is not an object");
vm.parseJsonKeys(jsonString, ".some_key_to_value");

vm.expectRevert("JSON value at \".*\" is not an object");
vm.expectRevert("key \".*\" must return exactly one JSON object");
vm.parseJsonKeys(jsonString, ".*");
}
}
Expand Down
Loading