Skip to content

Commit

Permalink
fix: patch create traces to work with foundry (#578)
Browse files Browse the repository at this point in the history
* patch create traces to work with foundry

* update gix-path for cargo-deny
  • Loading branch information
nbaztec authored Sep 17, 2024
1 parent df011e0 commit 02346a9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 32 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub trait CheatcodesExecutor {
self.get_inspector::<DB>(ccx.state).console_log(message);
}

fn trace<DB: DatabaseExt>(
fn trace_zksync<DB: DatabaseExt>(
&mut self,
ccx_state: &mut Cheatcodes,
ecx: &mut EvmContext<DB>,
Expand Down Expand Up @@ -998,7 +998,7 @@ impl Cheatcodes {
});

// append traces
executor.trace(self, ecx, result.call_traces);
executor.trace_zksync(self, ecx, result.call_traces);

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

// append traces
executor.trace(self, ecx, result.call_traces);
executor.trace_zksync(self, ecx, result.call_traces);

// for each log in cloned logs call handle_expect_emit
if !self.expected_emits.is_empty() {
Expand Down
5 changes: 1 addition & 4 deletions crates/evm/evm/src/inspectors/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,10 @@ impl<DB: Database> InspectorExt<DB> for TraceCollector {
address: None,
}
} else {
// zkEVM traces do not have the create address in output
// it's always an empty slice, so we fill it from the `call.to`
// which contains the correct address
CreateOutcome {
result: InterpreterResult {
result: InstructionResult::Return,
output: Bytes::from(call.to.to_h256().0),
output: Bytes::from(call.output),
gas: Gas::new_spent(call.gas_used + extra_gas),
},
address: Some(call.to.to_address()),
Expand Down
1 change: 1 addition & 0 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ foundry-test-utils.workspace = true

mockall = "0.12"
criterion = "0.5"
once_cell = "1.20.0"
paste = "1.0"
path-slash = "0.2"
similar-asserts.workspace = true
Expand Down
53 changes: 44 additions & 9 deletions crates/forge/tests/it/zk/traces.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//! Forge tests for zksync logs.
use std::path::Path;

use crate::{config::*, test_helpers::TEST_DATA_DEFAULT};
use alloy_primitives::{address, hex, Address, Bytes};
use forge::{
revm::primitives::SpecId,
traces::{CallKind, CallTraceArena, CallTraceNode, TraceKind},
};
use foundry_common::fs;
use foundry_test_utils::Filter;
use foundry_zksync_core::convert::ConvertAddress;
use itertools::Itertools;
use once_cell::sync::Lazy;
use serde::Deserialize;

const ADDRESS_ZK_TRACE_TEST: Address = address!("7fa9385be102ac3eac297483dd6233d62b3e1496");
const ADDRESS_ADDER: Address = address!("f9e9ba9ed9b96ab918c74b21dd0f1d5f2ac38a30");
Expand All @@ -31,6 +35,33 @@ const VALUE_LOG_UINT_TEN: Bytes = Bytes::from_static(
hex!("f5b1bba9000000000000000000000000000000000000000000000000000000000000000a").as_slice(),
); // selector: log(uint)

static BYTECODE_ADDER: Lazy<Vec<u8>> =
Lazy::new(|| get_zk_artifact_bytecode("Trace.t.sol/Adder.json"));
static BYTECODE_CONSTRUCTOR_ADDER: Lazy<Vec<u8>> =
Lazy::new(|| get_zk_artifact_bytecode("Trace.t.sol/ConstructorAdder.json"));
static BYTECODE_NUMBER: Lazy<Vec<u8>> =
Lazy::new(|| get_zk_artifact_bytecode("Trace.t.sol/Number.json"));
static BYTECODE_INNER_NUMBER: Lazy<Vec<u8>> =
Lazy::new(|| get_zk_artifact_bytecode("Trace.t.sol/InnerNumber.json"));

fn get_zk_artifact_bytecode<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Vec<u8> {
#[derive(Deserialize)]
struct Bytecode {
object: String,
}
#[derive(Deserialize)]
struct Artifact {
bytecode: Bytecode,
}

let artifact =
fs::read_json_file::<Artifact>(&Path::new("../../testdata/zk/zkout").join(&path))
.unwrap_or_else(|err| panic!("failed reading artifact file {path:?}: {err:?}"));

hex::decode(artifact.bytecode.object)
.unwrap_or_else(|err| panic!("failed decoding artifact object {path:?}: {err:?}"))
}

#[tokio::test(flavor = "multi_thread")]
async fn test_zk_traces_work_during_call() {
let runner = TEST_DATA_DEFAULT.runner_zksync();
Expand All @@ -49,7 +80,7 @@ async fn test_zk_traces_work_during_call() {
TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_ADDER),
output: Some(Bytes::from(ADDRESS_ADDER.to_h256().0)),
output: Some(Bytes::from(Lazy::force(&BYTECODE_ADDER).to_owned())),
..Default::default()
},
TraceAssertion {
Expand All @@ -61,7 +92,7 @@ async fn test_zk_traces_work_during_call() {
TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_NUMBER),
output: Some(Bytes::from(ADDRESS_NUMBER.to_h256().0)),
output: Some(Bytes::from(Lazy::force(&BYTECODE_NUMBER).to_owned())),
..Default::default()
},
TraceAssertion {
Expand All @@ -74,7 +105,7 @@ async fn test_zk_traces_work_during_call() {
kind: Some(CallKind::Create),
address: Some(ADDRESS_FIRST_INNER_NUMBER),
output: Some(Bytes::from(
ADDRESS_FIRST_INNER_NUMBER.to_h256().0,
Lazy::force(&BYTECODE_INNER_NUMBER).to_owned(),
)),
..Default::default()
},
Expand All @@ -97,7 +128,7 @@ async fn test_zk_traces_work_during_call() {
kind: Some(CallKind::Create),
address: Some(ADDRESS_SECOND_INNER_NUMBER),
output: Some(Bytes::from(
ADDRESS_SECOND_INNER_NUMBER.to_h256().0,
Lazy::force(&BYTECODE_INNER_NUMBER).to_owned(),
)),
..Default::default()
},
Expand Down Expand Up @@ -143,12 +174,12 @@ async fn test_zk_traces_work_during_create() {
children: vec![TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_ADDER),
output: Some(Bytes::from(ADDRESS_ADDER.to_h256().0)),
output: Some(Bytes::from(Lazy::force(&BYTECODE_CONSTRUCTOR_ADDER).to_owned())),
children: vec![
TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_NUMBER),
output: Some(Bytes::from(ADDRESS_NUMBER.to_h256().0)),
output: Some(Bytes::from(Lazy::force(&BYTECODE_NUMBER).to_owned())),
..Default::default()
},
TraceAssertion {
Expand All @@ -160,7 +191,9 @@ async fn test_zk_traces_work_during_create() {
TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_FIRST_INNER_NUMBER),
output: Some(Bytes::from(ADDRESS_FIRST_INNER_NUMBER.to_h256().0)),
output: Some(Bytes::from(
Lazy::force(&BYTECODE_INNER_NUMBER).to_owned(),
)),
..Default::default()
},
TraceAssertion {
Expand All @@ -181,7 +214,9 @@ async fn test_zk_traces_work_during_create() {
TraceAssertion {
kind: Some(CallKind::Create),
address: Some(ADDRESS_SECOND_INNER_NUMBER),
output: Some(Bytes::from(ADDRESS_SECOND_INNER_NUMBER.to_h256().0)),
output: Some(Bytes::from(
Lazy::force(&BYTECODE_INNER_NUMBER).to_owned(),
)),
..Default::default()
},
TraceAssertion {
Expand Down
Loading

0 comments on commit 02346a9

Please sign in to comment.