FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cool_python_apps/2_bitcoin_mining/bitcoin_mining.py at main · codebasics/cool_python_apps · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
codebasics
/
cool_python_apps
Public
Notifications
You must be signed in to change notification settings
Fork
270
Star
251
Code
Issues
6
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cool_python_apps
/
2_bitcoin_mining
/
bitcoin_mining.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (26 loc) · 1.11 KB
Breadcrumbs
cool_python_apps
/
2_bitcoin_mining
/
bitcoin_mining.py
Copy path
File metadata and controls
30 lines (26 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
for
nonce
in
range
(
MAX_NONCE
):
text
=
str
(
block_number
)
+
transactions
+
previous_hash
+
str
(
nonce
)
new_hash
=
SHA256
(
text
)
if
new_hash
.
startswith
(
prefix_str
):
print
(
f"Yay! Successfully mined bitcoins with nonce value:
{
nonce
}
"
)
return
new_hash
raise
BaseException
(
f"Couldn't find correct has after trying
{
MAX_NONCE
}
times"
)
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
import
time
start
=
time
.
time
()
print
(
"start mining"
)
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
)
Back
|
FazBrowse Home
|
New Git URL