-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle.py
310 lines (257 loc) · 12 KB
/
handle.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
#! /usr/bin/env python
# coding: utf-8
# Licensed under the MIT License.
from pymongo import MongoClient, DESCENDING, ASCENDING
from Block.RpcClient import RpcClient
from Storage.Mongo import Mongo
from bson.objectid import ObjectId
import time
import os
from multiprocessing import Pool, cpu_count
from binascii import unhexlify
from utils.tools import Tool
from binascii import unhexlify
# import argparse
from dotenv import load_dotenv, find_dotenv
import logzero
from logzero import logger
from utils.redisHelper import RedisHelper
from utils.tools import get_random_node
load_dotenv(find_dotenv(), override=True)
print('RPC', os.environ.get('RPC'))
print('MONGODB', os.environ.get('MONGODB'))
print('DB', os.environ.get('DB'))
print('WORK_COUNT', cpu_count())
# b = RpcClient(os.environ.get('RPC'))
m = Mongo(os.environ.get('MONGODB'), os.environ.get('DB'))
# obj = RedisHelper(os.environ.get('REDIS'))
chan = "nep5"
# create index
def create_index():
m.connection()['block'].create_index([('index', DESCENDING)], unique=True)
m.connection()['transaction'].create_index(
[('txid', ASCENDING)], unique=True)
m.connection()['transaction'].create_index([('type', ASCENDING)])
m.connection()['transaction'].create_index([('blockIndex', ASCENDING)])
m.connection()['transaction'].create_index(
[('vin.utxo.address', ASCENDING)])
m.connection()['transaction'].create_index(
[('vin.utxo.asset', ASCENDING)])
m.connection()['transaction'].create_index([('vout.address', ASCENDING)])
m.connection()['transaction'].create_index([('vout.asset', ASCENDING)])
m.connection()['transaction'].create_index([('nep5.to', ASCENDING)])
m.connection()['transaction'].create_index([('nep5.from', ASCENDING)])
m.connection()['transaction'].create_index([('nep5.assetId', ASCENDING)])
m.connection()['address'].create_index(
[('address', DESCENDING)], unique=True)
m.connection()['address'].create_index([('blockIndex', ASCENDING)])
m.connection()['asset'].create_index(
[('assetId', DESCENDING)], unique=True)
m.connection()['asset'].create_index([('blockIndex', ASCENDING)])
m.connection()['state'].create_index([('index', DESCENDING)])
m.connection()['state'].delete_many({})
m.connection()['state'].insert_one(
{'_id': ObjectId('5a95047efc2a4961941484e6'), 'height': 0})
def del_all():
m.connection()['block'].delete_many({})
m.connection()['transaction'].delete_many({})
m.connection()['address'].delete_many({})
m.connection()['asset'].delete_many({})
m.connection()['state'].delete_many({})
def save_block(b, start, length):
print('start', start)
print('length', length)
# b = RpcClient()
# print('RpcClient', b.url)
try:
index = start
while index <= start + length:
#print('index', index)
# 判断 m_block 是否已经存在
m_block = m.connection()['block'].find_one({
'index': index
})
# print('m_block', m_block)
if m_block is None:
block = b.get_block(index)
m_block = block
#print('block', block)
m.connection()['block'].insert_one(block)
# m_block = m_block or b.get_block(index)
for tx in m_block['tx']:
# print('tx',tx['txid'])
# 判断 m_transaction 是否已经存在
m_transaction = m.connection()['transaction'].find_one({
'txid': tx['txid']
})
if m_transaction is None:
save_transaction(b, tx, m_block['index'])
# 保存address
save_address(b, tx, m_block['index'])
index = index + 1
return True
except Exception as e:
logger.error('save_block index %s', index)
logger.exception(e)
time.sleep(1)
b = RpcClient()
b.url = get_random_node()
save_block(b, start, length)
# m.connection()['state'].insert_one({
# 'index': index,
# 'error': True
# })
def save_transaction(b, tx, blockIndex):
# InvocationTransaction 需要单独处理
if tx['type'] == 'InvocationTransaction':
tx['nep5'] = handle_nep5(b, tx['txid'], blockIndex) or []
# print('nep5', tx['nep5'])
for vin in tx['vin']:
utxo = b.get_raw_transaction(vin['txid'])['vout'][vin['vout']]
vin['utxo'] = utxo
tx['blockIndex'] = blockIndex
#print('tx', tx)
m.connection()['transaction'].insert_one(tx)
def save_address(b, tx, blockIndex):
for vout in tx['vout']:
#print('save_address', vout)
# 判断 m_address 是否已经存在
m_address = m.connection()['address'].find_one({
'address': vout['address']
})
if m_address is None:
m.connection()['address'].insert_one({
'address': vout['address'],
'blockIndex': blockIndex
})
# 判断 m_assert 是否已经存在
m_assert = m.connection()['asset'].find_one({
'assetId': vout['asset']
})
if m_assert is None:
save_assert(b, vout['asset'], blockIndex)
def save_assert(b, assetId, blockIndex):
r = b.get_asset_state(assetId)
r['assetId'] = r['id']
r['blockIndex'] = blockIndex
del r['id']
m.connection()['asset'].insert_one(r)
def save_state():
pass
def handle_nep5(b, txid, blockIndex):
print("handle_nep5", txid)
# 0x9db4725a8b6a43ce91d5085fe88df59578993d7cd0b2397934215463c48d575f
try:
r = b.get_application_log(txid)
# print("r",r)
nep5_arr = []
if r is not None:
# print("rvmstate",r['executions']['vmstate'])
# vmstate"是虚拟机执行合约后的状态,如果包含"FAULT"的话,
for iteme in r['executions']:
# print("iteme",iteme)
if "FAULT" in iteme['vmstate']:
continue
# print("iteme",'notifications' in iteme.keys())
if 'notifications' in iteme.keys():
# not transfer
for item in iteme['notifications']:
# print("item",item)
if item['state']['value'][0]['value'] != "7472616e73666572":
continue
if 'contract' in item and 'state' in item and 'value' in item['state'] and len(item['state']['value']) == 4:
# handle decimals
decimals = b.get_nep5_decimals(item['contract'])[
'stack'][0]['value'] or 0
# print('decimals', decimals)
# print('handle_nep5')
nep5_assert = m.connection()['asset'].find_one({
"assetId": item['contract'],
})
#print('nep5_assert', nep5_assert)
# asserts
if nep5_assert is None:
m.connection()['asset'].insert_one({
"assetId": item['contract'],
'blockIndex': blockIndex,
'type': 'nep5'
})
# mintTokens
if item['state']['value'][1]['value'] == "":
# 判断地址
address_to = Tool.scripthash_to_address(
unhexlify(item['state']['value'][2]['value']))
nep5_address_to = m.connection()['address'].find_one({
'address': address_to
})
if nep5_address_to is None:
m.connection()['address'].insert_one({
'address': address_to,
'blockIndex': blockIndex
})
if(item['state']['value'][3]['type'] == "ByteArray"):
value = Tool.hex_to_num_str(
item['state']['value'][3]['value'], decimals)
if(item['state']['value'][3]['type'] == "Integer"):
value = Tool.hex_to_num_intstr(
item['state']['value'][3]['value'], decimals)
# obj.public(chan, address_to)
nep5_arr.append({
# "txid": txid,
"assetId": item['contract'],
"operation": 'mintTokens',
# 转出 为空
"from": '',
# 输入
"to": address_to,
"value": value,
})
else:
# 判断地址from
address_from = Tool.scripthash_to_address(
unhexlify(item['state']['value'][1]['value']))
nep5_address_from = m.connection()['address'].find_one({
'address': address_from
})
if nep5_address_from is None:
m.connection()['address'].insert_one({
'address': address_from,
'blockIndex': blockIndex
})
# 判断地址to
address_to = Tool.scripthash_to_address(
unhexlify(item['state']['value'][2]['value']))
nep5_address_to = m.connection()['address'].find_one({
'address': address_to
})
if nep5_address_to is None:
m.connection()['address'].insert_one({
'address': address_to,
'blockIndex': blockIndex
})
# handle value
if(item['state']['value'][3]['type'] == "ByteArray"):
value = Tool.hex_to_num_str(
item['state']['value'][3]['value'], decimals)
if(item['state']['value'][3]['type'] == "Integer"):
value = Tool.hex_to_num_intstr(
item['state']['value'][3]['value'], decimals)
# obj.public(chan, address_to)
# obj.public(chan, address_to)
# print('value',value)
nep5_arr.append({
# "txid": txid,
"assetId": item['contract'],
"operation": 'transfer',
# 转出
"from": address_from,
# 输入
"to": address_to,
"value": value,
})
# print('nep5_arr',nep5_arr)
return nep5_arr
except Exception as e:
print("Exception", e)
logger.error('handle_nep5 txid %s', txid)
return []