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

anvil: Fix snapshot revert not updating block-env correctly #6107

Merged
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
15 changes: 14 additions & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,20 @@ impl Backend {

let reset_time = block.timestamp.as_u64();
self.time.reset(reset_time);
self.set_block_number(num.into());

let mut env = self.env.write();
env.block = BlockEnv {
number: rU256::from(num),
timestamp: block.timestamp.to_alloy(),
difficulty: block.difficulty.to_alloy(),
// ensures prevrandao is set
prevrandao: Some(block.mix_hash.unwrap_or_default()).map(|h| h.to_alloy()),
gas_limit: block.gas_limit.to_alloy(),
// Keep previous `coinbase` and `basefee` value
coinbase: env.block.coinbase,
basefee: env.block.basefee,
..Default::default()
};
}
Ok(self.db.write().await.revert(id))
}
Expand Down
29 changes: 29 additions & 0 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,32 @@ async fn test_fork_revert_next_block_timestamp() {
let block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap();
assert!(block.timestamp > latest_block.timestamp);
}

// test that after a snapshot revert, the env block is reset
// to its correct value (block number, etc.)
#[tokio::test(flavor = "multi_thread")]
async fn test_fork_revert_call_latest_block_timestamp() {
let (api, handle) = spawn(fork_config()).await;
let provider = handle.http_provider();

// Mine a new block, and check the new block gas limit
api.mine_one().await;
let latest_block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap();

let snapshot_id = api.evm_snapshot().await.unwrap();
api.mine_one().await;
api.evm_revert(snapshot_id).await.unwrap();

let multicall = MulticallContract::new(
Address::from_str("0xeefba1e63905ef1d7acba5a8513c70307c1ce441").unwrap(),
provider.into(),
);

assert_eq!(multicall.get_current_block_timestamp().await.unwrap(), latest_block.timestamp);
assert_eq!(multicall.get_current_block_difficulty().await.unwrap(), latest_block.difficulty);
assert_eq!(multicall.get_current_block_gas_limit().await.unwrap(), latest_block.gas_limit);
assert_eq!(
multicall.get_current_block_coinbase().await.unwrap(),
latest_block.author.unwrap_or_default()
);
}