Esempio n. 1
0
def mine(a):
    func = inspect.currentframe().f_back.f_code
    logging.info("Starting to mine")
    # See if other blockchains exist
    blockchain = consensus()
    if not blockchain:
        logging.info("Didn't receive a blockchain from anyone and need to make one")
        # We didn't find one, need to make one
        variables.BLOCKCHAIN.append(Utility.create_genesis_block())
    else:
        logging.info("Received a blockchain from the net")
        # See if we got any blocks from someone, save it
        variables.BLOCKCHAIN = blockchain
    message = Utility.buildmessage("blockchain", variables.BLOCKCHAIN)
    logging.debug("Adding {} to queue".format(message))
    a.put(message)
    url = "http://" + variables.MINER_NODE_URL + ":" + str(variables.PORT) + "/blocks?update=" + User.public_key
    logging.debug("accessing url via GET")
    requests.get(url)
    logging.debug("Done accessing url")
    while True:
        last_block = variables.BLOCKCHAIN[len(variables.BLOCKCHAIN) - 1]
        #   -get transactions
        url = "http://" + variables.MINER_NODE_URL + ":" + str(variables.PORT) + "/txion?update=" + User.public_key
        logging.debug("Getting transactions from {}".format(url))
        transactions = requests.get(url).content
        logging.debug("Done getting transactions")
        variables.PENDING_TRANSACTIONS = json.loads(transactions)
        logging.warning("type of transaction: {}".format(type(variables.PENDING_TRANSACTIONS)))
        variables.PENDING_TRANSACTIONS.append({
            "from": "network",
            "to": User.public_key,
            "amount": 1.0})
        # mine using the updated blockchain
        pow, pow_output = proof_of_work(a, last_block, variables.PENDING_TRANSACTIONS)
        variables.PENDING_TRANSACTIONS = []
        if pow:
            logging.info("Mined a block {}".format(pow_output))
            variables.BLOCKCHAIN.append(pow_output)
        else:
            logging.info("Consensus returned a blockchain {}".format(pow_output))
            variables.BLOCKCHAIN = pow_output
        logging.debug("Adding that blockchain to the Queue")
        a.put(["mine", variables.BLOCKCHAIN])
        url = "http://" + variables.MINER_NODE_URL + ":" + str(variables.PORT) + "/blocks?update=" + User.public_key
        logging.debug("accessing url via GET")
        requests.get(url)
        logging.debug("Done accessing url")
Esempio n. 2
0
def get_blocks():
    func = inspect.currentframe().f_back.f_code
    ip = request.remote_addr
    logging.info("/blocks accessed from {} via {}".format(ip, request.method))
    if request.method == 'POST':
        if str(ip) != "127.0.0.1" and ip not in variables.PEER_NODES:
            logging.debug("We didn't know that IP, adding it to Q")
            message = Utility.buildmessage("ip", ip)
            logging.debug("message: {}".format(message))
            q.put(message)
    # Load current blockchain. Only you should update your blockchain
    qfrom = "other"
    if request.args.get("update") == User.public_key:
        logging.debug("update was from our public, we updated our blockchain")
        qget = q.get()
        logging.debug("qget is {}".format(qget))
        qfrom = qget[0]
        variables.BLOCKCHAIN = qget[1]
        logging.info("Done updating our blockchain")

        return "200"
    else:
        chain_to_send = variables.BLOCKCHAIN
        logging.debug("Chain to send:{}".format(chain_to_send))
        logging.debug(
            "request was not from us, we need to give them our blockchain")
        # Converts our blocks into dictionaries so we can send them as json objects later
        chain_to_send_json = []
        for block in chain_to_send:
            logging.debug("block to send TYPE:{} details:{}".format(
                type(block), block))
            try:
                chain_to_send_json.append(block.exportjson())
            except AttributeError:
                logging.error("This is not a block {}".format(block))

        # Send our chain to whomever requested it
        chain_to_send = json.dumps(chain_to_send_json)
        logging.debug("Sending {}".format(chain_to_send))
        logging.info("Done sending out our blockchain")
        return chain_to_send