Skip to content

Commit

Permalink
feat: Add creation code printing in traces
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSandwich committed Sep 20, 2024
1 parent 46505ca commit 09afce4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/tracing/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct TraceWriter<W> {
use_colors: bool,
color_cheatcodes: bool,
indentation_level: u16,
write_creation_codes: bool,
}

impl<W: Write> TraceWriter<W> {
Expand All @@ -41,6 +42,7 @@ impl<W: Write> TraceWriter<W> {
use_colors: use_colors(ColorChoice::global()),
color_cheatcodes: false,
indentation_level: 0,
write_creation_codes: false,
}
}

Expand All @@ -65,6 +67,13 @@ impl<W: Write> TraceWriter<W> {
self
}

/// Sets the starting indentation level.
#[inline]
pub fn write_creation_codes(mut self, yes: bool) -> Self {
self.write_creation_codes = yes;
self
}

/// Returns a reference to the inner writer.
#[inline]
pub const fn writer(&self) -> &W {
Expand Down Expand Up @@ -175,6 +184,9 @@ impl<W: Write> TraceWriter<W> {
"{trace_kind_style}{CALL}new{trace_kind_style:#} {label}@{address}",
label = trace.decoded.label.as_deref().unwrap_or("<unknown>")
)?;
if self.write_creation_codes {
write!(self.writer, "({})", hex::encode(&trace.data))?;
}
} else {
let (func_name, inputs) = match &trace.decoded.call_data {
Some(DecodedCallData { signature, args }) => {
Expand Down
18 changes: 14 additions & 4 deletions tests/it/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,26 @@ where
}

pub fn write_traces(tracer: &TracingInspector) -> String {
write_traces_with(tracer, ColorChoice::Never)
write_traces_with(tracer, ColorChoice::Never, false)
}

pub fn write_traces_with(tracer: &TracingInspector, color: ColorChoice) -> String {
let mut w = revm_inspectors::tracing::TraceWriter::new(Vec::<u8>::new()).use_colors(color);
pub fn write_traces_with_creation_codes(tracer: &TracingInspector) -> String {
write_traces_with(tracer, ColorChoice::Never, true)
}

pub fn write_traces_with(
tracer: &TracingInspector,
color: ColorChoice,
write_creation_codes: bool,
) -> String {
let mut w = revm_inspectors::tracing::TraceWriter::new(Vec::<u8>::new())
.use_colors(color)
.write_creation_codes(write_creation_codes);
w.write_arena(tracer.traces()).expect("failed to write traces to Vec<u8>");
String::from_utf8(w.into_writer()).expect("trace writer wrote invalid UTF-8")
}

pub fn print_traces(tracer: &TracingInspector) {
// Use `println!` so that the output is captured by the test runner.
println!("{}", write_traces_with(tracer, ColorChoice::Auto));
println!("{}", write_traces_with(tracer, ColorChoice::Auto, false));
}
12 changes: 11 additions & 1 deletion tests/it/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{write_traces, TestEvm};
use crate::utils::{write_traces, write_traces_with_creation_codes, TestEvm};
use alloy_primitives::{address, b256, bytes, hex, Address, Bytes, B256, U256};
use alloy_sol_types::{sol, SolCall};
use revm_inspectors::tracing::{
Expand Down Expand Up @@ -28,6 +28,16 @@ fn test_trace_printing() {
"#]]
);

let s = write_traces_with_creation_codes(&tracer);
let raw = r#"
[209257] → new <unknown>@0xBd770416a3345F91E4B34576cb804a576fa48EB1(<BYTECODE>)
└─ ← [Return] 1045 bytes of code
"#
.strip_prefix("\n")
.unwrap()
.replace("<BYTECODE>", BYTECODE.to_string().strip_prefix("0x").unwrap());
assert_data_eq!(s, raw);

let mut index = 0;

let mut call = |data: Vec<u8>, raw: Inline, decoded: Inline| {
Expand Down

0 comments on commit 09afce4

Please sign in to comment.