From 61083efecbc90fb741afbdd7225d101f2e45856f Mon Sep 17 00:00:00 2001 From: grandizzy Date: Wed, 23 Oct 2024 14:16:04 +0300 Subject: [PATCH] chore: refactor debugger code --- crates/debugger/src/context.rs | 12 --- crates/debugger/src/debugger.rs | 12 ++- .../{file_dumper/mod.rs => file_dumper.rs} | 95 ++++++++++--------- crates/debugger/src/lib.rs | 2 +- crates/debugger/src/tui/context.rs | 2 +- crates/debugger/src/tui/mod.rs | 2 +- 6 files changed, 61 insertions(+), 64 deletions(-) delete mode 100644 crates/debugger/src/context.rs rename crates/debugger/src/{file_dumper/mod.rs => file_dumper.rs} (94%) diff --git a/crates/debugger/src/context.rs b/crates/debugger/src/context.rs deleted file mode 100644 index 1d89b549e82e..000000000000 --- a/crates/debugger/src/context.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::DebugNode; -use alloy_primitives::map::AddressHashMap; -use foundry_common::evm::Breakpoints; -use foundry_evm_traces::debug::ContractSources; - -pub struct DebuggerContext { - pub debug_arena: Vec, - pub identified_contracts: AddressHashMap, - /// Source map of contract sources - pub contracts_sources: ContractSources, - pub breakpoints: Breakpoints, -} diff --git a/crates/debugger/src/debugger.rs b/crates/debugger/src/debugger.rs index 723a4cb85f4f..0e4de437c844 100644 --- a/crates/debugger/src/debugger.rs +++ b/crates/debugger/src/debugger.rs @@ -1,14 +1,20 @@ //! Debugger implementation. -use crate::{ - context::DebuggerContext, tui::TUI, DebugNode, DebuggerBuilder, ExitReason, FileDumper, -}; +use crate::{tui::TUI, DebugNode, DebuggerBuilder, ExitReason, FileDumper}; use alloy_primitives::map::AddressHashMap; use eyre::Result; use foundry_common::evm::Breakpoints; use foundry_evm_traces::debug::ContractSources; use std::path::PathBuf; +pub struct DebuggerContext { + pub debug_arena: Vec, + pub identified_contracts: AddressHashMap, + /// Source map of contract sources + pub contracts_sources: ContractSources, + pub breakpoints: Breakpoints, +} + pub struct Debugger { context: DebuggerContext, } diff --git a/crates/debugger/src/file_dumper/mod.rs b/crates/debugger/src/file_dumper.rs similarity index 94% rename from crates/debugger/src/file_dumper/mod.rs rename to crates/debugger/src/file_dumper.rs index 969ad882f593..909530c42159 100644 --- a/crates/debugger/src/file_dumper/mod.rs +++ b/crates/debugger/src/file_dumper.rs @@ -1,6 +1,6 @@ -//! The file dumper implementation +//! The debug file dumper implementation. -use crate::{context::DebuggerContext, DebugNode}; +use crate::{debugger::DebuggerContext, DebugNode}; use alloy_primitives::Address; use eyre::Result; use foundry_common::fs::write_json_file; @@ -12,9 +12,11 @@ use foundry_evm_traces::debug::{ArtifactData, ContractSources, SourceData}; use serde::Serialize; use std::{collections::HashMap, ops::Deref, path::PathBuf}; -/// The file dumper +/// Generates and writes debugger dump in a json file. pub struct FileDumper<'a> { + /// Path to json file to write dump into. path: &'a PathBuf, + /// Debugger context to generate dump for. debugger_context: &'a mut DebuggerContext, } @@ -30,6 +32,13 @@ impl<'a> FileDumper<'a> { } } +/// Holds info of debugger dump. +#[derive(Serialize)] +struct DebuggerDump { + contracts: ContractsDump, + debug_arena: Vec, +} + impl DebuggerDump { fn from(debugger_context: &DebuggerContext) -> Self { Self { @@ -39,12 +48,6 @@ impl DebuggerDump { } } -#[derive(Serialize)] -struct DebuggerDump { - contracts: ContractsDump, - debug_arena: Vec, -} - #[derive(Serialize)] pub struct SourceElementDump { offset: u32, @@ -54,6 +57,22 @@ pub struct SourceElementDump { modifier_depth: u32, } +impl SourceElementDump { + pub 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 { // Map of call address to contract name @@ -61,29 +80,6 @@ struct ContractsDump { sources: ContractsSourcesDump, } -#[derive(Serialize)] -struct ContractsSourcesDump { - sources_by_id: HashMap>, - artifacts_by_name: HashMap>, -} - -#[derive(Serialize)] -struct SourceDataDump { - source: String, - language: MultiCompilerLanguage, - path: PathBuf, -} - -#[derive(Serialize)] -struct ArtifactDataDump { - pub source_map: Option>, - pub source_map_runtime: Option>, - pub pc_ic_map: Option>, - pub pc_ic_map_runtime: Option>, - pub build_id: String, - pub file_id: u32, -} - impl ContractsDump { pub fn new(debugger_context: &DebuggerContext) -> Self { Self { @@ -97,6 +93,12 @@ impl ContractsDump { } } +#[derive(Serialize)] +struct ContractsSourcesDump { + sources_by_id: HashMap>, + artifacts_by_name: HashMap>, +} + impl ContractsSourcesDump { pub fn new(contracts_sources: &ContractSources) -> Self { Self { @@ -124,26 +126,27 @@ impl ContractsSourcesDump { } } +#[derive(Serialize)] +struct SourceDataDump { + source: String, + language: MultiCompilerLanguage, + path: PathBuf, +} + impl SourceDataDump { pub fn new(v: &SourceData) -> Self { Self { source: v.source.deref().clone(), language: v.language, path: v.path.clone() } } } -impl SourceElementDump { - pub 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 ArtifactDataDump { + pub source_map: Option>, + pub source_map_runtime: Option>, + pub pc_ic_map: Option>, + pub pc_ic_map_runtime: Option>, + pub build_id: String, + pub file_id: u32, } impl ArtifactDataDump { diff --git a/crates/debugger/src/lib.rs b/crates/debugger/src/lib.rs index db8476e4056e..aa561973cc63 100644 --- a/crates/debugger/src/lib.rs +++ b/crates/debugger/src/lib.rs @@ -11,12 +11,12 @@ extern crate tracing; mod op; mod builder; -mod context; mod debugger; mod file_dumper; mod tui; mod node; + pub use node::DebugNode; pub use builder::DebuggerBuilder; diff --git a/crates/debugger/src/tui/context.rs b/crates/debugger/src/tui/context.rs index 80e16046c212..0c61a1fcd41c 100644 --- a/crates/debugger/src/tui/context.rs +++ b/crates/debugger/src/tui/context.rs @@ -1,6 +1,6 @@ //! Debugger context and event handler implementation. -use crate::{context::DebuggerContext, DebugNode, ExitReason}; +use crate::{debugger::DebuggerContext, DebugNode, ExitReason}; use alloy_primitives::{hex, Address}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}; use foundry_evm_core::buffer::BufferKind; diff --git a/crates/debugger/src/tui/mod.rs b/crates/debugger/src/tui/mod.rs index 69ec0c201a06..abc7c4a9706e 100644 --- a/crates/debugger/src/tui/mod.rs +++ b/crates/debugger/src/tui/mod.rs @@ -19,7 +19,7 @@ use std::{ }; mod context; -use crate::context::DebuggerContext; +use crate::debugger::DebuggerContext; use context::TUIContext; mod draw;