Skip to content

Commit

Permalink
fix: f5aa05e upstream update compilation errors (#681)
Browse files Browse the repository at this point in the history
* chore: update foundry-block-explorers

* chore: remove deployments from LocalTraceIdentifier

* fix: use dyn DatabaseExt in TraceCollector

* chore: fix duplicate method definition

* fix: solve some dependency issues in cheatcodes crate

* fix: adapt cheatcode crate to usage of dyn DatabaseExt

* fix: adapt to usage of hash maps with different hashes

* fix: further adaptations to dyn DatabaseExt usage

* fix: hardhat console import

* fix: adapt zk mock code to multiple calls (#9024)

* fix lifetime issues in trace_zksync

* inline env and l1_block_info fields

* fix: remove deployments and fix usage of different HashMaps on evm crate

* fix: remove stale std HashMap reference

* fix: clone zk_metadata before taking mutable reference

* fix: remove deployment references in forge

* fix: use u64 for gas limit

* fix: adapt to compiler optimize being an Option

* fix: restore lost code during conflict resolution in tests

* chore: remove uneeded uses of std HashMap

---------

Co-authored-by: Nisheeth Barthwal <[email protected]>
  • Loading branch information
elfedy and nbaztec authored Oct 31, 2024
1 parent 427c240 commit 33034a2
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 341 deletions.
196 changes: 33 additions & 163 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ foundry-common.workspace = true
foundry-compilers.workspace = true
foundry-config.workspace = true
foundry-evm-core.workspace = true
foundry-evm-traces.workspace = true
foundry-wallets.workspace = true
foundry-zksync-core.workspace = true
foundry-zksync-compiler.workspace = true
Expand Down
10 changes: 4 additions & 6 deletions crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ use crate::{
};
use alloy_consensus::TxEnvelope;
use alloy_genesis::{Genesis, GenesisAccount};
use alloy_primitives::{map::HashMap, Address, Bytes, B256, U256};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rlp::Decodable;
use alloy_sol_types::SolValue;
use foundry_common::fs::{read_json_file, write_json_file};
use foundry_evm_core::{
abi::HARDHAT_CONSOLE_ADDRESS,
backend::{DatabaseExt, RevertStateSnapshotAction},
constants::{CALLER, CHEATCODE_ADDRESS, HARDHAT_CONSOLE_ADDRESS, TEST_CONTRACT_ADDRESS},
constants::{CALLER, CHEATCODE_ADDRESS, TEST_CONTRACT_ADDRESS},
};
use foundry_evm_traces::StackSnapshotType;
use rand::Rng;
use revm::{
primitives::{Account, Bytecode, SpecId, KECCAK_EMPTY},
InnerEvmContext,
};
use revm::primitives::{Account, Bytecode, SpecId, KECCAK_EMPTY};
use std::{collections::BTreeMap, path::Path};
mod record_debug_step;
use record_debug_step::{convert_call_trace_to_debug_step, flatten_call_trace};
Expand Down
101 changes: 53 additions & 48 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
journaled_account,
mapping::{self, MappingSlots},
prank::Prank,
DealRecord, GasRecord, RecordAccess,
DealRecord, GasRecord,
},
inspector::utils::CommonCreateInput,
script::{Broadcast, Wallets},
Expand All @@ -18,11 +18,10 @@ use crate::{
Vm::{self, AccountAccess},
};
use alloy_primitives::{
hex,
hex, keccak256,
map::{AddressHashMap, HashMap},
Address, Bytes, Log, TxKind, B256, U256,
};
use alloy_primitives::{hex, keccak256, Address, Bytes, Log, TxKind, B256, U256};
use alloy_rpc_types::request::{TransactionInput, TransactionRequest};
use alloy_sol_types::{SolCall, SolInterface, SolValue};
use foundry_cheatcodes_common::{
Expand All @@ -33,25 +32,25 @@ use foundry_cheatcodes_common::{
use foundry_common::{evm::Breakpoints, TransactionMaybeSigned, SELECTOR_LEN};
use foundry_config::Config;
use foundry_evm_core::{
abi::Vm::stopExpectSafeMemoryCall,
abi::{Vm::stopExpectSafeMemoryCall, HARDHAT_CONSOLE_ADDRESS},
backend::{DatabaseError, DatabaseExt, LocalForkId, RevertDiagnostic},
constants::{
CHEATCODE_ADDRESS, CHEATCODE_CONTRACT_HASH, DEFAULT_CREATE2_DEPLOYER,
DEFAULT_CREATE2_DEPLOYER_CODE, MAGIC_ASSUME, HARDHAT_CONSOLE_ADDRESS
DEFAULT_CREATE2_DEPLOYER_CODE, MAGIC_ASSUME,
},
decode::decode_console_log,
utils::new_evm_with_existing_context,
InspectorExt,
};
use foundry_evm_traces::TracingInspectorConfig;
use foundry_wallets::multi_wallet::MultiWallet;
use foundry_zksync_compiler::{DualCompiledContract, DualCompiledContracts};
use foundry_zksync_core::{
convert::{ConvertAddress, ConvertH160, ConvertH256, ConvertRU256, ConvertU256},
get_account_code_key, get_balance_key, get_nonce_key, Call, ZkPaymasterData,
ZkTransactionMetadata, DEFAULT_CREATE2_DEPLOYER_ZKSYNC,
};
use foundry_zksync_inspectors::TraceCollector;
use foundry_evm_traces::{TracingInspector, TracingInspectorConfig};
use foundry_wallets::multi_wallet::MultiWallet;
use itertools::Itertools;
use proptest::test_runner::{RngAlgorithm, TestRng, TestRunner};
use rand::Rng;
Expand All @@ -69,7 +68,7 @@ use revm::{
};
use serde_json::Value;
use std::{
collections::{BTreeMap, HashMap, HashSet, VecDeque},
collections::{BTreeMap, HashSet, VecDeque},
fs::File,
io::BufReader,
ops::Range,
Expand Down Expand Up @@ -154,13 +153,40 @@ pub trait CheatcodesExecutor {
None
}

fn trace_zksync(
&mut self,
ccx_state: &mut Cheatcodes,
ecx: &mut Ecx,
call_traces: Vec<Call>,
) {
self.get_inspector(ccx_state).trace_zksync(ecx, call_traces);
fn trace_zksync(&mut self, ccx_state: &mut Cheatcodes, ecx: Ecx, call_traces: Vec<Call>) {
let mut inspector = self.get_inspector(ccx_state);

// We recreate the EvmContext here to satisfy the lifetime parameters as 'static, with
// regards to the inspector's lifetime.
let mut ecx_inner = EvmContext {
inner: InnerEvmContext {
env: std::mem::replace(&mut ecx.env, Default::default()),
journaled_state: std::mem::replace(
&mut ecx.journaled_state,
revm::JournaledState::new(Default::default(), Default::default()),
),
error: std::mem::replace(&mut ecx.error, Ok(())),
l1_block_info: std::mem::take(&mut ecx.l1_block_info),
db: &mut ecx.db as &mut dyn DatabaseExt,
},
precompiles: Default::default(),
};
inspector.trace_zksync(&mut ecx_inner, call_traces);

// re-apply the modified fields to the original ecx.
let env = std::mem::replace(&mut ecx_inner.env, Default::default());
let journaled_state = std::mem::replace(
&mut ecx_inner.journaled_state,
revm::JournaledState::new(Default::default(), Default::default()),
);
let error = std::mem::replace(&mut ecx_inner.error, Ok(()));
let l1_block_info = std::mem::take(&mut ecx_inner.l1_block_info);
drop(ecx_inner);

ecx.env = env;
ecx.journaled_state = journaled_state;
ecx.error = error;
ecx.l1_block_info = l1_block_info;
}
}

Expand Down Expand Up @@ -735,11 +761,7 @@ impl Cheatcodes {
/// Additionally:
/// * Translates block information
/// * Translates all persisted addresses
pub fn select_fork_vm<DB: DatabaseExt>(
&mut self,
data: &mut InnerEvmContext<DB>,
fork_id: LocalForkId,
) {
pub fn select_fork_vm(&mut self, data: InnerEcx, fork_id: LocalForkId) {
let fork_info = data.db.get_fork_info(fork_id).expect("failed getting fork info");
if fork_info.fork_type.is_evm() {
self.select_evm(data)
Expand All @@ -750,7 +772,7 @@ impl Cheatcodes {

/// Switch to EVM and translate block info, balances, nonces and deployed codes for persistent
/// accounts
pub fn select_evm<DB: DatabaseExt>(&mut self, data: &mut InnerEvmContext<DB>) {
pub fn select_evm(&mut self, data: InnerEcx) {
if !self.use_zk_vm {
tracing::info!("already in EVM");
return
Expand Down Expand Up @@ -822,11 +844,7 @@ impl Cheatcodes {

/// Switch to ZK-VM and translate block info, balances, nonces and deployed codes for persistent
/// accounts
pub fn select_zk_vm<DB: DatabaseExt>(
&mut self,
data: &mut InnerEvmContext<DB>,
new_env: Option<&Env>,
) {
pub fn select_zk_vm(&mut self, data: InnerEcx, new_env: Option<&Env>) {
if self.use_zk_vm {
tracing::info!("already in ZK-VM");
return
Expand Down Expand Up @@ -929,13 +947,12 @@ impl Cheatcodes {
}

// common create functionality for both legacy and EOF.
fn create_common<DB, Input>(
fn create_common<Input>(
&mut self,
ecx: Ecx,
mut input: Input,
executor: &mut impl CheatcodesExecutor,
) -> Option<CreateOutcome>
fn create_common<Input>(&mut self, ecx: Ecx, mut input: Input) -> Option<CreateOutcome>
where
Input: CommonCreateInput,
{
Expand Down Expand Up @@ -1108,15 +1125,14 @@ impl Cheatcodes {
/// Try handling the `CREATE` within zkEVM.
/// If `Some` is returned then the result must be returned immediately, else the call must be
/// handled in EVM.
fn try_create_in_zk<DB, Input>(
fn try_create_in_zk<Input>(
&mut self,
ecx: &mut EvmContext<DB>,
ecx: Ecx,
input: Input,
executor: &mut impl CheatcodesExecutor,
) -> Option<CreateOutcome>
where
DB: DatabaseExt,
Input: CommonCreateInput<DB>,
Input: CommonCreateInput,
{
if self.skip_zk_vm {
self.skip_zk_vm = false; // handled the skip, reset flag
Expand Down Expand Up @@ -1736,16 +1752,13 @@ where {
/// Try handling the `CALL` within zkEVM.
/// If `Some` is returned then the result must be returned immediately, else the call must be
/// handled in EVM.
fn try_call_in_zk<DB>(
fn try_call_in_zk(
&mut self,
factory_deps: Vec<Vec<u8>>,
ecx: &mut EvmContext<DB>,
ecx: Ecx,
call: &mut CallInputs,
executor: &mut impl CheatcodesExecutor,
) -> Option<CallOutcome>
where
DB: DatabaseExt,
{
) -> Option<CallOutcome> {
// also skip if the target was created during a zkEVM skip
self.skip_zk_vm =
self.skip_zk_vm || self.skip_zk_vm_addresses.contains(&call.target_address);
Expand Down Expand Up @@ -2335,11 +2348,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
outcome
}

fn create(
&mut self,
ecx: Ecx,
call: &mut CreateInputs,
) -> Option<CreateOutcome> {
fn create(&mut self, ecx: Ecx, call: &mut CreateInputs) -> Option<CreateOutcome> {
self.create_common(ecx, call, &mut TransparentCheatcodesExecutor)
}

Expand All @@ -2352,11 +2361,7 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes {
self.create_end_common(ecx, outcome)
}

fn eofcreate(
&mut self,
ecx: Ecx,
call: &mut EOFCreateInputs,
) -> Option<CreateOutcome> {
fn eofcreate(&mut self, ecx: Ecx, call: &mut EOFCreateInputs) -> Option<CreateOutcome> {
self.create_common(ecx, call, &mut TransparentCheatcodesExecutor)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/cheatcodes/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) mod assume;
pub(crate) mod expect;

impl Cheatcode for zkVmCall {
fn apply_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { enable } = *self;

if enable {
Expand All @@ -28,15 +28,15 @@ impl Cheatcode for zkVmCall {
}

impl Cheatcode for zkVmSkipCall {
fn apply_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
ccx.state.skip_zk_vm = ccx.state.use_zk_vm;

Ok(Default::default())
}
}

impl Cheatcode for zkUsePaymasterCall {
fn apply_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self { paymaster_address, paymaster_input } = self;
ccx.state.paymaster_params =
Some(ZkPaymasterData { address: *paymaster_address, input: paymaster_input.clone() });
Expand All @@ -45,7 +45,7 @@ impl Cheatcode for zkUsePaymasterCall {
}

impl Cheatcode for zkRegisterContractCall {
fn apply_stateful<DB: DatabaseExt>(&self, ccx: &mut CheatsCtxt<DB>) -> Result {
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
let Self {
name,
evmBytecodeHash,
Expand Down
5 changes: 1 addition & 4 deletions crates/cheatcodes/src/test/expect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{Cheatcode, Cheatcodes, CheatsCtxt, Error, Result, Vm::*};
use alloy_primitives::{
address, hex,
map::{hash_map::Entry, HashMap},
Address, Bytes, LogData as RawLog, U256,
address, hex, map::hash_map::Entry, Address, Bytes, LogData as RawLog, U256,
};
use alloy_sol_types::{SolError, SolValue};
use foundry_cheatcodes_common::expect::{ExpectedCallData, ExpectedCallType};
Expand All @@ -12,7 +10,6 @@ use revm::interpreter::{
return_ok, InstructionResult, Interpreter, InterpreterAction, InterpreterResult,
};
use spec::Vm;
use std::collections::hash_map::Entry;

/// For some cheatcodes we may internally change the status of the call, i.e. in `expectRevert`.
/// Solidity will see a successful call and attempt to decode the return data. Therefore, we need
Expand Down
7 changes: 2 additions & 5 deletions crates/evm/core/src/backend/cow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
InspectorExt,
};
use alloy_genesis::GenesisAccount;
use alloy_primitives::{Address, B256, U256};
use alloy_primitives::{map::HashMap, Address, B256, U256};
use alloy_rpc_types::TransactionRequest;
use eyre::WrapErr;
use foundry_fork_db::DatabaseError;
Expand All @@ -21,10 +21,7 @@ use revm::{
},
Database, DatabaseCommit, JournaledState,
};
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
};
use std::{borrow::Cow, collections::BTreeMap};

/// A wrapper around `Backend` that ensures only `revm::DatabaseRef` functions are called.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
InspectorExt,
};
use alloy_genesis::GenesisAccount;
use alloy_primitives::{keccak256, uint, Address, B256, U256};
use alloy_primitives::{keccak256, map::HashMap, uint, Address, B256, U256};
use alloy_rpc_types::{Block, BlockNumberOrTag, Transaction, TransactionRequest};
use alloy_serde::WithOtherFields;
use eyre::Context;
Expand All @@ -29,7 +29,7 @@ use revm::{
Database, DatabaseCommit, JournaledState,
};
use std::{
collections::{BTreeMap, HashMap, HashSet},
collections::{BTreeMap, HashSet},
time::Instant,
};

Expand Down
9 changes: 3 additions & 6 deletions crates/evm/evm/src/executors/invariant/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use indicatif::ProgressBar;
use parking_lot::RwLock;
use proptest::test_runner::TestError;
use revm::primitives::U256;
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;

/// Replays a call sequence for collecting logs and traces.
/// Returns counterexample to be used when the call sequence is a failed scenario.
Expand Down Expand Up @@ -60,11 +60,8 @@ pub fn replay_run(
}

// Identify newly generated contracts, if they exist.
ided_contracts.extend(load_contracts(
call_result.traces.iter().map(|a| &a.arena),
known_contracts,
&HashMap::new(),
));
ided_contracts
.extend(load_contracts(call_result.traces.iter().map(|a| &a.arena), known_contracts));

// Create counter example to be used in failed case.
counterexample_sequence.push(BaseCounterExample::from_invariant_call(
Expand Down
Loading

0 comments on commit 33034a2

Please sign in to comment.