Пример #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
Пример #2
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
Пример #3
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)
Пример #4
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)