Esempio n. 1
0
def create_genesis_block():
    func = inspect.currentframe().f_back.f_code
    logging.info("Creating a genesis block")
    logging.debug("Work:{}".format(variables.WORK))
    work_ez = int(variables.WORK / 4) + 1
    pow = "0" * work_ez
    pad = "1337"
    for i in range(4, 64):
        pow += pad[i % len(pad)]
    b = Block(0, time.time(), pow, "e", [],
              "0")
    b.data = [{"FROM": 0, "TO": 0, "AMOUNT": 0}]
    logging.info("Returning block: {}".format(b))
    return b
Esempio n. 2
0
def proof_of_work(a, last_block, data):
    func = inspect.currentframe().f_back.f_code
    logging.info("Starting proof of work")
    start = time.time()
    interval = 20
    now = time.time() + 1
    effort, pow_hash_object = Utility.genhash(last_block.index + 1, time.time(), data, last_block.hash)
    leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest())
    while leading_zeroes <= variables.WORK:
        now = time.time() + 1
        if int(now - start) % interval + 1 == 0:
            logging.debug("Checking for messages")
            messages = []
            while not a.empty():
                obj = a.get()
                logging.debug("Got {} from queue".format(obj))
                messages.append(obj)
            for message in messages:
                if message[0] == "ip":
                    logging.debug("That's an ip {} adding to peers".format(message[1]))
                    variables.PEER_NODES.append(str(messages[1]))
                    continue
                logging.debug("not an IP, putting it back message:{}".format(message))
                a.put(message)
            start = time.time()
            consensus = consensus()

            if consensus:
                logging.info("Received a consensus while doing POW")
                return False, consensus
        effort, pow_hash_object = Utility.genhash(last_block.index + 1, now, data, last_block.hash)
        leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest())
    retBlock = Block(last_block.index + 1, now, pow_hash_object.hexdigest(), effort, data, last_block.hash)
    logging.info("Farmed a block returning: {}".format(retBlock))
    return True, retBlock
Esempio n. 3
0
def find_new_chains():
    func = inspect.currentframe().f_back.f_code
    logging.info("Starting to find new chains")
    # Get the blockchains of every other node
    peers = variables.PEER_NODES
    logging.debug("peers: {}".format(len(peers)))
    other_chains = []
    for node_url in peers:

        blockchain_json = None
        found_blockchain = []

        url = "http://" + node_url + ":" + str(variables.PORT) + "/blocks"
        try:
            logging.debug("Attempting to access {}".format(node_url))
            blockchain_json = requests.post(url)
        except:
            logging.warning("Failed to access {}, removing from peers".format(node_url))
            variables.PEER_NODES.remove(node_url)
            continue
        # Convert the JSON object to a Python dictionary
        if blockchain_json is not None:
            blockchain_json = json.loads(blockchain_json.content)
            for block_json in blockchain_json:
                temp = Block()
                temp.importjson(block_json)
                if Utility.validate(temp):
                    logging.debug("Block validated, adding")
                    found_blockchain.append(temp)
                else:
                    logging.warning("Block NOT valid, next peer")
                    continue
            # Verify other node block is correct
            logging.debug("Attempting to validate this: {}".format(found_blockchain))
            validated = Utility.validate_blockchain(found_blockchain)
            if validated:
                logging.debug("Blockchain did validate")
                other_chains.append(found_blockchain)
            else:
                logging.warning("Blockchain did not validate")
            continue
    logging.info("Ending find new chains")
    return other_chains
Esempio n. 4
0
import logging
FORMAT = "[{%(levelname)s} %(filename)s:%(lineno)s 	- %(funcName)20s() ] %(message)s"
logging.basicConfig(filename='scratch.log', level=logging.DEBUG, format=FORMAT)
from Mining.Block import Block

import time
import User.User as User
import Utilities.Utility as Utility
WORK = 3
BLOCKCHAIN = []
BLOCKCHAIN.append(Utility.create_genesis_block())
while True:
    if len(BLOCKCHAIN) == 2500:
        break
    last_block = BLOCKCHAIN[len(BLOCKCHAIN) - 1]
    now = time.time()
    data = [{"from": "network", "to": User.public_key, "amount": 1.0}]
    done = False
    block = None
    while not done:
        effort, pow_hash_object = Utility.genhash(last_block.index + 1, now,
                                                  data, last_block.hash)
        leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest())
        if leading_zeroes >= WORK:
            done = True
    block = Block(last_block.index + 1, now, pow_hash_object.hexdigest(),
                  effort, data, last_block.hash)
    BLOCKCHAIN.append(block)
Utility.validate_blockchain(BLOCKCHAIN)