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

Invariants feat: add config option to turn off shrinking #4868

Merged
merged 6 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ call_override = false
dictionary_weight = 80
include_storage = true
include_push_bytes = true
shrink_sequence = true

[fmt]
line_length = 100
Expand Down
3 changes: 3 additions & 0 deletions config/src/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct InvariantConfig {
/// The fuzz dictionary configuration
#[serde(flatten)]
pub dictionary: FuzzDictionaryConfig,
/// Attempt to shrink the failure case to its smallest sequence of calls
pub shrink_sequence: bool,
}

impl Default for InvariantConfig {
Expand All @@ -28,6 +30,7 @@ impl Default for InvariantConfig {
fail_on_revert: false,
call_override: false,
dictionary: FuzzDictionaryConfig { dictionary_weight: 80, ..Default::default() },
shrink_sequence: true,
}
}
}
1 change: 1 addition & 0 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3374,6 +3374,7 @@ mod tests {
depth = 15
fail_on_revert = false
call_override = false
shrink_sequence = true
"#,
)?;

Expand Down
10 changes: 9 additions & 1 deletion evm/src/fuzz/invariant/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct InvariantFuzzError {
pub func: Option<ethers::prelude::Bytes>,
/// Inner fuzzing Sequence coming from overriding calls.
pub inner_sequence: Vec<Option<BasicTxDetails>>,
/// Shrink the failed test case to the smallest sequence.
pub shrink: bool,
}

impl InvariantFuzzError {
Expand All @@ -37,6 +39,7 @@ impl InvariantFuzzError {
calldata: &[BasicTxDetails],
call_result: RawCallResult,
inner_sequence: &[Option<BasicTxDetails>],
shrink_sequence: bool,
) -> Self {
let mut func = None;
let origin: String;
Expand Down Expand Up @@ -77,6 +80,7 @@ impl InvariantFuzzError {
addr: invariant_contract.address,
func,
inner_sequence: inner_sequence.to_vec(),
shrink: shrink_sequence,
}
}

Expand All @@ -96,7 +100,11 @@ impl InvariantFuzzError {
TestError::Fail(_, ref calls) => calls,
};

let calls = self.try_shrinking(calls, &executor);
if self.shrink {
let _ = self.try_shrinking(calls, &executor);
} else {
trace!(target: "forge::test", "Shrinking disabled.");
}

// We want traces for a failed case.
executor.set_tracing(true);
Expand Down
12 changes: 10 additions & 2 deletions evm/src/fuzz/invariant/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl<'a> InvariantExecutor<'a> {
&inputs,
&mut failures.borrow_mut(),
self.config.fail_on_revert,
self.config.shrink_sequence,
);

if !can_continue {
Expand Down Expand Up @@ -558,6 +559,7 @@ fn can_continue(
calldata: &[BasicTxDetails],
failures: &mut InvariantFailures,
fail_on_revert: bool,
shrink_sequence: bool,
) -> (bool, Option<BTreeMap<String, RawCallResult>>) {
let mut call_results = None;
if !call_result.reverted {
Expand All @@ -571,8 +573,14 @@ fn can_continue(
// The user might want to stop all execution if a revert happens to
// better bound their testing space.
if fail_on_revert {
let error =
InvariantFuzzError::new(invariant_contract, None, calldata, call_result, &[]);
let error = InvariantFuzzError::new(
invariant_contract,
None,
calldata,
call_result,
&[],
shrink_sequence,
);

failures.revert_reason = Some(error.revert_reason.clone());

Expand Down
1 change: 1 addition & 0 deletions evm/src/fuzz/invariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub fn assert_invariants(
calldata,
call_result,
&inner_sequence,
true,
)),
);
found_case = true;
Expand Down
1 change: 1 addition & 0 deletions forge/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub static TEST_OPTS: TestOptions = TestOptions {
max_fuzz_dictionary_addresses: 10_000,
max_fuzz_dictionary_values: 10_000,
},
shrink_sequence: true,
},
};

Expand Down
1 change: 1 addition & 0 deletions testdata/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ffi = false
force = false
invariant_fail_on_revert = false
invariant_call_override = false
invariant_shrink_sequence = true
gas_limit = 9223372036854775807
gas_price = 0
gas_reports = ['*']
Expand Down