Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b504dc6

Browse files
added bitcoin mining program in python👩‍💻
credits - https://youtu.be/ZhnJ1bkIWWk
1 parent 18439fd commit b504dc6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from hashlib import sha256
2+
MAX_NONCE = 100000000000
3+
4+
def SHA256(text):
5+
return sha256(text.encode("ascii")).hexdigest()
6+
7+
def 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+
18+
if __name__=='__main__':
19+
transactions='''
20+
Dhaval->Bhavin->20,
21+
Mando->Cara->45
22+
'''
23+
difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
24+
import time
25+
start = time.time()
26+
print("start mining")
27+
new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
28+
total_time = str((time.time() - start))
29+
print(f"end mining. Mining took: {total_time} seconds")
30+
print(new_hash)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /