from hashlib import sha256
MAX_NONCE = 100000000000

def SHA256(text):
    return sha256(text.encode("ascii")).hexdigest()

def mine(block_number, transactions, previous_hash, prefix_zeros):
    prefix_str = '0'*prefix_zeros
    for nonce in range(MAX_NONCE):
        text = str(block_number) + transactions + previous_hash + str(nonce)
        new_hash = SHA256(text)
        if new_hash.startswith(prefix_str):
            print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
            return new_hash

    raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")

if __name__=='__main__':
    transactions='''
    Rakshith -> Rahul -> 20,
    Pratheek -> Praveen -> 45
    '''
    previou_block_hash='0000000000000000000022131d35dde7409ad4c022521a2e32c3819daf0b4347'
    difficulty=5
    block_number=5
    import time
    start = time.time()
    print("start mining")
    new_hash = mine(block_number,transactions,previou_block_hash, difficulty)
    total_time = str((time.time() - start))
    print(f"end mining. Mining took: {total_time} seconds")
    print(new_hash)