Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(anvil): fix tests #6383

Merged
merged 10 commits into from
Nov 21, 2023
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
44 changes: 22 additions & 22 deletions Cargo.lock

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

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ tower-http = "0.4"
#ethers-solc = { path = "../ethers-rs/ethers-solc" }

[patch.crates-io]
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-addressbook = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-core = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-contract = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-contract-abigen = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-providers = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-signers = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-middleware = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-etherscan = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers-solc = { git = "https://github.com/gakonst/ethers-rs", rev = "b4c366c964106b274e2eb528ac44eebc9bb49551" }
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-addressbook = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-core = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-contract = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-contract-abigen = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-providers = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-signers = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-middleware = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-etherscan = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }
ethers-solc = { git = "https://github.com/gakonst/ethers-rs", rev = "546ea029362a7502365667c6f99fac92ad0aeb9f" }

alloy-dyn-abi = { git = "https://github.com/alloy-rs/core/" }
alloy-primitives = { git = "https://github.com/alloy-rs/core/" }
Expand Down
1 change: 1 addition & 0 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod subscription;
pub mod transaction;
pub mod trie;
pub mod utils;
pub mod state;

#[cfg(feature = "serde")]
pub mod serde_helpers;
Expand Down
16 changes: 16 additions & 0 deletions crates/anvil/core/src/eth/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use ethers_core::types::{Address, Bytes, H256, U256};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[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<u64>,
pub code: Option<Bytes>,
pub balance: Option<U256>,
pub state: Option<HashMap<H256, H256>>,
pub state_diff: Option<HashMap<H256, H256>>,
}

pub type StateOverride = HashMap<Address, AccountOverride>;
45 changes: 28 additions & 17 deletions crates/anvil/core/src/eth/transaction/ethers_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::eth::{
transaction::{
EIP1559TransactionRequest, EIP2930TransactionRequest, LegacyTransactionRequest,
MaybeImpersonatedTransaction, TypedTransaction, TypedTransactionRequest,
},
}, state::{StateOverride as EthStateOverride, AccountOverride},
};
use alloy_primitives::{U128 as rU128, U256 as rU256, U64 as rU64};
use alloy_rpc_types::{
AccessList as AlloyAccessList, AccessListItem as AlloyAccessListItem, CallRequest,
EIP1186StorageProof, Signature, Transaction as AlloyTransaction,
TransactionRequest as AlloyTransactionRequest,
TransactionRequest as AlloyTransactionRequest, state::{StateOverride, AccountOverride as AlloyAccountOverride},
};
use ethers_core::types::{
transaction::{
Expand Down Expand Up @@ -98,28 +98,39 @@ pub fn to_ethers_access_list(access_list: AlloyAccessList) -> AccessList {
storage_keys: item
.storage_keys
.into_iter()
.map(|k| BigEndianHash::from_uint(&k.to_ethers()))
.map(|k| {
BigEndianHash::from_uint(&U256::from_big_endian(k.to_ethers().as_bytes()))
})
.collect(),
})
.collect(),
)
}

