Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow block time offset to be negative #663

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading