|
| 1 | +//! The file dumper implementation |
| 2 | +
|
| 3 | +use alloy_primitives::{Address, Bytes, U256}; |
| 4 | +use serde::Serialize; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::path::PathBuf; |
| 7 | +use std::option::Option; |
| 8 | + |
| 9 | +use crate::context::DebuggerContext; |
| 10 | +use eyre::Result; |
| 11 | +use foundry_common::compile::ContractSources; |
| 12 | +use foundry_common::fs::write_json_file; |
| 13 | +use foundry_compilers::artifacts::ContractBytecodeSome; |
| 14 | +use foundry_evm_core::debug::{DebugNodeFlat, DebugStep, Instruction}; |
| 15 | +use foundry_evm_core::utils::PcIcMap; |
| 16 | +use revm_inspectors::tracing::types::CallKind; |
| 17 | + |
| 18 | +/// The file dumper |
| 19 | +pub struct FileDumper<'a> { |
| 20 | + path: &'a PathBuf, |
| 21 | + debugger_context: &'a mut DebuggerContext, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'a> FileDumper<'a> { |
| 25 | + pub fn new(path: &'a PathBuf, debugger_context: &'a mut DebuggerContext) -> Self { |
| 26 | + Self { path, debugger_context } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn run(&mut self) -> Result<()> { |
| 30 | + let data = DebuggerDump::from(self.debugger_context); |
| 31 | + write_json_file(self.path, &data).unwrap(); |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl DebuggerDump { |
| 37 | + fn from(debugger_context: &DebuggerContext) -> DebuggerDump { |
| 38 | + Self { |
| 39 | + contracts: to_contracts_dump(debugger_context), |
| 40 | + executions: to_executions_dump(debugger_context), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Serialize)] |
| 46 | +struct DebuggerDump { |
| 47 | + contracts: ContractsDump, |
| 48 | + executions: ExecutionsDump, |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Serialize)] |
| 52 | +struct ExecutionsDump { |
| 53 | + calls: Vec<CallDump>, |
| 54 | + // Map of contract name to PcIcMapDump |
| 55 | + pc_ic_maps: HashMap<String, PcIcMapDump>, |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Serialize)] |
| 59 | +struct CallDump { |
| 60 | + address: Address, |
| 61 | + kind: CallKind, |
| 62 | + steps: Vec<StepDump>, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Serialize)] |
| 66 | +struct StepDump { |
| 67 | + /// Stack *prior* to running the associated opcode |
| 68 | + stack: Vec<U256>, |
| 69 | + /// Memory *prior* to running the associated opcode |
| 70 | + memory: Bytes, |
| 71 | + /// Calldata *prior* to running the associated opcode |
| 72 | + calldata: Bytes, |
| 73 | + /// Returndata *prior* to running the associated opcode |
| 74 | + returndata: Bytes, |
| 75 | + /// Opcode to be executed |
| 76 | + instruction: Instruction, |
| 77 | + /// Optional bytes that are being pushed onto the stack |
| 78 | + push_bytes: Option<Bytes>, |
| 79 | + /// The program counter at this step. |
| 80 | + pc: usize, |
| 81 | + /// Cumulative gas usage |
| 82 | + total_gas_used: u64, |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Serialize)] |
| 86 | +struct PcIcMapDump { |
| 87 | + deploy_code_map: HashMap<usize, usize>, |
| 88 | + runtime_code_map: HashMap<usize, usize>, |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Serialize)] |
| 92 | +struct ContractsDump { |
| 93 | + // Map of call address to contract name |
| 94 | + identified_calls: HashMap<Address, String>, |
| 95 | + sources: ContractsSourcesDump, |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Serialize)] |
| 99 | +struct ContractsSourcesDump { |
| 100 | + ids_by_name: HashMap<String, Vec<u32>>, |
| 101 | + sources_by_id: HashMap<u32, ContractSourceDetailsDump>, |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Serialize)] |
| 105 | +struct ContractSourceDetailsDump { |
| 106 | + source_code: String, |
| 107 | + contract_bytecode: ContractBytecodeSome, |
| 108 | + source_path: Option<PathBuf>, |
| 109 | +} |
| 110 | + |
| 111 | +fn to_executions_dump(debugger_context: &DebuggerContext) -> ExecutionsDump { |
| 112 | + ExecutionsDump { |
| 113 | + calls: debugger_context.debug_arena.iter().map(|call| to_call_dump(call)).collect(), |
| 114 | + pc_ic_maps: debugger_context.pc_ic_maps.iter().map(|(k, v)| (k.clone(), to_pc_ic_map_dump(&v))).collect(), |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fn to_call_dump(call: &DebugNodeFlat) -> CallDump { |
| 119 | + CallDump { |
| 120 | + address: call.address, |
| 121 | + kind: call.kind, |
| 122 | + steps: call.steps.iter().map(|step| to_step_dump(step.clone())).collect(), |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +fn to_step_dump(step: DebugStep) -> StepDump { |
| 127 | + StepDump { |
| 128 | + stack: step.stack, |
| 129 | + memory: step.memory, |
| 130 | + calldata: step.calldata, |
| 131 | + returndata: step.returndata, |
| 132 | + instruction: step.instruction, |
| 133 | + push_bytes: step.push_bytes.map(|v| Bytes::from(v)), |
| 134 | + pc: step.pc, |
| 135 | + total_gas_used: step.total_gas_used, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +fn to_pc_ic_map_dump(pc_ic_map: &(PcIcMap, PcIcMap)) -> PcIcMapDump { |
| 140 | + let mut deploy_code_map = HashMap::new(); |
| 141 | + |
| 142 | + for (k, v) in pc_ic_map.0.inner.iter() { |
| 143 | + deploy_code_map.insert(*k, *v); |
| 144 | + } |
| 145 | + |
| 146 | + let mut runtime_code_map = HashMap::new(); |
| 147 | + for (k, v) in pc_ic_map.1.inner.iter() { |
| 148 | + runtime_code_map.insert(*k, *v); |
| 149 | + } |
| 150 | + |
| 151 | + PcIcMapDump { deploy_code_map, runtime_code_map } |
| 152 | +} |
| 153 | + |
| 154 | +fn to_contracts_dump(debugger_context: &DebuggerContext) -> ContractsDump { |
| 155 | + ContractsDump { |
| 156 | + identified_calls: debugger_context.identified_contracts.clone(), |
| 157 | + sources: to_contracts_sources_dump(&debugger_context.contracts_sources), |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fn to_contracts_sources_dump(contracts_sources: &ContractSources) -> ContractsSourcesDump { |
| 162 | + ContractsSourcesDump { |
| 163 | + ids_by_name: contracts_sources.ids_by_name.clone(), |
| 164 | + sources_by_id: contracts_sources |
| 165 | + .sources_by_id |
| 166 | + .iter() |
| 167 | + .map(|(id, (source_code, contract_bytecode, source_path))| { |
| 168 | + ( |
| 169 | + *id, |
| 170 | + ContractSourceDetailsDump { |
| 171 | + source_code: source_code.clone(), |
| 172 | + contract_bytecode: contract_bytecode.clone(), |
| 173 | + source_path: source_path.clone(), |
| 174 | + }, |
| 175 | + ) |
| 176 | + }) |
| 177 | + .collect(), |
| 178 | + } |
| 179 | +} |
0 commit comments