foundry_config/
invariant.rs

1//! Configuration for invariant testing
2
3use crate::fuzz::FuzzDictionaryConfig;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7/// Contains for invariant testing
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
9pub struct InvariantConfig {
10    /// The number of runs that must execute for each invariant test group.
11    pub runs: u32,
12    /// The number of calls executed to attempt to break invariants in one run.
13    pub depth: u32,
14    /// Fails the invariant fuzzing if a revert occurs
15    pub fail_on_revert: bool,
16    /// Allows overriding an unsafe external call when running invariant tests. eg. reentrancy
17    /// checks
18    pub call_override: bool,
19    /// The fuzz dictionary configuration
20    #[serde(flatten)]
21    pub dictionary: FuzzDictionaryConfig,
22    /// The maximum number of attempts to shrink the sequence
23    pub shrink_run_limit: u32,
24    /// The maximum number of rejects via `vm.assume` which can be encountered during a single
25    /// invariant run.
26    pub max_assume_rejects: u32,
27    /// Number of runs to execute and include in the gas report.
28    pub gas_report_samples: u32,
29    /// Path where invariant failures are recorded and replayed.
30    pub failure_persist_dir: Option<PathBuf>,
31    /// Whether to collect and display fuzzed selectors metrics.
32    pub show_metrics: bool,
33    /// Optional timeout (in seconds) for each invariant test.
34    pub timeout: Option<u32>,
35    /// Display counterexample as solidity calls.
36    pub show_solidity: bool,
37}
38
39impl Default for InvariantConfig {
40    fn default() -> Self {
41        Self {
42            runs: 256,
43            depth: 500,
44            fail_on_revert: false,
45            call_override: false,
46            dictionary: FuzzDictionaryConfig { dictionary_weight: 80, ..Default::default() },
47            shrink_run_limit: 5000,
48            max_assume_rejects: 65536,
49            gas_report_samples: 256,
50            failure_persist_dir: None,
51            show_metrics: false,
52            timeout: None,
53            show_solidity: false,
54        }
55    }
56}
57
58impl InvariantConfig {
59    /// Creates invariant configuration to write failures in `{PROJECT_ROOT}/cache/fuzz` dir.
60    pub fn new(cache_dir: PathBuf) -> Self {
61        Self {
62            runs: 256,
63            depth: 500,
64            fail_on_revert: false,
65            call_override: false,
66            dictionary: FuzzDictionaryConfig { dictionary_weight: 80, ..Default::default() },
67            shrink_run_limit: 5000,
68            max_assume_rejects: 65536,
69            gas_report_samples: 256,
70            failure_persist_dir: Some(cache_dir),
71            show_metrics: false,
72            timeout: None,
73            show_solidity: false,
74        }
75    }
76}