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

feat: Add creation code printing in traces #202

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.
CodeSandwich marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn write_creation_codes(mut self, yes: bool) -> Self {
self.write_creation_codes = yes;
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
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
"#
CodeSandwich marked this conversation as resolved.
Show resolved Hide resolved
.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
Loading