-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
Need to add tests. DO NOT MERGE |
@mattsse ptal |
There was a problem hiding this 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:
foundry/crates/anvil/src/eth/miner.rs
Lines 157 to 158 in 487892d
/// 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 |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
// Set interval in TimeManager as milliseconds. | ||
backend.time.set_block_timestamp_interval((interval).as_millis().try_into().unwrap()); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
// Sleep for 1s | ||
tokio::time::sleep(Duration::from_secs(1)).await; |
There was a problem hiding this comment.
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?
Superseded by #9160. Turns out its a lot simpler than this approach. |
Motivation
Fixes #7931
Closes #9142
Solution
Adds
wall_clock_timestamp
inTimeManager
with millisecond precision.This
wall_clock_timestamp
is then used as a reference in interval mining to update thenext_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
as0.05
or0.75
etc.