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(tracing/builder): optimize the trace builder #191

Merged
merged 6 commits into from
Sep 9, 2024
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
39 changes: 20 additions & 19 deletions src/tracing/builder/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,34 @@ impl ParityTraceBuilder {
return (None, None, None);
}

let with_traces = trace_types.contains(&TraceType::Trace);
let with_diff = trace_types.contains(&TraceType::StateDiff);

let vm_trace =
if trace_types.contains(&TraceType::VmTrace) { Some(self.vm_trace()) } else { None };
// early return for StateDiff-only case
if trace_types.len() == 1 && with_diff {
return (None, None, Some(StateDiff::default()));
}

let mut traces = Vec::with_capacity(if with_traces { self.nodes.len() } else { 0 });
// Boolean marker to track if sorting for selfdestruct is needed
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
let mut sorting_selfdestruct = false;
let vm_trace = trace_types.contains(&TraceType::VmTrace).then(|| self.vm_trace());

for node in self.iter_traceable_nodes() {
let trace_address = self.trace_address(node.idx);
let traces = trace_types.contains(&TraceType::Trace).then(|| {
let mut traces = Vec::with_capacity(self.nodes.len());
// Boolean marker to track if sorting for selfdestruct is needed
let mut sorting_selfdestruct = false;

if with_traces {
for node in self.iter_traceable_nodes() {
let trace_address = self.trace_address(node.idx);
let trace = node.parity_transaction_trace(trace_address);
traces.push(trace);

// check if the trace node is a selfdestruct
if node.is_selfdestruct() {
// selfdestructs are not recorded as individual call traces but are derived from
// the call trace and are added as additional `TransactionTrace` objects in the
// trace array
let addr = {
let last = traces.last_mut().expect("exists");
let mut addr = last.trace_address.clone();
let mut addr = Vec::with_capacity(last.trace_address.len() + 1);
addr.extend_from_slice(&last.trace_address);
addr.push(last.subtraces);
// need to account for the additional selfdestruct trace
last.subtraces += 1;
addr
};
Expand All @@ -249,15 +250,15 @@ impl ParityTraceBuilder {
}
}
}
}

// Sort the traces only if a selfdestruct trace was encountered
if sorting_selfdestruct {
traces.sort_by(|a, b| a.trace_address.cmp(&b.trace_address));
}
// Sort the traces only if a selfdestruct trace was encountered
if sorting_selfdestruct {
traces.sort_unstable_by(|a, b| a.trace_address.cmp(&b.trace_address));
}
traces
});

let traces = with_traces.then_some(traces);
let diff = with_diff.then_some(StateDiff::default());
let diff = with_diff.then(StateDiff::default);

(traces, vm_trace, diff)
}
Expand Down
34 changes: 12 additions & 22 deletions src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ impl CallTraceNode {

/// Returns the call context's 4 byte selector
pub fn selector(&self) -> Option<FixedBytes<4>> {
if self.trace.data.len() < 4 {
None
} else {
Some(FixedBytes::from_slice(&self.trace.data[..4]))
}
(self.trace.data.len() >= 4).then(|| FixedBytes::from_slice(&self.trace.data[..4]))
}

/// Returns `true` if this trace was a selfdestruct.
Expand Down Expand Up @@ -321,30 +317,24 @@ impl CallTraceNode {

/// If the trace is a selfdestruct, returns the `Action` for a parity trace.
pub fn parity_selfdestruct_action(&self) -> Option<Action> {
if self.is_selfdestruct() {
Some(Action::Selfdestruct(SelfdestructAction {
self.is_selfdestruct().then(|| {
Action::Selfdestruct(SelfdestructAction {
address: self.trace.address,
refund_address: self.trace.selfdestruct_refund_target.unwrap_or_default(),
balance: self.trace.selfdestruct_transferred_value.unwrap_or_default(),
}))
} else {
None
}
})
})
}

/// If the trace is a selfdestruct, returns the `CallFrame` for a geth call trace
pub fn geth_selfdestruct_call_trace(&self) -> Option<CallFrame> {
if self.is_selfdestruct() {
Some(CallFrame {
typ: "SELFDESTRUCT".to_string(),
from: self.trace.address,
to: self.trace.selfdestruct_refund_target,
value: self.trace.selfdestruct_transferred_value,
..Default::default()
})
} else {
None
}
self.is_selfdestruct().then(|| CallFrame {
typ: "SELFDESTRUCT".to_string(),
from: self.trace.address,
to: self.trace.selfdestruct_refund_target,
value: self.trace.selfdestruct_transferred_value,
..Default::default()
})
}

/// If the trace is a selfdestruct, returns the `TransactionTrace` for a parity trace.
Expand Down
Loading