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: remove fork db snapshot #6486

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,51 @@ async fn test_fork_snapshotting() {
assert_eq!(block_number, provider.get_block_number().await.unwrap());
}

#[tokio::test(flavor = "multi_thread")]
async fn test_fork_snapshotting_repeated() {
let (api, handle) = spawn(fork_config()).await;
let provider = handle.http_provider();

let snapshot = api.evm_snapshot().await.unwrap();

let accounts: Vec<_> = handle.dev_wallets().collect();
let from = accounts[0].address();
let to = accounts[1].address();
let block_number = provider.get_block_number().await.unwrap();

let initial_nonce = provider.get_transaction_count(from, None).await.unwrap();
let balance_before = provider.get_balance(to, None).await.unwrap();
let amount = handle.genesis_balance().checked_div(2u64.into()).unwrap();

let tx = TransactionRequest::new().to(to).value(amount).from(from);

let _ = provider.send_transaction(tx, None).await.unwrap().await.unwrap().unwrap();

let nonce = provider.get_transaction_count(from, None).await.unwrap();
assert_eq!(nonce, initial_nonce + 1);
let to_balance = provider.get_balance(to, None).await.unwrap();
assert_eq!(balance_before.saturating_add(amount), to_balance);

let second_snapshot = api.evm_snapshot().await.unwrap();

assert!(api.evm_revert(snapshot).await.unwrap());

let nonce = provider.get_transaction_count(from, None).await.unwrap();
assert_eq!(nonce, initial_nonce);
let balance = provider.get_balance(from, None).await.unwrap();
assert_eq!(balance, handle.genesis_balance());
let balance = provider.get_balance(to, None).await.unwrap();
assert_eq!(balance, handle.genesis_balance());
assert_eq!(block_number, provider.get_block_number().await.unwrap());

// invalidated
// TODO enable after <https://github.com/foundry-rs/foundry/pull/6366>
// assert!(!api.evm_revert(second_snapshot).await.unwrap());

// nothing is reverted, snapshot gone
assert!(!api.evm_revert(snapshot).await.unwrap());
}

/// tests that the remote state and local state are kept separate.
/// changes don't make into the read only Database that holds the remote state, which is flushed to
/// a cache file.
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/core/src/fork/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ impl ForkedDatabase {
id
}

/// Removes the snapshot from the tracked snapshot and sets it as the current state
pub fn revert_snapshot(&mut self, id: U256) -> bool {
let snapshot = { self.snapshots().lock().remove_at(id) };
if let Some(snapshot) = snapshot {
self.snapshots().lock().insert_at(snapshot.clone(), id);
let ForkDbSnapshot {
local,
snapshot: StateSnapshot { accounts, storage, block_hashes },
Expand Down
Loading