Skip to content

Commit

Permalink
fix(anvil): on anvil_mine jump to next timestamp before mine new block (
Browse files Browse the repository at this point in the history
#9241)

* fix(anvil): on anvil_mine jump to next timestamp before mine new block

* Fix unrelated clippy, simplify test
  • Loading branch information
grandizzy authored Nov 1, 2024
1 parent 17e0981 commit 6b0c27e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,12 +1697,12 @@ impl EthApi {

// mine all the blocks
for _ in 0..blocks.to::<u64>() {
self.mine_one().await;

// If we have an interval, jump forwards in time to the "next" timestamp
if let Some(interval) = interval {
self.backend.time().increase_time(interval);
}

self.mine_one().await;
}

Ok(())
Expand Down
18 changes: 18 additions & 0 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,3 +1009,21 @@ async fn test_mine_blks_with_same_timestamp() {
// timestamps should be equal
assert_eq!(blks, vec![init_timestamp; 4]);
}

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

let init_block = api.block_by_number(0.into()).await.unwrap().unwrap();
let init_timestamp = init_block.header.timestamp;

// Mine 2 blocks with interval of 60.
let _ = api.anvil_mine(Some(U256::from(2)), Some(U256::from(60))).await;

let first_block = api.block_by_number(1.into()).await.unwrap().unwrap();
assert_eq!(first_block.header.timestamp, init_timestamp + 60);

let second_block = api.block_by_number(2.into()).await.unwrap().unwrap();
assert_eq!(second_block.header.timestamp, init_timestamp + 120);
}
2 changes: 1 addition & 1 deletion crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ where
/// # Ok(())
/// # }
/// ```
pub async fn call<'a>(
pub async fn call(
&self,
req: &WithOtherFields<TransactionRequest>,
func: Option<&Function>,
Expand Down

0 comments on commit 6b0c27e

Please sign in to comment.