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

perf(coverage): use u32 for IDs, improve analysis #9763

Merged
merged 5 commits into from
Jan 28, 2025
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ alloy-json-abi = "0.8.18"
alloy-primitives = { version = "0.8.18", features = [
"getrandom",
"rand",
"map-fxhash",
"map-foldhash",
] }
alloy-sol-macro-expander = "0.8.18"
Expand Down
6 changes: 4 additions & 2 deletions crates/common/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Contains various `std::fs` wrapper functions that also contain the target path in their errors
//! Contains various `std::fs` wrapper functions that also contain the target path in their errors.

use crate::errors::FsPathError;
use serde::{de::DeserializeOwned, Serialize};
use std::{
Expand All @@ -7,7 +8,8 @@ use std::{
path::{Component, Path, PathBuf},
};

type Result<T> = std::result::Result<T, FsPathError>;
/// The [`fs`](self) result type.
pub type Result<T> = std::result::Result<T, FsPathError>;

/// Wrapper for [`File::create`].
pub fn create_file(path: impl AsRef<Path>) -> Result<fs::File> {
Expand Down
10 changes: 4 additions & 6 deletions crates/debugger/src/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Debugger implementation.

use crate::{tui::TUI, DebugNode, DebuggerBuilder, ExitReason, FileDumper};
use crate::{tui::TUI, DebugNode, DebuggerBuilder, ExitReason};
use alloy_primitives::map::AddressHashMap;
use eyre::Result;
use foundry_common::evm::Breakpoints;
use foundry_evm_traces::debug::ContractSources;
use std::path::PathBuf;
use std::path::Path;

pub struct DebuggerContext {
pub debug_arena: Vec<DebugNode>,
Expand Down Expand Up @@ -64,10 +64,8 @@ impl Debugger {
}

/// Dumps debugger data to file.
pub fn dump_to_file(&mut self, path: &PathBuf) -> Result<()> {
pub fn dump_to_file(&mut self, path: &Path) -> Result<()> {
eyre::ensure!(!self.context.debug_arena.is_empty(), "debug arena is empty");

let mut file_dumper = FileDumper::new(path, &mut self.context);
file_dumper.run()
crate::dump::dump(path, &self.context)
}
}
148 changes: 148 additions & 0 deletions crates/debugger/src/dump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use crate::{debugger::DebuggerContext, DebugNode};
use alloy_primitives::map::AddressMap;
use foundry_common::fs::write_json_file;
use foundry_compilers::{
artifacts::sourcemap::{Jump, SourceElement},
multi::MultiCompilerLanguage,
};
use foundry_evm_core::utils::PcIcMap;
use foundry_evm_traces::debug::{ArtifactData, ContractSources, SourceData};
use serde::Serialize;
use std::{collections::HashMap, path::Path};

/// Dumps debugger data to a JSON file.
pub(crate) fn dump(path: &Path, context: &DebuggerContext) -> eyre::Result<()> {
write_json_file(path, &DebuggerDump::new(context))?;
Ok(())
}

/// Holds info of debugger dump.
#[derive(Serialize)]
struct DebuggerDump<'a> {
contracts: ContractsDump<'a>,
debug_arena: &'a [DebugNode],
}

impl<'a> DebuggerDump<'a> {
fn new(debugger_context: &'a DebuggerContext) -> Self {
Self {
contracts: ContractsDump::new(debugger_context),
debug_arena: &debugger_context.debug_arena,
}
}
}

#[derive(Serialize)]
struct SourceElementDump {
offset: u32,
length: u32,
index: i32,
jump: u32,
modifier_depth: u32,
}

impl SourceElementDump {
fn new(v: &SourceElement) -> Self {
Self {
offset: v.offset(),
length: v.length(),
index: v.index_i32(),
jump: match v.jump() {
Jump::In => 0,
Jump::Out => 1,
Jump::Regular => 2,
},
modifier_depth: v.modifier_depth(),
}
}
}

#[derive(Serialize)]
struct ContractsDump<'a> {
identified_contracts: &'a AddressMap<String>,
sources: ContractsSourcesDump<'a>,
}

impl<'a> ContractsDump<'a> {
fn new(debugger_context: &'a DebuggerContext) -> Self {
Self {
identified_contracts: &debugger_context.identified_contracts,
sources: ContractsSourcesDump::new(&debugger_context.contracts_sources),
}
}
}

#[derive(Serialize)]
struct ContractsSourcesDump<'a> {
sources_by_id: HashMap<&'a str, HashMap<u32, SourceDataDump<'a>>>,
artifacts_by_name: HashMap<&'a str, Vec<ArtifactDataDump<'a>>>,
}

impl<'a> ContractsSourcesDump<'a> {
fn new(contracts_sources: &'a ContractSources) -> Self {
Self {
sources_by_id: contracts_sources
.sources_by_id
.iter()
.map(|(name, inner_map)| {
(
name.as_str(),
inner_map
.iter()
.map(|(id, source_data)| (*id, SourceDataDump::new(source_data)))
.collect(),
)
})
.collect(),
artifacts_by_name: contracts_sources
.artifacts_by_name
.iter()
.map(|(name, data)| {
(name.as_str(), data.iter().map(ArtifactDataDump::new).collect())
})
.collect(),
}
}
}

#[derive(Serialize)]
struct SourceDataDump<'a> {
source: &'a str,
language: MultiCompilerLanguage,
path: &'a Path,
}

impl<'a> SourceDataDump<'a> {
fn new(v: &'a SourceData) -> Self {
Self { source: &v.source, language: v.language, path: &v.path }
}
}

#[derive(Serialize)]
struct ArtifactDataDump<'a> {
source_map: Option<Vec<SourceElementDump>>,
source_map_runtime: Option<Vec<SourceElementDump>>,
pc_ic_map: Option<&'a PcIcMap>,
pc_ic_map_runtime: Option<&'a PcIcMap>,
build_id: &'a str,
file_id: u32,
}

impl<'a> ArtifactDataDump<'a> {
fn new(v: &'a ArtifactData) -> Self {
Self {
source_map: v
.source_map
.as_ref()
.map(|source_map| source_map.iter().map(SourceElementDump::new).collect()),
source_map_runtime: v
.source_map_runtime
.as_ref()
.map(|source_map| source_map.iter().map(SourceElementDump::new).collect()),
pc_ic_map: v.pc_ic_map.as_ref(),
pc_ic_map_runtime: v.pc_ic_map_runtime.as_ref(),
build_id: &v.build_id,
file_id: v.file_id,
}
}
}
172 changes: 0 additions & 172 deletions crates/debugger/src/file_dumper.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod op;

mod builder;
mod debugger;
mod file_dumper;
mod dump;
mod tui;

mod node;
Expand All @@ -24,5 +24,4 @@ pub use node::DebugNode;

pub use builder::DebuggerBuilder;
pub use debugger::Debugger;
pub use file_dumper::FileDumper;
pub use tui::{ExitReason, TUI};
2 changes: 1 addition & 1 deletion crates/debugger/src/tui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl TUIContext<'_> {
.contracts_sources
.find_source_mapping(
contract_name,
self.current_step().pc,
self.current_step().pc as u32,
self.debug_call().kind.is_any_create(),
)
.ok_or_else(|| format!("No source map for contract {contract_name}"))
Expand Down
Loading
Loading