From f509d2b696588f68123746d0ae1d83170dc99a60 Mon Sep 17 00:00:00 2001 From: Vaclav Barta Date: Wed, 18 Sep 2024 13:01:49 +0200 Subject: [PATCH] !fix: Update hardhat_mine interval units from milliseconds to seconds (#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> --- e2e-tests/test/anvil-apis.test.ts | 2 +- e2e-tests/test/hardhat-apis.test.ts | 2 +- src/node/in_memory_ext.rs | 11 +++-------- src/utils.rs | 4 ++-- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/e2e-tests/test/anvil-apis.test.ts b/e2e-tests/test/anvil-apis.test.ts index 787b2fd6..8f97d088 100644 --- a/e2e-tests/test/anvil-apis.test.ts +++ b/e2e-tests/test/anvil-apis.test.ts @@ -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" ); }); diff --git a/e2e-tests/test/hardhat-apis.test.ts b/e2e-tests/test/hardhat-apis.test.ts index a91a76dc..6bbcaa28 100644 --- a/e2e-tests/test/hardhat-apis.test.ts +++ b/e2e-tests/test/hardhat-apis.test.ts @@ -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" ); }); diff --git a/src/node/in_memory_ext.rs b/src/node/in_memory_ext.rs index ef2379e5..59cd612e 100644 --- a/src/node/in_memory_ext.rs +++ b/src/node/in_memory_ext.rs @@ -255,15 +255,13 @@ impl 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) @@ -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); } } diff --git a/src/utils.rs b/src/utils.rs index 0b81cd02..876c0fde 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -96,7 +96,7 @@ pub fn bytecode_to_factory_dep(bytecode: Vec) -> Result<(U256, Vec), a pub fn mine_empty_blocks( node: &mut InMemoryNodeInner, num_blocks: u64, - interval_ms: u64, + interval_sec: u64, ) -> Result<(), anyhow::Error> { // build and insert new blocks for i in 0..num_blocks { @@ -110,7 +110,7 @@ pub fn mine_empty_blocks( 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