Skip to content

Commit

Permalink
fix: use block.number when creating fork (#4684)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Apr 2, 2023
1 parent 5de8ada commit 613073b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
8 changes: 3 additions & 5 deletions anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,10 @@ impl NodeConfig {
/// Returns the path where the cache file should be stored
///
/// See also [ Config::foundry_block_cache_file()]
pub fn block_cache_path(&self) -> Option<PathBuf> {
pub fn block_cache_path(&self, block: u64) -> Option<PathBuf> {
if self.no_storage_caching || self.eth_rpc_url.is_none() {
return None
}
// cache only if block explicitly set
let block = self.fork_block_number?;
let chain_id = self.get_chain_id();

Config::foundry_block_cache_file(chain_id, block)
Expand Down Expand Up @@ -902,9 +900,9 @@ impl NodeConfig {

let meta = BlockchainDbMeta::new(env.clone(), eth_rpc_url.clone());
let block_chain_db = if self.fork_chain_id.is_some() {
BlockchainDb::new_skip_check(meta, self.block_cache_path())
BlockchainDb::new_skip_check(meta, self.block_cache_path(fork_block_number))
} else {
BlockchainDb::new(meta, self.block_cache_path())
BlockchainDb::new(meta, self.block_cache_path(fork_block_number))
};

// This will spawn the background thread that will use the provider to fetch
Expand Down
2 changes: 1 addition & 1 deletion evm/src/executor/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ mod tests {
let mut evm_opts = config.extract::<EvmOpts>().unwrap();
evm_opts.fork_block_number = Some(block_num);

let env = evm_opts.fork_evm_env(ENDPOINT).await.unwrap();
let (env, _block) = evm_opts.fork_evm_env(ENDPOINT).await.unwrap();

let fork = CreateFork {
enable_caching: true,
Expand Down
6 changes: 3 additions & 3 deletions evm/src/executor/fork/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::apply_chain_and_block_specific_env_changes;
use ethers::{
providers::Middleware,
types::{Address, U256},
types::{Address, Block, TxHash, U256},
};
use eyre::WrapErr;
use futures::TryFutureExt;
Expand All @@ -16,7 +16,7 @@ pub async fn environment<M: Middleware>(
override_chain_id: Option<u64>,
pin_block: Option<u64>,
origin: Address,
) -> eyre::Result<Env>
) -> eyre::Result<(Env, Block<TxHash>)>
where
M::Error: 'static,
{
Expand Down Expand Up @@ -80,5 +80,5 @@ where

apply_chain_and_block_specific_env_changes(&mut env, &block);

Ok(env)
Ok((env, block))
}
10 changes: 7 additions & 3 deletions evm/src/executor/fork/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,14 @@ async fn create_fork(
);

// initialise the fork environment
fork.env = fork.evm_opts.fork_evm_env(&fork.url).await?;

let (env, block) = fork.evm_opts.fork_evm_env(&fork.url).await?;
fork.env = env;
let meta = BlockchainDbMeta::new(fork.env.clone(), fork.url.clone());
let number = meta.block_env.number.as_u64();

// we need to use the block number from the block because the env's number can be different on
// some L2s (e.g. Arbitrum).
let number =
block.number.map(|num| num.as_u64()).unwrap_or_else(|| meta.block_env.number.as_u64());

// determine the cache path if caching is enabled
let cache_path = if fork.enable_caching {
Expand Down
14 changes: 9 additions & 5 deletions evm/src/executor/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::executor::fork::CreateFork;
use ethers::{
providers::{Middleware, Provider},
solc::utils::RuntimeOrHandle,
types::{Address, Chain, H256, U256},
types::{Address, Block, Chain, TxHash, H256, U256},
};
use eyre::WrapErr;
use foundry_common::{self, ProviderBuilder, RpcUrl, ALCHEMY_FREE_TIER_CUPS};
Expand Down Expand Up @@ -59,7 +59,7 @@ impl EvmOpts {
/// id, )
pub async fn evm_env(&self) -> revm::Env {
if let Some(ref fork_url) = self.fork_url {
self.fork_evm_env(fork_url).await.expect("Could not instantiate forked environment")
self.fork_evm_env(fork_url).await.expect("Could not instantiate forked environment").0
} else {
self.local_evm_env()
}
Expand All @@ -72,14 +72,18 @@ impl EvmOpts {
/// Returns an error if a RPC request failed, or the fork url is not a valid url
pub fn evm_env_blocking(&self) -> eyre::Result<revm::Env> {
if let Some(ref fork_url) = self.fork_url {
RuntimeOrHandle::new().block_on(self.fork_evm_env(fork_url))
RuntimeOrHandle::new().block_on(self.fork_evm_env(fork_url)).map(|res| res.0)
} else {
Ok(self.local_evm_env())
}
}

/// Returns the `revm::Env` configured with settings retrieved from the endpoints
pub async fn fork_evm_env(&self, fork_url: impl AsRef<str>) -> eyre::Result<revm::Env> {
/// Returns the `revm::Env` that is configured with settings retrieved from the endpoint.
/// And the block that was used to configure the environment.
pub async fn fork_evm_env(
&self,
fork_url: impl AsRef<str>,
) -> eyre::Result<(revm::Env, Block<TxHash>)> {
let fork_url = fork_url.as_ref();
let provider = ProviderBuilder::new(fork_url)
.compute_units_per_second(self.get_compute_units_per_second())
Expand Down

0 comments on commit 613073b

Please sign in to comment.