Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Jun 23, 2024
1 parent 06dc30a commit 216e9da
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 208 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 20 additions & 27 deletions crates/cli/src/utils/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use foundry_compilers::{
Artifact, ProjectCompileOutput,
};
use foundry_config::{error::ExtractConfigError, figment::Figment, Chain, Config, NamedChain};
use foundry_debugger::{DebugTraceIdentifier, Debugger};
use foundry_debugger::Debugger;
use foundry_evm::{
debug::DebugArena,
executors::{DeployResult, EvmError, RawCallResult},
opts::EvmOpts,
traces::{
debug::DebugTraceIdentifier,
identifier::{EtherscanIdentifier, SignaturesIdentifier},
render_trace_arena, render_trace_arena_with_internals, CallTraceDecoder,
CallTraceDecoderBuilder, TraceKind, Traces,
render_trace_arena, CallTraceDecoder, CallTraceDecoderBuilder, TraceKind, Traces,
},
};
use std::{
Expand Down Expand Up @@ -386,6 +386,18 @@ pub async fn handle_traces(
}
}

if decode_internal {
let sources = if let Some(etherscan_identifier) = &etherscan_identifier {
etherscan_identifier.get_compiled_contracts().await?
} else {
Default::default()
};

let identifier = DebugTraceIdentifier::new(sources);

decoder.debug_identifier = Some(identifier);
}

if debug {
let sources = if let Some(etherscan_identifier) = etherscan_identifier {
etherscan_identifier.get_compiled_contracts().await?
Expand All @@ -394,42 +406,23 @@ pub async fn handle_traces(
};
let mut debugger = Debugger::builder()
.debug_arena(result.debug.as_ref().expect("missing debug arena"))
.identifier(|b| b.decoder(&decoder).sources(sources))
.decoder(&decoder)
.sources(sources)
.build();
debugger.try_run()?;
} else {
let identifier = if decode_internal {
let sources = if let Some(etherscan_identifier) = etherscan_identifier {
etherscan_identifier.get_compiled_contracts().await?
} else {
Default::default()
};
Some(DebugTraceIdentifier::builder().sources(sources).decoder(&decoder).build())
} else {
None
};
print_traces(&mut result, &decoder, identifier.as_ref()).await?;
print_traces(&mut result, &decoder).await?;
}

Ok(())
}

