foundry_evm_traces/identifier/
mod.rs1use 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#[derive(Debug)]
20pub struct IdentifiedAddress<'a> {
21 pub address: Address,
23 pub label: Option<String>,
25 pub contract: Option<String>,
29 pub abi: Option<Cow<'a, JsonAbi>>,
31 pub artifact_id: Option<ArtifactId>,
33}
34
35pub trait TraceIdentifier {
37 fn identify_addresses(&mut self, nodes: &[&CallTraceNode]) -> Vec<IdentifiedAddress<'_>>;
39}
40
41pub struct TraceIdentifiers<'a> {
43 pub local: Option<LocalTraceIdentifier<'a>>,
45 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 pub const fn new() -> Self {
78 Self { local: None, external: None }
79 }
80
81 pub fn with_local(mut self, known_contracts: &'a ContractsByArtifact) -> Self {
83 self.local = Some(LocalTraceIdentifier::new(known_contracts));
84 self
85 }
86
87 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 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 pub fn is_empty(&self) -> bool {
106 self.local.is_none() && self.external.is_none()
107 }
108}