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
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/anvil/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository.workspace = true
[dependencies]
# foundry internal
foundry-evm = { path = "../../evm" }
foundry-utils = { path = "../../utils"}
revm = { workspace = true, default-features = false, features = ["std", "serde", "memory_limit"] }


Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/core/src/eth/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ethers_core::{
types::{H256, U256},
utils::rlp,
};
use foundry_evm::utils::b256_to_h256;
use foundry_utils::types::ToEthers;
use revm::primitives::KECCAK_EMPTY;
// reexport for convenience
pub use ethers_core::types::{EIP1186ProofResponse as AccountProof, StorageProof};
Expand All @@ -29,7 +29,7 @@ impl Default for BasicAccount {
BasicAccount {
balance: 0.into(),
nonce: 0.into(),
code_hash: b256_to_h256(KECCAK_EMPTY),
code_hash: KECCAK_EMPTY.to_ethers(),
storage_root: KECCAK_NULL_RLP,
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/anvil/core/src/eth/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethers_core::{
rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream},
},
};
use foundry_evm::utils::{b160_to_h160, b256_to_h256, h160_to_b160, h256_to_b256};
use foundry_utils::types::{ToAlloy, ToEthers};

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "fastrlp", derive(open_fastrlp::RlpEncodable, open_fastrlp::RlpDecodable))]
Expand All @@ -21,8 +21,8 @@ impl From<revm::primitives::Log> for Log {
fn from(log: revm::primitives::Log) -> Self {
let revm::primitives::Log { address, topics, data } = log;
Log {
address: b160_to_h160(address),
topics: topics.into_iter().map(b256_to_h256).collect(),
address: address.to_ethers(),
topics: topics.into_iter().map(|h| h.to_ethers()).collect(),
data: ethers_core::types::Bytes(data.0),
}
}
Expand All @@ -32,8 +32,8 @@ impl From<Log> for revm::primitives::Log {
fn from(log: Log) -> Self {
let Log { address, topics, data } = log;
revm::primitives::Log {
address: h160_to_b160(address),
topics: topics.into_iter().map(h256_to_b256).collect(),
address: address.to_alloy(),
topics: topics.into_iter().map(|t| t.to_alloy()).collect(),
data: alloy_primitives::Bytes(data.0),
}
}
Expand Down
28 changes: 13 additions & 15 deletions crates/anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ use ethers_core::{
rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream},
},
};
use foundry_evm::{
trace::CallTraceArena,
utils::{h160_to_b160, u256_to_ru256},
};
use foundry_evm::trace::CallTraceArena;
use foundry_utils::types::ToAlloy;
use revm::{
interpreter::InstructionResult,
primitives::{CreateScheme, TransactTo, TxEnv},
Expand Down Expand Up @@ -1188,7 +1186,7 @@ impl PendingTransaction {
pub fn to_revm_tx_env(&self) -> TxEnv {
fn transact_to(kind: &TransactionKind) -> TransactTo {
match kind {
TransactionKind::Call(c) => TransactTo::Call(h160_to_b160(*c)),
TransactionKind::Call(c) => TransactTo::Call((*c).to_alloy()),
TransactionKind::Create => TransactTo::Create(CreateScheme::Create),
}
}
Expand All @@ -1199,13 +1197,13 @@ impl PendingTransaction {
let chain_id = tx.chain_id();
let LegacyTransaction { nonce, gas_price, gas_limit, value, kind, input, .. } = tx;
TxEnv {
caller: h160_to_b160(caller),
caller: caller.to_alloy(),
transact_to: transact_to(kind),
data: alloy_primitives::Bytes(input.0.clone()),
chain_id,
nonce: Some(nonce.as_u64()),
value: u256_to_ru256(*value),
gas_price: u256_to_ru256(*gas_price),
value: (*value).to_alloy(),
gas_price: (*gas_price).to_alloy(),
gas_priority_fee: None,
gas_limit: gas_limit.as_u64(),
access_list: vec![],
Expand All @@ -1225,13 +1223,13 @@ impl PendingTransaction {
..
} = tx;
TxEnv {
caller: h160_to_b160(caller),
caller: (caller).to_alloy(),
transact_to: transact_to(kind),
data: alloy_primitives::Bytes(input.0.clone()),
chain_id: Some(*chain_id),
nonce: Some(nonce.as_u64()),
value: u256_to_ru256(*value),
gas_price: u256_to_ru256(*gas_price),
value: (*value).to_alloy(),
gas_price: (*gas_price).to_alloy(),
gas_priority_fee: None,
gas_limit: gas_limit.as_u64(),
access_list: to_revm_access_list(access_list.0.clone()),
Expand All @@ -1252,14 +1250,14 @@ impl PendingTransaction {
..
} = tx;
TxEnv {
caller: h160_to_b160(caller),
caller: (caller).to_alloy(),
transact_to: transact_to(kind),
data: alloy_primitives::Bytes(input.0.clone()),
chain_id: Some(*chain_id),
nonce: Some(nonce.as_u64()),
value: u256_to_ru256(*value),
gas_price: u256_to_ru256(*max_fee_per_gas),
gas_priority_fee: Some(u256_to_ru256(*max_priority_fee_per_gas)),
value: (*value).to_alloy(),
gas_price: (*max_fee_per_gas).to_alloy(),
gas_priority_fee: Some((*max_priority_fee_per_gas).to_alloy()),
gas_limit: gas_limit.as_u64(),
access_list: to_revm_access_list(access_list.0.clone()),
..Default::default()
Expand Down
7 changes: 4 additions & 3 deletions crates/anvil/core/src/eth/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use ethers_core::{
rlp::{Encodable, RlpStream},
},
};
use foundry_evm::utils::{h160_to_b160, h256_to_u256_be, u256_to_ru256};
use foundry_evm::utils::h256_to_u256_be;
use foundry_utils::types::ToAlloy;

pub fn enveloped<T: Encodable>(id: u8, v: &T, s: &mut RlpStream) {
let encoded = rlp::encode(v);
Expand All @@ -26,8 +27,8 @@ pub fn to_revm_access_list(list: Vec<AccessListItem>) -> Vec<(rAddress, Vec<rU25
list.into_iter()
.map(|item| {
(
h160_to_b160(item.address),
item.storage_keys.into_iter().map(h256_to_u256_be).map(u256_to_ru256).collect(),
item.address.to_alloy(),
item.storage_keys.into_iter().map(|k| k.to_alloy()).map(|k| k.into()).collect(),
)
})
.collect()
Expand Down
25 changes: 12 additions & 13 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ use foundry_evm::{
},
revm,
revm::primitives::{BlockEnv, CfgEnv, SpecId, TxEnv, U256 as rU256},
utils::{
apply_chain_and_block_specific_env_changes, b160_to_h160, h256_to_b256, u256_to_ru256,
},
utils::apply_chain_and_block_specific_env_changes,
};
use foundry_utils::types::{ToAlloy, ToEthers};
use parking_lot::RwLock;
use serde_json::{json, to_writer, Value};
use std::{
Expand Down Expand Up @@ -797,8 +796,8 @@ impl NodeConfig {
let mut env = revm::primitives::Env {
cfg,
block: BlockEnv {
gas_limit: u256_to_ru256(self.gas_limit),
basefee: u256_to_ru256(self.get_base_fee()),
gas_limit: self.gas_limit.to_alloy(),
basefee: self.get_base_fee().to_alloy(),
..Default::default()
},
tx: TxEnv { chain_id: self.get_chain_id().into(), ..Default::default() },
Expand Down Expand Up @@ -879,17 +878,17 @@ latest block number: {latest_block}"
// limit is enabled, since there are networks where this is not used and is always
// `0x0` which would inevitably result in `OutOfGas` errors as soon as the evm is about to record gas, See also <https://github.com/foundry-rs/foundry/issues/3247>
let gas_limit = if self.disable_block_gas_limit || block.gas_limit.is_zero() {
u256_to_ru256(u64::MAX.into())
rU256::from(u64::MAX)
} else {
u256_to_ru256(block.gas_limit)
block.gas_limit.to_alloy()
};

env.block = BlockEnv {
number: rU256::from(fork_block_number),
timestamp: u256_to_ru256(block.timestamp),
difficulty: u256_to_ru256(block.difficulty),
timestamp: block.timestamp.to_alloy(),
difficulty: block.difficulty.to_alloy(),
// ensures prevrandao is set
prevrandao: Some(block.mix_hash.unwrap_or_default()).map(h256_to_b256),
prevrandao: Some(block.mix_hash.unwrap_or_default()).map(|h| h.to_alloy()),
gas_limit,
// Keep previous `coinbase` and `basefee` value
coinbase: env.block.coinbase,
Expand All @@ -904,7 +903,7 @@ latest block number: {latest_block}"
if self.base_fee.is_none() {
if let Some(base_fee) = block.base_fee_per_gas {
self.base_fee = Some(base_fee);
env.block.basefee = u256_to_ru256(base_fee);
env.block.basefee = base_fee.to_alloy();
// this is the base fee of the current block, but we need the base fee of
// the next block
let next_block_base_fee = fees.get_next_block_base_fee_per_gas(
Expand Down Expand Up @@ -995,7 +994,7 @@ latest block number: {latest_block}"

let genesis = GenesisConfig {
timestamp: self.get_genesis_timestamp(),
balance: u256_to_ru256(self.genesis_balance),
balance: self.genesis_balance.to_alloy(),
accounts: self.genesis_accounts.iter().map(|acc| acc.address()).collect(),
fork_genesis_account_infos: Arc::new(Default::default()),
genesis_init: self.genesis.clone(),
Expand All @@ -1019,7 +1018,7 @@ latest block number: {latest_block}"
// if the option is not disabled and we are not forking.
if !self.disable_default_create2_deployer && self.eth_rpc_url.is_none() {
backend
.set_create2_deployer(b160_to_h160(DEFAULT_CREATE2_DEPLOYER))
.set_create2_deployer(DEFAULT_CREATE2_DEPLOYER.to_ethers())
.await
.expect("Failed to create default create2 deployer");
}
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ use foundry_evm::{
interpreter::{return_ok, return_revert, InstructionResult},
primitives::BlockEnv,
},
utils::ru256_to_u256,
};
use foundry_utils::types::ToEthers;
use futures::channel::mpsc::Receiver;
use parking_lot::RwLock;
use std::{collections::HashSet, sync::Arc, time::Duration};
Expand Down Expand Up @@ -2063,7 +2063,7 @@ impl EthApi {

// get the highest possible gas limit, either the request's set value or the currently
// configured gas limit
let mut highest_gas_limit = request.gas.unwrap_or(ru256_to_u256(block_env.gas_limit));
let mut highest_gas_limit = request.gas.unwrap_or(block_env.gas_limit.to_ethers());

// check with the funds of the sender
if let Some(from) = request.from {
Expand Down
24 changes: 11 additions & 13 deletions crates/anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ use foundry_evm::{
primitives::{BlockEnv, CfgEnv, EVMError, Env, ExecutionResult, Output, SpecId},
},
trace::{node::CallTraceNode, CallTraceArena},
utils::{
b160_to_h160, eval_to_instruction_result, h160_to_b160, halt_to_instruction_result,
ru256_to_u256,
},
utils::{eval_to_instruction_result, halt_to_instruction_result},
};
use foundry_utils::types::{ToAlloy, ToEthers};
use std::sync::Arc;
use tracing::{trace, warn};

Expand Down Expand Up @@ -121,7 +119,7 @@ impl<'a, DB: Db + ?Sized, Validator: TransactionValidator> TransactionExecutor<'
let block_number = self.block_env.number;
let difficulty = self.block_env.difficulty;
let beneficiary = self.block_env.coinbase;
let timestamp = ru256_to_u256(self.block_env.timestamp).as_u64();
let timestamp = self.block_env.timestamp.to_ethers().as_u64();
let base_fee = if (self.cfg_env.spec_id as u8) >= (SpecId::LONDON as u8) {
Some(self.block_env.basefee)
} else {
Expand Down Expand Up @@ -164,7 +162,7 @@ impl<'a, DB: Db + ?Sized, Validator: TransactionValidator> TransactionExecutor<'
transaction_index,
from: *transaction.pending_transaction.sender(),
to: transaction.pending_transaction.transaction.to().copied(),
contract_address: contract_address.map(b160_to_h160),
contract_address: contract_address.map(|c| c.to_ethers()),
logs,
logs_bloom: *receipt.logs_bloom(),
traces: CallTraceArena { arena: traces },
Expand All @@ -186,19 +184,19 @@ impl<'a, DB: Db + ?Sized, Validator: TransactionValidator> TransactionExecutor<'

let partial_header = PartialHeader {
parent_hash,
beneficiary: b160_to_h160(beneficiary),
beneficiary: beneficiary.to_ethers(),
state_root: self.db.maybe_state_root().unwrap_or_default(),
receipts_root,
logs_bloom: bloom,
difficulty: ru256_to_u256(difficulty),
number: ru256_to_u256(block_number),
gas_limit: ru256_to_u256(gas_limit),
difficulty: difficulty.to_ethers(),
number: block_number.to_ethers(),
gas_limit: gas_limit.to_ethers(),
gas_used: cumulative_gas_used,
timestamp,
extra_data: Default::default(),
mix_hash: Default::default(),
nonce: Default::default(),
base_fee: base_fee.map(ru256_to_u256),
base_fee: base_fee.map(|b| b.to_ethers()),
};

let block = Block::new(partial_header, transactions.clone(), ommers);
Expand Down Expand Up @@ -231,14 +229,14 @@ impl<'a, 'b, DB: Db + ?Sized, Validator: TransactionValidator> Iterator
fn next(&mut self) -> Option<Self::Item> {
let transaction = self.pending.next()?;
let sender = *transaction.pending_transaction.sender();
let account = match self.db.basic(h160_to_b160(sender)).map(|acc| acc.unwrap_or_default()) {
let account = match self.db.basic(sender.to_alloy()).map(|acc| acc.unwrap_or_default()) {
Ok(account) => account,
Err(err) => return Some(TransactionExecutionOutcome::DatabaseError(transaction, err)),
};
let env = self.env_for(&transaction.pending_transaction);
// check that we comply with the block's gas limit
let max_gas = self.gas_used.saturating_add(U256::from(env.tx.gas_limit));
if max_gas > ru256_to_u256(env.block.gas_limit) {
if max_gas > env.block.gas_limit.to_ethers() {
return Some(TransactionExecutionOutcome::Exhausted(transaction))
}

Expand Down
10 changes: 5 additions & 5 deletions crates/anvil/src/eth/backend/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use foundry_evm::{
DatabaseRef,
},
revm::primitives::{AccountInfo, Bytecode, KECCAK_EMPTY},
utils::{b160_to_h160, ru256_to_u256, u256_to_ru256},
};
use foundry_utils::types::{ToAlloy, ToEthers};
use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc};
use tokio::sync::RwLockWriteGuard;
Expand Down Expand Up @@ -104,7 +104,7 @@ pub(crate) struct AtGenesisStateDb<'a> {
impl<'a> DatabaseRef for AtGenesisStateDb<'a> {
type Error = DatabaseError;
fn basic(&self, address: B160) -> DatabaseResult<Option<AccountInfo>> {
if let Some(acc) = self.accounts.get(&b160_to_h160(address)).cloned() {
if let Some(acc) = self.accounts.get(&(address.to_ethers())).cloned() {
return Ok(Some(acc))
}
self.db.basic(address)
Expand All @@ -121,14 +121,14 @@ impl<'a> DatabaseRef for AtGenesisStateDb<'a> {
if let Some(acc) = self
.genesis
.as_ref()
.and_then(|genesis| genesis.alloc.accounts.get(&b160_to_h160(address)))
.and_then(|genesis| genesis.alloc.accounts.get(&(address.to_ethers())))
{
let value = acc
.storage
.get(&H256::from_uint(&ru256_to_u256(index)))
.get(&H256::from_uint(&(index.to_ethers())))
.copied()
.unwrap_or_default();
return Ok(u256_to_ru256(value.into_uint()))
return Ok(value.into_uint().to_alloy())
}
self.db.storage(address, index)
}
Expand Down
Loading