Ejemplo n.º 1
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
Ejemplo n.º 2
0
def proof_of_work(last_block, data):
    index_to_use = last_block.index + 1
    func = inspect.currentframe().f_back.f_code
    logging.info("Starting proof of work")
    done = False
    while not done:
        now = time.time()
        effort, pow_hash_object = Utility.genhash(index_to_use, now, data,
                                                  last_block.hash)
        leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest())
        if leading_zeroes >= variables.WORK:
            done = True
    retBlock = Block(index_to_use, now, pow_hash_object.hexdigest(), effort,
                     data, last_block.hash)
    logging.info("Farmed a block returning: {}".format(retBlock))
    return retBlock
Ejemplo n.º 3
0
def proof_of_work(last_block, data):
    global event
    func = inspect.currentframe().f_back.f_code
    done = False
    now = None
    pow_hash_object = None
    effort = None
    while not done:
        if event.is_set():
            print("Exiting block creation")
            return False
        now = time.time()
        index_to_use = last_block.index + 1
        effort, pow_hash_object = Utility.genhash(index_to_use, now, data,
                                                  last_block.hash)
        leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest())
        if leading_zeroes >= Variables.WORK:
            done = True
    return_block = Block(index_to_use, now, pow_hash_object.hexdigest(),
                         effort, data, last_block.hash)
    return return_block
Ejemplo 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)