Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
malik672 committed Oct 23, 2024
1 parent 34da162 commit 5edae5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
3 changes: 1 addition & 2 deletions crates/evm/fuzz/src/strategies/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ fn fuzz_param_inner(
(None, None)
};

let strategy = super::UintStrategy::new(n, fuzz_fixtures, None);
strategy.with_bounds(Some(min.unwrap()), Some(max.unwrap()))
super::UintStrategy::new(n, fuzz_fixtures, min, max)
.prop_map(move |x| DynSolValue::Uint(x, n))
.boxed()
}
Expand Down
71 changes: 30 additions & 41 deletions crates/evm/fuzz/src/strategies/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,44 @@ pub struct UintStrategy {
random_weight: usize,
/// Optional bounds for generated values
bounds: Option<(U256, U256)>,


}

impl UintStrategy {
/// Create a new strategy without bounds.
/// Create a new strategy.
/// #Arguments
/// * `bits` - Size of uint in bits
/// * `fixtures` - A set of fixed values to be generated (according to fixtures weight)
pub fn new(
bits: usize,
fixtures: Option<&[DynSolValue]>,
bounds: Option<(U256, U256)>,
min_bound: Option<U256>,
max_bound: Option<U256>,
) -> Self {
let bounds = match (min_bound, max_bound) {
(Some(min), Some(max)) => Some((min, max)),
_ => None,
};

Self {
bits,
fixtures: Vec::from(fixtures.unwrap_or_default()),
edge_weight: 10usize,
fixtures_weight: 40usize,
random_weight: 50usize,
bounds: None
bounds,
}
}


/// Add bounds to an existing strategy
pub fn with_bounds(self, min_bound: Option<U256>, max_bound: Option<U256>) -> Self {
let bounds = match (min_bound, max_bound) {
(Some(min), Some(max)) => Some((min, max)),
_ => None
};
Self { bounds, ..self }
}

pub fn use_log_sampling(&self) -> bool {
self.bits > 8
}

fn generate_edge_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let rng = runner.rng();
// Choose if we want values around 0 or max
let is_min = rng.gen_bool(0.5);
let offset = U256::from(rng.gen_range(0..4));

let start = if let Some((min, max)) = self.bounds {
// If bounds are set, use them
if is_min {
Expand All @@ -161,7 +155,7 @@ impl UintStrategy {
type_max.saturating_sub(offset)
}
};

let (min, max) = self.bounds.unwrap_or((U256::ZERO, self.type_max()));
Ok(UintValueTree::new(start, false, min, max))
}
Expand Down Expand Up @@ -189,11 +183,11 @@ impl UintStrategy {

fn generate_random_values_uniformly(&self, runner: &mut TestRunner) -> U256 {
let rng = runner.rng();

//Generate the bits to use
let bits = self.bits;
// Generate lower and higher parts
let bits = self.bits;

// Generate lower and higher parts
let lower: u128 = rng.gen();
let higher: u128 = rng.gen();

Expand Down Expand Up @@ -232,12 +226,7 @@ impl UintStrategy {
}
};

Ok(UintValueTree::new(
start.clamp(min, max),
false,
min,
max
))
Ok(UintValueTree::new(start.clamp(min, max), false, min, max))
}

fn generate_log_uniform(&self, runner: &mut TestRunner) -> U256 {
Expand All @@ -248,15 +237,15 @@ impl UintStrategy {
}

let rng = runner.rng();

let exp = rng.gen::<u32>() % 256;
let mantissa = rng.gen::<u64>();

let base = U256::from(1) << exp;
let mut value = base | (U256::from(mantissa) & (base - U256::from(1)));

value = value.clamp(min, max);

if value == min && max > min {
let range = max - min;
let offset = U256::from(rng.gen::<u64>()) % range;
Expand All @@ -267,11 +256,11 @@ impl UintStrategy {
}

pub fn type_max(&self) -> U256 {
if self.bits < 256 {
(U256::from(1) << self.bits) - U256::from(1)
} else {
U256::MAX
}
if self.bits < 256 {
(U256::from(1) << self.bits) - U256::from(1)
} else {
U256::MAX
}
}
}

Expand All @@ -281,7 +270,7 @@ impl Strategy for UintStrategy {
fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let total_weight = self.random_weight + self.fixtures_weight + self.edge_weight;
let bias = runner.rng().gen_range(0..total_weight);
// randomly select one of 3 strategies
// randomly select one of 3 strategies
match bias {
x if x < self.edge_weight => self.generate_edge_tree(runner),
x if x < self.edge_weight + self.fixtures_weight => self.generate_fixtures_tree(runner),
Expand Down Expand Up @@ -312,7 +301,7 @@ mod tests {
fn test_uint_strategy_respects_bounds() {
let min = U256::from(1000u64);
let max = U256::from(2000u64);
let strategy = UintStrategy::new(16, None, Some((min, max)));
let strategy = UintStrategy::new(16, None, Some(min), Some(max));
let mut runner = TestRunner::default();

for _ in 0..1000 {
Expand Down Expand Up @@ -348,7 +337,7 @@ mod tests {
fn test_edge_case_generation() {
let min = U256::from(100u64);
let max = U256::from(1000u64);
let strategy = UintStrategy::new(64, None, Some((min, max)));
let strategy = UintStrategy::new(64, None, Some(min), Some(max));
let mut runner = TestRunner::default();

let mut found_min_area = false;
Expand Down Expand Up @@ -382,7 +371,7 @@ mod tests {
let valid_fixture = U256::from(500u64);
let fixtures = vec![DynSolValue::Uint(valid_fixture, 64)];

let strategy = UintStrategy::new(64, Some(&fixtures), Some((min, max)));
let strategy = UintStrategy::new(64, Some(&fixtures), Some(min), Some(max));
let mut runner = TestRunner::default();

for _ in 0..100 {
Expand All @@ -397,7 +386,7 @@ mod tests {

#[test]
fn test_log_uniform_sampling() {
let strategy = UintStrategy::new(256, None, None);
let strategy = UintStrategy::new(256, None, None, None);
let mut runner = TestRunner::default();
let mut log2_buckets = vec![0; 256];
let iterations = 100000;
Expand Down

0 comments on commit 5edae5c

Please sign in to comment.