Skip to content

Commit

Permalink
Put metrics behind show-metrics invariant config
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Oct 22, 2024
1 parent 4dc2aeb commit 3edde16
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/config/src/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct InvariantConfig {
pub gas_report_samples: u32,
/// Path where invariant failures are recorded and replayed.
pub failure_persist_dir: Option<PathBuf>,
/// Whether to collect and display fuzzed selectors metrics.
pub show_metrics: bool,
}

impl Default for InvariantConfig {
Expand All @@ -48,6 +50,7 @@ impl Default for InvariantConfig {
max_assume_rejects: 65536,
gas_report_samples: 256,
failure_persist_dir: None,
show_metrics: false,
}
}
}
Expand All @@ -65,6 +68,7 @@ impl InvariantConfig {
max_assume_rejects: 65536,
gas_report_samples: 256,
failure_persist_dir: Some(cache_dir),
show_metrics: false,
}
}

Expand Down Expand Up @@ -103,6 +107,7 @@ impl InlineConfigParser for InvariantConfig {
conf_clone.failure_persist_dir = Some(PathBuf::from(value))
}
"shrink-run-limit" => conf_clone.shrink_run_limit = parse_config_u32(key, value)?,
"show-metrics" => conf_clone.show_metrics = parse_config_bool(key, value)?,
_ => Err(InlineConfigParserError::InvalidConfigProperty(key.to_string()))?,
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/evm/evm/src/executors/invariant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ impl<'a> InvariantExecutor<'a> {
})?;

let discarded = call_result.result.as_ref() == MAGIC_ASSUME;
invariant_test.record_metrics(tx, call_result.reverted, discarded);
if self.config.show_metrics {
invariant_test.record_metrics(tx, call_result.reverted, discarded);
}

// Collect coverage from last fuzzed call.
invariant_test.merge_coverage(call_result.coverage.clone());
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ impl TestArgs {
for (name, result) in tests {
if !silent {
shell::println(result.short_result(name))?;

// Display invariant metrics if invariant kind.
if let TestKind::Invariant { runs: _, calls: _, reverts: _, metrics } =
&result.kind
{
Expand Down
2 changes: 2 additions & 0 deletions crates/forge/tests/it/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,11 @@ contract CounterTest is Test {
}
/// forge-config: default.invariant.runs = 10
/// forge-config: default.invariant.show-metrics = true
function invariant_counter() public {}
/// forge-config: default.invariant.runs = 10
/// forge-config: default.invariant.show-metrics = true
function invariant_counter2() public {}
}
Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl ForgeTestProfile {
max_assume_rejects: 65536,
gas_report_samples: 256,
failure_persist_dir: Some(tempfile::tempdir().unwrap().into_path()),
show_metrics: false,
})
.build(output, Path::new(self.project().root()))
.expect("Config loaded")
Expand Down

0 comments on commit 3edde16

Please sign in to comment.