foundry_evm/executors/invariant/
error.rsuse super::{BasicTxDetails, InvariantContract};
use crate::executors::RawCallResult;
use alloy_primitives::{Address, Bytes};
use foundry_config::InvariantConfig;
use foundry_evm_core::decode::RevertDecoder;
use foundry_evm_fuzz::{invariant::FuzzRunIdentifiedContracts, Reason};
use proptest::test_runner::TestError;
#[derive(Clone, Default)]
pub struct InvariantFailures {
pub reverts: usize,
pub revert_reason: Option<String>,
pub error: Option<InvariantFuzzError>,
}
impl InvariantFailures {
pub fn new() -> Self {
Self::default()
}
pub fn into_inner(self) -> (usize, Option<InvariantFuzzError>) {
(self.reverts, self.error)
}
}
#[derive(Clone, Debug)]
pub enum InvariantFuzzError {
Revert(FailedInvariantCaseData),
BrokenInvariant(FailedInvariantCaseData),
MaxAssumeRejects(u32),
}
impl InvariantFuzzError {
pub fn revert_reason(&self) -> Option<String> {
match self {
Self::BrokenInvariant(case_data) | Self::Revert(case_data) => {
(!case_data.revert_reason.is_empty()).then(|| case_data.revert_reason.clone())
}
Self::MaxAssumeRejects(allowed) => {
Some(format!("`vm.assume` rejected too many inputs ({allowed} allowed)"))
}
}
}
}
#[derive(Clone, Debug)]
pub struct FailedInvariantCaseData {
pub test_error: TestError<Vec<BasicTxDetails>>,
pub return_reason: Reason,
pub revert_reason: String,
pub addr: Address,
pub calldata: Bytes,
pub inner_sequence: Vec<Option<BasicTxDetails>>,
pub shrink_run_limit: u32,
pub fail_on_revert: bool,
}
impl FailedInvariantCaseData {
pub fn new(
invariant_contract: &InvariantContract<'_>,
invariant_config: &InvariantConfig,
targeted_contracts: &FuzzRunIdentifiedContracts,
calldata: &[BasicTxDetails],
call_result: RawCallResult,
inner_sequence: &[Option<BasicTxDetails>],
) -> Self {
let revert_reason = RevertDecoder::new()
.with_abis(targeted_contracts.targets.lock().iter().map(|(_, c)| &c.abi))
.with_abi(invariant_contract.abi)
.decode(call_result.result.as_ref(), Some(call_result.exit_reason));
let func = invariant_contract.invariant_function;
debug_assert!(func.inputs.is_empty());
let origin = func.name.as_str();
Self {
test_error: TestError::Fail(
format!("{origin}, reason: {revert_reason}").into(),
calldata.to_vec(),
),
return_reason: "".into(),
revert_reason,
addr: invariant_contract.address,
calldata: func.selector().to_vec().into(),
inner_sequence: inner_sequence.to_vec(),
shrink_run_limit: invariant_config.shrink_run_limit,
fail_on_revert: invariant_config.fail_on_revert,
}
}
}