def mining(): print("Into the mining process") # Initializing Election Class elec = Election() # Do not make a block if mempool is empty # if elec.redis_client.llen("mempool") == 0: # print("No block made! Mempool is empty!") # return # Transaction verification # Initializing Block Class blk = Block() # Create Coinbase Transaction blk.create_coinbase_transaction() # Connect to Previous Block blk.add_previous_block() # Scan Mempool vd = True for i in range(0, elec.redis_client.llen("mempool")): # Get Transaction tx = elec.redis_client.lindex('mempool', i).decode('utf-8') if tx == None: # Exit if tx is None break # Get tx verification verdict # verify_verdict = elec.verification.verify_tx(tx) # if verify_verdict == "verified": # # Sending data to block # blk.add_transaction(tx) # else: # vd = False # print("Some Transaction Verification Failed! Aborting Mining ...") # break # If Tx Verification Fails if vd == False: print("Mining Aborted!") return # create block blk.compute_hash() blk.calculate_merkle_root() block = blk # add block blkChain = BlockChain() blkChain.add_block(block) print("Block added to this Node's blockchain!") # check # full Blockchain verify # full_verify_message = elec.verification.full_chain_verify() # if msg != "verified": # return sync.chainsync() # if full_verify_message == "verified": # braodcast the block you made print("Broadcasting block made by this node ...") udphandler = UDPHandler() udphandler.sendblock(block.to_json())
def create_block(self): block = Block() block.create_coinbase_transaction() mempool = Mempool() blkc = BlockChain() mempool_size = mempool.get_len() if mempool_size == 0: return while mempool_size > 0: tx = mempool.get_transaction() block.add_transaction(tx) mempool_size -= 1 block.calcalute_block_size() block.calculate_merkle_root() block.compute_hash() blkc.add_block(block) return block
# "outputs": [ # { # "value": 50, # "n": 0, # "address": addr # } # ], # "hash": "eef9fda50a6bf6c11c5078d8772d94df4f60ce54573c009f145eb047426ad0fb", # "block": "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c" # }], # "previous_block_hash": "", # "merkle_root": "78bc90dcc3fe2ae1eca8b9d4e09b63653cd18f0b583e002b2b4d43cc09fca9cd", # "height": 0, # "version": "0.0.1", # "size": 1949 # })) for i in range(1): block = Block() block.add_previous_block() block.add_transaction(create_fake_transaction( global_addresses[random.randint(0, 9)])) block.create_coinbase_transaction() block.calculate_merkle_root() block.compute_hash() block.calcalute_block_size() redis_client.rpush("chain", json.dumps(block.to_json())) # print(block.to_json()) udphandler = UDPHandler() udphandler.sendblock(block.to_json())