Skip to content

Commit 5b7e4cb

Browse files
authored
chore: debugger improvements 2 (#6494)
* feat: debugger builder and clean up terminal handling * typos
1 parent 220f540 commit 5b7e4cb

File tree

13 files changed

+1541
-1463
lines changed

13 files changed

+1541
-1463
lines changed

crates/anvil/src/service.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type BlockMiningFuture =
106106
Pin<Box<dyn Future<Output = (MinedBlockOutcome, Arc<Backend>)> + Send + Sync>>;
107107

108108
/// A type that exclusively mines one block at a time
109-
#[must_use = "BlockProducer does nothing unless polled"]
109+
#[must_use = "streams do nothing unless polled"]
110110
struct BlockProducer {
111111
/// Holds the backend if no block is being mined
112112
idle_backend: Option<Arc<Backend>>,

crates/cli/src/utils/cmd.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use foundry_compilers::{
1010
Artifact, ProjectCompileOutput,
1111
};
1212
use foundry_config::{error::ExtractConfigError, figment::Figment, Chain, Config, NamedChain};
13-
use foundry_debugger::DebuggerArgs;
13+
use foundry_debugger::DebuggerBuilder;
1414
use foundry_evm::{
1515
debug::DebugArena,
1616
executors::{DeployResult, EvmError, ExecutionErr, RawCallResult},
@@ -404,13 +404,12 @@ pub async fn handle_traces(
404404

405405
if debug {
406406
let sources = etherscan_identifier.get_compiled_contracts().await?;
407-
let debugger = DebuggerArgs {
408-
debug: vec![result.debug],
409-
decoder: &decoder,
410-
sources,
411-
breakpoints: Default::default(),
412-
};
413-
debugger.run()?;
407+
let mut debugger = DebuggerBuilder::new()
408+
.debug_arena(&result.debug)
409+
.decoder(&decoder)
410+
.sources(sources)
411+
.build()?;
412+
debugger.try_run()?;
414413
} else {
415414
print_traces(&mut result, &decoder, verbose).await?;
416415
}

crates/debugger/src/builder.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

crates/debugger/src/debugger.rs

-48
This file was deleted.

0 commit comments

Comments
 (0)