def get_index_release(): bc = blockChain.get_instance() if not bc.vote_live and int(bc.current_vote) > 0: for index, entry in enumerate(bc.release_order): if entry == bc.public_key: return index + bc.current_block + 1 return False
def my_block_next(): bc = blockChain.get_instance() log.info(f'chekcing next block {bc.current_block} {bc.broad_block_num}') if int(bc.current_block) + 1 == int(bc.broad_block_num): # it is my turn broadcast it return True return False
def main_listen_loop(): log.info("listening") bc = blockChain.get_instance() log.info('blockchain in') zmq_obj = ZMQ_Soc.get_instance() log.info('zmq instance in') socket = zmq_obj.get_sub_sock() log.info('socket grabbed') while True: messagedata = str(socket.recv()) # split and process message data, topic topic, block = messagedata.split('#_|_#') blk = bc.extract_block(block) if int(blk.index) == len(bc.blocks) and bc.verify_block(blk): # process block # add block if bc.consume(blk): # echo block zmq_obj.broadcast(block) log.info('making next move') next_move() # if my block is next on receipt send out block # Or if I am oracle and I HAVE Peers # Or if I am oracle and I HAVE Peers and final vote closed and final release block recv else: log.error('block dropped: %s', blk.index)
def all_prev_vote_blocks_recvd(): bc = blockChain.get_instance() log.info( f'blocks check, {bc.current_block} {bc.current_vote_start} {len(bc.release_order)}' ) if int(bc.current_block) + 1 == int(bc.current_vote_start) + len( bc.release_order): return True return False
def next_move(): # grab state machine, and blockchain bc = blockChain.get_instance() if my_block_next(): log.info('Its my turn, sending...') bc.broad_block_queue() elif i_am_oracle(): log.info('Oracle action check') oracle_action()
def first_man(): bc = blockChain.get_instance() blk2 = bc.gen_block( 'register', f'hello|_|{args.my_topic}_|_{args.my_ip}_|_{bc.get_key_pair()}') log.info('registering') if bc.verify_block(blk2): bc.consume(blk2) blk3 = bc.gen_block('oracle', f'new|_|{bc.public_key}') if bc.verify_block(blk3): bc.consume(blk3)
def send_get_data(): # create block # send block bc = blockChain.get_instance() # zmq_obj = ZMQ_Soc.get_instance() bq = BlockQueue.get_instance() gen_block = bc.gen_block( 'register', f'hello|_|{args.my_topic}_|_{args.my_ip}_|_{bc.get_key_pair()}') bq.add_block(str(gen_block)) # zmq_obj.broadcast(str(gen_block)) return "Block added to broadcast queue"
def join_up(node_ip): bc = blockChain.get_instance() endpoint = f'http://{node_ip}:9999/join' payload = { 'con_str': args.my_ip, 'topic': args.my_topic, 'p_key': bc.get_key_pair() } res = requests.post(endpoint, headers={'Content-Type': 'application/json'}, data=json.dumps(payload)) # log.info(res.text) bc.extract_list_blocks(res.text)
def stubs(): data = request.get_json() log.info(f'stubs req: {data}') zmq = ZMQ_Soc.get_instance() bc = blockChain.get_instance() if zmq.find_in_list(data.get('p_key')): if data.get('p_key') not in bc.release_order: log.info('adding key to release') bc.release_order.append(data.get('p_key')) return json.dumps({}), 200 else: return json.dumps({'error': 'already has a slot'}), 405 else: return json.dumps({'error': 'peer not found'}), 406
def pick_new_oracle(): log.info('picking new oracle') zmq = ZMQ_Soc.get_instance() bc = blockChain.get_instance() res = False # while(not res): res = random.choice(zmq.sub_list) # if args.my_ip not in res.get('connect'): # log.info('got oracles') # p_key = res.get('p_key') # else: # log.info('bad oracles') # res = False log.info('new oracle found') block = bc.gen_block('oracle', f'new|_|{res.get("p_key")}') zmq.broadcast(str(block))
def oracle_action(): bc = blockChain.get_instance() log.info( f'oracle: {bc.prev_vote} {bc.current_vote} {bc.vote_live} {bc.oracle_move} {bc.res_out}' ) if not bc.vote_live and (int(bc.current_vote) == int(bc.prev_vote)): log.info('in vote not live') bc.vote_status('open') bc.oracle_move = True # create vote open block with my ip and min open time # as oracle wait for min open time to finish # send close_vote threading.Thread(target=countdown, args=(5, )).start() # time.sleep(5) # bc.vote_status('closed') # consume all stubs, create release order # send release_order return elif bc.oracle_move and not bc.vote_live and (int(bc.current_vote) > int( bc.prev_vote)): log.info(f'in vote broadcast {bc.release_order}') if bc.release_order: log.info('in release order') random.shuffle(bc.release_order) bc.broad_results(bc.release_order) else: log.info('in list only') bc.broad_results([]) bc.res_out = True bc.oracle_move = False return elif bc.res_out: log.info('res out true') if all_prev_vote_blocks_recvd(): log.info('in vote oracle') # choose new oracle (New Oracle is XXX.XXX.XXX.XXX) # needs to happen after pick_new_oracle() bc.res_out = False bc.oracle_move = False bc.vote_live = False bc.release_order = [] bc.prev_vote = bc.current_vote return
def countdown(delay): log.info('running vote closed now') bc = blockChain.get_instance() time.sleep(delay) bc.vote_status('closed')
def i_am_oracle(): bc = blockChain.get_instance() if bc.current_oracle == bc.public_key: return True return False
def i_am_oracle(): bc = blockChain.get_instance() return bc.current_oracle == args.my_topic
# p_key=<p_key> @app.route('/stubs', methods=['POST']) def stubs(): data = request.get_json() log.info(f'stubs req: {data}') zmq = ZMQ_Soc.get_instance() bc = blockChain.get_instance() if zmq.find_in_list(data.get('p_key')): if data.get('p_key') not in bc.release_order: log.info('adding key to release') bc.release_order.append(data.get('p_key')) return json.dumps({}), 200 else: return json.dumps({'error': 'already has a slot'}), 405 else: return json.dumps({'error': 'peer not found'}), 406 if __name__ == "__main__": cmd_args = parse_args() bc = blockChain.get_instance() ZMQ_Soc.get_instance().add_subscription(args.my_ip, args.my_topic, bc.public_key) log.info(cmd_args) if cmd_args.node_ip: join_up(cmd_args.node_ip) else: first_man() thd = threading.Thread(target=main_listen_loop).start() app.run(host='0.0.0.0', port=9999, use_debugger=False)