Skip to content

Commit d878f67

Browse files
Merge pull request #994 from NEERAJAP2001/Bit_coin
Added Bit coin Mining script
2 parents 7b938a9 + 14b80f1 commit d878f67

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
# INPUT : Transactions = '''Neeraj->Zara->50,Nandu->Allu->5''' , difficulty=2 ,
3+
# Previous_hash = a7sdxa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7
4+
5+
# OUTPUT : Successfully mined bitcoins with nonce value:336 end mining. Mining took: 12.207852363586426 seconds
6+
# 006f74cef9d071afa15c58b38198be14f9b4aabb4cd6f7a44afffd9f6968efcd
7+
8+
# Import the sha256 function
9+
from hashlib import sha256
10+
11+
# Nonce value
12+
MAX_NONCE = 10000
13+
14+
15+
# Function for encoding text to a 64 bit hexadecimal value
16+
def SHA256(text):
17+
return sha256(text.encode("ascii")).hexdigest()
18+
19+
20+
# function for guessing nonce value
21+
def mine(block_number, transactions, previous_hash, prefix_zeros):
22+
23+
# string with difficulty zeroes
24+
prefix_str = '0'*prefix_zeros
25+
26+
# nonce is the value we want
27+
for nonce in range(MAX_NONCE):
28+
29+
# concatinating the string and encoding it
30+
text = str(block_number) + transactions + previous_hash + str(nonce)
31+
new_hash = SHA256(text)
32+
33+
# if matched the mined successfully
34+
if new_hash.startswith(prefix_str):
35+
print(f"Successfully mined bitcoins with nonce value:{nonce}")
36+
return new_hash
37+
38+
# might raise exception due to hardware issues etc
39+
raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")
40+
41+
42+
# Driver Code
43+
if __name__=='__main__':
44+
45+
# Transactions string
46+
transactions=input('Enter Transactions : ')
47+
48+
# Number of prefix zeroes
49+
difficulty=int(input('Enter Difficulty level : '))
50+
51+
# For knowing time taken for mining
52+
import time
53+
start = time.time()
54+
print("start mining")
55+
56+
previous_hash=input('Enter Previous has value : ')
57+
58+
59+
# Calling mine function with all required parameters
60+
new_hash = mine(5,transactions,previous_hash, difficulty)
61+
62+
# total time for refrence
63+
total_time = str((time.time() - start))
64+
print(f"end mining. Mining took: {total_time} seconds")
65+
print(new_hash)
Loading
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# What Is Bitcoin Mining?
2+
Bitcoin mining is performed by high-powered computers that solve complex computational math problems; these problems are so complex that they cannot be solved by hand and are complicated enough to tax even compelling computers.
3+
4+
## Terminology :
5+
1. Transaction: An transaction is a transfer of Bitcoin value and is collected in Blocks
6+
7+
2. SHA256: a function that can generate an almost-unique 256-bit (32-byte) signature(Hexa-Decimal) for a text.
8+
9+
3. Block: Blocks are files where data about the Bitcoin network are permanently recorded
10+
11+
4. Nonce: Miners have to guess this number which will reward them BTC. A nonce is an abbreviation for "number only used once," which is a number added to a hashed—or encrypted—block in a blockchain that, when rehashed, meets the difficulty level restrictions.
12+
13+
5. Difficulty Level: Number of prefix Contagious Zeroes
14+
15+
16+
## Theory :
17+
BTC will be rewarded if Nonce gives us a string with Zeroes' prefix the difficulty number of times. Sounds confusing?
18+
So, Let's take an example :
19+
1. Let's assume we have a difficulty level = 20
20+
2. Now, Let's again assume that we have the previous hash as: 'a5x208fecf9a66be9a2bc7...'
21+
3. Now we create text as : text = str(block_number) + transactions + previous_hash + str(nonce)
22+
4. Now, we pass text to SHA256 Function to generate hash
23+
5. Finally, that hash prefix must be equal to the number of zeroes as difficulty level.
24+
6. And Boom!! You have mined successfully.
25+
26+
## To Run the script :
27+
1. Run Command ```BitCoin_Mining.py```.
28+
2. Now Enter Required Values.
29+
3. You have Mined succesfully!!.
30+
31+
## Sample Test Case :
32+
INPUT : Transactions = '''Neeraj->Zara->50,Nandu->Allu->5''', difficulty=2,
33+
Previous_hash = a7sdxa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7.
34+
OUTPUT : Successfully mined bitcoins with nonce value:336 end mining. Mining took: 12.207852363586426 seconds
35+
006f74cef9d071afa15c58b38198be14f9b4aabb4cd6f7a44afffd9f6968efcd.
36+
37+
## Draw Backs:
38+
The script has no drawbacks, but due to the increase in miners, the difficulty level increases, and hence we'd require the best hardware as the fastest wins.
39+
40+
# Refrences
41+
[Investopedia](https://www.investopedia.com/terms/b/bitcoin.asp)
42+
43+
[Computerphile Video](https://www.youtube.com/watch?v=wTC31ZI6QM4)
44+
45+
[Codebasics Video](https://www.youtube.com/watch?v=ZhnJ1bkIWWk&t=143s)
46+
47+
48+
## OUTPUT :
49+
![Sample](https://user-images.githubusercontent.com/65017645/121875123-b9d28e00-cd25-11eb-83cf-08980046c540.png)
50+
51+
52+
53+
54+
Script By : Neeraj Ap
55+
56+
57+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)