Skip to content

Commit

Permalink
!fix: Update hardhat_mine interval units from milliseconds to seconds (
Browse files Browse the repository at this point in the history
…#338)

* changed evm_setNextBlockTimestamp argument type to U64

* fixed caller of evm_setNextBlockTimestamp

* setting the timestamp of the next block in evm_setNextBlockTimestamp

* formatted

* treating block timestamps as seconds

* timestamp in seconds

---------

Co-authored-by: Nicolas Villanueva <1890113+MexicanAce@users.noreply.github.com>
  • Loading branch information
vbar and MexicanAce authored Sep 18, 2024
1 parent 5ad9652 commit f509d2b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/test/anvil-apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("anvil_mine", function () {
const latestBlock = await provider.getBlock("latest");
expect(latestBlock.number).to.equal(startingBlock.number + numberOfBlocks, "Block number mismatch");
expect(latestBlock.timestamp).to.equal(
startingTimestamp + (numberOfBlocks - 1) * intervalInSeconds * 1000 + 1,
startingTimestamp + (numberOfBlocks - 1) * intervalInSeconds + 1,
"Timestamp mismatch"
);
});
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/test/hardhat-apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("hardhat_mine", function () {
const latestBlock = await provider.getBlock("latest");
expect(latestBlock.number).to.equal(startingBlock.number + numberOfBlocks, "Block number mismatch");
expect(latestBlock.timestamp).to.equal(
startingTimestamp + (numberOfBlocks - 1) * intervalInSeconds * 1000 + 1,
startingTimestamp + (numberOfBlocks - 1) * intervalInSeconds + 1,
"Timestamp mismatch"
);
});
Expand Down
11 changes: 3 additions & 8 deletions src/node/in_memory_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
.map_err(|err| anyhow!("failed acquiring lock: {:?}", err))
.and_then(|mut writer| {
let num_blocks = num_blocks.unwrap_or_else(|| U64::from(1));
let interval_ms = interval
.unwrap_or_else(|| U64::from(1))
.saturating_mul(1_000.into());
let interval_sec = interval.unwrap_or_else(|| U64::from(1));
if num_blocks.is_zero() {
return Err(anyhow!(
"Number of blocks must be greater than 0".to_string(),
));
}
utils::mine_empty_blocks(&mut writer, num_blocks.as_u64(), interval_ms.as_u64())?;
utils::mine_empty_blocks(&mut writer, num_blocks.as_u64(), interval_sec.as_u64())?;
tracing::info!("👷 Mined {} blocks", num_blocks);

Ok(true)
Expand Down Expand Up @@ -498,10 +496,7 @@ mod tests {
.unwrap()
.expect("block exists");
assert_eq!(start_block.number + i + 1, current_block.number);
assert_eq!(
start_timestamp + i * interval * 1_000,
current_block.timestamp
);
assert_eq!(start_timestamp + i * interval, current_block.timestamp);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn bytecode_to_factory_dep(bytecode: Vec<u8>) -> Result<(U256, Vec<U256>), a
pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
node: &mut InMemoryNodeInner<S>,
num_blocks: u64,
interval_ms: u64,
interval_sec: u64,
) -> Result<(), anyhow::Error> {
// build and insert new blocks
for i in 0..num_blocks {
Expand All @@ -110,7 +110,7 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
let (batch_env, mut block_ctx) = node.create_l1_batch_env(storage.clone());
// override the next block's timestamp to match up with interval for subsequent blocks
if i != 0 {
block_ctx.timestamp = node.current_timestamp.saturating_add(interval_ms);
block_ctx.timestamp = node.current_timestamp.saturating_add(interval_sec);
}

// init vm
Expand Down

0 comments on commit f509d2b

Please sign in to comment.