foundry_evm_traces/debug/
recording.rs1use alloy_primitives::{Bytes, U256, hex};
4use evm2::interpreter::op as opcode;
5use soldb_core::{StepRecord, StepTable, StepView, StorageChange};
6use std::{
7 collections::BTreeMap,
8 fmt,
9 ops::Range,
10 sync::{Arc, Mutex, MutexGuard},
11};
12
13#[derive(Clone)]
18pub struct DebugStepSink(Arc<Mutex<Option<StepTable>>>);
19
20impl DebugStepSink {
21 pub fn new() -> Self {
23 Self(Arc::new(Mutex::new(Some(StepTable::new()))))
24 }
25
26 pub fn take(&self) -> Option<StepTable> {
28 self.lock().take()
29 }
30
31 pub fn put(&self, table: StepTable) {
33 *self.lock() = Some(table);
34 }
35
36 pub fn lock(&self) -> MutexGuard<'_, Option<StepTable>> {
38 self.0.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
39 }
40
41 pub fn same(&self, other: &Self) -> bool {
43 Arc::ptr_eq(&self.0, &other.0)
44 }
45}
46
47impl Default for DebugStepSink {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl fmt::Debug for DebugStepSink {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.debug_struct("DebugStepSink").finish_non_exhaustive()
56 }
57}
58
59#[derive(Clone, Debug)]
61pub struct DebugRecording {
62 pub sink: DebugStepSink,
64 pub steps: Range<usize>,
66 pub frames: Vec<RecordedFrame>,
68}
69
70#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
72pub struct RecordedFrame {
73 pub depth: usize,
75 pub entry_step: usize,
77 pub exit_step: usize,
79}
80
81#[derive(Debug, Default)]
85pub struct StepConverter {
86 return_data: Option<Bytes>,
88 no_storage: Arc<BTreeMap<String, String>>,
90}
91
92#[derive(Debug)]
94pub struct StepStorage {
95 pub storage: Arc<BTreeMap<String, String>>,
97 pub storage_diff: Option<BTreeMap<String, StorageChange>>,
99 pub transient_storage: Option<BTreeMap<String, String>>,
101}
102
103impl StepConverter {
104 pub fn return_data(&mut self, buffer: &Bytes) -> Option<Arc<str>> {
109 match &self.return_data {
110 Some(last)
113 if (last.as_ptr() == buffer.as_ptr() && last.len() == buffer.len())
114 || last == buffer =>
115 {
116 None
117 }
118 _ => {
119 self.return_data = Some(buffer.clone());
120 Some(Arc::from(hex::encode(buffer)))
121 }
122 }
123 }
124
125 pub fn storage(
130 &self,
131 op: u8,
132 inputs: &[U256],
133 loaded: Option<U256>,
134 had_value: Option<U256>,
135 ) -> StepStorage {
136 let touched = match op {
137 opcode::SLOAD | opcode::TLOAD => inputs.last().copied().zip(loaded),
138 opcode::SSTORE | opcode::TSTORE => match inputs {
139 [.., value, slot] => Some((*slot, *value)),
140 _ => None,
141 },
142 _ => None,
143 };
144 let mut step = StepStorage {
145 storage: self.no_storage.clone(),
146 storage_diff: None,
147 transient_storage: None,
148 };
149 let Some((slot, value)) = touched else { return step };
150 let (slot, value) = (quantity(slot), quantity(value));
151 if matches!(op, opcode::SLOAD | opcode::SSTORE) {
152 let before = had_value.filter(|_| op == opcode::SSTORE).map(quantity);
153 let change = StorageChange { before, after: Some(value.clone()) };
154 step.storage = Arc::new(BTreeMap::from([(slot.clone(), value)]));
155 step.storage_diff = Some(BTreeMap::from([(slot, change)]));
156 } else {
157 step.transient_storage = Some(BTreeMap::from([(slot, value)]));
158 }
159 step
160 }
161}
162
163pub fn quantity(value: U256) -> String {
165 format!("{value:#x}")
166}
167
168pub fn step_record(step: &StepView<'_>) -> StepRecord {
170 StepRecord {
171 pc: step.pc(),
172 op: step.op(),
173 gas: step.gas(),
174 gas_cost: step.gas_cost(),
175 depth: step.depth(),
176 error: step.error().map(str::to_owned),
177 memory: step.memory(),
178 storage: Some(Arc::new(step.storage().clone())),
179 storage_diff: Some(step.storage_diff().clone()).filter(|diff| !diff.is_empty()),
180 transient_storage: Some(step.transient_storage().clone()).filter(|slots| !slots.is_empty()),
181 return_data: Some(Arc::from(step.return_data().unwrap_or_default())),
182 }
183}