pub async fn print_traces(
result: &mut TraceResult,
decoder: &CallTraceDecoder,
identifier: Option<&DebugTraceIdentifier>,
) -> Result<()> {
pub async fn print_traces(result: &mut TraceResult, decoder: &CallTraceDecoder) -> Result<()> {
let traces = result.traces.as_ref().expect("No traces found");

println!("Traces:");
for (_, arena) in traces {
let arena = if let Some(identifier) = identifier {
render_trace_arena_with_internals(arena, decoder, &identifier.identify_arena(arena))
.await?
} else {
render_trace_arena(arena, decoder).await?
};
println!("{arena}");
println!("{}", render_trace_arena(arena, decoder).await?);
}
println!();

Expand Down
2 changes: 0 additions & 2 deletions crates/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ extern crate tracing;

mod op;

mod identifier;
mod tui;
pub use identifier::DebugTraceIdentifier;
pub use tui::{Debugger, DebuggerBuilder, ExitReason};
60 changes: 44 additions & 16 deletions crates/debugger/src/tui/builder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
//! TUI debugger builder.

use crate::{identifier::DebugTraceIdentifierBuilder, Debugger};
use foundry_common::evm::Breakpoints;
use crate::Debugger;
use alloy_primitives::Address;
use foundry_common::{evm::Breakpoints, get_contract_name};
use foundry_evm_core::debug::{DebugArena, DebugNodeFlat};
use foundry_evm_traces::{debug::ContractSources, 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<DebugNodeFlat>,
/// Builder for [crate::DebugTraceIdentifier].
identifier: DebugTraceIdentifierBuilder,
/// Identified contracts.
identified_contracts: HashMap<Address, String>,
/// Map of source files.
sources: ContractSources,
/// Map of the debugger breakpoints.
breakpoints: Breakpoints,
}
Expand All @@ -23,16 +28,6 @@ impl DebuggerBuilder {
Self::default()
}

/// Configures the [crate::DebugTraceIdentifier].
#[inline]
pub fn identifier(
mut self,
f: impl FnOnce(DebugTraceIdentifierBuilder) -> DebugTraceIdentifierBuilder,
) -> Self {
self.identifier = f(self.identifier);
self
}

/// Extends the debug arena.
#[inline]
pub fn debug_arenas(mut self, arena: &[DebugArena]) -> Self {
Expand All @@ -49,6 +44,39 @@ impl DebuggerBuilder {
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 {
Expand All @@ -59,7 +87,7 @@ impl DebuggerBuilder {
/// Builds the debugger.
#[inline]
pub fn build(self) -> Debugger {
let Self { debug_arena, identifier, breakpoints } = self;
Debugger::new(debug_arena, identifier.build(), breakpoints)
let Self { debug_arena, identified_contracts, sources, breakpoints } = self;
Debugger::new(debug_arena, identified_contracts, sources, breakpoints)
}
}
14 changes: 11 additions & 3 deletions crates/debugger/src/tui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::context::{BufferKind, DebuggerContext};
use crate::op::OpcodeParam;
use alloy_primitives::U256;
use foundry_compilers::artifacts::sourcemap::SourceElement;
use foundry_evm_traces::identifier::SourceData;
use foundry_evm_traces::debug::SourceData;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
Expand Down Expand Up @@ -342,11 +342,19 @@ impl DebuggerContext<'_> {

/// Returns source map, source code and source name of the current line.
fn src_map(&self) -> Result<(SourceElement, &SourceData), String> {
let Some(contract_name) = self.identified_contracts.get(address) else {
let address = self.address();
let Some(contract_name) = self.debugger.identified_contracts.get(address) else {
return Err(format!("Unknown contract at address {address}"));
};


self.debugger
.contracts_sources
.find_source_mapping(
contract_name,
self.current_step().pc,
self.debug_call().kind.is_any_create(),
)
.ok_or_else(|| format!("No source map for contract {contract_name}"))
}

fn draw_op_list(&self, f: &mut Frame<'_>, area: Rect) {
Expand Down
13 changes: 9 additions & 4 deletions crates/debugger/src/tui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The TUI implementation.

use crate::DebugTraceIdentifier;
use alloy_primitives::Address;
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event},
execute,
Expand All @@ -9,11 +9,13 @@ use crossterm::{
use eyre::Result;
use foundry_common::evm::Breakpoints;
use foundry_evm_core::debug::DebugNodeFlat;
use foundry_evm_traces::debug::ContractSources;
use ratatui::{
backend::{Backend, CrosstermBackend},
Terminal,
};
use std::{
collections::HashMap,
io,
ops::ControlFlow,
sync::{mpsc, Arc},
Expand Down Expand Up @@ -41,7 +43,9 @@ pub enum ExitReason {
/// The TUI debugger.
pub struct Debugger {
debug_arena: Vec<DebugNodeFlat>,
identifier: DebugTraceIdentifier,
identified_contracts: HashMap<Address, String>,
/// Source map of contract sources
contracts_sources: ContractSources,
breakpoints: Breakpoints,
}

Expand All @@ -55,10 +59,11 @@ impl Debugger {
/// Creates a new debugger.
pub fn new(
debug_arena: Vec<DebugNodeFlat>,
identifier: DebugTraceIdentifier,
identified_contracts: HashMap<Address, String>,
contracts_sources: ContractSources,
breakpoints: Breakpoints,
) -> Self {
Self { debug_arena, identifier, breakpoints }
Self { debug_arena, identified_contracts, contracts_sources, breakpoints }
}

/// Starts the debugger TUI. Terminates the current process on failure or user exit.
Expand Down
3 changes: 2 additions & 1 deletion crates/evm/traces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ alloy-primitives = { workspace = true, features = [
alloy-sol-types.workspace = true
revm-inspectors.workspace = true

eyre .workspace = true
eyre.workspace = true
futures.workspace = true
hex.workspace = true
itertools.workspace = true
Expand All @@ -45,6 +45,7 @@ rustc-hash.workspace = true
tempfile.workspace = true
rayon.workspace = true
solang-parser.workspace = true
revm.workspace = true

[dev-dependencies]
tempfile.workspace = true
Loading

0 comments on commit 216e9da

Please sign in to comment.