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

chore: Enhance Call Tracer Performance and Code Reliability #3125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 29 additions & 25 deletions client/evm-tracing/src/formatters/call_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ impl super::ResponseFormatter for Formatter {
type Response = Vec<BlockTransactionTrace>;

fn format(mut listener: Listener) -> Option<Vec<BlockTransactionTrace>> {
// Remove empty BTreeMaps pushed to `entries`.
// I.e. InvalidNonce or other pallet_evm::runner exits
// Skip empty transactions early to avoid unnecessary processing
if listener.entries.is_empty() {
return None;
}

// Remove empty BTreeMaps
listener.entries.retain(|x| !x.is_empty());
let mut traces = Vec::new();

let mut traces = Vec::with_capacity(listener.entries.len());
for (eth_tx_index, entry) in listener.entries.iter().enumerate() {
let mut result: Vec<Call> = entry
.into_iter()
Expand Down Expand Up @@ -169,28 +174,8 @@ impl super::ResponseFormatter for Formatter {
trace_address: Some(b),
..
}),
) => {
let a_len = a.len();
let b_len = b.len();
let sibling_greater_than = |a: &Vec<u32>, b: &Vec<u32>| -> bool {
for (i, a_value) in a.iter().enumerate() {
if a_value > &b[i] {
return true;
} else if a_value < &b[i] {
return false;
} else {
continue;
}
}
return false;
};
if b_len > a_len || (a_len == b_len && sibling_greater_than(&a, &b)) {
Ordering::Less
} else {
Ordering::Greater
}
}
_ => unreachable!(),
) => compare_trace_address(a, b),
_ => unreachable!("All calls should have trace addresses"),
});
// Stack pop-and-push.
while result.len() > 1 {
Expand Down Expand Up @@ -279,6 +264,7 @@ pub struct CallTracerCall {
pub inner: CallTracerInner,

#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Vec::is_empty")]

pub calls: Vec<Call>,
}

Expand All @@ -296,6 +282,7 @@ pub enum CallTracerInner {
res: CallResult,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]

value: Option<U256>,

#[serde(skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -327,3 +314,20 @@ pub enum CallTracerInner {
value: U256,
},
}

fn compare_trace_address(a: &[u32], b: &[u32]) -> Ordering {
let a_len = a.len();
let b_len = b.len();

if b_len != a_len {
return a_len.cmp(&b_len);
}

for (a_value, b_value) in a.iter().zip(b.iter()) {
match a_value.cmp(b_value) {
Ordering::Equal => continue,
other => return other,
}
}
Ordering::Equal
}