def blockchain_page(): blocks, _, _ = Block.all(page=1, per_page=50) mempool, _, _ = MempoolTx.all(page=1, per_page=50) return render_template( 'blockchain/blockchain.j2', blocks=blocks, mempool=mempool )
def domains_page(): popular_names, _, _ = PopularNames.latest(page=1, per_page=30) return render_template( 'blockchain/domains.j2', title='Domains', block_height=Block.max_height(), ending_soon=Name.ending_soon(), popular_names=popular_names, )
def index_info(): info = node_rpc_client.get_info() hashrate = node_rpc_client.get_network_hash_ps(height=Block.max_height()) it = InfoTick(difficulty=info['difficulty'], hashrate=hashrate, created_at=int(time.time())) db.session.add(it) db.session.commit()
def home_page(): block_height = Block.max_height() txs, _, _ = Tx.all() info = InfoTick.latest() return render_template('blockchain/home.j2', title='Home', block_height=block_height, latest_price=PriceTick.latest(), txs=txs, ending_soon=Name.ending_soon(), entity_counts=EntityCounts.latest(), mempool_count=MempoolTx.count(), info=info)
def name_page(name): if not valid_name(name): return None, 404 name_info = Name.find_by_name(name, inc_views=True) if name_info is None: return None, 404 block_height = Block.max_height() if name_info.status['status'] == 'CLOSED': return render_template('blockchain/name_closed.j2', title='{}/'.format(name), name=name_info, block_height=block_height) return render_template( 'blockchain/name_auction.j2', title='{}/'.format(name), height=Block.max_height(), name=name_info, )
def index_blocks(): current_app.logger.info('Started indexer.') lock_active = False def cleanup(sig, frame): if lock_active: redis_client.delete('lock.indexer') sys.exit(0) signal.signal(signal.SIGINT, cleanup) signal.signal(signal.SIGTERM, cleanup) while True: if not lock_block_indexer(): current_app.logger.info('Indexer is locked, sleeping.') time.sleep(60) continue lock_active = True try: indexed_height = Block.max_height() chain_height = node_client.get_server_info()['chain']['height'] new_height = indexed_height current_app.logger.info( 'Current height: {}, chain height: {}'.format( indexed_height, chain_height)) stop_height = indexed_height + 1 + 100 if stop_height > chain_height + 1: stop_height = chain_height + 1 for height in range(indexed_height + 1, stop_height): index_block(height) new_height = height db.session.execute( 'SELECT refresh_popular_names(:min_height)', { 'min_height': new_height - protocol.network_main.bidding_period - protocol.network_main.open_period }) db.session.commit() except Exception as e: current_app.logger.error('Error while indexing:') current_app.logger.exception(e) finally: redis_client.delete('lock.indexer') lock_active = False current_app.logger.info('Indexing done, sleeping.') time.sleep(60)
def blocks_page(): try: page = int(request.args.get('page', 1)) except Exception as e: page = 1 per_page = 50 blocks, page_count, result_count = Block.all(page=page, per_page=per_page) return render_template('blockchain/blocks.j2', title='Blocks', blocks=blocks, page=page, per_page=per_page, page_count=page_count, result_count=result_count)
def block_by_hash_page(block_hash): block = Block.find_by_height(block_hash) return block_page(block)
def block_by_height_page(height): block = Block.find_by_height(height) return block_page(block)
def invalidate_on_block(): return Block.max_height()
def index_block(height): entity_counts = EntityCounts.latest() block_json = node_client.get_block(height) block = Block(height=height, hash=block_json['hash'], prev_hash=block_json['prevBlock'], merkle_root=block_json['merkleRoot'], witness_root=block_json['witnessRoot'], tree_root=block_json['treeRoot'], reserved_root=block_json['reservedRoot'], time=block_json['time'], bits=block_json['bits'], nonce=block_json['nonce'], extra_nonce=block_json['extraNonce'], mask=block_json['mask']) db.session.add(block) for tx_json in block_json['txs']: entity_counts.txs += 1 tx = Tx(hash=tx_json['hash'], witness_hash=tx_json['witnessHash'], fee=tx_json['fee'], rate=tx_json['rate'], mtime=tx_json['mtime'], index=tx_json['index'], version=tx_json['version'], locktime=tx_json['locktime'], hex=tx_json['hex']) block.txs.append(tx) for i, in_json in enumerate(tx_json['inputs']): coinbase = in_json['prevout']['hash'] == ZERO_HASH inp = Input(index=i, prevout_hash=in_json['prevout']['hash'] if not coinbase else None, prevout_index=in_json['prevout']['index'] if not coinbase else None, prevout_address=in_json['coin']['address'] if not coinbase else None, coinbase=coinbase) tx.inputs.append(inp) distinct_addresses = set() for i, out_json in enumerate(tx_json['outputs']): cov_name_hash = out_json['covenant']['items'][ 0] if out_json['covenant']['action'] != 'NONE' else None out = Output(block=block, index=i, value=out_json['value'], address=out_json['address'], covenant_type=out_json['covenant']['type'], covenant_action=out_json['covenant']['action'], covenant_items=out_json['covenant']['items'], covenant_name_hash=cov_name_hash) if out_json['address'] not in distinct_addresses: address = Address.find_by_address(out_json['address']) if address is None: address = Address(address=out_json['address'], tx_count=0) db.session.add(address) address.tx_count += 1 if out.covenant_action == 'OPEN': entity_counts.opened_names += 1 index_open(out) elif out.covenant_action == 'CLAIM': index_claim(out) tx.outputs.append(out) db.session.commit() current_app.logger.info('Indexed block {}'.format(height))