Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: restore whitespace in fuzz files #6788

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/evm/fuzz/src/strategies/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct IntValueTree {
/// If true cannot be simplified or complexified
fixed: bool,
}

impl IntValueTree {
/// Create a new tree
/// # Arguments
Expand All @@ -37,25 +38,30 @@ impl IntValueTree {
true
}
}

fn magnitude_greater(lhs: I256, rhs: I256) -> bool {
if lhs.is_zero() {
return false
}
(lhs > rhs) ^ (lhs.is_negative())
}
}

impl ValueTree for IntValueTree {
type Value = I256;

fn current(&self) -> Self::Value {
self.curr
}

fn simplify(&mut self) -> bool {
if self.fixed || !IntValueTree::magnitude_greater(self.hi, self.lo) {
return false
}
self.hi = self.curr;
self.reposition()
}

fn complicate(&mut self) -> bool {
if self.fixed || !IntValueTree::magnitude_greater(self.hi, self.lo) {
return false
Expand All @@ -66,6 +72,7 @@ impl ValueTree for IntValueTree {
self.reposition()
}
}

/// Value tree for signed ints (up to int256).
/// The strategy combines 3 different strategies, each assigned a specific weight:
/// 1. Generate purely random value in a range. This will first choose bit size uniformly (up `bits`
Expand All @@ -85,6 +92,7 @@ pub struct IntStrategy {
/// The weight for purely random values
random_weight: usize,
}

impl IntStrategy {
/// Create a new strategy.
/// #Arguments
Expand All @@ -99,6 +107,7 @@ impl IntStrategy {
random_weight: 50usize,
}
}

fn generate_edge_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let rng = runner.rng();

Expand All @@ -117,6 +126,7 @@ impl IntStrategy {
};
Ok(IntValueTree::new(start, false))
}

fn generate_fixtures_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
// generate edge cases if there's no fixtures
if self.fixtures.is_empty() {
Expand All @@ -125,8 +135,10 @@ impl IntStrategy {
let idx = runner.rng().gen_range(0..self.fixtures.len());
Ok(IntValueTree::new(self.fixtures[idx], false))
}

fn generate_random_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let rng = runner.rng();

// generate random number of bits uniformly
let bits = rng.gen_range(0..=self.bits);

Expand All @@ -137,6 +149,7 @@ impl IntStrategy {
// init 2 128-bit randoms
let mut higher: u128 = rng.gen_range(0..=u128::MAX);
let mut lower: u128 = rng.gen_range(0..=u128::MAX);

// cut 2 randoms according to bits size
match bits - 1 {
x if x < 128 => {
Expand All @@ -154,17 +167,20 @@ impl IntStrategy {
inner[1] = (lower >> 64) as u64;
inner[2] = (higher & mask64) as u64;
inner[3] = (higher >> 64) as u64;
let sign = if rng.gen_bool(0.5) { Sign::Positive } else { Sign::Negative };

// we have a small bias here, i.e. intN::min will never be generated
// but it's ok since it's generated in `fn generate_edge_tree(...)`
let sign = if rng.gen_bool(0.5) { Sign::Positive } else { Sign::Negative };
let (start, _) = I256::overflowing_from_sign_and_abs(sign, U256::from_limbs(inner));

Ok(IntValueTree::new(start, false))
}
}

impl Strategy for IntStrategy {
type Tree = IntValueTree;
type Value = I256;

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);
Expand Down
15 changes: 15 additions & 0 deletions crates/evm/fuzz/src/strategies/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct UintValueTree {
/// If true cannot be simplified or complexified
fixed: bool,
}

impl UintValueTree {
/// Create a new tree
/// # Arguments
Expand All @@ -38,18 +39,22 @@ impl UintValueTree {
}
}
}

impl ValueTree for UintValueTree {
type Value = U256;

fn current(&self) -> Self::Value {
self.curr
}

fn simplify(&mut self) -> bool {
if self.fixed || (self.hi <= self.lo) {
return false
}
self.hi = self.curr;
self.reposition()
}

fn complicate(&mut self) -> bool {
if self.fixed || (self.hi <= self.lo) {
return false
Expand All @@ -59,6 +64,7 @@ impl ValueTree for UintValueTree {
self.reposition()
}
}

/// Value tree for unsigned ints (up to uint256).
/// The strategy combines 3 different strategies, each assigned a specific weight:
/// 1. Generate purely random value in a range. This will first choose bit size uniformly (up `bits`
Expand All @@ -78,6 +84,7 @@ pub struct UintStrategy {
/// The weight for purely random values
random_weight: usize,
}

impl UintStrategy {
/// Create a new strategy.
/// #Arguments
Expand All @@ -92,6 +99,7 @@ impl UintStrategy {
random_weight: 50usize,
}
}

fn generate_edge_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let rng = runner.rng();
// Choose if we want values around 0 or max
Expand All @@ -102,6 +110,7 @@ impl UintStrategy {
let start = if is_min { offset } else { max.saturating_sub(offset) };
Ok(UintValueTree::new(start, false))
}

fn generate_fixtures_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
// generate edge cases if there's no fixtures
if self.fixtures.is_empty() {
Expand All @@ -110,13 +119,17 @@ impl UintStrategy {
let idx = runner.rng().gen_range(0..self.fixtures.len());
Ok(UintValueTree::new(self.fixtures[idx], false))
}

fn generate_random_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
let rng = runner.rng();

// generate random number of bits uniformly
let bits = rng.gen_range(0..=self.bits);

// init 2 128-bit randoms
let mut higher: u128 = rng.gen_range(0..=u128::MAX);
let mut lower: u128 = rng.gen_range(0..=u128::MAX);

// cut 2 randoms according to bits size
match bits {
x if x < 128 => {
Expand All @@ -126,6 +139,7 @@ impl UintStrategy {
x if (128..256).contains(&x) => higher &= (1u128 << (x - 128)) - 1,
_ => {}
};

// init U256 from 2 randoms
let mut inner: [u64; 4] = [0; 4];
let mask64 = (1 << 65) - 1;
Expand All @@ -138,6 +152,7 @@ impl UintStrategy {
Ok(UintValueTree::new(start, false))
}
}

impl Strategy for UintStrategy {
type Tree = UintValueTree;
type Value = U256;
Expand Down