foundry_evm_traces/identifier/
mod.rs

1use alloy_json_abi::JsonAbi;
2use alloy_primitives::{Address, Bytes, map::HashMap};
3use foundry_common::ContractsByArtifact;
4use foundry_compilers::ArtifactId;
5use foundry_config::{Chain, Config};
6use revm_inspectors::tracing::types::CallTraceNode;
7use std::borrow::Cow;
8
9mod local;
10pub use local::LocalTraceIdentifier;
11
12mod external;
13pub use external::ExternalIdentifier;
14
15mod signatures;
16pub use signatures::{SignaturesCache, SignaturesIdentifier};
17
18/// An address identified by a [`TraceIdentifier`].
19#[derive(Debug)]
20pub struct IdentifiedAddress<'a> {
21    /// The address.
22    pub address: Address,
23    /// The label for the address.
24    pub label: Option<String>,
25    /// The contract this address represents.
26    ///
27    /// Note: This may be in the format `"<artifact>:<contract>"`.
28    pub contract: Option<String>,
29    /// The ABI of the contract at this address.
30    pub abi: Option<Cow<'a, JsonAbi>>,
31    /// The artifact ID of the contract, if any.
32    pub artifact_id: Option<ArtifactId>,
33}
34
35/// Trace identifiers figure out what ABIs and labels belong to all the addresses of the trace.
36pub trait TraceIdentifier {
37    /// Attempts to identify an address in one or more call traces.
38    fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>>;
39}
40
41/// A collection of trace identifiers.
42pub struct TraceIdentifiers<'a> {
43    /// The local trace identifier.
44    pub local: Option<LocalTraceIdentifier<'a>>,
45    /// The optional external trace identifier.
46    pub external: Option<ExternalIdentifier>,
47}
48
49impl Default for TraceIdentifiers<'_> {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55impl TraceIdentifier for TraceIdentifiers<'_> {
56    fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>> {
57        if nodes.is_empty() {
58            return Vec::new();
59        }
60
61        let mut identities = Vec::with_capacity(nodes.len());
62        if let Some(local) = &mut self.local {
63            identities.extend(local.identify_addresses(nodes));
64            if identities.len() >= nodes.len() {
65                return identities;
66            }
67        }
68        if let Some(external) = &mut self.external {
69            identities.extend(external.identify_addresses(nodes));
70        }
71        identities
72    }
73}
74
75impl<'a> TraceIdentifiers<'a> {
76    /// Creates a new, empty instance.
77    pub const fn new() -> Self {
78        Self { local: None, external: None }
79    }
80
81    /// Sets the local identifier.
82    pub fn with_local(mut self, known_contracts: &'a ContractsByArtifact) -> Self {
83        self.local = Some(LocalTraceIdentifier::new(known_contracts));
84        self
85    }
86
87    /// Sets the local identifier.
88    pub fn with_local_and_bytecodes(
89        mut self,
90        known_contracts: &'a ContractsByArtifact,
91        contracts_bytecode: &'a HashMap<Address, Bytes>,
92    ) -> Self {
93        self.local =
94            Some(LocalTraceIdentifier::new(known_contracts).with_bytecodes(contracts_bytecode));
95        self
96    }
97
98    /// Sets the external identifier.
99    pub fn with_external(mut self, config: &Config, chain: Option<Chain>) -> eyre::Result<Self> {
100        self.external = ExternalIdentifier::new(config, chain)?;
101        Ok(self)
102    }
103
104    /// Returns `true` if there are no set identifiers.
105    pub fn is_empty(&self) -> bool {
106        self.local.is_none() && self.external.is_none()
107    }
108}