|
| 1 | +use crate::Debugger; |
| 2 | +use alloy_primitives::Address; |
| 3 | +use eyre::Result; |
| 4 | +use foundry_common::{compile::ContractSources, evm::Breakpoints, get_contract_name}; |
| 5 | +use foundry_evm_core::{ |
| 6 | + debug::{DebugArena, DebugStep}, |
| 7 | + utils::CallKind, |
| 8 | +}; |
| 9 | +use foundry_evm_traces::CallTraceDecoder; |
| 10 | +use std::collections::HashMap; |
| 11 | + |
| 12 | +/// Debugger builder. |
| 13 | +#[derive(Debug, Default)] |
| 14 | +#[must_use = "builders do nothing unless you call `build` on them"] |
| 15 | +pub struct DebuggerBuilder { |
| 16 | + /// Debug traces returned from the EVM execution. |
| 17 | + debug_arena: Vec<(Address, Vec<DebugStep>, CallKind)>, |
| 18 | + /// Identified contracts. |
| 19 | + identified_contracts: HashMap<Address, String>, |
| 20 | + /// Map of source files. |
| 21 | + sources: ContractSources, |
| 22 | + /// Map of the debugger breakpoints. |
| 23 | + breakpoints: Breakpoints, |
| 24 | +} |
| 25 | + |
| 26 | +impl DebuggerBuilder { |
| 27 | + /// Creates a new debugger builder. |
| 28 | + #[inline] |
| 29 | + pub fn new() -> Self { |
| 30 | + Self::default() |
| 31 | + } |
| 32 | + |
| 33 | + /// Extends the debug arena. |
| 34 | + #[inline] |
| 35 | + pub fn debug_arenas(mut self, arena: &[DebugArena]) -> Self { |
| 36 | + for arena in arena { |
| 37 | + self = self.debug_arena(arena); |
| 38 | + } |
| 39 | + self |
| 40 | + } |
| 41 | + |
| 42 | + /// Extends the debug arena. |
| 43 | + #[inline] |
| 44 | + pub fn debug_arena(mut self, arena: &DebugArena) -> Self { |
| 45 | + arena.flatten_to(0, &mut self.debug_arena); |
| 46 | + self |
| 47 | + } |
| 48 | + |
| 49 | + /// Extends the identified contracts from multiple decoders. |
| 50 | + #[inline] |
| 51 | + pub fn decoders(mut self, decoders: &[CallTraceDecoder]) -> Self { |
| 52 | + for decoder in decoders { |
| 53 | + self = self.decoder(decoder); |
| 54 | + } |
| 55 | + self |
| 56 | + } |
| 57 | + |
| 58 | + /// Extends the identified contracts from a decoder. |
| 59 | + #[inline] |
| 60 | + pub fn decoder(self, decoder: &CallTraceDecoder) -> Self { |
| 61 | + let c = decoder.contracts.iter().map(|(k, v)| (*k, get_contract_name(v).to_string())); |
| 62 | + self.identified_contracts(c) |
| 63 | + } |
| 64 | + |
| 65 | + /// Extends the identified contracts. |
| 66 | + #[inline] |
| 67 | + pub fn identified_contracts( |
| 68 | + mut self, |
| 69 | + identified_contracts: impl IntoIterator<Item = (Address, String)>, |
| 70 | + ) -> Self { |
| 71 | + self.identified_contracts.extend(identified_contracts); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Sets the sources for the debugger. |
| 76 | + #[inline] |
| 77 | + pub fn sources(mut self, sources: ContractSources) -> Self { |
| 78 | + self.sources = sources; |
| 79 | + self |
| 80 | + } |
| 81 | + |
| 82 | + /// Sets the breakpoints for the debugger. |
| 83 | + #[inline] |
| 84 | + pub fn breakpoints(mut self, breakpoints: Breakpoints) -> Self { |
| 85 | + self.breakpoints = breakpoints; |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Builds the debugger. |
| 90 | + #[inline] |
| 91 | + pub fn build(self) -> Result<Debugger> { |
| 92 | + let Self { debug_arena, identified_contracts, sources, breakpoints } = self; |
| 93 | + Debugger::new(debug_arena, 0, identified_contracts, sources, breakpoints) |
| 94 | + } |
| 95 | +} |
0 commit comments