foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
7.86k stars 1.57k forks source link

request: getLastBlockWallTime debug rpc method #8217

Open transmissions11 opened 1 week ago

transmissions11 commented 1 week ago

Component

Anvil

Describe the feature you would like

To sync clocks effectively for real-time games, it would be really helpful to be able to get the unix wall time in ms when the last block was created. What I've been doing up until now is start anvil with a script like this:

function checkTimestamp() {
  const timestamp = Date.now();
  if ((timestamp + 25) % 1000 === 0) {
    console.log(`Timestamp ${timestamp} is divisible by 1000.`);
    process.exit(0); // Stop the program
  } else {
    setImmediate(checkTimestamp);
  }
}

console.log("Waiting for a timestamp divisible by 1000...");
checkTimestamp();

// startup anvil

But the interval between anvil's blocks sometimes drifts a little, resulting in timestamps drifting backwards or ahead of real time. Having an rpc method to get the exact offset would allow clients to adjust to any minor timestamp offset without the need for anvil to robustly sync to wall time.

Additional context

No response

ktarun1419 commented 1 week ago

Hi @transmissions11 i want to work on this one