def blockchain():
    blocks_dict = {}
    files = int_and_sort(os.listdir(blockchain_dir))
    for filename in reversed(files[1:]):
        block = json.load(open(blockchain_dir + str(filename), 'rb'))
        blocks_dict[filename] = block
    return render_template('blockchain.html', blocks=blocks_dict)
def edit(num):
    block_number = num
    files = int_and_sort(os.listdir(blockchain_dir))
    last_file = files[-1]
    blockchain_filename = blockchain_dir + str(num)
    block = json.load(open(blockchain_filename, 'rb'))
    if request.method == 'GET':
        return render_template('edit.html', block=block, number=num, last_file=last_file)
    else:
        if block_number == last_file:

            nodes_numbers = []
            for i in range(1, 7):
                nodes_numbers.append(request.form.get(f'node{i}'))
            if nodes_numbers.count(None) < 3:
                from_who = request.form['from']
                to_whom = request.form['to']
                amount = request.form['amount']
                new_hash = request.form['hash']
                change_block(block_number, from_who,
                             to_whom, amount, new_hash)
                return render_template('edit.html', edited='BLOCK IS CHANGED!', number=num, block=block, last_file=last_file)
            else:
                return render_template('edit.html', deny='YOU CANT CHANGE THIS BLOG WITHOUT ACCESS TO ENOUGH NUMBER OF NODES!', number=num, block=block, last_file=last_file)
        else:
            from_who = request.form['from']
            to_whom = request.form['to']
            amount = request.form['amount']
            new_hash = request.form['hash']
            change_block(block_number, from_who,  to_whom, amount, new_hash)
            return render_template('edit.html', edited='You have changed the block!', number=num, block=block)
def node(i):
    n = i
    if n in range(1, len(node_dirs()) + 1):
        node = nodes_route + str(n) + '/'
        files = int_and_sort(os.listdir(node))
        blocks_dict = {}
        for filename in reversed(files[1:]):
            block = json.load(open(node + str(filename), 'rb'))
            blocks_dict[filename] = block
        return render_template('node.html', blocks=blocks_dict, number=n)
def check_integrity():
    # get hash of the previous block
    # again count its hash
    # compare results
    blocks_dict = {}
    results = []
    files = int_and_sort(os.listdir(blockchain_dir))
    for filename in reversed(files[1:]):
        block = json.load(open(blockchain_dir + str(filename), 'rb'))
        blocks_dict[filename] = block
        file_hash = block['hash']
        prev_file = str(filename - 1)
        actual_hash = get_hash(prev_file)
        if file_hash == actual_hash:
            result = 'ok'
        else:
            result = 'corrupted'
        results.append({prev_file: result})
    return render_template('integrity.html', results=results)
def edit(num):
    files = int_and_sort(os.listdir(blockchain_dir))
    last_file = files[-1]
    block = json.load(open(filename, 'rb'))
    if request.method == 'GET':
        return render_template('edit.html',
                               block=block,
                               number=num,
                               last_file=last_file)
    else:
        from_who = request.form['from']
        to_whom = request.form['to']
        amount = request.form['amount']
        new_hash = request.form['hash']
        change_block(filename, from_who, to_whom, amount, new_hash)
        return render_template('edit.html',
                               edited='BLOCK IS CHANGED!',
                               number=num,
                               block=block,
                               last_file=last_file)
def edit(num):
    files = int_and_sort(os.listdir(blockchain_dir))
    last_file = files[-1]
     filename = blockchain_dir + str(num)