Beispiel #1
0
def mine_block(txn):
    from BlockchainCode import Blockchain

    bch = Blockchain.Blockchain()
    # Adding the block to a virtual blockchain
    new_block = bch.add_block(txn, "node1")

    # Validating it with other nodes and the network logic and appending it to the actual, if validated successfully
    if bch.validate_block("node1"):
        new_block_data = [
            new_block.hash,
            "0",
            new_block.timestamp,
            new_block.nonce,
            "0",
            new_block.transactions,
            len(new_block.transactions[0])
        ]

        new_blockname = new_block.block_identifier(new_block.transactions)
        # Access and insert the newest block in the database "user_information" and table node1
        import pymysql.cursors
        conn = pymysql.connect(host="localhost",
                               user="******",
                               password="******",
                               db="user_information",
                               charset='utf8mb4',
                               cursorclass=pymysql.cursors.DictCursor)
        try:
            with conn.cursor() as cursor:
                # Create a new record
                sql = "INSERT INTO `node1` (`blockname`, `blockdata`) VALUES (%s, %s)"
                cursor.execute(sql, (str(new_blockname), str(new_block_data)))
            conn.commit()
        finally:
            conn.close()
        return True
    else:
        # Print an error message
        print("CANNOT MINE BLOCK!")
        return False
Beispiel #2
0
 def __init__(self):
     self.bch = Blockchain.Blockchain()