Skip to content

Commit 16c7ddd

Browse files
committed
first pass
1 parent 3a01a97 commit 16c7ddd

File tree

9 files changed

+344
-24
lines changed

9 files changed

+344
-24
lines changed

Cargo.lock

Lines changed: 59 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/chisel/src/dispatcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ impl ChiselDispatcher {
935935
for (kind, trace) in &result.traces {
936936
// Display all Setup + Execution traces.
937937
if matches!(kind, TraceKind::Setup | TraceKind::Execution) {
938-
println!("{}", render_trace_arena(trace, decoder).await?);
938+
println!("{}", render_trace_arena(trace, decoder).await?.0);
939939
}
940940
}
941941

crates/cli/src/utils/cmd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ pub async fn print_traces(result: &mut TraceResult, decoder: &CallTraceDecoder)
412412

413413
println!("Traces:");
414414
for (_, arena) in traces {
415-
println!("{}", render_trace_arena(arena, decoder).await?);
415+
println!("{}", render_trace_arena(arena, decoder).await?.0);
416416
}
417417
println!();
418418

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#[derive(Debug, Clone, Default)]
2+
pub struct FoldedStackTrace {
3+
traces: Vec<(Vec<String>, i64)>,
4+
exits: Option<u64>,
5+
}
6+
7+
impl FoldedStackTrace {
8+
pub fn enter(&mut self, label: String, gas: i64) {
9+
let mut trace_entry = self.traces.last().map(|entry| entry.0.clone()).unwrap_or_default();
10+
11+
let mut exits = self.exits.unwrap_or_default();
12+
while exits > 0 {
13+
trace_entry.pop();
14+
exits -= 1;
15+
}
16+
self.exits = None;
17+
18+
trace_entry.push(label);
19+
self.traces.push((trace_entry, gas));
20+
}
21+
22+
pub fn exit(&mut self) {
23+
self.exits = self.exits.map(|exits| exits + 1).or(Some(1));
24+
}
25+
26+
pub fn fold(mut self) -> Vec<String> {
27+
self.subtract_children();
28+
self.fold_without_subtraction()
29+
}
30+
31+
pub fn fold_without_subtraction(&mut self) -> Vec<String> {
32+
let mut lines = Vec::new();
33+
for (trace, gas) in self.traces.iter() {
34+
lines.push(format!("{} {}", trace.join(";"), gas));
35+
}
36+
lines
37+
}
38+
39+
pub fn subtract_children(&mut self) {
40+
// Iterate over each trace to find the children and subtract their values from the parents
41+
for i in 0..self.traces.len() {
42+
let (left, right) = self.traces.split_at_mut(i);
43+
let (trace, gas) = &right[0];
44+
if trace.len() > 1 {
45+
let parent_trace_to_match = &trace[..trace.len() - 1];
46+
for parent in left {
47+
if parent.0 == parent_trace_to_match {
48+
parent.1 -= gas;
49+
break;
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
mod tests {
58+
#[test]
59+
fn test_insert_1() {
60+
let mut trace = super::FoldedStackTrace::default();
61+
trace.enter("top".to_string(), 500);
62+
trace.enter("child_a".to_string(), 100);
63+
trace.exit();
64+
trace.enter("child_b".to_string(), 200);
65+
66+
assert_eq!(
67+
trace.fold_without_subtraction(),
68+
vec![
69+
"top 500", //
70+
"top;child_a 100",
71+
"top;child_b 200",
72+
]
73+
);
74+
assert_eq!(
75+
trace.fold(),
76+
vec![
77+
"top 200", // 500 - 100 - 200
78+
"top;child_a 100",
79+
"top;child_b 200",
80+
]
81+
);
82+
}
83+
84+
#[test]
85+
fn test_insert_2() {
86+
let mut trace = super::FoldedStackTrace::default();
87+
trace.enter("top".to_string(), 500);
88+
trace.enter("child_a".to_string(), 300);
89+
trace.enter("child_b".to_string(), 100);
90+
trace.exit();
91+
trace.exit();
92+
trace.enter("child_c".to_string(), 100);
93+
94+
assert_eq!(
95+
trace.fold_without_subtraction(),
96+
vec![
97+
"top 500", //
98+
"top;child_a 300",
99+
"top;child_a;child_b 100",
100+
"top;child_c 100",
101+
]
102+
);
103+
104+
assert_eq!(
105+
trace.fold(),
106+
vec![
107+
"top 100", // 500 - 300 - 100
108+
"top;child_a 200", // 300 - 100
109+
"top;child_a;child_b 100",
110+
"top;child_c 100",
111+
]
112+
);
113+
}
114+
115+
#[test]
116+
fn test_insert_3() {
117+
let mut trace = super::FoldedStackTrace::default();
118+
trace.enter("top".to_string(), 1700);
119+
trace.enter("child_a".to_string(), 500);
120+
trace.exit();
121+
trace.enter("child_b".to_string(), 500);
122+
trace.enter("child_c".to_string(), 500);
123+
trace.exit();
124+
trace.exit();
125+
trace.exit();
126+
trace.enter("top2".to_string(), 1700);
127+
128+
assert_eq!(
129+
trace.fold_without_subtraction(),
130+
vec![
131+
"top 1700", //
132+
"top;child_a 500",
133+
"top;child_b 500",
134+
"top;child_b;child_c 500",
135+
"top2 1700",
136+
]
137+
);
138+
139+
assert_eq!(
140+
trace.fold(),
141+
vec![
142+
"top 700", //
143+
"top;child_a 500",
144+
"top;child_b 0",
145+
"top;child_b;child_c 500",
146+
"top2 1700",
147+
]
148+
);
149+
}
150+
}

0 commit comments

Comments
 (0)