def publish_consensus(): peers = get_peers() for peer in peers: try: response = requests.get(f'{peer.hostname}/consensus') logger.debug( f'publish consensus: {peer.hostname} => {response.status_code}' ) except (requests.ConnectionError, requests.ConnectTimeout): continue
async def search_products(request): for peer in get_peers(): try: async with aiohttp.ClientSession() as session: async with session.get(f'{peer.hostname}/products') as response: if response.status == 200: new_product_list = {} for product in await response.json(): new_product_list[product['product_id']] = product['description'] app.product_list[peer.identifier] = new_product_list except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError): continue return json(app.product_list, status=200)
async def register_nodes(request): values = request.get_json() nodes = values.get('nodes') if nodes is None: return text(body="Error: Please supply a valid list of nodes", status=400) for node in nodes: add_peer(node['identifier'], node['hostname']) peers = get_peers() response = { 'message': 'New nodes have been added', 'total_nodes': [peer.to_dict() for peer in peers] } return json(response, status=201)
async def as_publish_consensus(): peers = get_peers() for peer in peers: try: async with aiohttp.ClientSession() as session: async with session.get( f'{peer.hostname}/consensus') as response: logger.debug( f'publish consensus: {peer.hostname} => {response.status}' ) except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError): continue return True
def resolve_conflicts(): new_chain = None max_length = Blockchain.get_length(Blockchain.get_blocks()) peers = get_peers() for peer in peers: try: response = requests.get(f'{peer.hostname}/blocks') if response.status_code == 200: blocks = response.json() length = Blockchain.get_length(blocks) if length > max_length and valid_blocks(blocks): max_length = length new_chain = blocks except (requests.ConnectionError, requests.ConnectTimeout): continue if new_chain: Blockchain.replace_blocks(blocks) return True return False
async def as_resolve_conflicts(): new_chain = None max_length = Blockchain.get_length(Blockchain.get_blocks()) peers = get_peers() for peer in peers: try: async with aiohttp.ClientSession() as session: async with session.get( f'{peer.hostname}/blocks') as response: if response.status == 200: blocks = await response.json() length = Blockchain.get_length(blocks) if length > max_length and valid_blocks(blocks): max_length = length new_chain = blocks except aiohttp.client_exceptions.ClientConnectorError: continue if new_chain: Blockchain.replace_blocks(blocks) return True return False
def eliminate(dict_board): for coor, value in dict_board.items(): if len(value) == 1: for peer in (get_peers(coor)): dict_board = assign_value(dict_board, peer, dict_board[peer].replace(value, "")) return dict_board