Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Code #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions 2_bitcoin_mining/bitcoin_mining.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from hashlib import sha256

MAX_NONCE = 100000000000


def SHA256(text):
return sha256(text.encode("ascii")).hexdigest()


def mine(block_number, transactions, previous_hash, prefix_zeros):
prefix_str = '0'*prefix_zeros
prefix_str = "0" * prefix_zeros
for nonce in range(MAX_NONCE):
text = str(block_number) + transactions + previous_hash + str(nonce)
new_hash = SHA256(text)
Expand All @@ -15,16 +18,23 @@ def mine(block_number, transactions, previous_hash, prefix_zeros):

raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")

if __name__=='__main__':
transactions='''

if __name__ == "__main__":
transactions = """
Dhaval->Bhavin->20,
Mando->Cara->45
'''
difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
"""
difficulty = 4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
import time

start = time.time()
print("start mining")
new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
new_hash = mine(
5,
transactions,
"0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7",
difficulty,
)
total_time = str((time.time() - start))
print(f"end mining. Mining took: {total_time} seconds")
print(new_hash)
print(new_hash)