Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: debugger improvements 2 #6494

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()?;
Comment on lines +407 to +412
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this

} 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()
}
Comment on lines +27 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I think new -> default is redundant


/// 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