(block_number, transactions, previous_hash, prefix_zeros)
| 16 | |
| 17 | # Try nonces until the hash starts with enough zeros |
| 18 | def mine(block_number, transactions, previous_hash, prefix_zeros): |
| 19 | prefix_str = "0" * prefix_zeros |
| 20 | for nonce in range(MAX_NONCE): |
| 21 | # Build the block data string with current nonce |
| 22 | text = str(block_number) + transactions + previous_hash + str(nonce) |
| 23 | new_hash = SHA256(text) |
| 24 | # Return the hash if it meets the difficulty target |
| 25 | if new_hash.startswith(prefix_str): |
| 26 | print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}") |
| 27 | return new_hash |
| 28 | |
| 29 | raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times") |
| 30 | |
| 31 | |
| 32 | if __name__ == "__main__": |
no test coverage detected