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

fix(anvil): reset cache path during anvil_reset without fork url #9729

Merged
merged 17 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ alloy-transport-ws.workspace = true
alloy-json-rpc.workspace = true
alloy-pubsub.workspace = true
foundry-test-utils.workspace = true
regex = { workspace = true, default-features = false }
similar-asserts.workspace = true
tokio = { workspace = true, features = ["full"] }

Expand Down
42 changes: 30 additions & 12 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,17 @@ impl Backend {
// update all settings related to the forked block
{
if let Some(fork_url) = forking.json_rpc_url {
// Set the fork block number
let mut node_config = self.node_config.write().await;
node_config.fork_choice = Some(ForkChoice::Block(fork_block_number));

let mut env = self.env.read().clone();
let (forked_db, client_fork_config) =
node_config.setup_fork_db_config(fork_url, &mut env, &self.fees).await?;

*self.db.write().await = Box::new(forked_db);
let fork = ClientFork::new(client_fork_config, Arc::clone(&self.db));
*self.fork.write() = Some(fork);
*self.env.write() = env;
self.reset_block_number(fork_url, fork_block_number).await?;
} else {
// If rpc url is unspecified, then update the fork with the new block number and
// existing rpc url, this updates the cache path
{
let maybe_fork_url = { self.node_config.read().await.eth_rpc_url.clone() };
if let Some(fork_url) = maybe_fork_url {
self.reset_block_number(fork_url, fork_block_number).await?;
}
}

let gas_limit = self.node_config.read().await.fork_gas_limit(&fork_block);
let mut env = self.env.write();

Expand Down Expand Up @@ -592,6 +590,26 @@ impl Backend {
}
}

async fn reset_block_number(
&self,
fork_url: String,
fork_block_number: u64,
) -> Result<(), BlockchainError> {
let mut node_config = self.node_config.write().await;
node_config.fork_choice = Some(ForkChoice::Block(fork_block_number));

let mut env = self.env.read().clone();
let (forked_db, client_fork_config) =
node_config.setup_fork_db_config(fork_url, &mut env, &self.fees).await?;

*self.db.write().await = Box::new(forked_db);
let fork = ClientFork::new(client_fork_config, Arc::clone(&self.db));
*self.fork.write() = Some(fork);
*self.env.write() = env;

Ok(())
}

/// Returns the `TimeManager` responsible for timestamps
pub fn time(&self) -> &TimeManager {
&self.time
Expand Down
45 changes: 45 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,51 @@ async fn test_reset_dev_account_nonce() {
assert!(receipt.status());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_reset_updates_cache_path_when_rpc_url_not_provided() {
let config: NodeConfig = fork_config();

let (mut api, _handle) = spawn(config).await;
let info = api.anvil_node_info().await.unwrap();
let number = info.fork_config.fork_block_number.unwrap();
assert_eq!(number, BLOCK_NUMBER);

async fn get_block_from_cache_path(api: &mut EthApi) -> u64 {
let db = api.backend.get_db().read().await;
let cache_debug = format!("{:?}", db.maybe_inner().unwrap().cache());
let re = regex::Regex::new(r#"JsonBlockCacheDB \{ cache_path: Some\("([^"]+)"\)"#).unwrap();
let cache_path = re
.captures_iter(&cache_debug)
.next()
.expect("must have JsonBlockCacheDB match")
.get(1)
.expect("must have matching path")
.as_str();

std::path::PathBuf::from(cache_path)
.parent()
.expect("must have filename")
.file_name()
.expect("must have block number as dir name")
.to_str()
.expect("must be valid string")
.parse::<u64>()
.expect("must be valid number")
}

assert_eq!(BLOCK_NUMBER, get_block_from_cache_path(&mut api).await);

// Reset to older block without specifying a new rpc url
api.anvil_reset(Some(Forking {
json_rpc_url: None,
block_number: Some(BLOCK_NUMBER - 1_000_000),
}))
.await
.unwrap();

assert_eq!(BLOCK_NUMBER - 1_000_000, get_block_from_cache_path(&mut api).await);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_fork_get_account() {
let (_api, handle) = spawn(fork_config()).await;
Expand Down
Loading