Skip to content

Commit 226b98c

Browse files
authored
fix(sim): Make simulation process actually continuous (#86)
* fix(sim): Make simulation process actually continuous Right now, the simulator stops as soon as there are no more items to simulate. This actually is not correct behavior, as we could be early in the slot, with more items coming in later in the slot which could yield a higher total payout on the block. What we actually want, is to simply skip doing the round at all if there are no items, sleep for a certain amount of time (bounded by the deadline), and either break (if we slept until the deadline) or continue if there's still time remaining. The resulting behavior is that the simulator always runs until the deadline, ensuring that items that come later in the slot, up until the deadline, can be simulated and included in the block. * chore: properly break if we sleep until deadline * chore: make amount of time in ms that the sim sleeps a constant * chore: rm dumb comparison * chore: rm newline * chore: simplify logic * chore: comment * chore: bias select!
1 parent f22f7d0 commit 226b98c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

crates/sim/src/task.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
use crate::{env::SimEnv, BuiltBlock, SharedSimEnv, SimCache, SimDb};
22
use signet_types::constants::SignetSystemConstants;
3-
use tokio::select;
3+
use std::time::Duration;
4+
use tokio::{select, time::Instant};
45
use tracing::{debug, info_span, trace, Instrument};
56
use trevm::{
67
helpers::Ctx,
78
revm::{inspector::NoOpInspector, DatabaseRef, Inspector},
89
Block, Cfg,
910
};
1011

12+
/// The amount of time to sleep between simulation rounds when there are no items to simulate.
13+
pub(crate) const SIM_SLEEP_MS: u64 = 50;
14+
1115
/// Builds a single block by repeatedly invoking [`SimEnv`].
1216
#[derive(Debug)]
1317
pub struct BlockBuild<Db, Insp = NoOpInspector> {
@@ -76,9 +80,29 @@ where
7680
loop {
7781
let span = info_span!("build", round = i);
7882
let finish_by = self.finish_by.into();
83+
84+
let next_round_time = Instant::now() + Duration::from_millis(SIM_SLEEP_MS);
85+
86+
// If the next round time is past the deadline, we stop the simulation loop.
87+
// This will stop the simulation even if there are items, but that is an acceptable tradeoff
88+
// as we must ensure there's enough time to submit the blob to the host chain.
89+
if next_round_time >= finish_by {
90+
debug!("Next round time is past the deadline, stopping sim loop");
91+
break;
92+
}
93+
94+
// Only simulate if there are items to simulate.
95+
// If there are not items, we sleep for [`SIM_SLEEP_MS`] and restart the loop.
96+
if self.env.sim_items().is_empty() {
97+
tokio::time::sleep_until(next_round_time).await;
98+
continue;
99+
}
100+
101+
// If there are items to simulate, we run a simulation round.
79102
let fut = self.round().instrument(span);
80103

81104
select! {
105+
biased;
82106
_ = tokio::time::sleep_until(finish_by) => {
83107
debug!("Deadline reached, stopping sim loop");
84108
break;
@@ -87,10 +111,6 @@ where
87111
i+= 1;
88112
let remaining = self.env.sim_items().len();
89113
trace!(%remaining, round = i, "Round completed");
90-
if remaining == 0 {
91-
debug!("No more items to simulate, stopping sim loop");
92-
break;
93-
}
94114
}
95115
}
96116
}

0 commit comments

Comments
 (0)