Movatterモバイル変換


[0]ホーム

URL:


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

Commitb504dc6

Browse files
authored
added bitcoin mining program in python👩‍💻
credits -https://youtu.be/ZhnJ1bkIWWk
1 parent18439fd commitb504dc6

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+
fromhashlibimportsha256
2+
MAX_NONCE=100000000000
3+
4+
defSHA256(text):
5+
returnsha256(text.encode("ascii")).hexdigest()
6+
7+
defmine(block_number,transactions,previous_hash,prefix_zeros):
8+
prefix_str='0'*prefix_zeros
9+
fornonceinrange(MAX_NONCE):
10+
text=str(block_number)+transactions+previous_hash+str(nonce)
11+
new_hash=SHA256(text)
12+
ifnew_hash.startswith(prefix_str):
13+
print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
14+
returnnew_hash
15+
16+
raiseBaseException(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+
importtime
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)

[8]ページ先頭

©2009-2025 Movatter.jp