-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.py
223 lines (208 loc) · 9.18 KB
/
check.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
# -*- coding:utf-8 -*-
from PythonMiddleware.notify import Notify
from PythonMiddleware.graphene import Graphene
from PythonMiddlewarebase.operationids import operations
#import base64
# from PythonMiddlewarebase import memo
#from PythonMiddleware.block import Block
#from PythonMiddleware.account import Account, AccountUpdate
# from PythonMiddlewarebase.account import PrivateKey, PublicKey
# from PythonMiddleware.asset import Asset
# from PythonMiddleware.transactionbuilder import TransactionBuilder
import sys
import pymongo
import datetime
from time import sleep
from collections import deque
from threading import Thread, Lock
from prometheus_client import CollectorRegistry, Gauge, pushadd_to_gateway
from config import *
from utils import Logging
from handle_block import logger, parse_operations, handle_operations, init_gauges
#logger = Logging().getLogger()
block_info_q = deque()
pending_block_num_q = deque()
op_d = deque()
#thread lock
block_info_deque_lock = Lock()
pending_block_num_deque_lock = Lock()
op_deque_lock = Lock()
def check_block(args):
def one_block_check(block_num):
logger.info('recv block number: {}'.format(block_num))
try:
block = gph.rpc.get_block(block_num)
if block is None:
logger.debug("block({}) is None. block num: {}".format(block, block_num))
return
#witness_id = block['witness']
witness_obj = gph.rpc.get_object(block['witness'])
witness_account_obj = gph.rpc.get_object(witness_obj['witness_account'])
except Exception as e:
logger.error('get_object exception. block {}, error {}'.format(block_num, repr(e)))
return
block_time = block['timestamp']
transactions = block["transactions"]
witness_sign = block['witness_signature']
trx_total = 0
ops_total = 0
transactions_id = []
if transactions:
trx_total = len(transactions)
for trx in transactions:
transactions_id.append(trx[0])
ops_total += len(trx[1]["operations"])
block_data = {
"block_num": block_num,
"time": block_time,
"witness_id": witness_obj["id"],
"witness_account_id": witness_account_obj['id'],
"witness_account_name":witness_account_obj["name"],
"witness_sign": witness_sign,
"transactions_total": trx_total,
"transactions_id": transactions_id,
"operations_total": ops_total
}
block_info_deque_lock.acquire()
block_info_q.append(block_data)
block_info_deque_lock.release()
start = args[0]
end = args[1]
gph = Graphene(node=nodeaddress)
info = gph.info()
last_block_num = info['head_block_number']
#logger.info('last_block_num: {}, block start: {}, end: {}, info: {}'.format(last_block_num, start, end, info))
logger.info('last_block_num: {}, block start: {}, end: {}'.format(last_block_num, start, end))
if start > last_block_num:
logger.error("start:{} < end:{}".format(start, end))
return
if end > last_block_num:
end = last_block_num
conn = pymongo.MongoClient(mongodb_params['host'], mongodb_params['port'])
conn_db = conn[mongodb_params['db_name']]
for index in range(start, end+1):
result = conn_db.block.find({'block_num':index})
if result.count() == 0:
logger.info('check block number: {}'.format(index))
one_block_check(index)
else:
logger.info('block({}) already exists in mongodb'.format(index))
sleep(0.1)
conn.close()
# 解析区块
def analysis_block():
gph = Graphene(node=nodeaddress)
from PythonMiddleware.instance import set_shared_graphene_instance
set_shared_graphene_instance(gph)
while 1:
if pending_block_num_q:
try:
pending_block_num_deque_lock.acquire()
block_num = pending_block_num_q.popleft()
pending_block_num_deque_lock.release()
logger.debug('pop block number: {}'.format(block_num))
try:
block_info = gph.rpc.get_block(block_num)
time = block_info["timestamp"]
transactions = block_info["transactions"]
operations_list = parse_operations(gph, block_num, time, transactions)
#logger.debug('block: {}, trx_list: {}'.format(block_num, operations_list))
except Exception as e:
logger.error('parse block exception. block {}, error {}'.format(block_num, repr(e)))
if operations_list:
op_deque_lock.acquire()
op_d.append(operations_list)
op_deque_lock.release()
except Exception as e:
logger.error("pending_block_num_q: {}, except: '{}'".format(pending_block_num_q, repr(e)))
sleep(0.7)
#将区块数据写入数据库中block表中
def block2db():
while 1:
if block_info_q:
try:
#global block_info_deque_lock
block_info_deque_lock.acquire()
block = block_info_q.popleft()
block_info_deque_lock.release()
# logger.debug(">>> block: {}".format(block))
#update mongodb
conn = pymongo.MongoClient(mongodb_params['host'], mongodb_params['port'])
conn_db = conn[mongodb_params['db_name']]
try:
conn_db.block.insert_one({
'block_num': block["block_num"],
'time': block["time"],
'witness': block["witness_account_name"],
'witness_id': block["witness_id"],
'witness_account_id': block["witness_account_id"],
# 'witness_sign': block["witness_sign"],
'witness_sign': "",
'transactions_id': str(block["transactions_id"]),
'transactions_total': block["transactions_total"],
'operations_total': block["operations_total"]
})
# witness generate block count
witness_blocks = {
'witness_id': block["witness_id"],
'witness_account_id': block["witness_account_id"],
'witness_account_name': block["witness_account_name"],
"last_block": str(block["block_num"]),
"last_block_time": block["time"]
}
witness_object = conn_db.witnesses.find_one({'witness_id':block["witness_id"]})
if (witness_object is not None):
if (int(block["block_num"]) > int(witness_object["last_block"])):
witness_blocks['_id'] = witness_object['_id']
witness_blocks["total"] = str(int(witness_object["total"]) + 1)
conn_db.witnesses.save(witness_blocks)
else:
logger.info("witness({}) block:{}, not exists.".format(block["witness_id"], block["block_num"]))
witness_blocks["total"] = "1"
conn_db.witnesses.save(witness_blocks)
except Exception as e:
logger.error("block: {}, except: '{}'".format(block["block_num"], repr(e)))
finally:
conn.close()
logger.info('block num: {} done.'.format(block["block_num"]))
except Exception as e:
logger.error("except: '{}'".format(repr(e)))
sleep(0.7)
#将区块解析过的数据写入到数据库中的op表和transaction表中
def data2db():
while 1:
if op_d:
try:
op_deque_lock.acquire()
operations_list = op_d.popleft()
op_deque_lock.release()
handle_operations(operations_list)
# status = handle_operations(operations_list)
# if not status:
# op_deque_lock.acquire()
# block_trx_ops = op_d.appendleft(operations_list)
# op_deque_lock.release()
# logger.warn('consume status {}, trx list: {}'.format(status, operations_list))
except Exception as e:
logger.error("except: '{}'".format(repr(e)))
sleep(0.5)
if __name__ == '__main__':
logger.info('args: {}'.format(sys.argv))
if len(sys.argv) < 3:
logger.error('Usage: python3 check.py block_number_start, block_number_end')
sys.exit(1)
start = int(sys.argv[1])
end = int(sys.argv[2])
if start > end or start <= 0 or end <= 0:
logger.error('block_number_start: {} > block_number_end: {} or start <= 0 or end <= 0'.format(start, end))
sys.exit(1)
args = [start, end]
init_gauges()
t1 = Thread(target=check_block, args=(args,))
t1.start()
t2 = Thread(target=block2db)
t2.start()
t3 = Thread(target=analysis_block)
t3.start()
t4 = Thread(target=data2db)
t4.start()