foundry_evm_traces/identifier/
signatures.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use alloy_json_abi::{Error, Event, Function};
use alloy_primitives::{hex, map::HashSet};
use foundry_common::{
    abi::{get_error, get_event, get_func},
    fs,
    selectors::{OpenChainClient, SelectorType},
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};
use tokio::sync::RwLock;

pub type SingleSignaturesIdentifier = Arc<RwLock<SignaturesIdentifier>>;

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct CachedSignatures {
    pub errors: BTreeMap<String, String>,
    pub events: BTreeMap<String, String>,
    pub functions: BTreeMap<String, String>,
}

impl CachedSignatures {
    #[instrument(target = "evm::traces")]
    pub fn load(cache_path: PathBuf) -> Self {
        let path = cache_path.join("signatures");
        if path.is_file() {
            fs::read_json_file(&path)
                .map_err(
                    |err| warn!(target: "evm::traces", ?path, ?err, "failed to read cache file"),
                )
                .unwrap_or_default()
        } else {
            if let Err(err) = std::fs::create_dir_all(cache_path) {
                warn!(target: "evm::traces", "could not create signatures cache dir: {:?}", err);
            }
            Self::default()
        }
    }
}
/// An identifier that tries to identify functions and events using signatures found at
/// `https://openchain.xyz` or a local cache.
#[derive(Debug)]
pub struct SignaturesIdentifier {
    /// Cached selectors for functions, events and custom errors.
    cached: CachedSignatures,
    /// Location where to save `CachedSignatures`.
    cached_path: Option<PathBuf>,
    /// Selectors that were unavailable during the session.
    unavailable: HashSet<String>,
    /// The OpenChain client to fetch signatures from.
    client: Option<OpenChainClient>,
}

impl SignaturesIdentifier {
    #[instrument(target = "evm::traces")]
    pub fn new(
        cache_path: Option<PathBuf>,
        offline: bool,
    ) -> eyre::Result<SingleSignaturesIdentifier> {
        let client = if !offline { Some(OpenChainClient::new()?) } else { None };

        let identifier = if let Some(cache_path) = cache_path {
            let path = cache_path.join("signatures");
            trace!(target: "evm::traces", ?path, "reading signature cache");
            let cached = CachedSignatures::load(cache_path);
            Self { cached, cached_path: Some(path), unavailable: HashSet::default(), client }
        } else {
            Self {
                cached: Default::default(),
                cached_path: None,
                unavailable: HashSet::default(),
                client,
            }
        };

        Ok(Arc::new(RwLock::new(identifier)))
    }

    #[instrument(target = "evm::traces", skip(self))]
    pub fn save(&self) {
        if let Some(cached_path) = &self.cached_path {
            if let Some(parent) = cached_path.parent() {
                if let Err(err) = std::fs::create_dir_all(parent) {
                    warn!(target: "evm::traces", ?parent, ?err, "failed to create cache");
                }
            }
            if let Err(err) = fs::write_json_file(cached_path, &self.cached) {
                warn!(target: "evm::traces", ?cached_path, ?err, "failed to flush signature cache");
            } else {
                trace!(target: "evm::traces", ?cached_path, "flushed signature cache")
            }
        }
    }
}

impl SignaturesIdentifier {
    async fn identify<T>(
        &mut self,
        selector_type: SelectorType,
        identifiers: impl IntoIterator<Item = impl AsRef<[u8]>>,
        get_type: impl Fn(&str) -> eyre::Result<T>,
    ) -> Vec<Option<T>> {
        let cache = match selector_type {
            SelectorType::Function => &mut self.cached.functions,
            SelectorType::Event => &mut self.cached.events,
            SelectorType::Error => &mut self.cached.errors,
        };

        let hex_identifiers: Vec<String> =
            identifiers.into_iter().map(hex::encode_prefixed).collect();

        if let Some(client) = &self.client {
            let query: Vec<_> = hex_identifiers
                .iter()
                .filter(|v| !cache.contains_key(v.as_str()))
                .filter(|v| !self.unavailable.contains(v.as_str()))
                .collect();

            if let Ok(res) = client.decode_selectors(selector_type, query.clone()).await {
                for (hex_id, selector_result) in query.into_iter().zip(res.into_iter()) {
                    let mut found = false;
                    if let Some(decoded_results) = selector_result {
                        if let Some(decoded_result) = decoded_results.into_iter().next() {
                            cache.insert(hex_id.clone(), decoded_result);
                            found = true;
                        }
                    }
                    if !found {
                        self.unavailable.insert(hex_id.clone());
                    }
                }
            }
        }

        hex_identifiers.iter().map(|v| cache.get(v).and_then(|v| get_type(v).ok())).collect()
    }

