MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / mine

Function mine

web/public/playground/bitcoin-mining.py:18–29  ·  view source on GitHub ↗
(block_number, transactions, previous_hash, prefix_zeros)

Source from the content-addressed store, hash-verified

16
17# Try nonces until the hash starts with enough zeros
18def 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
32if __name__ == "__main__":

Callers 1

bitcoin-mining.pyFile · 0.70

Calls 1

SHA256Function · 0.70

Tested by

no test coverage detected