Skip to content

Commit

Permalink
chore: use InspectorExt trait for logs (#543)
Browse files Browse the repository at this point in the history
* use InspectorExt trait for logs
  • Loading branch information
nbaztec authored Aug 23, 2024
1 parent c59f966 commit b93845a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use foundry_evm_core::{
abi::{Vm::stopExpectSafeMemoryCall, HARDHAT_CONSOLE_ADDRESS},
backend::{DatabaseError, DatabaseExt, LocalForkId, RevertDiagnostic},
constants::{CHEATCODE_ADDRESS, CHEATCODE_CONTRACT_HASH, DEFAULT_CREATE2_DEPLOYER_CODE},
decode::decode_console_log,
utils::new_evm_with_existing_context,
InspectorExt,
};
Expand Down Expand Up @@ -341,11 +342,6 @@ pub struct Cheatcodes {
/// Dual compiled contracts
pub dual_compiled_contracts: DualCompiledContracts,

/// Logs printed during ZK-VM execution.
/// EVM logs have the value `None` so they can be interpolated later, since
/// they are recorded by [foundry_evm::inspectors::LogCollector] tracer.
pub combined_logs: Vec<Option<Log>>,

/// Starts the cheatcode inspector in ZK mode.
/// This is set to `false`, once the startup migration is completed.
pub startup_zk: bool,
Expand Down Expand Up @@ -438,7 +434,6 @@ impl Cheatcodes {
mapping_slots: Default::default(),
pc: Default::default(),
breakpoints: Default::default(),
combined_logs: Default::default(),
use_zk_vm: Default::default(),
persisted_factory_deps: Default::default(),
}
Expand Down Expand Up @@ -929,7 +924,21 @@ impl Cheatcodes {
emitter: log.address,
}));
}
self.combined_logs.extend(result.logs.clone().into_iter().map(Some));

// append console logs from zkEVM to the current executor's LogTracer
let executor = &mut TransparentCheatcodesExecutor;
result.logs.iter().filter_map(decode_console_log).for_each(|decoded_log| {
executor.console_log(
&mut CheatsCtxt {
state: self,
ecx: &mut ecx.inner,
precompiles: &mut ecx.precompiles,
gas_limit: create_inputs.gas_limit,
caller: create_inputs.caller,
},
decoded_log,
);
});

// for each log in cloned logs call handle_expect_emit
if !self.expected_emits.is_empty() {
Expand Down Expand Up @@ -1150,7 +1159,6 @@ impl Cheatcodes {
}

if call.target_address == HARDHAT_CONSOLE_ADDRESS {
self.combined_logs.push(None);
return None;
}

Expand Down Expand Up @@ -1394,8 +1402,22 @@ impl Cheatcodes {
emitter: log.address,
}));
}
self.combined_logs.extend(result.logs.clone().into_iter().map(Some));
//for each log in cloned logs call handle_expect_emit

// append console logs from zkEVM to the current executor's LogTracer
result.logs.iter().filter_map(decode_console_log).for_each(|decoded_log| {
executor.console_log(
&mut CheatsCtxt {
state: self,
ecx: &mut ecx.inner,
precompiles: &mut ecx.precompiles,
gas_limit: call.gas_limit,
caller: call.caller,
},
decoded_log,
);
});

// for each log in cloned logs call handle_expect_emit
if !self.expected_emits.is_empty() {
for log in result.logs {
expect::handle_expect_emit(self, &log);
Expand Down Expand Up @@ -1537,7 +1559,6 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
emitter: log.address,
});
}
self.combined_logs.push(None);
}

fn call(
Expand Down
1 change: 0 additions & 1 deletion crates/evm/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ parking_lot.workspace = true
proptest.workspace = true
thiserror.workspace = true
tracing.workspace = true
itertools.workspace = true
indicatif = "0.17"
26 changes: 1 addition & 25 deletions crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use foundry_evm_core::{
use foundry_evm_coverage::HitMaps;
use foundry_evm_traces::{CallTraceArena, TraceMode};
use foundry_zksync_core::ZkTransactionMetadata;
use itertools::Itertools;
use revm::{
db::{DatabaseCommit, DatabaseRef},
interpreter::{return_ok, InstructionResult},
Expand Down Expand Up @@ -936,32 +935,9 @@ fn convert_executed_result(
_ => Bytes::new(),
};

let combined_logs =
inspector.cheatcodes.as_ref().map(|cheatcodes| cheatcodes.combined_logs.clone());
let InspectorData { mut logs, labels, traces, coverage, cheatcodes, chisel_state } =
let InspectorData { logs, labels, traces, coverage, cheatcodes, chisel_state } =
inspector.collect();

let logs = match combined_logs {
Some(combined_logs) => {
let new_logs = combined_logs
.into_iter()
// .map(|log| log.unwrap_or_else(|| logs.remove(0)))
.map(|log| {
log.unwrap_or_else(|| {
if !logs.is_empty() {
logs.remove(0)
} else {
Default::default()
}
})
})
.collect_vec();
assert!(logs.is_empty(), "logs were not fully combined");
new_logs
}
None => logs,
};

let transactions = cheatcodes
.as_ref()
.map(|c| c.broadcastable_transactions.clone())
Expand Down

0 comments on commit b93845a

Please sign in to comment.