Skip to content

Commit

Permalink
Support assert revert of the given contract at any subcall of the nex…
Browse files Browse the repository at this point in the history
…t call
  • Loading branch information
grandizzy committed Sep 16, 2024
1 parent 3336ec0 commit 6e3b7ff
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 159 deletions.
15 changes: 11 additions & 4 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,6 @@ impl Cheatcodes {
&expected_revert,
outcome.result.result,
outcome.result.output.clone(),
None,
&self.config.available_artifacts,
) {
Ok((address, retdata)) => {
Expand Down Expand Up @@ -1234,8 +1233,17 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
}
}

// Handle expected reverts
if let Some(expected_revert) = &self.expected_revert {
// Handle expected reverts.
if let Some(expected_revert) = &mut self.expected_revert {
// Record current reverter address before processing the expect revert if call reverted,
// expect revert is set with expected reverter address and no actual reverter set yet.
if outcome.result.is_revert() &&
expected_revert.reverter.is_some() &&
expected_revert.reverted_by.is_none()
{
expected_revert.reverted_by = Some(call.target_address);
}

if ecx.journaled_state.depth() <= expected_revert.depth {
let needs_processing = match expected_revert.kind {
ExpectedRevertKind::Default => !cheatcode_call,
Expand All @@ -1254,7 +1262,6 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
&expected_revert,
outcome.result.result,
outcome.result.output.clone(),
Some(call.target_address),
&self.config.available_artifacts,
) {
Err(error) => {
Expand Down
6 changes: 4 additions & 2 deletions crates/cheatcodes/src/test/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub struct ExpectedRevert {
pub partial_match: bool,
/// Contract expected to revert next call.
pub reverter: Option<Address>,
/// Actual reverter of the call.
pub reverted_by: Option<Address>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -672,6 +674,7 @@ fn expect_revert(
},
partial_match,
reverter,
reverted_by: None,
});
Ok(Default::default())
}
Expand All @@ -682,7 +685,6 @@ pub(crate) fn handle_expect_revert(
expected_revert: &ExpectedRevert,
status: InstructionResult,
retdata: Bytes,
target_address: Option<Address>,
known_contracts: &Option<ContractsByArtifact>,
) -> Result<(Option<Address>, Bytes)> {
let success_return = || {
Expand All @@ -697,7 +699,7 @@ pub(crate) fn handle_expect_revert(

// If expected reverter address is set then check it matches the actual reverter.
if let (Some(expected_reverter), Some(actual_reverter)) =
(expected_revert.reverter, target_address)
(expected_revert.reverter, expected_revert.reverted_by)
{
if expected_reverter != actual_reverter {
return Err(fmt_err!(
Expand Down
Loading

0 comments on commit 6e3b7ff

Please sign in to comment.