Skip to content

fix(sim): Make simulation process actually continuous #86

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

Merged
merged 8 commits into from
Jul 8, 2025
Merged
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
30 changes: 25 additions & 5 deletions crates/sim/src/task.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::{env::SimEnv, BuiltBlock, SharedSimEnv, SimCache, SimDb};
use signet_types::constants::SignetSystemConstants;
use tokio::select;
use std::time::Duration;
use tokio::{select, time::Instant};
use tracing::{debug, info_span, trace, Instrument};
use trevm::{
helpers::Ctx,
revm::{inspector::NoOpInspector, DatabaseRef, Inspector},
Block, Cfg,
};

/// The amount of time to sleep between simulation rounds when there are no items to simulate.
pub(crate) const SIM_SLEEP_MS: u64 = 50;
Copy link
Member

Choose a reason for hiding this comment

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

future idea: make the sim-cache return a future that resolves when it has more items to sim

Copy link
Member Author

Choose a reason for hiding this comment

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

oh i like that


/// Builds a single block by repeatedly invoking [`SimEnv`].
#[derive(Debug)]
pub struct BlockBuild<Db, Insp = NoOpInspector> {
Expand Down Expand Up @@ -76,9 +80,29 @@ where
loop {
let span = info_span!("build", round = i);
let finish_by = self.finish_by.into();

let next_round_time = Instant::now() + Duration::from_millis(SIM_SLEEP_MS);

// If the next round time is past the deadline, we stop the simulation loop.
// This will stop the simulation even if there are items, but that is an acceptable tradeoff
// as we must ensure there's enough time to submit the blob to the host chain.
if next_round_time >= finish_by {
debug!("Next round time is past the deadline, stopping sim loop");
break;
}

// Only simulate if there are items to simulate.
// If there are not items, we sleep for [`SIM_SLEEP_MS`] and restart the loop.
if self.env.sim_items().is_empty() {
tokio::time::sleep_until(next_round_time).await;
continue;
}

// If there are items to simulate, we run a simulation round.
let fut = self.round().instrument(span);

select! {
biased;
_ = tokio::time::sleep_until(finish_by) => {
debug!("Deadline reached, stopping sim loop");
break;
Expand All @@ -87,10 +111,6 @@ where
i+= 1;
let remaining = self.env.sim_items().len();
trace!(%remaining, round = i, "Round completed");
if remaining == 0 {
debug!("No more items to simulate, stopping sim loop");
break;
}
}
}
}
Expand Down