Advertisement
A_GUES

cryptocurrency

Jul 20th, 2023
1,495
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. from hashlib import sha256
  2. from time import sleep
  3. MAX_NONCE = 100000000000
  4.  
  5. def SHA256(text):
  6.     return sha256(text.encode("ascii")).hexdigest()
  7.  
  8. def mine(block_number, transactions, previous_hash, prefix_zeros):
  9.     prefix_str = '0'*prefix_zeros
  10.     for nonce in range(MAX_NONCE):
  11.         text = str(block_number) + transactions + previous_hash + str(nonce)
  12.         new_hash = SHA256(text)
  13.         if new_hash.startswith(prefix_str):
  14.             print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
  15.             return new_hash
  16.  
  17.     raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")
  18.  
  19. if __name__=='__main__':
  20.     transactions='''
  21.    Dhaval->Bhavin->20,
  22.    Mando->Cara->45
  23.    '''
  24.     difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
  25.     import time
  26.     start = time.time()
  27.     sleep(3)
  28.     print("started mining...")
  29.     sleep(3)
  30.     print("||")
  31.     sleep(1)
  32.     print("||")
  33.     sleep(1)
  34.     print("||")
  35.     sleep(1)
  36.     print("||")
  37.     sleep(1)
  38.     print("||")
  39.     sleep(1)
  40.     print("\/")
  41.     sleep(2)
  42.     new_hash = mine(5,transactions,'0000000a036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
  43.     sleep(2)
  44.     total_time = str((time.time() - start))
  45.     sleep(3)
  46.     print(f"ending mining. Mining took: {total_time} seconds")
  47.     sleep(2)
  48.     print(new_hash)
  49.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement