Exemplo n.º 1
0
def mine():
    # Run the proof-of-work algorithm to get the next proof
    last_block = Blockchain.last_block
    proof = Blockchain.proof_of_work(last_block)

    # Receive a reward for finding the proof
    # A sender "0" signifies that the node has mined a new coin
    Blockchain.new_transaction(
        sender="0",
        recipient=node_identifier,
        amount=1,
    )

    # Forge a new block by adding transaction to the chain
    previous_hash = Blockchain.hash(last_block)
    block = Blockchain.new_block(proof, previous_hash)

    response = {
        'message': "New Block forged",
        'index': block['index'],
        'transactions': block['transactions'],
        'proof': block['proof'],
        'previous_hash': block['previous_hash'],
    }

    return jsonify(response), 200
Exemplo n.º 2
0
def new_transaction():
    values = request.get_json()

    # Are the required fields in the POSTed data?
    required = ['sender', 'receiver', 'amount']
    if not all(k in values for k in required):
        return 'Missing Values', 400

    # Create a new transaction
    index = Blockchain.new_transaction(values['sender'], values['receiver'],
                                       values['amount'])
    response = {'message': f'Transaction will be added to Block {index}'}

    return jsonify(response), 201