-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_trace_data.py
216 lines (167 loc) · 6.24 KB
/
parse_trace_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import sys, json, re, heapq
def bulk_copy_bounds_check(start_src, start_dst, consec_count, next_src, next_dst):
if next_src == start_src + consec_count * 32 and next_dst == start_dst + consec_count * 32:
if start_src < start_dst:
if start_src + consec_count * 32 >= start_dst:
return False
else:
return True
else:
if start_dst + consec_count * 32 >= start_src:
return False
else:
return True
else:
return False
def measure_consecutive_copies(call_copies):
# quick-n-diry: just track ascending offsets in (start_offset, start_offset + 32, start_offset + 64, ...)
cur_start = int(call_copies[0]['srcOffset'], 16)
cur_dst = int(call_copies[0]['dstOffset'], 16)
consecutive_counts = []
cur_consecutive = 1
for copy in call_copies[1:]:
if bulk_copy_bounds_check(cur_start, cur_dst, cur_consecutive, int(copy['srcOffset'], 16), int(copy['dstOffset'], 16)):
cur_consecutive += 1
else:
consecutive_counts.append(cur_consecutive)
cur_consecutive = 1
cur_start = int(copy['srcOffset'], 16)
cur_dst = int(copy['dstOffset'], 16)
consecutive_counts.append(cur_consecutive)
return consecutive_counts
# cost for mcopy from EIP (assuming no memory expansion)
def mcopy_cost_model(num_words):
return 3 + 3 * num_words
def estimate_mcopy_savings(non_mcopy_gas, copies) -> int:
COST_PUSH = 3
COST_MLOAD = 3
COST_MSTORE = 3
result = non_mcopy_gas
for copy_size in copies:
result -= (COST_PUSH + COST_MLOAD + COST_PUSH + COST_PUSH + COST_MSTORE) * copy_size
result += COST_PUSH + COST_PUSH + mcopy_cost_model(copy_size)
return result
# traverse the callgraph removing the cost of calling child contracts from parent gasUsed
# we need the aggregate gas spent in each contract (which doesn't include the cost of subcalls, other than cost of CALL itself)
# ---
# modifies tx_trace in-place
def trim_subcall_costs(tx_trace):
if not 'gasUsed' in tx_trace:
return 0
if not 'calls' in tx_trace:
# base case
return int(tx_trace['gasUsed'], 16)
else:
call_gas_used = int(tx_trace['gasUsed'], 16)
original_call_gas_used = call_gas_used
for subcall in tx_trace['calls']:
call_gas_used -= trim_subcall_costs(subcall)
tx_trace['gasUsed'] = hex(call_gas_used)
return call_gas_used
def aggregate_mcopy_savings(tx_trace):
result = []
# ignore calls to EOAs
if not 'gasUsed' in tx_trace:
return []
# ignore copies executed in CREATE
if not "to" in tx_trace:
return []
gas_used = int(tx_trace['gasUsed'], 16)
gas_used_mcopy = gas_used
consec_copies = []
copies = []
if "copies" in tx_trace:
copies = tx_trace['copies']
if len(copies) > 0:
consec_copies = measure_consecutive_copies(copies)
gas_used_mcopy = estimate_mcopy_savings(gas_used, consec_copies)
assert gas_used_mcopy > 0, "shite"
result += [{'account': tx_trace['to'], 'gas_used': gas_used, 'gas_used_mcopy': gas_used_mcopy, 'copies': copies, "consecutive_copies": consec_copies}]
if 'calls' in tx_trace:
for subcall in tx_trace['calls']:
result += aggregate_mcopy_savings(subcall)
return result
# return a map of contract_address: (copy count, gas spent), include reverted calls
# TODO figure out how to make the tracer emit gas spent in each call frame (without including internal calls)
def parse_contract_copies(tx_trace):
trim_subcall_costs(tx_trace)
# TODO sanity check which sums up call costs and makes sure that they sum up to the original "un-trimmed" trace
savings = aggregate_mcopy_savings(tx_trace)
return savings
def main():
trace_file = sys.argv[1]
trace_lines = None
with open(trace_file) as f:
trace_lines = f.readlines()
cur_tx = None
cur_block = None
consecutive_copies = {}
aggregate_savings = {}
result = {}
copy_size_occurances = {}
total_gas_used = 0
i = 0
tx = None
while i < len(trace_lines):
line = trace_lines[i]
if line.startswith('tx: '):
tx = line[4:]
# print(tx)
if i + 1 >= len(trace_lines):
break
elif trace_lines[i + 1] == 'error':
i += 1
elif trace_lines[i + 1].startswith("{"):
trace_result = json.loads(trace_lines[i + 1].replace("'", '"').replace('None', '"None"'))
if 'gasUsed' in trace_result:
total_gas_used += int(trace_result['gasUsed'], 16)
copies = parse_contract_copies(trace_result)
for acct in copies:
consecutive_copies = acct['consecutive_copies']
if len(consecutive_copies) == 0:
consecutive_copies = [0]
for copy_size in consecutive_copies:
if copy_size == 0:
continue
if copy_size in copy_size_occurances:
copy_size_occurances[copy_size] += 1
else:
copy_size_occurances[copy_size] = 1
if acct['account'] in result:
entry = result[acct['account']]
entry['gas_used'] += acct['gas_used']
entry['gas_used_mcopy'] += acct['gas_used_mcopy']
entry['max_consecutive_copies'] = max(entry['max_consecutive_copies'], max(consecutive_copies))
else:
result[acct['account']] = {
'gas_used': acct['gas_used'],
'gas_used_mcopy': acct['gas_used_mcopy'],
'max_consecutive_copies': max(consecutive_copies)
}
i += 1
i += 1
max_consecutive = 0
max_consecutive_acct = None
for acct, value in result.items():
if value['max_consecutive_copies'] > max_consecutive:
max_consecutive_acct = acct
max_consecutive = value['max_consecutive_copies']
ENTRY_NUM=5
# store data for graphs to csvs:
with open('data/guzzlers.csv', 'w') as f:
# pct savings for contracts with most gas usage
max_savings = sorted(result.items(), key=lambda x: x[1]['gas_used'])
f.write("account,\"aggregate percentage savings for most popular contracts with mcopy\"\n")
for i in range(ENTRY_NUM):
entry = max_savings[len(max_savings) - i - 1]
pct_savings = 100 * ((entry[1]['gas_used'] - entry[1]['gas_used_mcopy']) / entry[1]['gas_used'])
f.write("{},{}\n".format(max_savings[len(max_savings) - i - 1][0], pct_savings))
with open('data/copy-size-distribution.csv', 'w') as f:
f.write("\"copy size (32 bytes)\",\"number of occurances\"\n")
for copy_size, num in sorted(copy_size_occurances.items(), key=lambda x: x[0]):
f.write("{},{}\n".format(copy_size, num))
# MCOPY savings for most popular contracts
# not substantial enough to consider
# savings_popular = sorted(result.items(), key=lambda x: x[1]['gas_used'])
if __name__ == "__main__":
main()