pub fn from_ethers_access_list(access_list: AccessList) -> AlloyAccessList {
AlloyAccessList(
access_list
.0
.into_iter()
.map(|item| AlloyAccessListItem {
address: item.address.to_alloy(),
storage_keys: item
.storage_keys
.into_iter()
.map(|k| rU256::from_be_bytes(k.to_alloy().0))
.collect(),
})
.collect(),
)
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(|n| rU64::from(n)),
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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod ethers_compat;

pub use ethers_compat::{
call_to_internal_tx_request, from_ethers_access_list, to_alloy_proof, to_ethers_access_list,
to_internal_tx_request,
to_internal_tx_request, to_ethers_state_override, to_alloy_state_override
};

/// The signature used to bypass signing via the `eth_sendUnsignedTransaction` cheat RPC
Expand Down
6 changes: 3 additions & 3 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ impl NodeConfig {
};

let block = provider
.get_block(BlockNumberOrTag::Number(U64::from(fork_block_number)).into(), false)
.get_block(BlockNumberOrTag::Number(fork_block_number).into(), false)
.await
.expect("Failed to get fork block");

Expand All @@ -969,7 +969,7 @@ latest block number: {latest_block}"
// If the `eth_getBlockByNumber` call succeeds, but returns null instead of
// the block, and the block number is less than equal the latest block, then
// the user is forking from a non-archive node with an older block number.
if fork_block_number <= latest_block.to::<u64>() {
if fork_block_number <= latest_block {
message.push_str(&format!("\n{}", NON_ARCHIVE_NODE_WARNING));
}
panic!("{}", message);
Expand Down Expand Up @@ -1189,7 +1189,7 @@ pub fn anvil_tmp_dir() -> Option<PathBuf> {
/// This fetches the "latest" block and checks whether the `Block` is fully populated (`hash` field
/// is present). This prevents edge cases where anvil forks the "latest" block but `eth_getBlockByNumber` still returns a pending block, <https://github.com/foundry-rs/foundry/issues/2036>
async fn find_latest_fork_block<P: TempProvider>(provider: P) -> Result<u64, TransportError> {
let mut num = provider.get_block_number().await?.to::<u64>();
let mut num = provider.get_block_number().await?;

// walk back from the head of the chain, but at most 2 blocks, which should be more than enough
// leeway
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl EthApi {
fork.storage_at(
address,
B256::from(index),
Some(BlockNumber::Number(*number)),
Some(BlockNumber::Number(number.to::<u64>())),
)
.await
.map_err(|_| BlockchainError::DataUnavailable)?,
Expand Down Expand Up @@ -1260,7 +1260,7 @@ impl EthApi {
let number = match newest_block {
BlockNumber::Latest | BlockNumber::Pending => current,
BlockNumber::Earliest => 0,
BlockNumber::Number(n) => n.to::<u64>(),
BlockNumber::Number(n) => n,
BlockNumber::Safe => current.saturating_sub(slots_in_an_epoch),
BlockNumber::Finalized => current.saturating_sub(slots_in_an_epoch * 2),
};
Expand All @@ -1273,7 +1273,7 @@ impl EthApi {
return Ok(fork
.fee_history(
block_count,
BlockNumber::Number(U64::from(number)),
BlockNumber::Number(number),
&reward_percentiles,
)
.await
Expand Down Expand Up @@ -1906,7 +1906,7 @@ impl EthApi {
for offset in (0..mined_blocks).rev() {
let block_num = latest - offset;
if let Some(mut block) =
self.backend.block_by_number_full(BlockNumber::Number(U64::from(block_num))).await?
self.backend.block_by_number_full(BlockNumber::Number(block_num)).await?
{
let mut block_txs = match block.transactions {
BlockTransactions::Full(txs) => txs,
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl ClientFork {

if let BlockNumber::Number(num) = block {
// check if this request was already been sent
let key = (request.clone(), num.to::<u64>());
let key = (request.clone(), num);
if let Some(res) = self.storage_read().eth_call.get(&key).cloned() {
return Ok(res);
}
Expand All @@ -191,7 +191,7 @@ impl ClientFork {
if let BlockNumber::Number(num) = block {
// cache result
let mut storage = self.storage_write();
storage.eth_call.insert((request, num.to::<u64>()), res.clone());
storage.eth_call.insert((request, num), res.clone());
}

Ok(res)
Expand All @@ -208,7 +208,7 @@ impl ClientFork {

if let BlockNumber::Number(num) = block {
// check if this request was already been sent
let key = (request.clone(), num.to::<u64>());
let key = (request.clone(), num);
if let Some(res) = self.storage_read().eth_gas_estimations.get(&key).cloned() {
return Ok(res);
}
Expand All @@ -221,7 +221,7 @@ impl ClientFork {
if let BlockNumber::Number(num) = block {
// cache result
let mut storage = self.storage_write();
storage.eth_gas_estimations.insert((request, num.to::<u64>()), res);
storage.eth_gas_estimations.insert((request, num), res);
}

Ok(res)
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl BlockRequest {
pub fn block_number(&self) -> BlockNumber {
match self {
BlockRequest::Pending(_) => BlockNumber::Pending,
BlockRequest::Number(n) => BlockNumber::Number(*n),
BlockRequest::Number(n) => BlockNumber::Number(n.to::<u64>()),
}
}
}
Expand Down Expand Up @@ -1602,7 +1602,7 @@ impl Backend {
BlockId::Number(num) => match num {
BlockNumber::Latest | BlockNumber::Pending => self.best_number().to::<u64>(),
BlockNumber::Earliest => U64::ZERO.to::<u64>(),
BlockNumber::Number(num) => num.to::<u64>(),
BlockNumber::Number(num) => num,
BlockNumber::Safe => {
U64::from(current).saturating_sub(U64::from(slots_in_an_epoch)).to::<u64>()
}
Expand All @@ -1625,7 +1625,7 @@ impl Backend {
match block.unwrap_or(BlockNumber::Latest) {
BlockNumber::Latest | BlockNumber::Pending => current,
BlockNumber::Earliest => 0,
BlockNumber::Number(num) => num.to::<u64>(),
BlockNumber::Number(num) => num,
BlockNumber::Safe => current.saturating_sub(slots_in_an_epoch),
BlockNumber::Finalized => current.saturating_sub(slots_in_an_epoch * 2),
}
Expand Down Expand Up @@ -1660,7 +1660,7 @@ impl Backend {
.await;
return Ok(result);
}
Some(BlockRequest::Number(bn)) => Some(BlockNumber::Number(bn)),
Some(BlockRequest::Number(bn)) => Some(BlockNumber::Number(bn.to::<u64>())),
None => None,
};
let block_number: U256 = U256::from(self.convert_block_number(block_number));
Expand Down
Loading
Loading