From fb0a3ab7e13d99e91df504c78a9ab1537adaa908 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Fri, 13 Sep 2024 13:57:53 +0200 Subject: [PATCH 1/4] fix: Allow block time offset to be negative --- crates/edr_provider/src/data.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/edr_provider/src/data.rs b/crates/edr_provider/src/data.rs index c8ee01bfa..fd7c95a14 100644 --- a/crates/edr_provider/src/data.rs +++ b/crates/edr_provider/src/data.rs @@ -2691,11 +2691,13 @@ fn create_blockchain_and_state( .timestamp, ); - let elapsed_time = timer - .since(fork_block_timestamp) - .expect("current time must be after fork block"); + let elapsed = match timer.since(fork_block_timestamp) { + Ok(elapsed) => i128::from(elapsed), + Err(forward_drift) => -i128::from(forward_drift.duration().as_secs()), + }; - -i64::try_from(elapsed_time) + elapsed + .try_into() .expect("Elapsed time since fork block must be representable as i64") }; From 7dc9689a2c2b6841cd196d3f219d6dd8b2fe69a6 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Fri, 13 Sep 2024 14:08:22 +0200 Subject: [PATCH 2/4] Add a changeset file --- .changeset/hungry-cats-enjoy.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-cats-enjoy.md diff --git a/.changeset/hungry-cats-enjoy.md b/.changeset/hungry-cats-enjoy.md new file mode 100644 index 000000000..911a92474 --- /dev/null +++ b/.changeset/hungry-cats-enjoy.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/edr": patch +--- + +Allow forked block time offset to be negative From 05ebc6376af48ae3edf6469253ef8c40b7a3544f Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Fri, 13 Sep 2024 15:41:11 +0200 Subject: [PATCH 3/4] fix: Use a correct sign for block time offset --- crates/edr_provider/src/data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/edr_provider/src/data.rs b/crates/edr_provider/src/data.rs index fd7c95a14..2d0031536 100644 --- a/crates/edr_provider/src/data.rs +++ b/crates/edr_provider/src/data.rs @@ -2692,8 +2692,8 @@ fn create_blockchain_and_state( ); let elapsed = match timer.since(fork_block_timestamp) { - Ok(elapsed) => i128::from(elapsed), - Err(forward_drift) => -i128::from(forward_drift.duration().as_secs()), + Ok(elapsed) => -i128::from(elapsed), + Err(forward_drift) => i128::from(forward_drift.duration().as_secs()), }; elapsed From fd26e31e08e8c8ae90deb606cf5b68c9f2cf2998 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Fri, 13 Sep 2024 15:42:30 +0200 Subject: [PATCH 4/4] test: Add a regression test for forking blocks with future timestamps --- crates/edr_provider/src/time.rs | 2 +- crates/edr_provider/tests/issues/issue_588.rs | 36 +++++++++++++++++++ crates/edr_provider/tests/issues/mod.rs | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 crates/edr_provider/tests/issues/issue_588.rs diff --git a/crates/edr_provider/src/time.rs b/crates/edr_provider/src/time.rs index b23a71ef7..8eb7f18bd 100644 --- a/crates/edr_provider/src/time.rs +++ b/crates/edr_provider/src/time.rs @@ -42,7 +42,7 @@ mod test_utils { use super::{CurrentTime, TimeSinceEpoch}; - /// An internally mutable mock implementation of `TimeSinceEpoch`. + /// An internally mutable mock implementation of [`TimeSinceEpoch`]. #[derive(Debug)] pub struct MockTime(AtomicU64); diff --git a/crates/edr_provider/tests/issues/issue_588.rs b/crates/edr_provider/tests/issues/issue_588.rs new file mode 100644 index 000000000..6c09bd087 --- /dev/null +++ b/crates/edr_provider/tests/issues/issue_588.rs @@ -0,0 +1,36 @@ +//! Allow forking blocks with future timestamps. +//! +//! See + +use std::sync::Arc; + +use edr_provider::{ + hardhat_rpc_types::ForkConfig, test_utils::create_test_config_with_fork, time::MockTime, + NoopLogger, Provider, +}; +use edr_test_utils::env::get_alchemy_url; +use tokio::runtime; + +#[tokio::test(flavor = "multi_thread")] +async fn issue_588() -> anyhow::Result<()> { + let logger = Box::new(NoopLogger); + let subscriber = Box::new(|_event| {}); + + let early_mainnet_fork = create_test_config_with_fork(Some(ForkConfig { + json_rpc_url: get_alchemy_url(), + block_number: Some(2_675_000), + http_headers: None, + })); + + let current_time_is_1970 = Arc::new(MockTime::with_seconds(0)); + + let _forking_succeeds = Provider::new( + runtime::Handle::current(), + logger, + subscriber, + early_mainnet_fork, + current_time_is_1970, + )?; + + Ok(()) +} diff --git a/crates/edr_provider/tests/issues/mod.rs b/crates/edr_provider/tests/issues/mod.rs index 6581b6b7f..87498548a 100644 --- a/crates/edr_provider/tests/issues/mod.rs +++ b/crates/edr_provider/tests/issues/mod.rs @@ -11,3 +11,4 @@ mod issue_407; mod issue_503; mod issue_533; mod issue_570; +mod issue_588;