From 5b92ee160f909432761f204683a5e7fef1f816f5 Mon Sep 17 00:00:00 2001 From: grandizzy Date: Tue, 19 Mar 2024 13:14:07 +0200 Subject: [PATCH] Cleanup --- crates/common/src/traits.rs | 2 +- crates/evm/evm/src/executors/fuzz/mod.rs | 4 ++-- crates/evm/evm/src/executors/invariant/mod.rs | 6 ++--- crates/evm/fuzz/src/strategies/address.rs | 2 +- crates/forge/src/runner.rs | 24 +++++++++---------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/common/src/traits.rs b/crates/common/src/traits.rs index 0e7cf0dece313..d43b860f43b93 100644 --- a/crates/common/src/traits.rs +++ b/crates/common/src/traits.rs @@ -113,7 +113,7 @@ impl TestFunctionExt for str { } fn is_fixtures(&self) -> bool { - self.starts_with("fixtures") + self.starts_with("fixtures_") } } diff --git a/crates/evm/evm/src/executors/fuzz/mod.rs b/crates/evm/evm/src/executors/fuzz/mod.rs index 5aaa45c144b1d..5ac4e6f6e932c 100644 --- a/crates/evm/evm/src/executors/fuzz/mod.rs +++ b/crates/evm/evm/src/executors/fuzz/mod.rs @@ -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, @@ -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), ]; diff --git a/crates/evm/evm/src/executors/invariant/mod.rs b/crates/evm/evm/src/executors/invariant/mod.rs index cca0d94f0f231..7db9dd268f1ff 100644 --- a/crates/evm/evm/src/executors/invariant/mod.rs +++ b/crates/evm/evm/src/executors/invariant/mod.rs @@ -148,7 +148,7 @@ impl<'a> InvariantExecutor<'a> { pub fn invariant_fuzz( &mut self, invariant_contract: InvariantContract<'_>, - fuzz_fixtures: FuzzFixtures, + fuzz_fixtures: &FuzzFixtures, ) -> Result { // Throw an error to abort test run if the invariant function accepts input params if !invariant_contract.invariant_function.inputs.is_empty() { @@ -156,7 +156,7 @@ impl<'a> InvariantExecutor<'a> { } 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> = RefCell::new(Default::default()); @@ -346,7 +346,7 @@ impl<'a> InvariantExecutor<'a> { fn prepare_fuzzing( &mut self, invariant_contract: &InvariantContract<'_>, - fuzz_fixtures: FuzzFixtures, + fuzz_fixtures: &FuzzFixtures, ) -> eyre::Result { // Finds out the chosen deployed contracts and/or senders. self.select_contract_artifacts(invariant_contract.address)?; diff --git a/crates/evm/fuzz/src/strategies/address.rs b/crates/evm/fuzz/src/strategies/address.rs index c3ce0f5f205bb..1bebf7c74d4b5 100644 --- a/crates/evm/fuzz/src/strategies/address.rs +++ b/crates/evm/fuzz/src/strategies/address.rs @@ -29,10 +29,10 @@ impl AddressStrategy { fixtures.iter().enumerate().map(|(_, value)| value.to_owned()).collect(); let address_fixtures_len = address_fixtures.len(); any::() - .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); diff --git a/crates/forge/src/runner.rs b/crates/forge/src/runner.rs index cde44236c976c..adc019181fd95 100644 --- a/crates/forge/src/runner.rs +++ b/crates/forge/src/runner.rs @@ -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) @@ -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 { @@ -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();