def start(): global node, blockchain genesis = Utility.create_genesis_block() blockchain = Blockchain(genesis.index, genesis.timemade, genesis.proof_of_work, genesis.effort, genesis.transactions, genesis.previous_hash) node.config['SECRET_KEY'] = Utility.createHexdigest(User.password) node.run(host="0.0.0.0", port=variables.PORT)
def start(e): global node, blockchain,mining_process,event if not len(Variables.PEER_NODES) > 0: genesis = Utility.create_genesis_block() blockchain = Blockchain(genesis) else: consensus() event = e mining_process = Process(target=Mining.mine, args=(event,)) mining_process.start() logging.debug("Mining_classes Started") node.config['SECRET_KEY'] = Utility.createHexdigest(User.password) node.run(host="0.0.0.0", port=Variables.PORT)
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")
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)
import logging FORMAT = "[{%(levelname)s} %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s" logging.basicConfig(filename='scratch.log', level=logging.DEBUG, format=FORMAT) from Blockchain_classes.Blockchain import Blockchain import time from Blockchain_classes.Block import Block import User_classes.User as User import Utilities.Utility as Utility WORK = 5 genesis = Utility.create_genesis_block() added = 0 blockchain = Blockchain(genesis) while added < 100: last_block = blockchain.last_added() 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) #this is a test .... leading_zeroes = Utility.leadingzeroes(pow_hash_object.digest()) if leading_zeroes >= WORK: done = True added += 1 b = Block(last_block.index + 1, now, pow_hash_object.hexdigest(), effort,