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

feat(anvil): support millisecond mining with correct block.timestamp #8010

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

yash-atreya
Copy link
Collaborator

@yash-atreya yash-atreya commented May 29, 2024

Motivation

Fixes #7931

Solution

Adds wall_clock_timestamp in TimeManager with millisecond precision.
This wall_clock_timestamp is then used as a reference in interval mining to update the next_timestamp.

Moreover, we now interpret TimeManager.interval as milliseconds instead of seconds. This brings breaking changes to the following anvil API method: evm_set_block_timestamp_interval.

Note: We still interpret --block-time from CLI as seconds. Hence, for millisecond mining user sets the --block-time as 0.05 or 0.75 etc.

@yash-atreya
Copy link
Collaborator Author

Need to add tests. DO NOT MERGE

testdata/lib/forge-std Outdated Show resolved Hide resolved
crates/anvil/src/eth/api.rs Outdated Show resolved Hide resolved
@yash-atreya
Copy link
Collaborator Author

@mattsse ptal

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this does not really address because this only changes the tracked timestamp value and has no effect on interval mining.

I think the right fix for this issue, which is much simpler would be here:

/// Creates a new instance with an interval of `duration`
pub fn new(duration: Duration) -> Self {

and we could leave the setblocktimestamp interval alone, we can't even express sub 1s intervals anyway.

@@ -1845,12 +1845,17 @@ impl EthApi {
Ok(true)
}

/// Sets an interval for the block timestamp
/// Sets an interval for the block timestamp in milliseconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect and should be seconds

@@ -581,7 +581,7 @@ pub enum EthRequest {
feature = "serde",
serde(rename = "anvil_setBlockTimestampInterval", with = "sequence")
)]
EvmSetBlockTimeStampInterval(u64),
EvmSetBlockTimeStampInterval(f64),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is okay due to how json numbers work and the existing test should cover this as well.

Comment on lines +248 to +249
// Set interval in TimeManager as milliseconds.
backend.time.set_block_timestamp_interval((interval).as_millis().try_into().unwrap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this additional call now required?

@@ -68,11 +81,11 @@ impl TimeManager {
self.add_offset(seconds as i128)
}

/// Sets the exact timestamp to use in the next block
/// Sets the exact timestamp (`next_exact_timestamp`) to use in the next block
/// Fails if it's before (or at the same time) the last timestamp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is no longer accurate

@@ -100,30 +113,58 @@ impl TimeManager {
}
}

/// Updates `wall_clock_timestamp` by `interval_ms`
pub fn update_wall_clock_timestamp_by_interval(&self, interval_ms: u64) {
let current_wall_timestamp = self.wall_clock_timestamp.read().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no unwrap here please

fn compute_next_timestamp(&self) -> (u64, Option<i128>) {
let current = duration_since_unix_epoch().as_secs() as i128;
fn compute_next_timestamp(&self, update_wall: bool) -> (u64, Option<i128>) {
let current = duration_since_unix_epoch().as_secs() as i128; // TODO(yash): Getting current time here as seconds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's left todo here?

Comment on lines +593 to +594
// Sleep for 1s
tokio::time::sleep(Duration::from_secs(1)).await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to sleep now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

anvil's block timestamp interval does not go below 1s
3 participants