Skip to content

Commit

Permalink
chore(evm): rm more etherses
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jan 16, 2024
1 parent f180a13 commit b993384
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use foundry_evm::{
},
},
traces::{TracingInspector, TracingInspectorConfig},
utils::{eval_to_instruction_result, halt_to_instruction_result, u256_to_h256_be},
utils::{eval_to_instruction_result, halt_to_instruction_result},
};
use futures::channel::mpsc::{unbounded, UnboundedSender};
use hash_db::HashDB;
Expand Down Expand Up @@ -1757,7 +1757,7 @@ impl Backend {
self.with_database_at(block_request, |db, _| {
trace!(target: "backend", "get storage for {:?} at {:?}", address, index);
let val = db.storage_ref(address, index)?;
Ok(u256_to_h256_be(val.to_ethers()).to_alloy())
Ok(val.into())
})
.await?
}
Expand Down
24 changes: 14 additions & 10 deletions crates/evm/core/src/fork/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use crate::fork::{BackendHandler, BlockchainDb, BlockchainDbMeta, CreateFork, SharedBackend};
use alloy_providers::provider::Provider;
use alloy_transport::BoxTransport;
use ethers_core::types::BlockNumber;
use foundry_common::provider::alloy::ProviderBuilder;
use foundry_config::Config;
use futures::{
Expand All @@ -18,7 +17,7 @@ use futures::{
use revm::primitives::Env;
use std::{
collections::HashMap,
fmt,
fmt::{self, Write},
pin::Pin,
sync::{
atomic::AtomicUsize,
Expand All @@ -34,7 +33,18 @@ use std::{
pub struct ForkId(pub String);

impl ForkId {
/// Returns the identifier of the fork
/// Returns the identifier for a Fork from a URL and block number.
pub fn new(url: &str, num: Option<u64>) -> Self {
let mut id = url.to_string();
id.push('@');
match num {
Some(n) => write!(id, "{n:#x}").unwrap(),
None => id.push_str("latest"),
}
ForkId(id)
}

/// Returns the identifier of the fork.
pub fn as_str(&self) -> &str {
&self.0
}
Expand Down Expand Up @@ -245,7 +255,7 @@ impl MultiForkHandler {
}

fn create_fork(&mut self, fork: CreateFork, sender: CreateSender) {
let fork_id = create_fork_id(&fork.url, fork.evm_opts.fork_block_number);
let fork_id = ForkId::new(&fork.url, fork.evm_opts.fork_block_number);
trace!(?fork_id, "created new forkId");

if let Some(fork) = self.forks.get(&fork_id).cloned() {
Expand Down Expand Up @@ -470,12 +480,6 @@ impl Drop for ShutDownMultiFork {
}
}

/// Returns the identifier for a Fork which consists of the url and the block number
fn create_fork_id(url: &str, num: Option<u64>) -> ForkId {
let num = num.map(|num| BlockNumber::Number(num.into())).unwrap_or(BlockNumber::Latest);
ForkId(format!("{url}@{num}"))
}

/// Creates a new fork
///
/// This will establish a new `Provider` to the endpoint and return the Fork Backend
Expand Down
72 changes: 9 additions & 63 deletions crates/evm/core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::FixedBytes;
use alloy_primitives::{FixedBytes, U256};
use alloy_rpc_types::{Block, Transaction};
use ethers_core::types::{ActionType, CallType, Chain, H256, U256};
use eyre::ContextCompat;
use foundry_common::types::ToAlloy;
use foundry_config::NamedChain;
use revm::{
interpreter::{CallScheme, InstructionResult},
primitives::{CreateScheme, Eval, Halt, SpecId, TransactTo},
Expand Down Expand Up @@ -49,59 +48,6 @@ impl From<CreateScheme> for CallKind {
}
}

impl From<CallKind> for ActionType {
fn from(kind: CallKind) -> Self {
match kind {
CallKind::Call | CallKind::StaticCall | CallKind::DelegateCall | CallKind::CallCode => {
ActionType::Call
}
CallKind::Create => ActionType::Create,
CallKind::Create2 => ActionType::Create,
}
}
}

impl From<CallKind> for CallType {
fn from(ty: CallKind) -> Self {
match ty {
CallKind::Call => CallType::Call,
CallKind::StaticCall => CallType::StaticCall,
CallKind::CallCode => CallType::CallCode,
CallKind::DelegateCall => CallType::DelegateCall,
CallKind::Create => CallType::None,
CallKind::Create2 => CallType::None,
}
}
}

/// Small helper function to convert [U256] into [H256].
#[inline]
pub fn u256_to_h256_le(u: U256) -> H256 {
let mut h = H256::default();
u.to_little_endian(h.as_mut());
h
}

/// Small helper function to convert [U256] into [H256].
#[inline]
pub fn u256_to_h256_be(u: U256) -> H256 {
let mut h = H256::default();
u.to_big_endian(h.as_mut());
h
}

/// Small helper function to convert [H256] into [U256].
#[inline]
pub fn h256_to_u256_be(storage: H256) -> U256 {
U256::from_big_endian(storage.as_bytes())
}

/// Small helper function to convert [H256] into [U256].
#[inline]
pub fn h256_to_u256_le(storage: H256) -> U256 {
U256::from_little_endian(storage.as_bytes())
}

/// Small helper function to convert an Eval into an InstructionResult
#[inline]
pub fn eval_to_instruction_result(eval: Eval) -> InstructionResult {
Expand Down Expand Up @@ -144,27 +90,27 @@ pub fn halt_to_instruction_result(halt: Halt) -> InstructionResult {
/// This checks for:
/// - prevrandao mixhash after merge
pub fn apply_chain_and_block_specific_env_changes(env: &mut revm::primitives::Env, block: &Block) {
if let Ok(chain) = Chain::try_from(env.cfg.chain_id) {
if let Ok(chain) = NamedChain::try_from(env.cfg.chain_id) {
let block_number = block.header.number.unwrap_or_default();

match chain {
Chain::Mainnet => {
NamedChain::Mainnet => {
// after merge difficulty is supplanted with prevrandao EIP-4399
if block_number.to::<u64>() >= 15_537_351u64 {
env.block.difficulty = env.block.prevrandao.unwrap_or_default().into();
}

return;
}
Chain::Arbitrum |
Chain::ArbitrumGoerli |
Chain::ArbitrumNova |
Chain::ArbitrumTestnet => {
NamedChain::Arbitrum |
NamedChain::ArbitrumGoerli |
NamedChain::ArbitrumNova |
NamedChain::ArbitrumTestnet => {
// on arbitrum `block.number` is the L1 block which is included in the
// `l1BlockNumber` field
if let Some(l1_block_number) = block.other.get("l1BlockNumber").cloned() {
if let Ok(l1_block_number) = serde_json::from_value::<U256>(l1_block_number) {
env.block.number = l1_block_number.to_alloy();
env.block.number = l1_block_number;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ revm = { workspace = true, default-features = false, features = [
"arbitrary",
] }

ethers-core.workspace = true
ethers-signers.workspace = true
itertools.workspace = true

Expand All @@ -47,3 +46,4 @@ proptest = "1"
thiserror = "1"
tracing = "0.1"
rayon = "1"
rand.workspace = true
2 changes: 1 addition & 1 deletion crates/evm/evm/src/executors/invariant/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::{BasicTxDetails, InvariantContract};
use crate::executors::{Executor, RawCallResult};
use alloy_json_abi::Function;
use alloy_primitives::{Address, Bytes, Log};
use ethers_core::rand::{seq, thread_rng, Rng};
use eyre::Result;
use foundry_common::contracts::{ContractsByAddress, ContractsByArtifact};
use foundry_evm_core::{constants::CALLER, decode::decode_revert};
Expand All @@ -11,6 +10,7 @@ use foundry_evm_traces::{load_contracts, CallTraceArena, TraceKind, Traces};
use itertools::Itertools;
use parking_lot::RwLock;
use proptest::test_runner::TestError;
use rand::{seq, thread_rng, Rng};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use revm::primitives::U256;
use std::sync::Arc;
Expand Down

0 comments on commit b993384

Please sign in to comment.