def block(block): block = block.to_dict(flat=False) nodes = pp.read_nodes_from_file() my_node = nodes[0] if check_reward_target(block['transactions'][0], my_node, block['hash'][0]) == False: return False last_block = get_last_block(my_node) if (last_block == False): return False if last_block['hash'] != block['previous_hash'][0]: consensus() if float(last_block['timestamp']) > float(block['timestamp'][0]): return False merkle_norm = binascii.hexlify( merkle.create_merkle_tree(block['transactions'])).decode('utf-8') if (merkle_norm != block['merkle'][0]): return False # data_for_hash = bytes(str(block['timestamp'][0]) + str(block['nonce'][0]) + str(block['previous_hash'][0]) + str(block['transactions']) + str(['merkle'][0]), 'utf-8') # hash_norm = sha256(data_for_hash).hexdigest() # print(hash_norm) # print(block['hash'][0]) # if (hash_norm != block['hash'][0]): # return False # print("Hash passsed") if (len(block['transactions']) > 1): txs = deepcopy(block['transactions']) i = 0 for tx in txs: if i > 0: if script.is_it_valid(tx) == False: return False i += 1 return True
def block(block): block = block.to_dict(flat=False) nodes = pp.read_nodes_from_file() my_node = nodes[0] if check_reward_target(block['transactions'][0], my_node, block['hash'][0]) == False: return False last_block = get_last_block(my_node) if (last_block == False): return False if last_block['hash'] != block['previous_hash'][0]: consensus() if float(last_block['timestamp']) > float(block['timestamp'][0]): return False merkle_norm = binascii.hexlify(merkle.create_merkle_tree(block['transactions'])).decode('utf-8') if (merkle_norm != block['merkle'][0]): return False if (len(block['transactions']) > 1): txs = deepcopy(block['transactions']) i = 0 for tx in txs: if i > 0: if script.is_it_valid(tx) == False: return False i += 1 return True
def list_of_nodes(): if request.method == 'GET': data = pending_pool.read_nodes_from_file('node') ret = ''.join(data) if data != False: return (ret) else: return ("Something get wrong!\nYou have no nodes")
def broadcast_to_friend(data, where): nodes = [] nodes = pp.read_nodes_from_file() if (type(data) is block.Block): a = data.__dict__ data = a for node in nodes: try: req = requests.post("http://" + node + where, data=data) except: print("Node has no connection")
def consensus(): nodes = pp.read_nodes_from_file("node") len_list = [] dicti = {} for x in nodes: dicti = {} dicti["ip"] = x try: dicti["length"] = int( requests.get("http://" + x + "/chain/length").text) except: dicti["length"] = 0 len_list.append(dicti) newlist = sorted(len_list, key=lambda k: k['length']) if (newlist[-1]['ip'] != nodes[0]): req = requests.get("http://" + newlist[-1]['ip'] + "/chain") chain = req.text req = requests.get("http://" + newlist[-1]['ip'] + "/utxo") utxo = req.text
def main(): ip = pending_pool.read_nodes_from_file() if ip == False: PORT = input("Choose port\n") try: val = int(PORT) try: fd = open("port.txt", 'w') fd.write(PORT) fd.close() pending_pool.add_node_to_file("0.0.0.0:" + PORT) app.run(host='0.0.0.0', port=PORT, debug=False) print("!!!Server port was saved in a file port.txt!!!") except IOError: print("IOError") except (ValueError, PermissionError, OverflowError) as e: print("Invalid port, stupid") else: app.run(host=ip[0][:7], port=ip[0][8:13], debug=False)