Skip to content

Commit

Permalink
fix: fork reset (#857)
Browse files Browse the repository at this point in the history
* fix fork reset

* try fix test

* fmt
  • Loading branch information
nbaztec authored Jan 22, 2025
1 parent 5c46959 commit 2179ac1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
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 @@ -120,6 +120,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
20 changes: 20 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,26 @@ impl Backend {
*self.fork.write() = Some(fork);
*self.env.write() = env;
} else {
// Set cache path on correct block
{
let maybe_fork_url = { self.node_config.read().await.eth_rpc_url.clone() };
if let Some(fork_url) = maybe_fork_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;
}
}

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

Expand Down
36 changes: 36 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,42 @@ 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 m = re
.captures_iter(&cache_debug)
.next()
.expect("must have JsonBlockCacheDB match")
.get(1)
.expect("must have matching path")
.as_str();
m.strip_suffix("/storage.json").unwrap().split("/").last().unwrap().parse::<u64>().unwrap()
}

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
2 changes: 1 addition & 1 deletion crates/zksync/core/src/vm/farcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl FarCallHandler {
PrimitiveValue { value: return_fat_ptr.to_u256(), is_pointer: false };

// Just rewriting `code_page` is very error-prone, since the same memory page would be
// re-used for decommitments. We'll use a different approach:
// reused for decommitments. We'll use a different approach:
// - Set `previous_code_word` to the value that we want
// - Set `previous_code_memory_page` to the current code page + `previous_super_pc` to 0
// (as it corresponds
Expand Down

0 comments on commit 2179ac1

Please sign in to comment.