Skip to content

Commit

Permalink
fix: Allow block time offset to be negative (#663)
Browse files Browse the repository at this point in the history
* fix: Allow block time offset to be negative

* Add a changeset file

* fix: Use a correct sign for block time offset

* test: Add a regression test for forking blocks with future timestamps
  • Loading branch information
Xanewok committed Sep 16, 2024
1 parent bb3808b commit c8fbf3b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-cats-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/edr": patch
---

Allow forked block time offset to be negative
10 changes: 6 additions & 4 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};

Expand Down
2 changes: 1 addition & 1 deletion crates/edr_provider/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
36 changes: 36 additions & 0 deletions crates/edr_provider/tests/issues/issue_588.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Allow forking blocks with future timestamps.
//!
//! See <https://github.com/NomicFoundation/edr/issues/588>

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(())
}
1 change: 1 addition & 0 deletions crates/edr_provider/tests/issues/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ mod issue_407;
mod issue_503;
mod issue_533;
mod issue_570;
mod issue_588;

0 comments on commit c8fbf3b

Please sign in to comment.