-
Notifications
You must be signed in to change notification settings - Fork 459
/
supplement.py
358 lines (270 loc) · 8.87 KB
/
supplement.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import sqlite3
import json
from zipfile import ZipFile
import urllib.request
import sys
import time
import os
from .helpers import opcode, padded_hex, cached
from .helpers import (
COLOR_HEADER,
COLOR_BLUE,
COLOR_OKGREEN,
COLOR_WARNING,
FAIL,
ENDC,
COLOR_BOLD,
COLOR_UNDERLINE,
COLOR_GREEN,
COLOR_GRAY,
)
"""
a module for management of bytes4 signatures from the database
db schema:
hash - 0x12345678
name - transferFrom
folded_name - transferFrom(address,address,uint256)
cooccurs - comma-dellimeted list of hashes: `0x12312312,0xabababab...`
params - json: `[
{
"type": "address",
"name": "_from"
},
{
"type": "address",
"name": "_to"
},
{
"type": "uint256",
"name": "_value"
}
]`
"""
conn = None
def check_supplements():
if not os.path.isfile("supplement.db"):
print(COLOR_OKGREEN + "Hello, is this your first Panoramix run?" + ENDC)
print()
print("I need to fetch the signatures database from the web")
print("This will be done just once.")
print()
fetch_db()
assert os.path.isfile("supplement.db")
if not os.path.isfile("supp2.db"):
print("creating supp2.db...")
c = sqlite3.connect("supplement.db").cursor()
d_conn = sqlite3.connect("supp2.db")
d = d_conn.cursor()
d.execute(
"create table functions (hash integer, name text, folded_name text, params text, primary KEY(hash))"
)
c.execute("SELECT hash, name, folded_name, params from functions group by hash")
results = c.fetchall()
for r in results:
insert_row = int(r[0], 16), r[1], r[2], r[3]
d.execute("INSERT INTO functions VALUES (?, ?, ?, ?)", insert_row)
d_conn.commit()
d_conn.close()
print("done.")
assert os.path.isfile("supp2.db")
def _cursor():
global conn
check_supplements()
if conn is None:
conn = sqlite3.connect("supplement.db")
try:
c = conn.cursor()
except:
# fails in multi-threading, this should help
conn = sqlite3.connect("supplement.db")
return conn.cursor()
return c
conn2 = None
def _cursor2():
global conn2
check_supplements()
if conn2 is None:
conn2 = sqlite3.connect("supp2.db")
try:
c = conn2.cursor()
except:
# fails in multi-threading, this should help
conn2 = sqlite3.connect("supp2.db")
return conn2.cursor()
return c
@cached
def fetch_sigs(hash):
c = _cursor()
c.execute("SELECT * from functions where hash=?", (hash,))
results = c.fetchall()
res = []
for row in results:
res.append(
{
"hash": row[0],
"name": row[1],
"folded_name": row[2],
"params": json.loads(row[3]),
"cooccurs": row[4].split(","),
}
)
return res
@cached
def fetch_sig(hash):
if type(hash) == str:
hash = int(hash, 16)
c = _cursor2()
c.execute(
f"SELECT hash, name, folded_name, params from functions where hash={hash}"
)
results = c.fetchall()
if len(results) == 0:
return None
row = results[0]
return {
"hash": padded_hex(row[0], 8),
"name": row[1],
"folded_name": row[2],
"params": json.loads(row[3]),
}
def fetch_db():
def reporthook(count, block_size, total_size):
# https://blog.shichao.io/2012/10/04/progress_speed_indicator_for_urlretrieve_in_python.html
global start_time
if count == 0:
start_time = time.time()
return
duration = time.time() - start_time
progress_size = int(count * block_size)
speed = int(progress_size / (1024 * duration))
percent = min(int((count * block_size * 100) / total_size), 100)
sys.stdout.write(
"\r%d of 27 MB, %d KB/s, %d seconds passed..."
% (progress_size / (1024 * 1024), speed, duration)
)
sys.stdout.flush()
print("fetching supplement.zip...")
url = "http://eveem.org/static/supplement.zip"
urllib.request.urlretrieve(url, "tmp.supplement.db.zip", reporthook)
print("")
print("unzipping into supplement.db...")
with ZipFile("tmp.supplement.db.zip") as myzip:
with myzip.open("supplement.db") as myfile:
with open("supplement.db", "wb") as file:
file.write(myfile.read())
os.remove("tmp.supplement.db.zip")
print("done.")
"""
Abi crawler and parser. used to refill supplement.py with new ABI/func definitions.
It's used by scripts that are not a part of panoramix repo.
The function is here, so people wanting to parse ABIs on their own can use parse_insert_abi
implementation as a reference. It handles some unobvious edge-cases, like arrays of tuples.
"""
def crawl_abis_from_cache():
# imports here, because this is not used as a part of a regular panoramix run,
# and we don't want to import stuff unnecessarily.
import json
import os
import urllib
import urllib.request
import time
import re
import sqlite3
import sys
try:
from web3 import Web3
except:
print(
"install web3:\n\t`pip install web3`"
) # the only dependency in the project :D
conn = sqlite3.connect("supplement.db")
cursor = conn.cursor()
conn2 = sqlite3.connect("supp2.db")
cursor2 = conn2.cursor()
def parse_insert_abi(abi):
def parse_inputs(func_inputs):
inputs = []
params = []
param_counter = 0
for r in func_inputs:
param_counter += 1
type_ = r["type"]
name_ = r["name"]
if len(name_) == 0:
name_ = "param" + str(param_counter)
if name_[0] != "_":
name_ = "_" + name_
params.append({"type": r["type"], "name": name_})
if "tuple" not in type_:
inputs.append(type_)
else:
type_ = f"({parse_inputs(r['components'])[0]})" + type_[5:]
inputs.append(type_)
return ",".join(inputs), params
output = {}
for func in abi:
if func["type"] in ["constructor", "fallback"]:
continue
inputs, params = parse_inputs(func["inputs"])
fname = f"{func['name']}({inputs})"
sha3 = Web3.sha3(text=fname).hex()[:10]
if sha3 in output:
print("double declaration for the same hash! {}".format(fname))
continue
output[sha3] = {
"name": func["name"],
"folded_name": fname,
"params": params,
}
for sha3, row in output.items():
row["cooccurs"] = list(output.keys())
insert_row = (
sha3,
row["name"],
row["folded_name"],
json.dumps(row["params"]),
",".join(row["cooccurs"]),
)
insert_row2 = (
int(sha3, 16),
row["name"],
row["folded_name"],
json.dumps(row["params"]),
)
test_hash, test_cooccurs = insert_row[0], insert_row[4]
cursor.execute(
"SELECT * from functions where hash=? and cooccurs=?",
(test_hash, test_cooccurs),
)
results = cursor.fetchall()
if len(results) == 0:
print("inserting", sha3, row["folded_name"])
cursor.execute(
"INSERT INTO functions VALUES (?, ?, ?, ?, ?)", insert_row
)
conn.commit()
cursor2.execute("SELECT * from functions where hash=?", (insert_row2[0],))
results = cursor2.fetchall()
if len(results) == 0:
print("inserting2", sha3, row["folded_name"])
cursor2.execute(
"INSERT INTO functions VALUES (?, ?, ?, ?)", insert_row2
)
conn2.commit()
def crawl_cache():
idx = 0
path = "./cache_abis/"
if not os.path.isdir(path):
print(
"dir cache_abis doesn't exist. it should be there and it should contain abi files"
)
return
for fname in os.listdir(path):
address = fname[:-4]
fname = path + fname
idx += 1
print(idx, address)
with open(fname) as f:
abi = json.loads(f.read())
parse_insert_abi(abi)
crawl_cache()