foundry_evm_core/backend/
diagnostic.rs

1use crate::backend::LocalForkId;
2use alloy_primitives::{map::AddressHashMap, Address};
3use itertools::Itertools;
4
5/// Represents possible diagnostic cases on revert
6#[derive(Clone, Debug)]
7pub enum RevertDiagnostic {
8    /// The `contract` does not exist on the `active` fork but exist on other fork(s)
9    ContractExistsOnOtherForks {
10        contract: Address,
11        active: LocalForkId,
12        available_on: Vec<LocalForkId>,
13    },
14    ContractDoesNotExist {
15        contract: Address,
16        active: LocalForkId,
17        persistent: bool,
18    },
19}
20
21impl RevertDiagnostic {
22    /// Converts the diagnostic to a readable error message
23    pub fn to_error_msg(&self, labels: &AddressHashMap<String>) -> String {
24        let get_label =
25            |addr: &Address| labels.get(addr).cloned().unwrap_or_else(|| addr.to_string());
26
27        match self {
28            Self::ContractExistsOnOtherForks { contract, active, available_on } => {
29                let contract_label = get_label(contract);
30
31                format!(
32                    r#"Contract {} does not exist on active fork with id `{}`
33        But exists on non active forks: `[{}]`"#,
34                    contract_label,
35                    active,
36                    available_on.iter().format(", ")
37                )
38            }
39            Self::ContractDoesNotExist { contract, persistent, .. } => {
40                let contract_label = get_label(contract);
41                if *persistent {
42                    format!("Contract {contract_label} does not exist")
43                } else {
44                    format!("Contract {contract_label} does not exist and is not marked as persistent, see `vm.makePersistent()`")
45                }
46            }
47        }
48    }
49}