Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Mar 19, 2024
1 parent e1f8a95 commit 254017c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/common/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl TestFunctionExt for str {
}

fn is_fixtures(&self) -> bool {
self.starts_with("fixtures")
self.starts_with("fixtures_")
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/evm/evm/src/executors/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl FuzzedExecutor {
pub fn fuzz(
&self,
func: &Function,
fuzz_fixtures: FuzzFixtures,
fuzz_fixtures: &FuzzFixtures,
address: Address,
should_fail: bool,
rd: &RevertDecoder,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl FuzzedExecutor {
let dictionary_weight = self.config.dictionary.dictionary_weight.min(100);

let strat = proptest::prop_oneof![
100 - dictionary_weight => fuzz_calldata(func.clone(), &fuzz_fixtures),
100 - dictionary_weight => fuzz_calldata(func.clone(), fuzz_fixtures),
dictionary_weight => fuzz_calldata_from_state(func.clone(), &state),
];

Expand Down
6 changes: 3 additions & 3 deletions crates/evm/evm/src/executors/invariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ impl<'a> InvariantExecutor<'a> {
pub fn invariant_fuzz(
&mut self,
invariant_contract: InvariantContract<'_>,
fuzz_fixtures: FuzzFixtures,
fuzz_fixtures: &FuzzFixtures,
) -> Result<InvariantFuzzTestResult> {
// Throw an error to abort test run if the invariant function accepts input params
if !invariant_contract.invariant_function.inputs.is_empty() {
return Err(eyre!("Invariant test function should have no inputs"))
}

let (fuzz_state, targeted_contracts, strat) =
self.prepare_fuzzing(&invariant_contract, fuzz_fixtures.clone())?;
self.prepare_fuzzing(&invariant_contract, fuzz_fixtures)?;

// Stores the consumed gas and calldata of every successful fuzz call.
let fuzz_cases: RefCell<Vec<FuzzedCases>> = RefCell::new(Default::default());
Expand Down Expand Up @@ -346,7 +346,7 @@ impl<'a> InvariantExecutor<'a> {
fn prepare_fuzzing(
&mut self,
invariant_contract: &InvariantContract<'_>,
fuzz_fixtures: FuzzFixtures,
fuzz_fixtures: &FuzzFixtures,
) -> eyre::Result<InvariantPreparation> {
// Finds out the chosen deployed contracts and/or senders.
self.select_contract_artifacts(invariant_contract.address)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/fuzz/src/strategies/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl AddressStrategy {
fixtures.iter().enumerate().map(|(_, value)| value.to_owned()).collect();
let address_fixtures_len = address_fixtures.len();
any::<prop::sample::Index>()
.prop_map(move |index| index.index(address_fixtures_len))
.prop_map(move |index| {
// Generate value tree from fixture.
// If fixture is not a valid address, raise error and generate random value.
let index = index.index(address_fixtures_len);
if let Some(addr_fixture) = address_fixtures.get(index) {
if let Some(addr_fixture) = addr_fixture.as_address() {
return DynSolValue::Address(addr_fixture);
Expand Down
6 changes: 1 addition & 5 deletions crates/evm/fuzz/src/strategies/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ impl IntStrategy {
/// * `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]>) -> Self {
let int_fixtures = match fixtures {
Some(values) => Vec::from(values),
None => vec![],
};
Self {
bits,
fixtures: int_fixtures,
fixtures: Vec::from(fixtures.unwrap_or_default()),
edge_weight: 10usize,
fixtures_weight: 40usize,
random_weight: 50usize,
Expand Down
6 changes: 1 addition & 5 deletions crates/evm/fuzz/src/strategies/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,9 @@ impl UintStrategy {
/// * `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]>) -> Self {
let uint_fixtures = match fixtures {
Some(values) => Vec::from(values),
None => vec![],
};
Self {
bits,
fixtures: uint_fixtures,
fixtures: Vec::from(fixtures.unwrap_or_default()),
edge_weight: 10usize,
fixtures_weight: 40usize,
random_weight: 50usize,
Expand Down
24 changes: 12 additions & 12 deletions crates/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,17 @@ impl<'a> ContractRunner<'a> {
/// current test.
fn fuzz_fixtures(&mut self, address: Address) -> FuzzFixtures {
// collect test fixtures param:array of values
let fixtures_fns: Vec<_> =
self.contract.functions().filter(|func| func.name.is_fixtures()).collect();
let mut fixtures = HashMap::new();
fixtures_fns.iter().for_each(|func| {
if let Ok(CallResult { raw: _, decoded_result }) =
self.executor.call(CALLER, address, func, &[], U256::ZERO, None)
{
fixtures.insert(
func.name.strip_prefix("fixtures_").unwrap().to_string(),
decoded_result,
);
self.contract.functions().for_each(|func| {
if func.name.is_fixtures() {
if let Ok(CallResult { raw: _, decoded_result }) =
self.executor.call(CALLER, address, func, &[], U256::ZERO, None)
{
fixtures.insert(
func.name.strip_prefix("fixtures_").unwrap().to_string(),
decoded_result,
);
}
}
});
FuzzFixtures::new(fixtures)
Expand Down Expand Up @@ -538,7 +538,7 @@ impl<'a> ContractRunner<'a> {
InvariantContract { address, invariant_function: func, abi: self.contract };

let InvariantFuzzTestResult { error, cases, reverts, last_run_inputs, gas_report_traces } =
match evm.invariant_fuzz(invariant_contract.clone(), fuzz_fixtures) {
match evm.invariant_fuzz(invariant_contract.clone(), &fuzz_fixtures) {
Ok(x) => x,
Err(e) => {
return TestResult {
Expand Down Expand Up @@ -665,7 +665,7 @@ impl<'a> ContractRunner<'a> {
);
let state = fuzzed_executor.build_fuzz_state();
let result =
fuzzed_executor.fuzz(func, fuzz_fixtures, address, should_fail, self.revert_decoder);
fuzzed_executor.fuzz(func, &fuzz_fixtures, address, should_fail, self.revert_decoder);

let mut debug = Default::default();
let mut breakpoints = Default::default();
Expand Down

0 comments on commit 254017c

Please sign in to comment.