Exemplo n.º 1
0
def create_purchase_transaction():
    # Resolve chain conflicts before making changes
    blockchain.resolve_conflicts()

    # Run the proof of work algorithm to get the next proof
    last_block = blockchain.last_block
    proof = blockchain.proof_of_work(last_block)

    # Get data from the URL request
    values = request.get_json()

    # Create a new transaction
    blockchain.new_transaction({
        "sender": values["sender"],
        "recipient": node_identifier,
        "amount": int(values["amount"]),
        "type": "transaction"
    })

    # Forge the new block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)

    response = {
        "index": block["index"],
        "transactions": block["transactions"],
        "proof": block["proof"],
        "previous_hash": block["previous_hash"],
    }

    return jsonify(response), 201
Exemplo n.º 2
0
def mining():
    # find new proof by previous proof
    latest_block = blockchain.latest_block
    prev_proof = latest_block[PROOF]
    new_proof = blockchain.proof_of_work(prev_proof)

    # get reward for found a proof
    blockchain.add_new_transaction(
        sender="0",  # sender=0 means that recipient got an award
        recipient=node_id,
        total=1)

    # create a new block, add to a chain
    prev_hash = blockchain.hash_block(latest_block)
    new_block = blockchain.add_block(new_proof, prev_hash)

    resp = {
        MESSAGE: "New Block created",
        INDEX: new_block[INDEX],
        TRANSACTIONS: new_block[TRANSACTIONS],
        PROOF: new_block[PROOF],
        PREV_HASH: new_block[PREV_HASH]
    }

    return jsonify(resp), 200
Exemplo n.º 3
0
def mineBlock():
    # We run the proof of work algorithm to get the next proof...
    last_block = blockchain.last_block
    proof = blockchain.proof_of_work(last_block)

    # We must receive a reward for finding the proof.
    # The sender is "0" to signify that this node has mined a new coin.
    blockchain.new_transaction(
        sender="0",
        recipient=node_identifier,
        amount=1,
    )

    # Forge the new Block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)

    minedBlock = {
        'message': "Congrats you mined and attached block",
        'index': block['index'],
        'transactions': block['transactions'],
        'proof': block['proof'],
        'previous_hash': block['previous_hash'],
    }
    return minedBlock
Exemplo n.º 4
0
def create_user():
    blockchain.resolve_conflicts()

    # Run the proof of work algorithm to get the next proof
    last_block = blockchain.last_block
    proof = blockchain.proof_of_work(last_block)

    # Generate a globally unique address for this user
    user_identifier = str(uuid4()).replace("-", "")

    # Retrieve user data from request
    values = request.get_json()
    name = values["name"]

    # Add an initial amount to the new user's balance
    blockchain.new_transaction({
        "user_id": user_identifier,
        "balance": 10,
        "name": name,
        "type": "new_user"
    })

    # Forge the new block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)

    response = {
        "index": block["index"],
        "transactions": block["transactions"],
        "proof": block["proof"],
        "previous_hash": block["previous_hash"],
    }

    return jsonify(response)
Exemplo n.º 5
0
def create_shelter():
    # Resolve chain conflicts before making changes
    blockchain.resolve_conflicts()

    # Run the proof of work algorithm to get the next proof
    last_block = blockchain.last_block
    proof = blockchain.proof_of_work(last_block)

    # Generate a globally unique address for this shelter
    shelter_identifier = str(uuid4()).replace('-', '')

    # Retrieve shelter data from request
    values = request.get_json()
    name = values["name"]
    address = values["address"]
    lat = float(values["lat"])
    lng = float(values["lng"])
    data = json.loads(values["data"])

    # Create this shelter in the blockchain
    blockchain.new_transaction({
        "shelter_id": shelter_identifier,
        "name": name,
        "address": address,
        "lat": lat,
        "lng": lng,
        "data": data,
        "type": "new_shelter"
    })

    # Forge the new block by adding it to the chain
    previous_hash = blockchain.hash(last_block)
    block = blockchain.new_block(proof, previous_hash)

    response = {
        "index": block["index"],
        "transactions": block["transactions"],
        "proof": block["proof"],
        "previous_hash": block["previous_hash"],
    }

    return jsonify(response)