Skip to content

Commit

Permalink
fix(forge): exclude artifacts without mutable functions on invariant …
Browse files Browse the repository at this point in the history
…testing (foundry-rs#2811)

* exclude artifacts without mutable functions

* fmt
  • Loading branch information
joshieDo authored Aug 17, 2022
1 parent 4862e99 commit fd5a4b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
20 changes: 20 additions & 0 deletions evm/src/fuzz/invariant/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ impl<'a> InvariantExecutor<'a> {
/// format). They will be used to filter contracts after the `setUp`, and more importantly,
/// during the runs.
///
/// Also excludes any contract without any mutable functions.
///
/// Priority:
///
/// targetArtifactSelectors > excludeArtifacts > targetArtifacts
Expand Down Expand Up @@ -315,6 +317,24 @@ impl<'a> InvariantExecutor<'a> {
}
}

// Exclude any artifact without mutable functions.
for (artifact, (abi, _)) in self.project_contracts.iter() {
if abi
.functions()
.filter(|func| {
!matches!(
func.state_mutability,
ethers::abi::StateMutability::Pure | ethers::abi::StateMutability::View
)
})
.count() ==
0 &&
!self.artifact_filters.excluded.contains(&artifact.identifier())
{
self.artifact_filters.excluded.push(artifact.identifier());
}
}

// Insert `targetArtifacts` into the executor `targeted_abi`, if they have not been seen
// before.
for contract in selected_abi {
Expand Down
5 changes: 1 addition & 4 deletions evm/src/fuzz/strategies/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ pub fn fuzz_param_from_state(param: &ParamType, arc_state: EvmFuzzState) -> Boxe

#[cfg(test)]
mod tests {
use crate::{
fuzz::strategies::{build_initial_state, fuzz_calldata, fuzz_calldata_from_state},
CALLER,
};
use crate::fuzz::strategies::{build_initial_state, fuzz_calldata, fuzz_calldata_from_state};
use ethers::abi::HumanReadableParser;
use revm::db::{CacheDB, EmptyDB};

Expand Down
7 changes: 7 additions & 0 deletions testdata/fuzz/invariant/targetAbi/ExcludeArtifacts.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ pragma solidity >=0.8.0;

import "ds-test/test.sol";

// Will get automatically excluded. Otherwise it would throw error.
contract NoMutFunctions {
function no_change() public pure {
}
}

contract Excluded {
bool public world = true;

Expand All @@ -25,6 +31,7 @@ contract ExcludeArtifacts is DSTest {
function setUp() public {
excluded = new Excluded();
new Hello();
new NoMutFunctions();
}

function excludeArtifacts() public returns (string[] memory) {
Expand Down

0 comments on commit fd5a4b6

Please sign in to comment.