From 99080d52295e287dd0e845cbbccedac09d2054d5 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 11 Jan 2024 15:40:28 -0400 Subject: [PATCH] chore: use alloy state override on anvil --- crates/anvil/core/src/eth/mod.rs | 1 - crates/anvil/core/src/eth/state.rs | 16 ------ .../core/src/eth/transaction/ethers_compat.rs | 52 ------------------- crates/anvil/core/src/eth/transaction/mod.rs | 4 +- crates/anvil/tests/it/api.rs | 36 ++++++++----- 5 files changed, 24 insertions(+), 85 deletions(-) delete mode 100644 crates/anvil/core/src/eth/state.rs diff --git a/crates/anvil/core/src/eth/mod.rs b/crates/anvil/core/src/eth/mod.rs index dbe28104a7a3..c2532bcb8760 100644 --- a/crates/anvil/core/src/eth/mod.rs +++ b/crates/anvil/core/src/eth/mod.rs @@ -14,7 +14,6 @@ use ethers_core::types::transaction::eip712::TypedData; pub mod block; pub mod proof; pub mod receipt; -pub mod state; pub mod subscription; pub mod transaction; pub mod trie; diff --git a/crates/anvil/core/src/eth/state.rs b/crates/anvil/core/src/eth/state.rs deleted file mode 100644 index b492a06a0d59..000000000000 --- a/crates/anvil/core/src/eth/state.rs +++ /dev/null @@ -1,16 +0,0 @@ -use ethers_core::types::{Address, Bytes, H256, U256}; -use std::collections::HashMap; - -#[derive(Clone, Debug, Default, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct AccountOverride { - pub nonce: Option, - pub code: Option, - pub balance: Option, - pub state: Option>, - pub state_diff: Option>, -} - -pub type StateOverride = HashMap; diff --git a/crates/anvil/core/src/eth/transaction/ethers_compat.rs b/crates/anvil/core/src/eth/transaction/ethers_compat.rs index 8844c1bf43fd..64e60151670f 100644 --- a/crates/anvil/core/src/eth/transaction/ethers_compat.rs +++ b/crates/anvil/core/src/eth/transaction/ethers_compat.rs @@ -3,7 +3,6 @@ use super::EthTransactionRequest; use crate::eth::{ proof::AccountProof, - state::{AccountOverride, StateOverride as EthStateOverride}, transaction::{ DepositTransactionRequest, EIP1559TransactionRequest, EIP2930TransactionRequest, LegacyTransactionRequest, MaybeImpersonatedTransaction, TypedTransaction, @@ -12,7 +11,6 @@ use crate::eth::{ }; use alloy_primitives::{U128 as rU128, U256 as rU256, U64 as rU64}; use alloy_rpc_types::{ - state::{AccountOverride as AlloyAccountOverride, StateOverride}, transaction::request::TransactionRequest as AlloyTransactionRequest, AccessList as AlloyAccessList, CallRequest, Signature, Transaction as AlloyTransaction, }; @@ -118,56 +116,6 @@ pub fn from_ethers_access_list(access_list: AccessList) -> AlloyAccessList { AlloyAccessList(access_list.0.into_iter().map(ToAlloy::to_alloy).collect()) } -pub fn to_ethers_state_override(ov: StateOverride) -> EthStateOverride { - ov.into_iter() - .map(|(addr, o)| { - ( - addr.to_ethers(), - AccountOverride { - nonce: o.nonce.map(|n| n.to::()), - balance: o.balance.map(|b| b.to_ethers()), - code: o.code.map(|c| c.0.into()), - state_diff: o.state_diff.map(|s| { - s.into_iter() - .map(|(k, v)| (k.to_ethers(), H256::from_uint(&v.to_ethers()))) - .collect() - }), - state: o.state.map(|s| { - s.into_iter() - .map(|(k, v)| (k.to_ethers(), H256::from_uint(&v.to_ethers()))) - .collect() - }), - }, - ) - }) - .collect() -} - -pub fn to_alloy_state_override(ov: EthStateOverride) -> StateOverride { - ov.into_iter() - .map(|(addr, o)| { - ( - addr.to_alloy(), - AlloyAccountOverride { - nonce: o.nonce.map(rU64::from), - balance: o.balance.map(|b| b.to_alloy()), - code: o.code.map(|c| c.0.into()), - state_diff: o.state_diff.map(|s| { - s.into_iter() - .map(|(k, v)| (k.to_alloy(), rU256::from_be_bytes(v.to_alloy().0))) - .collect() - }), - state: o.state.map(|s| { - s.into_iter() - .map(|(k, v)| (k.to_alloy(), rU256::from_be_bytes(v.to_alloy().0))) - .collect() - }), - }, - ) - }) - .collect() -} - impl From for EthersTypedTransactionRequest { fn from(tx: TypedTransactionRequest) -> Self { match tx { diff --git a/crates/anvil/core/src/eth/transaction/mod.rs b/crates/anvil/core/src/eth/transaction/mod.rs index 7c259b8cdbe0..3fcedd7f6638 100644 --- a/crates/anvil/core/src/eth/transaction/mod.rs +++ b/crates/anvil/core/src/eth/transaction/mod.rs @@ -26,8 +26,8 @@ use std::ops::Deref; mod ethers_compat; pub use ethers_compat::{ - call_to_internal_tx_request, from_ethers_access_list, to_alloy_proof, to_alloy_state_override, - to_ethers_access_list, to_ethers_state_override, to_internal_tx_request, + call_to_internal_tx_request, from_ethers_access_list, to_alloy_proof, to_ethers_access_list, + to_internal_tx_request, }; /// The signature used to bypass signing via the `eth_sendUnsignedTransaction` cheat RPC diff --git a/crates/anvil/tests/it/api.rs b/crates/anvil/tests/it/api.rs index 7a6a64ae379a..84c93ea0338e 100644 --- a/crates/anvil/tests/it/api.rs +++ b/crates/anvil/tests/it/api.rs @@ -1,21 +1,23 @@ //! general eth api tests use crate::abi::{MulticallContract, SimpleStorage}; -use alloy_primitives::U256 as rU256; -use alloy_rpc_types::{CallInput, CallRequest}; +use alloy_primitives::{B256, U256 as rU256}; +use alloy_rpc_types::{ + state::{AccountOverride, StateOverride}, + CallInput, CallRequest, +}; use anvil::{ eth::{api::CLIENT_VERSION, EthApi}, spawn, NodeConfig, CHAIN_ID, }; -use anvil_core::eth::{state::AccountOverride, transaction::to_alloy_state_override}; use ethers::{ abi::{Address, Tokenizable}, prelude::{builders::ContractCall, decode_function_data, Middleware, SignerMiddleware}, signers::Signer, - types::{Block, BlockNumber, Chain, Transaction, TransactionRequest, H256, U256}, + types::{Block, BlockNumber, Chain, Transaction, TransactionRequest, U256}, utils::get_contract_address, }; -use foundry_common::types::ToAlloy; +use foundry_common::types::{ToAlloy, ToEthers}; use std::{collections::HashMap, sync::Arc, time::Duration}; #[tokio::test(flavor = "multi_thread")] @@ -227,7 +229,7 @@ async fn call_with_override( api: &EthApi, call: ContractCall, to: Address, - overrides: HashMap, + overrides: StateOverride, ) -> D where D: Tokenizable, @@ -240,7 +242,7 @@ where ..Default::default() }, None, - Some(to_alloy_state_override(overrides)), + Some(overrides), ) .await .unwrap(); @@ -268,25 +270,28 @@ async fn can_call_with_state_override() { .unwrap(); // Test the `balance` account override - let balance = 42u64.into(); + let balance = rU256::from(42u64); let result = call_with_override( &api, multicall.get_eth_balance(account), multicall.address(), HashMap::from([( - account, + account.to_alloy(), AccountOverride { balance: Some(balance), ..Default::default() }, )]), ) .await; - assert_eq!(result, balance); + assert_eq!(result, balance.to_ethers()); // Test the `state_diff` account override let overrides = HashMap::from([( - simple_storage.address(), + simple_storage.address().to_alloy(), AccountOverride { // The `lastSender` is in the first storage slot - state_diff: Some(HashMap::from([(H256::from_low_u64_be(0), account.into())])), + state_diff: Some(HashMap::from([( + B256::ZERO, + rU256::from_be_slice(B256::from(account.to_alloy().into_word()).as_slice()), + )])), ..Default::default() }, )]); @@ -317,10 +322,13 @@ async fn can_call_with_state_override() { // Test the `state` account override let overrides = HashMap::from([( - simple_storage.address(), + simple_storage.address().to_alloy(), AccountOverride { // The `lastSender` is in the first storage slot - state: Some(HashMap::from([(H256::from_low_u64_be(0), account.into())])), + state: Some(HashMap::from([( + B256::ZERO, + rU256::from_be_slice(B256::from(account.to_alloy().into_word()).as_slice()), + )])), ..Default::default() }, )]);