def mineBlock(blockData): newBlock = generateNextBlock(blockData) if addBlock(newBlock) is True: # 유효한 블록을 추가했다면 nw.broadcast(nw.responseLatestMsg()) # 네트워크에 블록 전파 return newBlock else: return None # 체인에 추가가 되지 않은 경우
async def collator(network, address, smc_handler): if address not in smc_handler.collator_pool: raise ValueError( "Collator pool in SMC does not contain the given address") # collation_tasks_and_periods = [] # [(coroutine, period), ...] collation_tasks = {} while True: # remove finished tasks collation_tasks = {(shard_id, period): task for (shard_id, period), task in collation_tasks.items() if not task.done()} # when a new period starts, check if we're eligible for some shard and if so start to # collate for (shard_id, period) in smc_handler.get_eligible_periods(address): if (shard_id, period) in collation_tasks: continue # collation coro already running logger.info( "Detected eligibility of collator {} for period {} in shard {}" .format( address, period, shard_id, )) message_queue = asyncio.Queue() node_id = address + '_' + str(period) node = Node(network, node_id, message_queue=message_queue) network.login(node) # TODO: disconnection broadcast_task = asyncio.ensure_future(broadcast(network, node_id)) network.add_peers(node, message_queue, num_peers=4) collate_task = asyncio.ensure_future( collate(network, shard_id, period, address, smc_handler, node_id)) collation_tasks[shard_id, period] = collate_task await smc_handler.wait_for_next_period()
def replaceChain(newBlocks): if (isValidChain(newBlocks) is True) and (len(newBlocks)>len(blockchain) or (len(newBlocks)==len(blockchain) and random.boolean())): print("Received blockchain is valid. Replacing current blockchain with received blockchain") nw.broadcast(nw.responseLatestMsg())
timestamp = int(time.time()) element = { 'order': { 'floor': floor, 'button': button }, 'timestamp': timestamp, 'costs': [] } ordersAndCosts.append(element) message = emptyMessage.copy() message['type'] = 'cost_request' message['floor'] = floor message['button'] = button network.broadcast(message) elif task['type'] == 'broadcast_order': if order_elevator_id == 'MY_ID': order_elevator_id = MY_ID message = emptyMessage.copy() message['type'] = 'order' message['floor'] = floor message['button'] = button message['order_elevator_id'] = order_elevator_id network.broadcast(message) elif task['type'] == 'receive_cost': cost = task['cost'] for element in ordersAndCosts: if element['order']['floor'] == floor and element['order'][ 'button'] == button:
# Set the p2p connections between proposers nodes = [Node(network, address) for address in proposer_pool] network.nodes = nodes network.generate_peers() general_coros = [ main_chain.run(), smc_handler.run(), stop(), ] collator_coros = [ collator(network, address, smc_handler) for address in collator_pool ] proposer_coros = [ proposer(network, 0, address, smc_handler) for address in proposer_pool[:len(proposer_pool) // 2] ] + [ proposer(network, 1, address, smc_handler) for address in proposer_pool[len(proposer_pool) // 2:] ] + [broadcast(network, address) for address in proposer_pool] coros = general_coros + collator_coros + proposer_coros loop = asyncio.get_event_loop() # asyncio.gather(*coros) for coro in coros: loop.create_task(coro) loop.run_forever()
def send(event, port): msg = json.dumps(event) network.broadcast(msg, port)