Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/evm/traces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ pub fn render_trace_arena(arena: &SparsedTraceArena) -> String {
render_trace_arena_inner(arena, false, false)
}

/// Prunes trace depth if depth is provided as an argument
pub fn prune_trace_depth(arena: &mut CallTraceArena, depth: usize) {
for node in arena.nodes_mut() {
if node.trace.depth >= depth {
node.ordering.clear();
}
}
}

/// Render a collection of call traces to a string optionally including contract creation bytecodes
/// and in JSON format.
pub fn render_trace_arena_inner(
Expand Down
17 changes: 16 additions & 1 deletion crates/forge/src/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use foundry_config::{
use foundry_debugger::Debugger;
use foundry_evm::{
opts::EvmOpts,
traces::{backtrace::BacktraceBuilder, identifier::TraceIdentifiers},
traces::{backtrace::BacktraceBuilder, identifier::TraceIdentifiers, prune_trace_depth},
};
use regex::Regex;
use std::{
Expand Down Expand Up @@ -136,6 +136,10 @@ pub struct TestArgs {
#[arg(long, short, env = "FORGE_SUPPRESS_SUCCESSFUL_TRACES", help_heading = "Display options")]
suppress_successful_traces: bool,

/// Defines the depth of a trace
#[arg(long)]
trace_depth: Option<usize>,

/// Output test results as JUnit XML report.
#[arg(long, conflicts_with_all = ["quiet", "json", "gas_report", "summary", "list", "show_progress"], help_heading = "Display options")]
pub junit: bool,
Expand Down Expand Up @@ -652,6 +656,11 @@ impl TestArgs {

if should_include {
decode_trace_arena(arena, &decoder).await;

if let Some(trace_depth) = self.trace_depth {
prune_trace_depth(arena, trace_depth);
}

decoded_traces.push(render_trace_arena_inner(arena, false, verbosity > 4));
}
}
Expand Down Expand Up @@ -1037,6 +1046,12 @@ mod tests {
assert!(args.fuzz_seed.is_some());
}

#[test]
fn depth_trace() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

please apply the new option in similar test as with test_trace test here

[..] TraceTest::testRecurseCall()
├─ [..] Node 0::recurseCall(8, 0)
│ ├─ [..] Node 0::recurseCall(8, 1)
│ │ ├─ [..] Node 0::recurseCall(8, 2)
│ │ │ ├─ [..] Node 0::recurseCall(8, 3)
│ │ │ │ ├─ [..] Node 0::recurseCall(8, 4)
│ │ │ │ │ ├─ [..] Node 0::recurseCall(8, 5)
│ │ │ │ │ │ ├─ [..] Node 0::recurseCall(8, 6)
│ │ │ │ │ │ │ ├─ [..] Node 0::recurseCall(8, 7)
│ │ │ │ │ │ │ │ ├─ [..] Node 0::recurseCall(8, 8)
│ │ │ │ │ │ │ │ │ ├─ [..] Node 0::negativeNum() [staticcall]
│ │ │ │ │ │ │ │ │ │ └─ ← [Return] -1000000000 [-1e9]

let args: TestArgs = TestArgs::parse_from(["foundry-cli", "--trace-depth", "2"]);
assert!(args.trace_depth.is_some());
}

// <https://github.com/foundry-rs/foundry/issues/5913>
#[test]
fn fuzz_seed_exists() {
Expand Down