    /// Identifies `Function`s from its cache or `https://api.openchain.xyz`
    pub async fn identify_functions(
        &mut self,
        identifiers: impl IntoIterator<Item = impl AsRef<[u8]>>,
    ) -> Vec<Option<Function>> {
        self.identify(SelectorType::Function, identifiers, get_func).await
    }

    /// Identifies `Function` from its cache or `https://api.openchain.xyz`
    pub async fn identify_function(&mut self, identifier: &[u8]) -> Option<Function> {
        self.identify_functions(&[identifier]).await.pop().unwrap()
    }

    /// Identifies `Event`s from its cache or `https://api.openchain.xyz`
    pub async fn identify_events(
        &mut self,
        identifiers: impl IntoIterator<Item = impl AsRef<[u8]>>,
    ) -> Vec<Option<Event>> {
        self.identify(SelectorType::Event, identifiers, get_event).await
    }

    /// Identifies `Event` from its cache or `https://api.openchain.xyz`
    pub async fn identify_event(&mut self, identifier: &[u8]) -> Option<Event> {
        self.identify_events(&[identifier]).await.pop().unwrap()
    }

    /// Identifies `Error`s from its cache or `https://api.openchain.xyz`.
    pub async fn identify_errors(
        &mut self,
        identifiers: impl IntoIterator<Item = impl AsRef<[u8]>>,
    ) -> Vec<Option<Error>> {
        self.identify(SelectorType::Error, identifiers, get_error).await
    }

    /// Identifies `Error` from its cache or `https://api.openchain.xyz`.
    pub async fn identify_error(&mut self, identifier: &[u8]) -> Option<Error> {
        self.identify_errors(&[identifier]).await.pop().unwrap()
    }
}

impl Drop for SignaturesIdentifier {
    fn drop(&mut self) {
        self.save();
    }
}

#[cfg(test)]
#[allow(clippy::needless_return)]
mod tests {
    use super::*;

    #[tokio::test(flavor = "multi_thread")]
    async fn can_query_signatures() {
        let tmp = tempfile::tempdir().unwrap();
        {
            let sigs = SignaturesIdentifier::new(Some(tmp.path().into()), false).unwrap();

            assert!(sigs.read().await.cached.events.is_empty());
            assert!(sigs.read().await.cached.functions.is_empty());

            let func = sigs.write().await.identify_function(&[35, 184, 114, 221]).await.unwrap();
            let event = sigs
                .write()
                .await
                .identify_event(&[
                    39, 119, 42, 220, 99, 219, 7, 170, 231, 101, 183, 30, 178, 181, 51, 6, 79, 167,
                    129, 189, 87, 69, 126, 27, 19, 133, 146, 216, 25, 141, 9, 89,
                ])
                .await
                .unwrap();

            assert_eq!(func, get_func("transferFrom(address,address,uint256)").unwrap());
            assert_eq!(event, get_event("Transfer(address,address,uint128)").unwrap());

            // dropping saves the cache
        }

        let sigs = SignaturesIdentifier::new(Some(tmp.path().into()), false).unwrap();
        assert_eq!(sigs.read().await.cached.events.len(), 1);
        assert_eq!(sigs.read().await.cached.functions.len(), 1);
    }
}