Skip to content
Merged
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
60 changes: 24 additions & 36 deletions crates/cheatcodes/src/credible.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{inspector::Ecx, Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, Result, Vm::*};
use alloy_primitives::{Bytes, FixedBytes, TxKind};
use alloy_sol_types::{Revert, SolError, SolValue};
use alloy_sol_types::{Revert, SolError};
use assertion_executor::{
db::{fork_db::ForkDb, DatabaseCommit, DatabaseRef},
primitives::{
Expand Down Expand Up @@ -108,8 +108,7 @@ pub fn execute_assertion(
ecx: Ecx,
executor: &mut dyn CheatcodesExecutor,
cheats: &mut Cheatcodes,
is_create: bool,
) -> Result<Option<Address>, crate::Error> {
) -> Result<(), crate::Error> {
let spec_id = ecx.cfg.spec;
let block = ecx.block.clone();
let state = ecx.journaled_state.state.clone();
Expand Down Expand Up @@ -173,12 +172,6 @@ pub fn execute_assertion(
.validate_transaction_ext_db(block.clone(), tx_env.clone(), &mut fork_db, &mut ext_db)
.map_err(|e| format!("Assertion Executor Error: {e:#?}"))?;

ecx.journaled_state.inner.checkpoint();

if let Some(expected) = &mut cheats.expected_revert {
expected.max_depth = max(ecx.journaled_state.depth(), expected.max_depth);
}

let mut inspector = executor.get_inspector(cheats);
// if transaction execution reverted, log the revert reason
if !tx_validation.result_and_state.result.is_success() {
Expand All @@ -194,6 +187,15 @@ pub fn execute_assertion(
let tx_gas_used = tx_validation.result_and_state.result.gas_used();

if total_assertions_ran != 1 {
// If assertions were not executed, we need to update expect revert depth to
// allow for matching on this revert condition, as we will not execute against
// test evm in this case.
ecx.journaled_state.inner.checkpoint();

std::mem::drop(inspector);
if let Some(expected) = &mut cheats.expected_revert {
expected.max_depth = max(ecx.journaled_state.depth(), expected.max_depth);
}
bail!("Expected 1 assertion to be executed, but {total_assertions_ran} were executed.");
}

Expand All @@ -220,7 +222,19 @@ pub fn execute_assertion(
);
inspector.console_log(&assertion_gas_message);

// Drop the inspector to avoid borrow checker issues
std::mem::drop(inspector);

if !tx_validation.is_valid() {
// If invalidated, we don't execute against test evm, so we must update expected depth
// for expect revert cheatcode.
ecx.journaled_state.inner.checkpoint();

if let Some(expected) = &mut cheats.expected_revert {
expected.max_depth = max(ecx.journaled_state.depth(), expected.max_depth);
}

let mut inspector = executor.get_inspector(cheats);
match &assertion_fn_result.result {
AssertionFunctionExecutionResult::AssertionContractDeployFailure(result) => {
inspector.console_log(&format!(
Expand All @@ -239,34 +253,8 @@ pub fn execute_assertion(
return Err(crate::Error::from(output.clone()));
}
}
} else {
let journaled_state = &mut ecx.journaled_state.state;
for (address, account) in tx_validation.result_and_state.state {
let journaled_acct = journaled_state.get_mut(&address);
match journaled_acct {
Some(journaled_acct) => {
journaled_acct.info = account.info;
journaled_acct.status = journaled_acct.status.union(account.status);
for (index, value) in account.storage.iter() {
journaled_acct.storage.insert(*index, value.clone());
}
}
None => {
journaled_state.insert(address, account);
}
}
}
}

if is_create {
let address: Option<Address> =
tx_validation.result_and_state.result.output().map(|output| {
Address::abi_decode(output).expect("Could not decode address from create output")
});
Ok(address)
} else {
Ok(None)
}
Ok(())
}

fn decode_invalidated_assertion(execution_result: &ExecutionResult) -> Revert {
Expand Down
28 changes: 4 additions & 24 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,22 +994,14 @@ impl Cheatcodes {
kind: TxKind::Call(call.target_address),
};

let call_outcome = match crate::credible::execute_assertion(
return match crate::credible::execute_assertion(
&assertion,
tx_attributes,
ecx,
executor,
self,
false,
) {
Ok(_) => Some(CallOutcome {
result: InterpreterResult {
result: InstructionResult::Return,
output: Default::default(),
gas,
},
memory_offset: call.return_memory_offset.clone(),
}),
Ok(_) => None,
Err(err) => Some(CallOutcome {
result: InterpreterResult {
result: InstructionResult::Revert,
Expand All @@ -1019,8 +1011,6 @@ impl Cheatcodes {
memory_offset: call.return_memory_offset.clone(),
}),
};

return call_outcome;
}

None
Expand Down Expand Up @@ -1712,22 +1702,14 @@ impl Inspector<EthEvmContext<&mut dyn DatabaseExt>> for Cheatcodes {
kind: TxKind::Create,
};

let call_outcome = match crate::credible::execute_assertion(
return match crate::credible::execute_assertion(
&assertion,
tx_attributes,
ecx,
&mut TransparentCheatcodesExecutor,
self,
true,
) {
Ok(address) => Some(CreateOutcome {
result: InterpreterResult {
result: InstructionResult::Return,
output: Default::default(),
gas,
},
address,
}),
Ok(_) => None,
Err(err) => Some(CreateOutcome {
result: InterpreterResult {
result: InstructionResult::Revert,
Expand All @@ -1737,8 +1719,6 @@ impl Inspector<EthEvmContext<&mut dyn DatabaseExt>> for Cheatcodes {
address: None,
}),
};

return call_outcome;
}

None
Expand Down
Loading