Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created January 18, 2021 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/c6a58e8f6f65606d18ea111e640b6129 to your computer and use it in GitHub Desktop.
Save amankharwal/c6a58e8f6f65606d18ea111e640b6129 to your computer and use it in GitHub Desktop.
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment