Created
March 14, 2022 09:53
-
-
Save stereosupersonic/337fe1c08797623b615db5567b65c1a2 to your computer and use it in GitHub Desktop.
bitcoin miner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import sha256 | |
MAX_NONCE = 10000000 | |
def create_sha256(text): | |
return sha256(text.encode("ascii")).hexdigest() | |
def mining(block_number, transactions, prev_hash, difficulty): | |
prefix = "0" * difficulty | |
for nonce in range(MAX_NONCE): | |
data = str(block_number) + transactions + prev_hash + str(nonce) | |
new_hash = create_sha256(data) | |
print(f"Hash: #{new_hash}") | |
if new_hash.startswith(prefix): | |
return new_hash | |
raise BaseException(f"{MAX_NONCE} tries!, Not found!") | |
if __name__ == "__main__": | |
current_block = "723456" # blockzeit | |
transactions = """ | |
Alice sent Bob 20BTC | |
Dan sent Alice 0.1BTC | |
Joe sent Bob 0.1BTC | |
""" | |
prev_hash = "4de47816db8157eee55ca16a14c0ef117978fd1ec6925b7bd629c57ba9bc9de1" | |
difficulty = 5 | |
import time | |
print("start mining") | |
start = time.time() | |
hash = mining(current_block, transactions, prev_hash, difficulty) | |
total_time_in_sec = time.time() - start | |
print("Hash gefunden: #" + hash) | |
print("Dauer: {:.1f} Sekunden".format(total_time_in_sec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment