-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.py
More file actions
348 lines (276 loc) · 10.4 KB
/
Copy pathhandlers.py
File metadata and controls
348 lines (276 loc) · 10.4 KB
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
import json
import logging
import traceback
from typing import List
from eth_abi import decode_abi
from eth_abi.exceptions import InsufficientDataBytes
from eth_utils.abi import (_abi_to_signature, event_abi_to_log_topic,
function_abi_to_4byte_selector)
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
HEADERS = {"Content-Type": "application/json"}
def to_signature_handler(event: dict, context: dict) -> dict:
"""Takes in Snowflake data and calculates an event/method signature"""
try:
event_data = _parse_event_for_data(event)
logger.info(f"event data: {event_data}")
for row in event_data:
row[1] = to_signature(row[1])
return {
"statusCode": 200,
"headers": HEADERS,
"body": json.dumps({"data": event_data})
}
except Exception as e:
logger.error(e, exc_info=True)
logger.error(f"event data: {event_data}")
return {
"statusCode": 500,
"headers": HEADERS,
"body": json.dumps(format_exception(e, event_data))
}
def to_signature_hash_handler(event: dict, context: dict) -> dict:
"""Takes in Snowflake data and calculates an event/method signature hash"""
try:
event_data = _parse_event_for_data(event)
logger.info(f"event data: {event_data}")
for row in event_data:
row[1] = to_signature_hash(row[1])
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"data": event_data})
}
except Exception as e:
logger.error(e, exc_info=True)
logger.error(f"event data: {event_data}")
return {
"statusCode": 500,
"headers": HEADERS,
"body": json.dumps(format_exception(e, event_data))
}
def decode_contract_event_handler(event: dict, context: dict) -> dict:
"""Takes in Snowflake data and decodes contract events"""
event_data = _parse_event_for_data(event)
logger.info(f"event data: {event_data}")
for row in event_data:
try:
row[1] = decode_contract_event(row[1], row[2], row[3])
except Exception as e:
logger.error(e, exc_info=True)
logger.error(f"event data: {row[1]}")
row[1] = {
"error": "could not decode row",
"abi": row[1],
"topics": row[2],
"data": row[3]}
row.pop(3)
row.pop(2)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"data": event_data}, default=_default)
}
def decode_contract_function_handler(event: dict, context: dict) -> dict:
"""Takes in Snowflake data and decodes contract functions"""
event_data = _parse_event_for_data(event)
logger.info(f"event data: {event_data}")
for row in event_data:
try:
row[1] = decode_contract_function(row[1], row[2], row[3])
except Exception as e:
logger.error(e, exc_info=True)
logger.error(f"event data: {row[1]}")
row[1] = {
"error": "could not decode row",
"abi": row[1],
"input": row[2],
"output": row[3]}
row.pop(3)
row.pop(2)
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"data": event_data}, default=_default)
}
def to_signature(abi: dict) -> str:
"""Takes ABI definition and returns an event/method signature
(i.e. Transfer(address,address,value))"""
return _abi_to_signature(abi)
def to_signature_hash(abi: dict) -> str:
"""Takes ABI definition and computes the SHA-3 hash"""
type = abi["type"]
if type.lower() == 'event':
return '0x'+str(event_abi_to_log_topic(abi).hex())
return '0x'+str(function_abi_to_4byte_selector(abi).hex())
def _parse_event_for_data(event: dict) -> dict:
logger.info(f"event: {event}")
body = event["body"]
try:
body = json.loads(event["body"])
return body["data"]
except Exception as err:
logger.error("error parsing data from body", exc_info=True)
return {
"statusCode": 400,
"headers": HEADERS,
"body": json.dumps({"data": str(err)})
}
def decode_contract_event(abi: dict, topics: str, data: str) -> dict:
"""
Takes ABI definition, topics, and data, and decodes into readable format.
it will use the placement index if no name is given.
example output of this method:
{
'name': 'Transfer',
'signature': 'Transfer(address,address,uint256)',
'signature_hash': '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'data': {
'from': '0xe59d5b5eb2a4e02c033a46a18e47f399f7a1e14b',
'to': '0x3349217670f9aa55c5640a2b3d806654d27d0569',
'value': 241002558777421199592
}
}
"""
decoded_successfully = True
topics_data = "".join(t[2:] for t in topics.split(',')[1:])
decoded_types_and_names = to_decodable_types(abi)
try:
decoded_input_values = decode_abi(
decoded_types_and_names['inputs']['types'], bytes.fromhex(data[2:]))
decoded_indexed_values = decode_abi(
decoded_types_and_names['indexed']['types'], bytes.fromhex(topics_data))
evt_data = {decoded_types_and_names['inputs']['names']
[i]: v for i, v in enumerate(decoded_input_values)}
for i, v in enumerate(decoded_indexed_values):
evt_data[decoded_types_and_names['indexed']['names'][i]] = v
except InsufficientDataBytes as e:
logger.error("exception", exc_info=True)
logger.error(json.dumps({"abi": abi, "topics": topics, "data": data}))
decoded_successfully = False
evt_data = {"exception": str(e)}
return {
'name': abi['name'],
'signature': to_signature(abi),
'signature_hash': topics.split(',')[0],
'data': evt_data,
'decoded_successfully': decoded_successfully,
}
def decode_contract_function(abi: dict, input: str, output: str) -> dict:
"""
Takes ABI definition, input, and output, and decodes into readable format.
it will use the placement index if no name is given.
outputs to functions may not be captured by the signature hash, therefore
multiple decoded traces may have the same signature with different output structure.
example output of this method:
{
'name': 'transfer',
'signature': 'transfer(address,uint256)',
'signature_hash': '0xa9059cbb',
'inputs': {
'recipient': '0x42e2e1afe6dc0701640601a6728a097a09efd44b',
'amount': 242859825102880658436213
},
'outputs': {
'0': True
}
}
"""
decoded_types_and_names = to_decodable_types(abi)
decoded_input_values = []
if input is not None:
decoded_input_values = decode_abi(
decoded_types_and_names['inputs']['types'], bytes.fromhex(input[10:]))
decoded_output_values = []
if output is not None:
decoded_output_values = decode_abi(
decoded_types_and_names['outputs']['types'], bytes.fromhex(output[2:]))
return {
'name': abi['name'],
'signature': to_signature(abi),
'signature_hash': input[:10],
'inputs': {decoded_types_and_names['inputs']['names'][i]: v for i,
v in enumerate(decoded_input_values)},
'outputs': {decoded_types_and_names['outputs']['names'][i]: v for i,
v in enumerate(decoded_output_values)}
}
def recursive_flatten_abi_types(abi_fragment: dict, prefix: str, indexed: bool, array_dims: str = None):
"""
docstring
"""
def to_name(n, p, i=0):
if n == "":
return p + '.' + "_"+str(i) if p else "_"+str(i)
return p + '.' + n if p else n
def to_type(t, a):
return t + a if a else t
type_arr = []
name_arr = []
idx = 0
for inp in abi_fragment:
if indexed != inp.get('indexed', False):
continue
typ = inp["type"]
name = inp["name"]
if not isinstance(typ, str):
continue
elif typ.endswith("storage"):
type_arr.append('uint256')
name_arr.append(to_name(name, prefix, idx))
continue
elif not typ.startswith("tuple"):
type_arr.append(to_type(typ, array_dims))
name_arr.append(to_name(name, prefix, idx))
continue
(tup_types, tup_names) = recursive_flatten_abi_types(
inp['components'], to_name(name, prefix, idx), False, typ[5:])
type_arr.extend(tup_types)
name_arr.extend(tup_names)
idx = idx+1
return (type_arr, name_arr)
def to_decodable_types(abi: dict) -> List[str]:
'''
Docstring
'''
i_t, i_n = recursive_flatten_abi_types(abi.get('inputs', []), '', False)
i_i_t, i_i_n = recursive_flatten_abi_types(abi.get('inputs', []), '', True)
o_t, o_n = recursive_flatten_abi_types(abi.get('outputs', []), '', False)
flattened_types = {
"inputs": {
"names": i_n,
"types": i_t
},
"indexed": {
"names": i_i_n,
"types": i_i_t
},
"outputs": {
"names": o_n,
"types": o_t
},
}
return flattened_types
def _default(obj):
if isinstance(obj, bytes):
try:
return '0x' + str(obj.hex())
except UnicodeDecodeError:
# TODO: Maybe find a better way to handle certain bytes data
# rather than ignore and strip out the invalid byte characters
return str(obj, errors='ignore')
raise TypeError(f"type {type(obj)} is not JSON serializable. obj: {obj}")
def format_exception(exc: Exception, event: dict):
return {
"exception": str(exc),
"stackTrace": traceback.format_exc(),
"event": event
}
if __name__ == "__main__":
import sys
from pprint import pprint
filename = sys.argv[1]
print(f"loading event {filename}")
with open(filename) as fp:
event = json.load(fp)
response = decode_contract_event_handler(event, {})
pprint(response, indent=2)