Skip to content

Commit

Permalink
chore(evm): clean up executor methods some more (#8104)
Browse files Browse the repository at this point in the history
* chore(evm): clean up executor methods some more

* fix: rename _committing to transact_, further clarify the separation

* chore: simplify is_success further

* chore: don't clone executor in unit tests
  • Loading branch information
DaniPopes authored Jun 13, 2024
1 parent 8801156 commit 8e9cb1d
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 238 deletions.
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl CallArgs {
TraceResult::try_from(deploy_result)?
}
TxKind::Call(to) => TraceResult::from_raw(
executor.call_raw_committing(sender, to, input, value)?,
executor.transact_raw(sender, to, input, value)?,
TraceKind::Execution,
),
};
Expand Down
4 changes: 2 additions & 2 deletions crates/cast/bin/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl RunArgs {

if let Some(to) = tx.to {
trace!(tx=?tx.hash,?to, "executing previous call transaction");
executor.commit_tx_with_env(env.clone()).wrap_err_with(|| {
executor.transact_with_env(env.clone()).wrap_err_with(|| {
format!(
"Failed to execute transaction: {:?} in block {}",
tx.hash, env.block.number
Expand Down Expand Up @@ -213,7 +213,7 @@ impl RunArgs {

if let Some(to) = tx.to {
trace!(tx=?tx.hash, to=?to, "executing call transaction");
TraceResult::from_raw(executor.commit_tx_with_env(env)?, TraceKind::Execution)
TraceResult::from_raw(executor.transact_with_env(env)?, TraceKind::Execution)
} else {
trace!(tx=?tx.hash, "executing create transaction");
TraceResult::try_from(executor.deploy_with_env(env, None))?
Expand Down
2 changes: 1 addition & 1 deletion crates/chisel/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl ChiselRunner {

if commit {
// if explicitly requested we can now commit the call
res = self.executor.call_raw_committing(from, to, calldata, value)?;
res = self.executor.transact_raw(from, to, calldata, value)?;
}

let RawCallResult { result, reverted, logs, traces, labels, chisel_state, .. } = res;
Expand Down
11 changes: 2 additions & 9 deletions crates/evm/evm/src/executors/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use foundry_evm_fuzz::{
use foundry_evm_traces::CallTraceArena;
use indicatif::ProgressBar;
use proptest::test_runner::{TestCaseError, TestError, TestRunner};
use std::{borrow::Cow, cell::RefCell};
use std::cell::RefCell;

mod types;
pub use types::{CaseOutcome, CounterExampleOutcome, FuzzOutcome};
Expand Down Expand Up @@ -202,7 +202,6 @@ impl FuzzedExecutor {
.executor
.call_raw(self.sender, address, calldata.clone(), U256::ZERO)
.map_err(|_| TestCaseError::fail(FuzzError::FailedContractCall))?;
let state_changeset = call.state_changeset.take().unwrap();

// When the `assume` cheatcode is called it returns a special string
if call.result.as_ref() == MAGIC_ASSUME {
Expand All @@ -214,13 +213,7 @@ impl FuzzedExecutor {
.as_ref()
.map_or_else(Default::default, |cheats| cheats.breakpoints.clone());

let success = self.executor.is_raw_call_success(
address,
Cow::Owned(state_changeset),
&call,
should_fail,
);

let success = self.executor.is_raw_call_mut_success(address, &mut call, should_fail);
if success {
Ok(FuzzOutcome::Case(CaseOutcome {
case: FuzzCase { calldata, gas: call.gas_used, stipend: call.stipend },
Expand Down
10 changes: 5 additions & 5 deletions crates/evm/evm/src/executors/invariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> InvariantExecutor<'a> {
let failures = RefCell::new(InvariantFailures::new());

// Stores the calldata in the last run.
let last_run_calldata: RefCell<Vec<BasicTxDetails>> = RefCell::new(vec![]);
let last_run_inputs: RefCell<Vec<BasicTxDetails>> = RefCell::new(vec![]);

// Stores additional traces for gas report.
let gas_report_traces: RefCell<Vec<Vec<CallTraceArena>>> = RefCell::default();
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a> InvariantExecutor<'a> {

// Execute call from the randomly generated sequence and commit state changes.
let call_result = executor
.call_raw_committing(
.transact_raw(
tx.sender,
tx.call_details.target,
tx.call_details.calldata.clone(),
Expand All @@ -233,7 +233,7 @@ impl<'a> InvariantExecutor<'a> {
}
} else {
// Collect data for fuzzing from the state changeset.
let mut state_changeset = call_result.state_changeset.to_owned().unwrap();
let mut state_changeset = call_result.state_changeset.clone().unwrap();

if !call_result.reverted {
collect_data(
Expand Down Expand Up @@ -281,7 +281,7 @@ impl<'a> InvariantExecutor<'a> {
.map_err(|e| TestCaseError::fail(e.to_string()))?;

if !result.can_continue || current_run == self.config.depth - 1 {
last_run_calldata.borrow_mut().clone_from(&inputs);
last_run_inputs.borrow_mut().clone_from(&inputs);
}

if !result.can_continue {
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<'a> InvariantExecutor<'a> {
error,
cases: fuzz_cases.into_inner(),
reverts,
last_run_inputs: last_run_calldata.take(),
last_run_inputs: last_run_inputs.into_inner(),
gas_report_traces: gas_report_traces.into_inner(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/executors/invariant/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn replay_run(

// Replay each call from the sequence, collect logs, traces and coverage.
for tx in inputs.iter() {
let call_result = executor.call_raw_committing(
let call_result = executor.transact_raw(
tx.sender,
tx.call_details.target,
tx.call_details.calldata.clone(),
Expand Down
10 changes: 3 additions & 7 deletions crates/evm/evm/src/executors/invariant/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ pub(crate) fn assert_invariants(
U256::ZERO,
)?;

let success = executor.is_raw_call_success(
invariant_contract.address,
Cow::Owned(call_result.state_changeset.take().unwrap()),
&call_result,
false,
);
let success =
executor.is_raw_call_mut_success(invariant_contract.address, &mut call_result, false);
if !success {
// We only care about invariants which we haven't broken yet.
if invariant_failures.error.is_none() {
Expand Down Expand Up @@ -111,7 +107,7 @@ pub(crate) fn can_continue(
let mut call_results = None;

let handlers_succeeded = || {
targeted_contracts.targets.lock().iter().all(|(address, ..)| {
targeted_contracts.targets.lock().keys().all(|address| {
executor.is_success(*address, false, Cow::Borrowed(state_changeset), false)
})
};
Expand Down
15 changes: 4 additions & 11 deletions crates/evm/evm/src/executors/invariant/shrink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use foundry_evm_core::constants::CALLER;
use foundry_evm_fuzz::invariant::BasicTxDetails;
use indicatif::ProgressBar;
use proptest::bits::{BitSetLike, VarBitSet};
use std::{borrow::Cow, cmp::min};
use std::cmp::min;

#[derive(Clone, Copy, Debug)]
struct Shrink {
Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn check_sequence(
// Apply the call sequence.
for call_index in sequence {
let tx = &calls[call_index];
let call_result = executor.call_raw_committing(
let call_result = executor.transact_raw(
tx.sender,
tx.call_details.target,
tx.call_details.calldata.clone(),
Expand All @@ -160,13 +160,6 @@ pub fn check_sequence(

// Check the invariant for call sequence.
let mut call_result = executor.call_raw(CALLER, test_address, calldata, U256::ZERO)?;
Ok((
executor.is_raw_call_success(
test_address,
Cow::Owned(call_result.state_changeset.take().unwrap()),
&call_result,
false,
),
true,
))
let success = executor.is_raw_call_mut_success(test_address, &mut call_result, false);
Ok((success, true))
}
Loading

0 comments on commit 8e9cb1d

Please sign in to comment.