Skip to content

Commit

Permalink
Revert "feat(cheatcodes): Make expectCall only work for the next ca…
Browse files Browse the repository at this point in the history
…ll's subcalls (foundry-rs#4986)" (foundry-rs#5027)

This reverts commit 3a82b48.
  • Loading branch information
mattsse authored May 24, 2023
1 parent 588ad27 commit 3764b55
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 142 deletions.
19 changes: 3 additions & 16 deletions evm/src/executor/inspector/cheatcodes/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ pub struct ExpectedCallData {
pub count: u64,
/// The type of call
pub call_type: ExpectedCallType,
/// The depth at which this call must be checked
pub depth: u64,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -291,7 +289,6 @@ fn expect_call(
min_gas: Option<u64>,
count: u64,
call_type: ExpectedCallType,
depth: u64,
) -> Result {
match call_type {
ExpectedCallType::Count => {
Expand All @@ -303,10 +300,8 @@ fn expect_call(
!expecteds.contains_key(&calldata),
"Counted expected calls can only bet set once."
);
expecteds.insert(
calldata,
(ExpectedCallData { value, gas, min_gas, count, call_type, depth }, 0),
);
expecteds
.insert(calldata, (ExpectedCallData { value, gas, min_gas, count, call_type }, 0));
Ok(Bytes::new())
}
ExpectedCallType::NonCount => {
Expand All @@ -324,7 +319,7 @@ fn expect_call(
// If it does not exist, then create it.
expecteds.insert(
calldata,
(ExpectedCallData { value, gas, min_gas, count, call_type, depth }, 0),
(ExpectedCallData { value, gas, min_gas, count, call_type }, 0),
);
}
Ok(Bytes::new())
Expand Down Expand Up @@ -389,7 +384,6 @@ pub fn apply<DB: DatabaseExt>(
None,
1,
ExpectedCallType::NonCount,
data.journaled_state.depth(),
),
HEVMCalls::ExpectCall1(inner) => expect_call(
state,
Expand All @@ -400,7 +394,6 @@ pub fn apply<DB: DatabaseExt>(
None,
inner.2,
ExpectedCallType::Count,
data.journaled_state.depth(),
),
HEVMCalls::ExpectCall2(inner) => expect_call(
state,
Expand All @@ -411,7 +404,6 @@ pub fn apply<DB: DatabaseExt>(
None,
1,
ExpectedCallType::NonCount,
data.journaled_state.depth(),
),
HEVMCalls::ExpectCall3(inner) => expect_call(
state,
Expand All @@ -422,7 +414,6 @@ pub fn apply<DB: DatabaseExt>(
None,
inner.3,
ExpectedCallType::Count,
data.journaled_state.depth(),
),
HEVMCalls::ExpectCall4(inner) => {
let value = inner.1;
Expand All @@ -439,7 +430,6 @@ pub fn apply<DB: DatabaseExt>(
None,
1,
ExpectedCallType::NonCount,
data.journaled_state.depth(),
)
}
HEVMCalls::ExpectCall5(inner) => {
Expand All @@ -457,7 +447,6 @@ pub fn apply<DB: DatabaseExt>(
None,
inner.4,
ExpectedCallType::Count,
data.journaled_state.depth(),
)
}
HEVMCalls::ExpectCallMinGas0(inner) => {
Expand All @@ -475,7 +464,6 @@ pub fn apply<DB: DatabaseExt>(
Some(inner.2 + positive_value_cost_stipend),
1,
ExpectedCallType::NonCount,
data.journaled_state.depth(),
)
}
HEVMCalls::ExpectCallMinGas1(inner) => {
Expand All @@ -493,7 +481,6 @@ pub fn apply<DB: DatabaseExt>(
Some(inner.2 + positive_value_cost_stipend),
inner.4,
ExpectedCallType::Count,
data.journaled_state.depth(),
)
}
HEVMCalls::MockCall0(inner) => {
Expand Down
28 changes: 9 additions & 19 deletions evm/src/executor/inspector/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,7 @@ where
// The gas matches, if provided
expected.gas.map_or(true, |gas| gas == call.gas_limit) &&
// The minimum gas matches, if provided
expected.min_gas.map_or(true, |min_gas| min_gas <= call.gas_limit) &&
// The expected depth is smaller than the actual depth,
// which means we're in the subcalls of the call were we expect to find the matches.
expected.depth < data.journaled_state.depth()
expected.min_gas.map_or(true, |min_gas| min_gas <= call.gas_limit)
{
*actual_count += 1;
}
Expand Down Expand Up @@ -821,18 +818,14 @@ where
}
}

// Match expected calls
for (address, calldatas) in &self.expected_calls {
// Loop over each address, and for each address, loop over each calldata it expects.
for (calldata, (expected, actual_count)) in calldatas {
// Grab the values we expect to see
let ExpectedCallData { gas, min_gas, value, count, call_type, depth } = expected;
// Only check calls in the corresponding depth,
// or if the expected depth is higher than the current depth. This is correct, as
// the expected depth can only be bigger than the current depth if
// we're either terminating the root call (the test itself), or exiting the intended
// call that contained the calls we expected to see.
if depth >= &data.journaled_state.depth() {
// If the depth is 0, then this is the root call terminating
if data.journaled_state.depth() == 0 {
// Match expected calls
for (address, calldatas) in &self.expected_calls {
// Loop over each address, and for each address, loop over each calldata it expects.
for (calldata, (expected, actual_count)) in calldatas {
// Grab the values we expect to see
let ExpectedCallData { gas, min_gas, value, count, call_type } = expected;
let calldata = Bytes::from(calldata.clone());

// We must match differently depending on the type of call we expect.
Expand Down Expand Up @@ -891,10 +884,7 @@ where
}
}
}
}

// If the depth is 0, then this is the root call terminating
if data.journaled_state.depth() == 0 {
// Check if we have any leftover expected emits
// First, if any emits were found at the root call, then we its ok and we remove them.
self.expected_emits.retain(|expected| !expected.found);
Expand Down
Loading

0 comments on commit 3764b55

Please sign in to comment.