def post_contract(): event = request.form['event'] team = request.form['team'] quantity = request.form['quantity'] expiration_date = request.form['expiration_date'] odds = request.form['odds'] source_of_truth = request.form['source_of_truth'] check_result_time = request.form['check_result_time'] party1_public_key = request.form['party1_public_key'] party1_digital_sig = request.form['party1_digital_sig'] expiration_date = time.mktime(datetime.datetime.strptime(expiration_date,'%Y-%m-%dT%H:%M').timetuple()) check_result_time = time.mktime(datetime.datetime.strptime(check_result_time,'%Y-%m-%dT%H:%M').timetuple()) the_contract = Contract(event,team,quantity,expiration_date,odds,source_of_truth, check_result_time,party1_public_key,party1_digital_sig) print (the_contract) with grpc.insecure_channel(f'{_APP_IP}:{_APP_PORT}') as channel: #stub = send_info_pb2_grpc.SendInfoStub(channel) stub = full_node_pb2_grpc.FullNodeStub(channel) contract = full_node_pb2.Contract(serialized_contract=pickle.dumps(the_contract), broadcast_node=_APP_IP) stub.new_contract_broadcast(contract) print ('made it successfully?') return redirect('/')
def get_blockchain(): with grpc.insecure_channel(f'{_APP_IP}:{_APP_PORT}') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) the_request = full_node_pb2.BlockchainRequest(message= '___') response = stub.display_full_blockchain(the_request) bc = pickle.loads(response.response) return render_template('blockchain.html',bc=bc)
def broadcast_transaction(self, transaction, broadcast_node): """Broadcast newly generated transaction. """ serialized_txn = pickle.dumps(transaction) for ip_addr in self.known_peers_list: if ip_addr != broadcast_node: print(f'Broadcasting transaction to: {ip_addr}') with grpc.insecure_channel(ip_addr+':12345') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) txn = full_node_pb2.Transaction(serialized_transaction=serialized_txn, broadcast_node=broadcast_node) stub.new_transaction_broadcast(txn)
def send_block_to_node(self, block_to_send, receiving_node, block_height): """Sends blocks to the newly joined node if that node has incomplete blockchain. This method is implemented with the purpose of avoiding forks. """ serialized_block = pickle.dumps(block_to_send) with grpc.insecure_channel(receiving_node+':12345') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) existing_block = full_node_pb2.Block(serialized_block=serialized_block) response = stub.existing_block_broadcast(existing_block) print(f"Sending BLOCK in BlockChain Height {block_height} to: {receiving_node}")
def broadcast_contract(self, contract, broadcast_node): """Serializes contract object and broadcasts them to other nodes in the network. """ serialized_contract = pickle.dumps(contract) for ip_addr in self.known_peers_list: if ip_addr != broadcast_node: print(f'Broadcasting contract to: {ip_addr}') with grpc.insecure_channel(ip_addr+':12345') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) contract = full_node_pb2.Contract(serialized_contract=serialized_contract, broadcast_node=broadcast_node) stub.new_contract_broadcast(contract)
def broadcast_block(self, new_block): """Broadcasts newly mined block to other peers in the network. """ serialized_block = pickle.dumps(new_block) for ip_addr in self.known_peers_list: with grpc.insecure_channel(ip_addr+':12345') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) new_block = full_node_pb2.Block(serialized_block=serialized_block) response = stub.new_block_broadcast(new_block) print(f"Broadcasting New Block to: {ip_addr}") # after publishing a new block to other known peers, sleep between 0-3 seconds sleep_interval = random.uniform(2, 3) print(f"... After broadcasting new block to other nodes, sleep for: {sleep_interval} seconds ...") time.sleep(sleep_interval)
def request_handshake(self, ip_addr): """Requests handshake to ip_addr and returns list of known ip address from the handshake receiving node. """ self.handshaken_peers_list.append(ip_addr) print("\nRequesting handshake to node: " + ip_addr) with grpc.insecure_channel(ip_addr+':12345') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) # hs_response is a list of ip address hs_response = stub.handshake( full_node_pb2.hs_request(nVersion=1, nTime=current_time(), addrMe=get_ip(), bestHeight=self.blockchain.height()) ) print(f"Received list of known nodes: {' '.join(hs_response.message)}") #print(f"Current Node's list of known nodes: {' '.join(self.handshaken_peers_list)}\n") print(f"Current Node's list of known nodes: {' '.join(self.known_peers_list)}\n") return hs_response.message
def index(): if request.method == 'POST': pass # connect to the main program and see what contracts currently exist real_bets = [] with grpc.insecure_channel(f'{_APP_IP}:{_APP_PORT}') as channel: stub = full_node_pb2_grpc.FullNodeStub(channel) the_request = full_node_pb2.ExistingContractRequest(message= '___') response = stub.show_all_existing_contracts(the_request) if len(response.serialized_contracts) == 0: print ('There are no bets to be found!') else: for contract in response.serialized_contracts: real_bets.append(pickle.loads(contract)) print ('success?') return render_template('bets.html',bets=real_bets) #change back to real_bets after implementing GRPC