Skip to content

Commit

Permalink
Review changes: simplify randomBytes and bool
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Sep 26, 2024
1 parent 10bf3aa commit e5ece74
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Cheatcode for loadCall {
// If storage slot is untouched and load from a target that copies storage from
// a source address with arbitrary storage, then copy existing arbitrary value.
// If no arbitrary value generated yet, then the random one is saved and set.
let rand_value = ccx.state.test_runner().rng().gen();
let rand_value = ccx.state.rng().gen();
val.data = ccx.state.arbitrary_storage.as_mut().unwrap().copy(
ccx.ecx,
target,
Expand Down
3 changes: 2 additions & 1 deletion crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ pub struct Cheatcodes {
/// `char -> (address, pc)`
pub breakpoints: Breakpoints,

/// Optional cheatcodes `TestRunner`.
/// Optional cheatcodes `TestRunner`. Used for generating random values from uint and int
/// strategies for.
test_runner: Option<TestRunner>,

/// Ignored traces.
Expand Down
31 changes: 7 additions & 24 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_sol_types::SolValue;
use foundry_common::ens::namehash;
use foundry_evm_core::{backend::DatabaseExt, constants::DEFAULT_CREATE2_DEPLOYER};
use proptest::strategy::{Strategy, ValueTree};
use rand::Rng;
use rand::{Rng, RngCore};
use std::collections::HashMap;

/// Contains locations of traces ignored via cheatcodes.
Expand Down Expand Up @@ -116,11 +116,8 @@ impl Cheatcode for randomInt_1Call {

impl Cheatcode for randomBoolCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
Ok(DynSolValue::type_strategy(&DynSolType::Bool)
.new_tree(state.test_runner())
.unwrap()
.current()
.abi_encode())
let rand_bool: bool = state.rng().gen();
Ok(rand_bool.abi_encode())
}
}

Expand All @@ -131,23 +128,9 @@ impl Cheatcode for randomBytesCall {
len <= U256::from(usize::MAX),
format!("bytes length cannot exceed {}", usize::MAX)
);
let mut val = DynSolValue::type_strategy(&DynSolType::Bytes)
.new_tree(state.test_runner())
.unwrap()
.current()
.as_bytes()
.unwrap_or_default()
.to_vec();
let required_len = len.to::<usize>();
let cur_len = val.len();
if cur_len > required_len {
// Slice to required length if random bytes length is lower than required length.
Ok(val[..required_len].abi_encode())
} else {
// Fill with zeroes if random bytes length is lower than required length.
val.extend(vec![0; required_len - cur_len]);
Ok(val.abi_encode())
}
let mut bytes = vec![0u8; len.to::<usize>()];
state.rng().fill_bytes(&mut bytes);
Ok(bytes.abi_encode())
}
}

Expand Down Expand Up @@ -246,7 +229,7 @@ fn random_uint(state: &mut Cheatcodes, bits: Option<U256>, bounds: Option<(U256,
ensure!(min <= max, "min must be less than or equal to max");
// Generate random between range min..=max
let exclusive_modulo = max - min;
let mut random_number: U256 = state.test_runner().rng().gen();
let mut random_number: U256 = state.rng().gen();
if exclusive_modulo != U256::MAX {
let inclusive_modulo = exclusive_modulo + U256::from(1);
random_number %= inclusive_modulo;
Expand Down

0 comments on commit e5ece74

Please sign in to comment.