Skip to content

Commit

Permalink
chore: debugger improvements 2 (#6494)
Browse files Browse the repository at this point in the history
* feat: debugger builder and clean up terminal handling

* typos
  • Loading branch information
DaniPopes authored Dec 1, 2023
1 parent 220f540 commit 5b7e4cb
Show file tree
Hide file tree
Showing 13 changed files with 1,541 additions and 1,463 deletions.
2 changes: 1 addition & 1 deletion crates/anvil/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type BlockMiningFuture =
Pin<Box<dyn Future<Output = (MinedBlockOutcome, Arc<Backend>)> + Send + Sync>>;

/// A type that exclusively mines one block at a time
#[must_use = "BlockProducer does nothing unless polled"]
#[must_use = "streams do nothing unless polled"]
struct BlockProducer {
/// Holds the backend if no block is being mined
idle_backend: Option<Arc<Backend>>,
Expand Down
15 changes: 7 additions & 8 deletions crates/cli/src/utils/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use foundry_compilers::{
Artifact, ProjectCompileOutput,
};
use foundry_config::{error::ExtractConfigError, figment::Figment, Chain, Config, NamedChain};
use foundry_debugger::DebuggerArgs;
use foundry_debugger::DebuggerBuilder;
use foundry_evm::{
debug::DebugArena,
executors::{DeployResult, EvmError, ExecutionErr, RawCallResult},
Expand Down Expand Up @@ -404,13 +404,12 @@ pub async fn handle_traces(

if debug {
let sources = etherscan_identifier.get_compiled_contracts().await?;
let debugger = DebuggerArgs {
debug: vec![result.debug],
decoder: &decoder,
sources,
breakpoints: Default::default(),
};
debugger.run()?;
let mut debugger = DebuggerBuilder::new()
.debug_arena(&result.debug)
.decoder(&decoder)
.sources(sources)
.build()?;
debugger.try_run()?;
} else {
print_traces(&mut result, &decoder, verbose).await?;
}
Expand Down
95 changes: 95 additions & 0 deletions crates/debugger/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::Debugger;
use alloy_primitives::Address;
use eyre::Result;
use foundry_common::{compile::ContractSources, evm::Breakpoints, get_contract_name};
use foundry_evm_core::{
debug::{DebugArena, DebugStep},
utils::CallKind,
};
use foundry_evm_traces::CallTraceDecoder;
use std::collections::HashMap;

/// Debugger builder.
#[derive(Debug, Default)]
#[must_use = "builders do nothing unless you call `build` on them"]
pub struct DebuggerBuilder {
/// Debug traces returned from the EVM execution.
debug_arena: Vec<(Address, Vec<DebugStep>, CallKind)>,
/// Identified contracts.
identified_contracts: HashMap<Address, String>,
/// Map of source files.
sources: ContractSources,
/// Map of the debugger breakpoints.
breakpoints: Breakpoints,
}

impl DebuggerBuilder {
/// Creates a new debugger builder.
#[inline]
pub fn new() -> Self {
Self::default()
}

/// Extends the debug arena.
#[inline]
pub fn debug_arenas(mut self, arena: &[DebugArena]) -> Self {
for arena in arena {
self = self.debug_arena(arena);
}
self
}

/// Extends the debug arena.
#[inline]
pub fn debug_arena(mut self, arena: &DebugArena) -> Self {
arena.flatten_to(0, &mut self.debug_arena);
self
}

/// Extends the identified contracts from multiple decoders.
#[inline]
pub fn decoders(mut self, decoders: &[CallTraceDecoder]) -> Self {
for decoder in decoders {
self = self.decoder(decoder);
}
self
}

/// Extends the identified contracts from a decoder.
#[inline]
pub fn decoder(self, decoder: &CallTraceDecoder) -> Self {
let c = decoder.contracts.iter().map(|(k, v)| (*k, get_contract_name(v).to_string()));
self.identified_contracts(c)
}

/// Extends the identified contracts.
#[inline]
pub fn identified_contracts(
mut self,
identified_contracts: impl IntoIterator<Item = (Address, String)>,
) -> Self {
self.identified_contracts.extend(identified_contracts);
self
}

/// Sets the sources for the debugger.
#[inline]
pub fn sources(mut self, sources: ContractSources) -> Self {
self.sources = sources;
self
}

/// Sets the breakpoints for the debugger.
#[inline]
pub fn breakpoints(mut self, breakpoints: Breakpoints) -> Self {
self.breakpoints = breakpoints;
self
}

/// Builds the debugger.
#[inline]
pub fn build(self) -> Result<Debugger> {
let Self { debug_arena, identified_contracts, sources, breakpoints } = self;
Debugger::new(debug_arena, 0, identified_contracts, sources, breakpoints)
}
}
48 changes: 0 additions & 48 deletions crates/debugger/src/debugger.rs

This file was deleted.

Loading

0 comments on commit 5b7e4cb

Please sign in to comment.