MCPcopy Index your code
hub / github.com/codebasics/cool_python_apps / mine

Function mine

2_bitcoin_mining/bitcoin_mining.py:7–16  ·  view source on GitHub ↗
(block_number, transactions, previous_hash, prefix_zeros)

Source from the content-addressed store, hash-verified

5 return sha256(text.encode("ascii")).hexdigest()
6
7def mine(block_number, transactions, previous_hash, prefix_zeros):
8 prefix_str = '0'*prefix_zeros
9 for nonce in range(MAX_NONCE):
10 text = str(block_number) + transactions + previous_hash + str(nonce)
11 new_hash = SHA256(text)
12 if new_hash.startswith(prefix_str):
13 print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
14 return new_hash
15
16 raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")
17
18if __name__=='__main__':
19 transactions='''

Callers 1

bitcoin_mining.pyFile · 0.85

Calls 1

SHA256Function · 0.85

Tested by

no test coverage detected