Skip to content

Commit 0cdfe1d

Browse files
committed
feat(testing/simulator): make simulations a bit more stable
re: #123
1 parent 21f4ffd commit 0cdfe1d

File tree

1 file changed

+14
-9
lines changed
  • node/testing/src/simulator

1 file changed

+14
-9
lines changed

node/testing/src/simulator/mod.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod config;
22
use std::time::Duration;
33

44
pub use config::*;
5-
use ledger::proofs::witness::transaction_snark::CONSTRAINT_CONSTANTS;
65
use node::{
76
p2p::connection::outgoing::P2pConnectionOutgoingInitOpts, ActionKind, BlockProducerConfig,
87
State,
@@ -195,28 +194,33 @@ impl Simulator {
195194
self.set_up_normal_nodes(&mut runner).await;
196195
self.set_up_block_producer_nodes(&mut runner).await;
197196

198-
let three_block_times =
199-
Duration::from_millis(CONSTRAINT_CONSTANTS.block_window_duration_ms * 3);
200197
let run_until = self.config.run_until.clone();
201198
let mut timeout = self.config.run_until_timeout;
202199
let mut rng = rand::rngs::StdRng::seed_from_u64(1);
203200

204201
while !timeout.is_zero() {
205202
let t = redux::Instant::now();
203+
tokio::task::yield_now().await;
206204
let _ = runner
207205
.run(
208-
Duration::from_millis(50),
206+
Duration::ZERO,
209207
|_, _, _| RunDecision::ContinueExec,
210208
|_, _, _, _| false,
211209
)
212210
.await;
213211

214-
for (_, node) in runner.nodes_iter() {
212+
for (node_id, node) in runner.nodes_iter() {
215213
let Some(best_tip) = node.state().transition_frontier.best_tip() else {
216214
continue;
217215
};
218216
let consensus_state = &best_tip.header().protocol_state.body.consensus_state;
219217

218+
eprintln!(
219+
"[node_status] node_{node_id} {} - {} [{}]",
220+
best_tip.height(),
221+
best_tip.hash(),
222+
best_tip.producer()
223+
);
220224
match &run_until {
221225
SimulatorRunUntil::Epoch(epoch) => {
222226
let cur_epoch = consensus_state.epoch_count.as_u32();
@@ -228,15 +232,16 @@ impl Simulator {
228232
}
229233

230234
// advance global time randomly.
231-
let elapsed = t.elapsed().as_nanos() as u64;
232-
let max_advance_time = (elapsed * 20).min(three_block_times.as_nanos() as u64);
233-
let by_nanos = rng.gen_range(elapsed..max_advance_time);
235+
let advance_time = Duration::from_millis(rng.gen_range(1..300));
236+
let elapsed = t.elapsed();
237+
let by_nanos = advance_time.as_nanos() as u64;
238+
eprintln!("[TIME] elapsed {elapsed:?}; advance_by: {advance_time:?}");
234239
runner
235240
.exec_step(ScenarioStep::AdvanceTime { by_nanos })
236241
.await
237242
.unwrap();
238243

239-
timeout -= t.elapsed();
244+
timeout = timeout.saturating_sub(elapsed);
240245
}
241246

242247
panic!("simulation timed out");

0 commit comments

Comments
 (0)