foundry_debugger/
builder.rs

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