Skip to content

Commit

Permalink
chore: use alloy state override on anvil (#6766)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir authored Jan 11, 2024
1 parent 06fc9ea commit b320027
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 85 deletions.
1 change: 0 additions & 1 deletion crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 0 additions & 16 deletions crates/anvil/core/src/eth/state.rs

This file was deleted.

52 changes: 0 additions & 52 deletions crates/anvil/core/src/eth/transaction/ethers_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use super::EthTransactionRequest;
use crate::eth::{
proof::AccountProof,
state::{AccountOverride, StateOverride as EthStateOverride},
transaction::{
DepositTransactionRequest, EIP1559TransactionRequest, EIP2930TransactionRequest,
LegacyTransactionRequest, MaybeImpersonatedTransaction, TypedTransaction,
Expand All @@ -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,
};
Expand Down Expand Up @@ -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::<u64>()),
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<TypedTransactionRequest> for EthersTypedTransactionRequest {
fn from(tx: TypedTransactionRequest) -> Self {
match tx {
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 22 additions & 14 deletions crates/anvil/tests/it/api.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -227,7 +229,7 @@ async fn call_with_override<M, D>(
api: &EthApi,
call: ContractCall<M, D>,
to: Address,
overrides: HashMap<Address, AccountOverride>,
overrides: StateOverride,
) -> D
where
D: Tokenizable,
Expand All @@ -240,7 +242,7 @@ where
..Default::default()
},
None,
Some(to_alloy_state_override(overrides)),
Some(overrides),
)
.await
.unwrap();
Expand Down Expand Up @@ -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()
},
)]);
Expand Down Expand Up @@ -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()
},
)]);
Expand Down

0 comments on commit b320027

Please sign in to comment.