I want to get python code that will work for real life bitcoin mining. Is there anybody who could help me? I have gotten the following script which works as simulator only.
import hashlib
import time
def calculate_hash(block_header):
return hashlib.sha256(block_header.encode()).hexdigest()
def mine_block(previous_hash, transactions, difficulty):
nonce = 0
start_time = time.time()
while True:
block_header = f"{previous_hash}{transactions}{nonce}"
block_hash = calculate_hash(block_header)
if block_hash[:difficulty] == '0' * difficulty:
end_time = time.time()
print(f"Block mined!")
print(f"Hash: {block_hash}")
print(f"Nonce: {nonce}")
print(f"Time taken: {end_time - start_time:.2f} seconds")
print(block_header)
return block_hash, nonce
nonce += 1
# Example usage
previous_hash = '0000000000000000000000000000000000000000000000000000000000000000'
transactions = 'tx1->tx2->tx3'
difficulty = 4 # Number of leading zeros required in the hash
mine_block(previous_hash, transactions, difficulty)