Skip to content

Commit

Permalink
fix: wrong subtract timestamp (#6097)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 24, 2023
1 parent e82f5bd commit 6ac22df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,7 @@ impl Backend {
let block =
self.block_by_hash(best_block_hash).await?.ok_or(BlockchainError::BlockNotFound)?;

// Note: In [`TimeManager::compute_next_timestamp`] we ensure that the next timestamp is
// always increasing by at least one. By subtracting 1 here, this is mitigated.
let reset_time = block.timestamp.as_u64().saturating_sub(1);
let reset_time = block.timestamp.as_u64();
self.time.reset(reset_time);
self.set_block_number(num.into());
}
Expand Down
20 changes: 20 additions & 0 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,23 @@ async fn test_set_chain_id() {
let chain_id = provider.get_chainid().await.unwrap();
assert_eq!(chain_id, U256::from(1234));
}

// <https://github.com/foundry-rs/foundry/issues/6096>
#[tokio::test(flavor = "multi_thread")]
async fn test_fork_revert_next_block_timestamp() {
let (api, _handle) = spawn(fork_config()).await;

// 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 block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap();
assert_eq!(block, latest_block);

api.mine_one().await;
let block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap();
assert!(block.timestamp > latest_block.timestamp);
}

0 comments on commit 6ac22df

Please sign in to comment.