1#[cfg(msim)]
5pub use msim::*;
6
7#[cfg(msim)]
8use std::hash::Hasher;
9
10use std::sync::atomic::{AtomicUsize, Ordering};
11
12pub use ::rand as rand_crate;
14pub use anemo;
15pub use anemo_tower;
16pub use fastcrypto;
17pub use lru;
18pub use move_package_alt;
19pub use move_package_alt_compilation;
20pub use mysten_network;
21pub use sui_framework;
22pub use sui_move_build;
23pub use sui_types;
24pub use telemetry_subscribers;
25pub use tempfile;
26pub use tower;
27
28#[cfg(msim)]
29pub mod configs {
30 use msim::*;
31 use std::collections::HashMap;
32 use std::ops::Range;
33 use std::time::Duration;
34
35 use tracing::info;
36
37 fn ms_to_dur(range: Range<u64>) -> Range<Duration> {
38 Duration::from_millis(range.start)..Duration::from_millis(range.end)
39 }
40
41 pub fn constant_latency_ms(latency: u64) -> SimConfig {
43 uniform_latency_ms(latency..(latency + 1))
44 }
45
46 pub fn uniform_latency_ms(range: Range<u64>) -> SimConfig {
48 let range = ms_to_dur(range);
49 SimConfig {
50 net: NetworkConfig {
51 latency: LatencyConfig {
52 default_latency: LatencyDistribution::uniform(range),
53 ..Default::default()
54 },
55 ..Default::default()
56 },
57 }
58 }
59
60 pub fn bimodal_latency_ms(
62 baseline: Range<u64>,
64 degraded: Range<u64>,
66 degraded_freq: f64,
68 ) -> SimConfig {
69 let baseline = ms_to_dur(baseline);
70 let degraded = ms_to_dur(degraded);
71 SimConfig {
72 net: NetworkConfig {
73 latency: LatencyConfig {
74 default_latency: LatencyDistribution::bimodal(
75 baseline,
76 degraded,
77 degraded_freq,
78 ),
79 ..Default::default()
80 },
81 ..Default::default()
82 },
83 }
84 }
85
86 pub fn env_config(
88 default: SimConfig,
90 env_configs: impl IntoIterator<Item = (&'static str, SimConfig)>,
93 ) -> SimConfig {
94 let mut env_configs = HashMap::<&'static str, SimConfig>::from_iter(env_configs);
95 if let Some(env) = std::env::var("SUI_SIM_CONFIG").ok() {
96 if let Some(cfg) = env_configs.remove(env.as_str()) {
97 info!("Using test config for SUI_SIM_CONFIG={}", env);
98 cfg
99 } else {
100 panic!(
101 "No config found for SUI_SIM_CONFIG={}. Available configs are: {:?}",
102 env,
103 env_configs.keys()
104 );
105 }
106 } else {
107 info!("Using default test config");
108 default
109 }
110 }
111}
112
113static NODE_COUNT: AtomicUsize = AtomicUsize::new(0);
114
115pub struct NodeLeakDetector(());
116
117impl NodeLeakDetector {
118 pub fn new() -> Self {
119 NODE_COUNT.fetch_add(1, Ordering::SeqCst);
120 Self(())
121 }
122
123 pub fn get_current_node_count() -> usize {
124 NODE_COUNT.load(Ordering::SeqCst)
125 }
126}
127
128impl Default for NodeLeakDetector {
129 fn default() -> Self {
130 Self::new()
131 }
132}
133
134impl Drop for NodeLeakDetector {
135 fn drop(&mut self) {
136 NODE_COUNT.fetch_sub(1, Ordering::SeqCst);
137 }
138}
139
140#[cfg(not(msim))]
141#[macro_export]
142macro_rules! return_if_killed {
143 () => {};
144}
145
146#[cfg(msim)]
147pub fn current_simnode_id() -> msim::task::NodeId {
148 msim::runtime::NodeHandle::current().id()
149}
150
151pub fn has_mainnet_protocol_config_override() -> bool {
152 use sui_types::{digests::ChainIdentifier, supported_protocol_versions::Chain};
153
154 ChainIdentifier::default().chain() == Chain::Mainnet
155}
156
157#[cfg(msim)]
158pub mod random {
159 use super::*;
160
161 use rand_crate::{Rng, SeedableRng, rngs::SmallRng, thread_rng};
162 use serde::Serialize;
163 use std::collections::HashSet;
164 use std::hash::Hash;
165 use std::sync::{Mutex, OnceLock};
166
167 pub fn deterministic_probability<T: Hash>(value: T, chance: f32) -> bool {
170 static SEED: OnceLock<u64> = OnceLock::new();
173 let seed = *SEED.get_or_init(|| thread_rng().r#gen());
174
175 chance > {
176 let mut hasher = std::collections::hash_map::DefaultHasher::new();
177 seed.hash(&mut hasher);
178 value.hash(&mut hasher);
179 let mut rng = SmallRng::seed_from_u64(hasher.finish());
180 rng.gen_range(0.0..1.0)
181 }
182 }
183
184 pub fn deterministic_probability_once<T: Hash + Serialize>(value: T, chance: f32) -> bool {
187 static FAILING_VALUES: Mutex<Option<HashSet<(msim::task::NodeId, Vec<u8>)>>> =
188 Mutex::new(None);
189
190 let bytes = bcs::to_bytes(&value).unwrap();
191 let key = (current_simnode_id(), bytes);
192
193 let mut guard = FAILING_VALUES.lock().unwrap();
194 let failing_values = guard.get_or_insert_with(HashSet::new);
195 if failing_values.contains(&key) {
196 false
197 } else if deterministic_probability(value, chance) {
198 failing_values.insert(key);
199 true
200 } else {
201 false
202 }
203 }
204}