foundry_evm_core/
decode.rs
1use 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#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct SkipReason(pub Option<String>);
19
20impl SkipReason {
21 pub fn decode(raw_result: &[u8]) -> Option<Self> {
23 raw_result.strip_prefix(crate::constants::MAGIC_SKIP).map(|reason| {
24 let reason = String::from_utf8_lossy(reason).into_owned();
25 Self((!reason.is_empty()).then_some(reason))
26 })
27 }
28
29 pub fn decode_self(s: &str) -> Option<Self> {
33 s.strip_prefix("skipped").map(|rest| Self(rest.strip_prefix(": ").map(ToString::to_string)))
34 }
35}
36
37impl fmt::Display for SkipReason {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str("skipped")?;
40 if let Some(reason) = &self.0 {
41 f.write_str(": ")?;
42 f.write_str(reason)?;
43 }
44 Ok(())
45 }
46}
47
48pub fn decode_console_logs(logs: &[Log]) -> Vec<String> {
50 logs.iter().filter_map(decode_console_log).collect()
51}
52
53pub fn decode_console_log(log: &Log) -> Option<String> {
58 console::ds::ConsoleEvents::decode_log(log).ok().map(|decoded| decoded.to_string())
59}
60
61#[derive(Clone, Debug, Default)]
63pub struct RevertDecoder {
64 pub errors: HashMap<Selector, Vec<Error>>,
66}
67
68impl Default for &RevertDecoder {
69 fn default() -> Self {
70 static EMPTY: OnceLock<RevertDecoder> = OnceLock::new();
71 EMPTY.get_or_init(RevertDecoder::new)
72 }
73}
74
75impl RevertDecoder {
76 pub fn new() -> Self {
78 Self::default()
79 }
80
81 pub fn with_abis<'a>(mut self, abi: impl IntoIterator<Item = &'a JsonAbi>) -> Self {
85 self.extend_from_abis(abi);
86 self
87 }
88
89 pub fn with_abi(mut self, abi: &JsonAbi) -> Self {
93 self.extend_from_abi(abi);
94 self
95 }
96
97 pub fn with_abi_opt(mut self, abi: Option<&JsonAbi>) -> Self {
101 if let Some(abi) = abi {
102 self.extend_from_abi(abi);
103 }
104 self
105 }
106
107 pub fn extend_from_abis<'a>(&mut self, abi: impl IntoIterator<Item = &'a JsonAbi>) {
109 for abi in abi {
110 self.extend_from_abi(abi);
111 }
112 }
113
114 pub fn extend_from_abi(&mut self, abi: &JsonAbi) {
116 for error in abi.errors() {
117 self.push_error(error.clone());
118 }
119 }
120
121 pub fn push_error(&mut self, error: Error) {
123 self.errors.entry(error.selector()).or_default().push(error);
124 }
125
126 pub fn decode(&self, err: &[u8], status: Option<InstructionResult>) -> String {
131 self.maybe_decode(err, status).unwrap_or_else(|| {
132 if err.is_empty() { "<empty revert data>".to_string() } else { trimmed_hex(err) }
133 })
134 }
135
136 pub fn maybe_decode(&self, err: &[u8], status: Option<InstructionResult>) -> Option<String> {
140 if let Some(reason) = SkipReason::decode(err) {
141 return Some(reason.to_string());
142 }
143
144 if let Some(ContractError(Revert(revert))) = RevertReason::decode(err) {
146 return Some(revert.reason);
147 }
148
149 if let Ok(e) = alloy_sol_types::ContractError::<Vm::VmErrors>::abi_decode(err) {
151 return Some(e.to_string());
152 }
153
154 let string_decoded = decode_as_non_empty_string(err);
155
156 if let Some((selector, data)) = err.split_first_chunk::<SELECTOR_LEN>() {
157 if let Some(errors) = self.errors.get(selector) {
159 for error in errors {
160 if let Ok(decoded) = error.abi_decode_input(data) {
163 return Some(format!(
164 "{}({})",
165 error.name,
166 decoded.iter().map(foundry_common::fmt::format_token).format(", ")
167 ));
168 }
169 }
170 }
171
172 if string_decoded.is_some() {
173 return string_decoded;
174 }
175
176 return Some({
178 let mut s = format!("custom error {}", hex::encode_prefixed(selector));
179 if !data.is_empty() {
180 s.push_str(": ");
181 match std::str::from_utf8(data) {
182 Ok(data) => s.push_str(data),
183 Err(_) => s.push_str(&hex::encode(data)),
184 }
185 }
186 s
187 });
188 }
189
190 if string_decoded.is_some() {
191 return string_decoded;
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
207fn decode_as_non_empty_string(err: &[u8]) -> Option<String> {
209 if let Ok(s) = String::abi_decode(err)
211 && !s.is_empty()
212 {
213 return Some(s);
214 }
215
216 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 #[test]
256 fn partial_decode() {
257 let mut decoder = RevertDecoder::default();
262 decoder.push_error("ValidationFailed(bytes)".parse().unwrap());
263
264 let data = &hex!(
268 "0xe17594de"
269 "756688fe00000000000000000000000000000000000000000000000000000000"
270 );
271 assert_eq!(
272 decoder.decode(data, None),
273 "custom error 0xe17594de: 756688fe00000000000000000000000000000000000000000000000000000000"
274 );
275
276 let data = &hex!(
280 "0xe17594de"
281 "0000000000000000000000000000000000000000000000000000000000000020"
282 "0000000000000000000000000000000000000000000000000000000000000004"
283 "756688fe00000000000000000000000000000000000000000000000000000000"
284 );
285 assert_eq!(decoder.decode(data, None), "ValidationFailed(0x756688fe)");
286 }
287}