Skip to main content

foundry_evm_core/
decode.rs

1//! Various utilities to decode test results.
2
3use crate::abi::{Vm, console};
4use alloy_dyn_abi::JsonAbiExt;
5use alloy_json_abi::{Error, JsonAbi};
6use alloy_primitives::{Log, Selector, hex, map::HashMap};
7use alloy_sol_types::{
8    ContractError::Revert, RevertReason, RevertReason::ContractError, SolEventInterface,
9    SolInterface, SolValue,
10};
11use foundry_common::SELECTOR_LEN;
12use itertools::Itertools;
13use revm::interpreter::InstructionResult;
14use std::{fmt, sync::OnceLock};
15
16/// Stable user-facing fallback for empty revert payloads.
17pub const EMPTY_REVERT_DATA: &str = "<empty revert data>";
18
19/// Prefix used by Foundry assertion helpers in user-visible failure messages.
20pub const ASSERTION_FAILED_PREFIX: &str = "assertion failed";
21
22/// A skip reason.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct SkipReason(pub Option<String>);
25
26impl SkipReason {
27    /// Decodes a skip reason, if any.
28    pub fn decode(raw_result: &[u8]) -> Option<Self> {
29        raw_result.strip_prefix(crate::constants::MAGIC_SKIP).map(|reason| {
30            let reason = String::from_utf8_lossy(reason).into_owned();
31            Self((!reason.is_empty()).then_some(reason))
32        })
33    }
34
35    /// Decodes a skip reason from a string that was obtained by formatting `Self`.
36    ///
37    /// This is a hack to support re-decoding a skip reason in proptest.
38    pub fn decode_self(s: &str) -> Option<Self> {
39        s.strip_prefix("skipped").map(|rest| Self(rest.strip_prefix(": ").map(ToString::to_string)))
40    }
41}
42
43impl fmt::Display for SkipReason {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str("skipped")?;
46        if let Some(reason) = &self.0 {
47            f.write_str(": ")?;
48            f.write_str(reason)?;
49        }
50        Ok(())
51    }
52}
53
54/// Decode a set of logs, only returning logs from DSTest logging events and Hardhat's `console.log`
55pub fn decode_console_logs(logs: &[Log]) -> Vec<String> {
56    logs.iter().filter_map(decode_console_log).collect()
57}
58
59/// Decode a single log.
60///
61/// This function returns [None] if it is not a DSTest log or the result of a Hardhat
62/// `console.log`.
63pub fn decode_console_log(log: &Log) -> Option<String> {
64    console::ds::ConsoleEvents::decode_log(log).ok().map(|decoded| decoded.to_string())
65}
66
67/// Decodes revert data.
68#[derive(Clone, Debug, Default)]
69pub struct RevertDecoder {
70    /// The custom errors to use for decoding.
71    errors: HashMap<Selector, Vec<Error>>,
72}
73
74impl Default for &RevertDecoder {
75    fn default() -> Self {
76        static EMPTY: OnceLock<RevertDecoder> = OnceLock::new();
77        EMPTY.get_or_init(RevertDecoder::new)
78    }
79}
80
81impl RevertDecoder {
82    /// Creates a new, empty revert decoder.
83    pub fn new() -> Self {
84        Self::default()
85    }
86
87    /// Sets the ABIs to use for error decoding.
88    ///
89    /// Note that this is decently expensive as it will hash all errors for faster indexing.
90    pub fn with_abis<'a>(mut self, abi: impl IntoIterator<Item = &'a JsonAbi>) -> Self {
91        self.extend_from_abis(abi);
92        self
93    }
94
95    /// Sets the ABI to use for error decoding.
96    ///
97    /// Note that this is decently expensive as it will hash all errors for faster indexing.
98    pub fn with_abi(mut self, abi: &JsonAbi) -> Self {
99        self.extend_from_abi(abi);
100        self
101    }
102
103    /// Extends the decoder with the given ABI's custom errors.
104    fn extend_from_abis<'a>(&mut self, abi: impl IntoIterator<Item = &'a JsonAbi>) {
105        for abi in abi {
106            self.extend_from_abi(abi);
107        }
108    }
109
110    /// Extends the decoder with the given ABI's custom errors.
111    fn extend_from_abi(&mut self, abi: &JsonAbi) {
112        for error in abi.errors() {
113            self.push_error(error.clone());
114        }
115    }
116
117    /// Adds a custom error to use for decoding.
118    pub fn push_error(&mut self, error: Error) {
119        self.errors.entry(error.selector()).or_default().push(error);
120    }
121
122    /// Tries to decode an error message from the given revert bytes.
123    ///
124    /// Note that this is just a best-effort guess, and should not be relied upon for anything other
125    /// than user output.
126    pub fn decode(&self, err: &[u8], status: Option<InstructionResult>) -> String {
127        self.maybe_decode(err, status).unwrap_or_else(|| {
128            if err.is_empty() { EMPTY_REVERT_DATA.to_string() } else { trimmed_hex(err) }
129        })
130    }
131
132    /// Tries to decode an error message from the given revert bytes.
133    ///
134    /// See [`decode`](Self::decode) for more information.
135    pub fn maybe_decode(&self, err: &[u8], status: Option<InstructionResult>) -> Option<String> {
136        self.maybe_decode_known(err)
137            .or_else(|| decode_as_non_empty_string(err))
138            .or_else(|| Self::maybe_decode_fallback(err, status))
139    }
140
141    /// Tries to decode the given revert bytes as one of the errors known to this decoder:
142    /// Solidity's `Error(string)` and `Panic(uint256)`, `Vm`'s custom errors, or custom errors
143    /// registered with this decoder.
144    ///
145    /// Unlike [`maybe_decode`](Self::maybe_decode), this returns `None` for unrecognized custom
146    /// errors instead of falling back to a generic `custom error <selector>` representation.
147    pub fn maybe_decode_known(&self, err: &[u8]) -> Option<String> {
148        // Solidity's `Error(string)` (handled separately in order to strip revert: prefix)
149        if let Some(ContractError(Revert(revert))) = RevertReason::decode(err) {
150            return Some(revert.reason);
151        }
152
153        // Solidity's `Panic(uint256)` and `Vm`'s custom errors.
154        if let Ok(e) = alloy_sol_types::ContractError::<Vm::VmErrors>::abi_decode(err) {
155            return Some(e.to_string());
156        }
157
158        // Custom errors.
159        if let Some((selector, data)) = err.split_first_chunk::<SELECTOR_LEN>()
160            && let Some(errors) = self.errors.get(selector)
161        {
162            for error in errors {
163                // If we don't decode, don't return an error, try to decode as a string later.
164                if let Ok(decoded) = error.abi_decode_input(data) {
165                    return Some(format!(
166                        "{}({})",
167                        error.name,
168                        decoded.iter().map(foundry_common::fmt::format_token).format(", ")
169                    ));
170                }
171            }
172        }
173
174        None
175    }
176
177    /// Formats revert bytes that could not be decoded as a known error.
178    fn maybe_decode_fallback(err: &[u8], status: Option<InstructionResult>) -> Option<String> {
179        // Generic custom error.
180        if let Some((selector, data)) = err.split_first_chunk::<SELECTOR_LEN>() {
181            return Some({
182                let mut s = format!("custom error {}", hex::encode_prefixed(selector));
183                if !data.is_empty() {
184                    s.push_str(": ");
185                    match std::str::from_utf8(data) {
186                        Ok(data) => s.push_str(data),
187                        Err(_) => s.push_str(&hex::encode(data)),
188                    }
189                }
190                s
191            });
192        }
193
194        if let Some(status) = status
195            && !status.is_ok()
196        {
197            return Some(format!("EvmError: {status:?}"));
198        }
199        if err.is_empty() {
200            None
201        } else {
202            Some(format!("custom error bytes {}", hex::encode_prefixed(err)))
203        }
204    }
205}
206
207/// Helper function that decodes provided error as an ABI encoded or an ASCII string (if not empty).
208fn decode_as_non_empty_string(err: &[u8]) -> Option<String> {
209    // ABI-encoded `string`.
210    if let Ok(s) = String::abi_decode(err)
211        && !s.is_empty()
212    {
213        return Some(s);
214    }
215
216    // ASCII string.
217    if err.is_ascii() {
218        let msg = std::str::from_utf8(err).unwrap().to_string();
219        if !msg.is_empty() {
220            return Some(msg);
221        }
222    }
223
224    None
225}
226
227fn trimmed_hex(s: &[u8]) -> String {
228    let n = 32;
229    if s.len() <= n {
230        hex::encode(s)
231    } else {
232        format!(
233            "{}…{} ({} bytes)",
234            hex::encode(&s[..n / 2]),
235            hex::encode(&s[s.len() - n / 2..]),
236            s.len(),
237        )
238    }
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244
245    #[test]
246    fn test_trimmed_hex() {
247        assert_eq!(trimmed_hex(&hex::decode("1234567890").unwrap()), "1234567890");
248        assert_eq!(
249            trimmed_hex(&hex::decode("492077697368207275737420737570706F72746564206869676865722D6B696E646564207479706573").unwrap()),
250            "49207769736820727573742073757070…6865722d6b696e646564207479706573 (41 bytes)"
251        );
252    }
253
254    // https://github.com/foundry-rs/foundry/issues/10162
255    #[test]
256    fn partial_decode() {
257        /*
258        error ValidationFailed(bytes);
259        error InvalidNonce();
260        */
261        let mut decoder = RevertDecoder::default();
262        decoder.push_error("ValidationFailed(bytes)".parse().unwrap());
263
264        /*
265        abi.encodeWithSelector(ValidationFailed.selector, InvalidNonce.selector)
266        */
267        let data = &hex!(
268            "0xe17594de"
269            "756688fe00000000000000000000000000000000000000000000000000000000"
270        );
271        assert_eq!(
272            decoder.decode(data, None),
273            "custom error 0xe17594de: 756688fe00000000000000000000000000000000000000000000000000000000"
274        );
275
276        /*
277        abi.encodeWithSelector(ValidationFailed.selector, abi.encodeWithSelector(InvalidNonce.selector))
278        */
279        let data = &hex!(
280            "0xe17594de"
281            "0000000000000000000000000000000000000000000000000000000000000020"
282            "0000000000000000000000000000000000000000000000000000000000000004"
283            "756688fe00000000000000000000000000000000000000000000000000000000"
284        );
285        assert_eq!(decoder.decode(data, None), "ValidationFailed(0x756688fe)");
286    }
287
288    #[test]
289    fn maybe_decode_magic_skip_is_not_skip_marker() {
290        let decoder = RevertDecoder::new();
291        let reason = decoder.maybe_decode(crate::constants::MAGIC_SKIP, None).unwrap();
292
293        assert_eq!(reason, "FOUNDRY::SKIP");
294        assert!(SkipReason::decode_self(&reason).is_none());
295    }
296
297    #[test]
298    fn plain_string_is_not_a_known_error() {
299        let decoder = RevertDecoder::new();
300        let data = b".,Bo";
301
302        assert_eq!(decoder.maybe_decode_known(data), None);
303        assert_eq!(decoder.maybe_decode(data, None).as_deref(), Some(".,Bo"));
304    }
305}