Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit a0a04af

Browse files
authored
Merge pull request #61 from heliaxdev/yuji/remove-rng
Remove `rng` from `State`
2 parents 3a403e8 + dbdaa4f commit a0a04af

28 files changed

+105
-148
lines changed

check/src/checks/status.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ pub struct StatusCheck;
99

1010
impl DoCheck for StatusCheck {
1111
async fn check(&self, sdk: &Sdk, _state: &mut crate::state::State) -> Result<(), String> {
12-
let client = sdk.namada.clone_client();
13-
let status = client
12+
let status = sdk
13+
.namada
14+
.client
1415
.status()
1516
.await
1617
.map_err(|e| format!("Failed to query status: {e}"))?;

workload/src/config.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub struct AppConfig {
1212
#[arg(required = true)]
1313
pub chain_id: String,
1414
#[clap(long, env)]
15-
pub seed: Option<u64>,
16-
#[clap(long, env)]
1715
pub id: u64,
1816
#[clap(long, env)]
1917
#[arg(required = true)]

workload/src/executor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl WorkloadExecutor {
135135
step_type.is_valid(&self.sdk, &self.state).await
136136
}
137137

138-
pub async fn build(&mut self, step_type: &StepType) -> Result<Vec<Task>, StepError> {
139-
step_type.build_task(&self.sdk, &mut self.state).await
138+
pub async fn build(&self, step_type: &StepType) -> Result<Vec<Task>, StepError> {
139+
step_type.build_task(&self.sdk, &self.state).await
140140
}
141141

142142
pub async fn build_check(&self, tasks: &[Task]) -> Result<Vec<Check>, StepError> {

workload/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn inner_main() -> Code {
6161
Ok(result) => result,
6262
Err(StateError::EmptyFile) => {
6363
tracing::warn!("State file is empty, creating new one");
64-
match State::create_new(config.id, config.seed) {
64+
match State::create_new(config.id) {
6565
Ok(result) => result,
6666
Err(e) => return Code::StateFatal(e),
6767
}
@@ -70,7 +70,6 @@ async fn inner_main() -> Code {
7070
};
7171

7272
tracing::info!("Using base dir: {}", state.base_dir.as_path().display());
73-
tracing::info!("Using seed: {}", state.seed);
7473

7574
let url = Url::from_str(&config.rpc).expect("invalid RPC address");
7675
tracing::debug!("Opening connection to {url}");

workload/src/sdk/namada.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Sdk {
4242
.insert_keypair("faucet".to_string(), true, sk, None, Some(address), None)
4343
.unwrap();
4444

45-
let native_token = rpc::query_native_token(&namada.clone_client())
45+
let native_token = rpc::query_native_token(&namada.client)
4646
.await
4747
.map_err(|e| e.to_string())?;
4848
namada_wallet

workload/src/state.rs

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::{
2-
collections::{BTreeSet, HashMap, HashSet},
3-
env, fs,
4-
path::PathBuf,
5-
};
1+
use std::collections::{BTreeSet, HashMap, HashSet};
2+
use std::path::PathBuf;
3+
use std::{env, fs};
64

5+
use antithesis_sdk::random::AntithesisRng;
76
use fs2::FileExt;
8-
use rand::{seq::IteratorRandom, Rng, RngCore};
7+
use rand::seq::IteratorRandom;
98
use serde::{Deserialize, Serialize};
109
use thiserror::Error;
1110

@@ -82,15 +81,12 @@ pub struct State {
8281
pub deactivated_validators: HashMap<Alias, Account>,
8382
pub proposals: HashMap<u64, (u64, u64)>,
8483
pub id: u64,
85-
pub seed: u64,
86-
pub rng: AntithesisRng,
8784
pub base_dir: PathBuf,
8885
pub stats: HashMap<String, u64>,
8986
}
9087

9188
impl State {
92-
pub fn new(id: u64, seed: Option<u64>) -> Self {
93-
let seed = seed.unwrap_or(rand::thread_rng().gen_range(0..u64::MAX));
89+
pub fn new(id: u64) -> Self {
9490
Self {
9591
accounts: HashMap::default(),
9692
masp_accounts: HashMap::default(),
@@ -103,8 +99,6 @@ impl State {
10399
deactivated_validators: HashMap::default(),
104100
proposals: HashMap::default(),
105101
id,
106-
seed,
107-
rng: AntithesisRng::default(),
108102
base_dir: env::current_dir().unwrap().join("base"),
109103
stats: HashMap::default(),
110104
}
@@ -130,11 +124,11 @@ impl State {
130124
Ok(())
131125
}
132126

133-
pub fn create_new(id: u64, seed: Option<u64>) -> Result<(Self, fs::File), StateError> {
127+
pub fn create_new(id: u64) -> Result<(Self, fs::File), StateError> {
134128
// Lock the state file before writing the new
135129
let file = Self::lock_state_file(id)?;
136130

137-
let state = Self::new(id, seed);
131+
let state = Self::new(id);
138132
state.save(None)?;
139133

140134
Ok((state, file))
@@ -269,16 +263,16 @@ impl State {
269263

270264
/// GET
271265
272-
pub fn random_account(&mut self, blacklist: Vec<Alias>) -> Option<Account> {
266+
pub fn random_account(&self, blacklist: Vec<Alias>) -> Option<Account> {
273267
self.accounts
274268
.iter()
275269
.filter(|(alias, _)| !blacklist.contains(alias))
276-
.choose(&mut self.rng)
270+
.choose(&mut AntithesisRng)
277271
.map(|(_, account)| account.clone())
278272
}
279273

280274
pub fn random_masp_account_with_min_balance(
281-
&mut self,
275+
&self,
282276
blacklist: Vec<Alias>,
283277
min_value: u64,
284278
) -> Option<MaspAccount> {
@@ -294,74 +288,74 @@ impl State {
294288
None
295289
}
296290
})
297-
.choose(&mut self.rng)
291+
.choose(&mut AntithesisRng)
298292
}
299293

300-
pub fn random_payment_address(&mut self, blacklist: Vec<Alias>) -> Option<MaspAccount> {
294+
pub fn random_payment_address(&self, blacklist: Vec<Alias>) -> Option<MaspAccount> {
301295
self.masp_accounts
302296
.iter()
303297
.filter(|(alias, _)| !blacklist.contains(alias))
304-
.choose(&mut self.rng)
298+
.choose(&mut AntithesisRng)
305299
.map(|(_, account)| account.clone())
306300
}
307301

308302
pub fn random_implicit_accounts(
309-
&mut self,
303+
&self,
310304
blacklist: Vec<Alias>,
311305
sample_size: usize,
312306
) -> Vec<Account> {
313307
self.accounts
314308
.iter()
315309
.filter(|(alias, _)| !blacklist.contains(alias))
316310
.filter(|(_, account)| account.is_implicit())
317-
.choose_multiple(&mut self.rng, sample_size)
311+
.choose_multiple(&mut AntithesisRng, sample_size)
318312
.into_iter()
319313
.map(|(_, account)| account.clone())
320314
.collect()
321315
}
322316

323317
pub fn random_enstablished_account(
324-
&mut self,
318+
&self,
325319
blacklist: Vec<Alias>,
326320
sample_size: usize,
327321
) -> Vec<Account> {
328322
self.accounts
329323
.iter()
330324
.filter(|(alias, _)| !blacklist.contains(alias))
331325
.filter(|(_, account)| account.is_enstablished())
332-
.choose_multiple(&mut self.rng, sample_size)
326+
.choose_multiple(&mut AntithesisRng, sample_size)
333327
.into_iter()
334328
.map(|(_, account)| account.clone())
335329
.collect()
336330
}
337331

338-
pub fn random_validator(&mut self, blacklist: Vec<Alias>, sample_size: usize) -> Vec<Account> {
332+
pub fn random_validator(&self, blacklist: Vec<Alias>, sample_size: usize) -> Vec<Account> {
339333
self.validators
340334
.iter()
341335
.filter(|(alias, _)| !blacklist.contains(alias))
342336
.filter(|(_, account)| account.is_enstablished())
343-
.choose_multiple(&mut self.rng, sample_size)
337+
.choose_multiple(&mut AntithesisRng, sample_size)
344338
.into_iter()
345339
.map(|(_, account)| account.clone())
346340
.collect()
347341
}
348342

349343
pub fn random_deactivated_validator(
350-
&mut self,
344+
&self,
351345
blacklist: Vec<Alias>,
352346
sample_size: usize,
353347
) -> Vec<Account> {
354348
self.deactivated_validators
355349
.iter()
356350
.filter(|(alias, _)| !blacklist.contains(alias))
357351
.filter(|(_, account)| account.is_enstablished())
358-
.choose_multiple(&mut self.rng, sample_size)
352+
.choose_multiple(&mut AntithesisRng, sample_size)
359353
.into_iter()
360354
.map(|(_, account)| account.clone())
361355
.collect()
362356
}
363357

364-
pub fn random_bond(&mut self) -> Bond {
358+
pub fn random_bond(&self) -> Bond {
365359
self.bonds
366360
.iter()
367361
.flat_map(|(source, bonds)| {
@@ -377,12 +371,12 @@ impl State {
377371
}
378372
})
379373
})
380-
.choose(&mut self.rng)
374+
.choose(&mut AntithesisRng)
381375
.unwrap()
382376
}
383377

384378
pub fn random_account_with_min_balance(
385-
&mut self,
379+
&self,
386380
blacklist: Vec<Alias>,
387381
min_balance: u64,
388382
) -> Option<Account> {
@@ -398,7 +392,7 @@ impl State {
398392
None
399393
}
400394
})
401-
.choose(&mut self.rng)
395+
.choose(&mut AntithesisRng)
402396
}
403397

404398
pub fn get_account_by_alias(&self, alias: &Alias) -> Account {
@@ -423,14 +417,14 @@ impl State {
423417
.unwrap_or_default()
424418
}
425419

426-
pub fn get_redelegations_targets_for(&mut self, alias: &Alias) -> HashSet<String> {
420+
pub fn get_redelegations_targets_for(&self, alias: &Alias) -> HashSet<String> {
427421
self.redelegations
428422
.get(alias)
429423
.map(|data| data.keys().cloned().collect::<HashSet<String>>())
430424
.unwrap_or_default()
431425
}
432426

433-
pub fn random_votable_proposal(&mut self, current_epoch: u64) -> u64 {
427+
pub fn random_votable_proposal(&self, current_epoch: u64) -> u64 {
434428
self.proposals
435429
.iter()
436430
.filter_map(|(proposal_id, (start_epoch, end_epoch))| {
@@ -440,7 +434,7 @@ impl State {
440434
None
441435
}
442436
})
443-
.choose(&mut self.rng)
437+
.choose(&mut AntithesisRng)
444438
.unwrap()
445439
}
446440

@@ -626,35 +620,3 @@ impl State {
626620
.insert(latest_proposal_id, (start_epoch, end_epoch));
627621
}
628622
}
629-
630-
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
631-
pub struct AntithesisRng {}
632-
633-
impl RngCore for AntithesisRng {
634-
fn next_u32(&mut self) -> u32 {
635-
(antithesis_sdk::random::get_random() & 0xFFFF_FFFF) as u32
636-
}
637-
638-
fn next_u64(&mut self) -> u64 {
639-
antithesis_sdk::random::get_random()
640-
}
641-
642-
fn fill_bytes(&mut self, dest: &mut [u8]) {
643-
let mut i = 0;
644-
while i + 8 <= dest.len() {
645-
let random = antithesis_sdk::random::get_random();
646-
dest[i..i + 8].copy_from_slice(&random.to_le_bytes());
647-
i += 8;
648-
}
649-
if i < dest.len() {
650-
let random = antithesis_sdk::random::get_random();
651-
let dest_len = dest.len();
652-
dest[i..].copy_from_slice(&random.to_le_bytes()[..dest_len - i]);
653-
}
654-
}
655-
656-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
657-
self.fill_bytes(dest);
658-
Ok(())
659-
}
660-
}

workload/src/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub trait StepContext {
104104
async fn is_valid(&self, sdk: &Sdk, state: &State) -> Result<bool, StepError>;
105105

106106
#[allow(async_fn_in_trait)]
107-
async fn build_task(&self, sdk: &Sdk, state: &mut State) -> Result<Vec<Task>, StepError>;
107+
async fn build_task(&self, sdk: &Sdk, state: &State) -> Result<Vec<Task>, StepError>;
108108

109109
fn assert(&self, code: &Code);
110110
}

workload/src/step/batch.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use antithesis_sdk::random::AntithesisRng;
12
use rand::seq::SliceRandom;
23
use serde_json::json;
34

@@ -23,7 +24,7 @@ impl StepContext for BatchBond {
2324
Ok(state.min_n_account_with_min_balance(3, MIN_TRANSFER_BALANCE))
2425
}
2526

26-
async fn build_task(&self, sdk: &Sdk, state: &mut State) -> Result<Vec<Task>, StepError> {
27+
async fn build_task(&self, sdk: &Sdk, state: &State) -> Result<Vec<Task>, StepError> {
2728
Box::pin(build_batch(
2829
sdk,
2930
vec![StepType::Bond(Default::default())],
@@ -67,7 +68,7 @@ impl StepContext for BatchRandom {
6768
Ok(state.min_n_account_with_min_balance(3, MIN_TRANSFER_BALANCE) && state.min_bonds(3))
6869
}
6970

70-
async fn build_task(&self, sdk: &Sdk, state: &mut State) -> Result<Vec<Task>, StepError> {
71+
async fn build_task(&self, sdk: &Sdk, state: &State) -> Result<Vec<Task>, StepError> {
7172
Box::pin(build_batch(
7273
sdk,
7374
vec![
@@ -111,16 +112,16 @@ async fn build_batch(
111112
sdk: &Sdk,
112113
possibilities: Vec<StepType>,
113114
max_size: u64,
114-
state: &mut State,
115+
state: &State,
115116
) -> Result<Vec<Task>, StepError> {
116117
let mut tmp_state = state.clone();
117118

118119
let mut batch_tasks = vec![];
119120
for _ in 0..max_size {
120121
let step = possibilities
121-
.choose(&mut state.rng)
122+
.choose(&mut AntithesisRng)
122123
.expect("at least one StepType should exist");
123-
let tasks = step.build_task(sdk, &mut tmp_state).await?;
124+
let tasks = step.build_task(sdk, &tmp_state).await?;
124125
for task in &tasks {
125126
task.update_state(&mut tmp_state, false);
126127
}

workload/src/step/become_validator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ impl StepContext for BecomeValidator {
2424
Ok(state.min_n_enstablished_accounts(1))
2525
}
2626

27-
async fn build_task(&self, _sdk: &Sdk, state: &mut State) -> Result<Vec<Task>, StepError> {
28-
let commission_rate = utils::random_between::<u64>(state, 0, 100);
27+
async fn build_task(&self, _sdk: &Sdk, state: &State) -> Result<Vec<Task>, StepError> {
28+
let commission_rate = utils::random_between::<u64>(0, 100);
2929
let commission_rate = Dec::new(commission_rate as i128, 2).unwrap();
3030

31-
let commission_rate_change = utils::random_between::<u64>(state, 0, 100);
31+
let commission_rate_change = utils::random_between::<u64>(0, 100);
3232
let commission_rate_change = Dec::new(commission_rate_change as i128, 2).unwrap();
3333

34-
let random_alias = utils::random_alias(state);
34+
let random_alias = utils::random_alias();
3535
let consensus_key_alias = format!("{}-consensus", random_alias.name);
3636
let eth_cold_key_alias = format!("{}-eth-cold", random_alias.name);
3737
let eth_hot_key_alias = format!("{}-eth-hot", random_alias.name);

0 commit comments

Comments
 (0)