def check_block(args): 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: {}'.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 for block_num in range(end, start, -1): try: block_info = gph.rpc.get_block(block_num) operations_list = parse_gas_collateral_operations( gph, block_num, block_info["timestamp"], block_info["transactions"]) if operations_list: op_deque_lock.acquire() operations_deque.append(operations_list) op_deque_lock.release() except Exception as e: logger.error('parse block exception. block {}, error {}'.format( block_num, repr(e)))
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) #witness_id = block['witness'] block_witness = gph.rpc.get_object( gph.rpc.get_object( block['witness'])['witness_account'])['name'] except Exception as e: logger.error('get_object exception. block {}, error {}'.format( block_num, repr(e))) 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": block_witness, "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 check_block(start_block, end_block=-1): global interval_blocks, block_count_result print("interval_blocks: {}".format(interval_blocks)) gph = Graphene(node=nodeaddress) if end_block == -1: info = gph.info() print("info: {}".format(info)) end_block = info['head_block_number'] print("start block: {}, end_block: {}".format(start_block, end_block)) key_block_num = str(start_block) for i in range(int(start_block), int(end_block)): try: block = gph.rpc.get_block(i) # print("block: {}".format(block)) timestamp = block["timestamp"] block_date = timestamp.split("T")[0] if i % interval_blocks == 0 or key_block_num == str(i): # print("start_block: {}, end_block: {}, block_id: {}".format(key_block_num, i, block["block_id"])) if key_block_num in block_count_result.keys(): print(">>>> {}: {}".format( key_block_num, block_count_result[key_block_num])) key_block_num = i block_count_result[key_block_num] = { "start_block": int(key_block_num), "block_total": 0, "trx_total": 0, "ops_total": 0 } block_data = block_count_result[key_block_num] block_data["block_total"] += 1 block_data["end_block"] = i transactions = block["transactions"] if transactions: block_data["trx_total"] += len(transactions) for trx in transactions: block_data["ops_total"] += len(trx[1]["operations"]) block_count_result[key_block_num] = block_data except Exception as e: print('get_object exception. block {}, error {}'.format( block_num, repr(e))) print("\n\n>>>> total result: \n{}".format(block_count_result))
def check_block(): def one_block_check(block_num): logger.info('check block number: {}'.format(block_num)) try: block = gph.rpc.get_block(block_num) #witness_id = block['witness'] block_witness = gph.rpc.get_object( gph.rpc.get_object( block['witness'])['witness_account'])['name'] 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": block_witness, "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() try: gph = Graphene(node=nodeaddress) #info = gph.info() #logger.info('info: {}'.format(info)) conn = pymongo.MongoClient(mongodb_params['host'], mongodb_params['port']) conn_db = conn[mongodb_params['db_name']] sort_limit_result = conn_db.block.find().sort("block_num", -1).limit(1) db_last_block_str = jsonb.dumps(sort_limit_result) logger.info("db_last_block_str: {}".format(db_last_block_str)) db_last_block = json.loads(db_last_block_str) if len(db_last_block) != 1: logger.error( "conn_db.block.find().sort('block_num', -1).limit(1) exception" ) conn.close() return start_num = db_last_block[0]['block_num'] info = gph.info() logger.info('info: {}'.format(info)) last_block_num = info['head_block_number'] increase = int( (last_block_num - start_num) / 8) + 10 # 尽可能处理listen之前新增的区块 logger.info(">>> start: {}, end: {}, step: {}".format( start_num, last_block_num, increase)) for index in range(start_num, last_block_num + increase): result = conn_db.block.find({'block_num': index}) if result.count() != 0: logger.info( 'block({}) already exists in mongo db'.format(index)) continue one_block_check(index) conn.close() except Exception as e: logger.error("except: '{}'".format(repr(e)))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from PythonMiddleware.graphene import Graphene from PythonMiddleware.instance import set_shared_graphene_instance from pprint import pprint import time #nodeAddress = "wss://api.cocosbcx.net" nodeAddress = "ws://test.cocosbcx.net" #nodeAddress = "ws://127.0.0.1:8049" gph = Graphene(node=nodeAddress) set_shared_graphene_instance(gph) while True: print('>> info') pprint(gph.info()) time.sleep(2)
def check_block(start_date): global last_block_date, AFTER_DAYS, result_block_data start_date = start_date gph = Graphene(node=nodeaddress) info = gph.info() logger.info("info: {}".format(info)) last_block_num = info['head_block_number'] logger.info("time: {}".format(info["time"])) current_time = info["time"] current_date = info["time"].split("T")[0] start_block_num = 1 end_block_num = last_block_num seconds = compare_time(current_date, start_date) logger.info("current_date: {}, start_date: {}, seconds: {}".format( current_date, start_date, seconds)) if seconds < 3600 * 24 * AFTER_DAYS: logger.info("before {} days".format(AFTER_DAYS)) logger.info("last_block_num: {}, delta: {}".format( last_block_num, 1800 * 24 * AFTER_DAYS)) end_block_num = last_block_num start_block_num = last_block_num - 1800 * 24 * AFTER_DAYS else: logger.info("after {} days".format(AFTER_DAYS)) start_block_num = int(last_block_num - seconds / 2) end_block_num = int(start_block_num + (1800 * 24 * AFTER_DAYS)) if last_block_num < end_block_num: end_block_num = last_block_num logger.info('[block num]start: {}, end: {}, last: {}, seconds: {}'.format( start_block_num, end_block_num, last_block_num, seconds)) for block_num in range(start_block_num, end_block_num + 1): try: block = gph.rpc.get_block(block_num) # logger.info("block: {}".format(block)) timestamp = block["timestamp"] block_date = timestamp.split("T")[0] if block_date != last_block_date: # logger.info("last_date: {}, block_num: {}, block: {}".format(last_block_date, block_num, block)) logger.info( "last_date: {}, block_num: {}, block_id: {}, block timestamp: {}" .format(last_block_date, block_num, block["block_id"], block["timestamp"])) if last_block_date in result_block_data.keys(): logger.info(">>>>>>>>>>>> {}: {}".format( last_block_date, result_block_data[last_block_date])) last_block_date = block_date result_block_data[block_date] = { "block_total": 0, "trx_total": 0, "ops_total": 0 } block_data = result_block_data[block_date] block_data["block_total"] += 1 transactions = block["transactions"] if transactions: block_data["trx_total"] += len(transactions) for trx in transactions: block_data["ops_total"] += len(trx[1]["operations"]) result_block_data[block_date] = block_data except Exception as e: logger.error('get_object exception. block {}, error {}'.format( block_num, repr(e))) logger.info("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>> total result: \n{}".format( result_block_data))