-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreate_transaction_features.py
304 lines (244 loc) · 12.3 KB
/
create_transaction_features.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
import argparse
import numpy as np
from honeypot_detection.database.contract import Contract
from honeypot_detection.database.transaction import NormalTransaction, InternalTransaction
from honeypot_detection.multiprocess_by_address import multiprocess_by_address, Worker
from honeypot_detection.utils import address_list_from_file
COLUMNS = [
"contract_address",
"normal_transaction_count",
"normal_transaction_block_count",
"normal_transaction_before_creation_count",
"normal_transaction_from_other_count",
"normal_transaction_first_block",
"normal_transaction_last_block",
"normal_transaction_block_span",
"normal_transaction_other_sender_count",
"normal_transaction_count_per_block_mean",
"normal_transaction_count_per_block_std",
"normal_transaction_value_mean",
"normal_transaction_value_std",
"normal_transaction_gas_mean",
"normal_transaction_gas_std",
"normal_transaction_gas_used_mean",
"normal_transaction_gas_used_std",
"normal_transaction_time_delta_mean",
"normal_transaction_time_delta_std",
"normal_transaction_block_delta_mean",
"normal_transaction_block_delta_std",
"internal_transaction_count",
"internal_transaction_block_count",
"internal_transaction_creation_count",
"internal_transaction_from_other_count",
"internal_transaction_to_other_count",
"internal_transaction_other_sender_count",
"internal_transaction_other_receiver_count",
"internal_transaction_count_per_block_mean",
"internal_transaction_count_per_block_std",
"internal_transaction_value_mean",
"internal_transaction_value_std",
"internal_transaction_gas_mean",
"internal_transaction_gas_std",
"internal_transaction_gas_used_mean",
"internal_transaction_gas_used_std",
]
class TransactionFeatureWorker(Worker):
def process_address(self, address):
features = {"contract_address": address}
contract = self.sqlalchemy_session.query(Contract).filter(Contract.address == address).one()
internal_transactions = self._build_normal_transaction_features(contract, features)
self._build_internal_transaction_features(contract, features, internal_transactions)
self.send_output(features)
def _build_normal_transaction_features(self, contract, features):
transactions = self.sqlalchemy_session.query(NormalTransaction). \
filter(NormalTransaction.crawled_from == contract.address). \
order_by(NormalTransaction.block_number.asc(),
NormalTransaction.transaction_index.asc()).all()
features["normal_transaction_count"] = 0
features["normal_transaction_block_count"] = 0
features["normal_transaction_before_creation_count"] = 0
features["normal_transaction_from_other_count"] = 0
last_block = None
last_block_count = 0
count_per_block = []
other_senders = set()
values = []
gas = []
gas_used = []
last_timestamp = None
time_deltas = []
block_deltas = []
contract_created = False
children = []
for transaction in transactions:
# transactions should always have source
assert transaction.source is not None, transaction.hash
# transaction is not sent and received by the same party
assert transaction.source != transaction.target, transaction.hash
features["normal_transaction_count"] += 1
# creation transaction
if transaction.target is None:
# there should be only one creation transaction
assert not contract_created, transaction.hash
# this was the creation transaction
contract_created = True
# in the creation transaction the creator is the source
assert transaction.source == contract.creator, transaction.hash
# the contract address added by me should match
assert transaction.crawled_from == contract.address, transaction.hash
# other transaction
else:
# non-creation transactions have no contract address
assert transaction.contract_address is None, transaction.hash
# only the one added by me
assert transaction.crawled_from == contract.address, transaction.hash
# the contract is always the target
assert transaction.target == contract.address
# this was before the creation transaction
if not contract_created:
features["normal_transaction_before_creation_count"] += 1
# first or new block
if last_block is None or transaction.block_number != last_block:
# new block
if last_block is not None and last_block_count > 0:
count_per_block.append(last_block_count)
last_block_count = 0
# count block
features["normal_transaction_block_count"] += 1
# count transaction
last_block_count += 1
# the creator is the source
if transaction.source != contract.creator:
other_senders.add(transaction.source)
features["normal_transaction_from_other_count"] += 1
# only count eth movement if there is no error
if not transaction.is_error:
values.append(transaction.value)
gas.append(transaction.gas)
gas_used.append(transaction.gas_used)
# time delta
if last_timestamp is not None:
time_deltas.append(transaction.timestamp - last_timestamp)
# update timestamp
last_timestamp = transaction.timestamp
# block delta
if last_block is not None:
block_deltas.append(transaction.block_number - last_block)
# first block
else:
features["normal_transaction_first_block"] = transaction.block_number
# update block
last_block = transaction.block_number
# append all the children from this transaction
children.extend(self._fetch_transaction_children(transaction))
# last block
if last_block is not None and last_block_count > 0:
count_per_block.append(last_block_count)
features["normal_transaction_last_block"] = last_block
features["normal_transaction_block_span"] = last_block - features["normal_transaction_first_block"]
else:
features["normal_transaction_last_block"] = features["normal_transaction_first_block"]
features["normal_transaction_block_span"] = 0
# aggregate features
features["normal_transaction_other_sender_count"] = len(other_senders)
self._aggregate(features, "normal_transaction_count_per_block", count_per_block)
self._aggregate(features, "normal_transaction_value", values)
self._aggregate(features, "normal_transaction_gas", gas)
self._aggregate(features, "normal_transaction_gas_used", gas_used)
self._aggregate(features, "normal_transaction_time_delta", time_deltas)
self._aggregate(features, "normal_transaction_block_delta", block_deltas)
# return the internal transactions ordered by normal transactions
return children
def _build_internal_transaction_features(self, contract, features, transactions):
features["internal_transaction_count"] = 0
features["internal_transaction_block_count"] = 0
features["internal_transaction_creation_count"] = 0
features["internal_transaction_from_other_count"] = 0
features["internal_transaction_to_other_count"] = 0
last_block = None
last_block_count = 0
count_per_block = []
other_senders = set()
other_receivers = set()
values = []
gas = []
gas_used = []
for transaction in transactions:
# transactions should always have source
assert transaction.source is not None, transaction.hash
features["internal_transaction_count"] += 1
# other contract creation
if transaction.target is None:
# the source is the contract that created this transaction
assert transaction.source == transaction.crawled_from, transaction.hash
# the new contract should not be the contract that created this transaction
assert transaction.contract_address != transaction.crawled_from, transaction.hash
# creation features
features["internal_transaction_creation_count"] += 1
# no other contract is created
else:
# non-creation transactions have no contract address
assert transaction.contract_address is None, transaction.hash
# IMPORTANT: _contract_address can be != contract["address"]
# children from a transaction can be from another contract
# first or new block
if last_block is None or transaction.block_number != last_block:
# new block
if last_block is not None and last_block_count > 0:
count_per_block.append(last_block_count)
last_block_count = 0
# count block
features["internal_transaction_block_count"] += 1
# count transaction
last_block_count += 1
# from other
if transaction.source not in [contract.address, contract.creator]:
other_senders.add(transaction.source)
features["internal_transaction_from_other_count"] += 1
# to other
if transaction.target not in [contract.address, contract.creator]:
other_receivers.add(transaction.target)
features["internal_transaction_to_other_count"] += 1
# update block
last_block = transaction.block_number
# only count eth movement if there is no error
if not transaction.is_error:
values.append(transaction.value)
gas.append(transaction.gas)
gas_used.append(transaction.gas_used)
# last block
if last_block is not None and last_block_count > 0:
count_per_block.append(last_block_count)
# aggregate features
features["internal_transaction_other_sender_count"] = len(other_senders)
features["internal_transaction_other_receiver_count"] = len(other_receivers)
self._aggregate(features, "internal_transaction_count_per_block", count_per_block)
self._aggregate(features, "internal_transaction_value", values)
self._aggregate(features, "internal_transaction_gas", gas)
self._aggregate(features, "internal_transaction_gas_used", gas_used)
@staticmethod
def _aggregate(features, name, values):
if len(values) > 0:
features[name + "_mean"] = np.mean(values)
features[name + "_std"] = np.std(values)
def _fetch_transaction_children(self, transaction):
children = self.sqlalchemy_session.query(InternalTransaction).\
filter(InternalTransaction.hash == transaction.hash).all()
return list(children)
def main():
argument_parser = argparse.ArgumentParser(description="Create features per contract.")
argument_parser.add_argument("contracts", type=argparse.FileType("r"),
help="File path containing contracts to crawl, one address per line.")
argument_parser.add_argument("output", type=str, help="Output file in csv format.")
argument_parser.add_argument("--processes", type=int, help="Number of processes. Default is cpu_count() - 1.")
argument_parser.add_argument("--log_every", type=int, default=5, help="How many seconds between count logs.")
arguments = argument_parser.parse_args()
addresses = address_list_from_file(arguments.contracts)
multiprocess_by_address(addresses,
TransactionFeatureWorker,
arguments.output,
COLUMNS,
num_processes=arguments.processes,
log_every=arguments.log_every)
if __name__ == '__main__':
main()