forked from bloominstituteoftechnology/CS-Build-Week-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.py
41 lines (32 loc) · 1.19 KB
/
miner.py
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
31
32
33
34
35
36
37
38
39
40
41
import hashlib
import requests
import sys
from timeit import default_timer as timer
import random
def proof_of_work(last_proof, difficulty):
start = timer()
print("Searching for next proof")
proof = 0
total_tries = 0
prev_proof = f'{last_proof}'.encode()
last_hash = hashlib.sha256(prev_proof).hexdigest()
#while valid_proof(last_hash, proof, difficulty) is False:
while valid_proof(last_proof, proof, difficulty) is False:
#proof = random.randint(0, 10000)
proof+=1
total_tries += 1
if total_tries % 1000000 == 0:
print(total_tries/1000000,'million tries')
print("Proof found: " + str(proof) + " in " + str(timer() - start))
return proof
def valid_proof(last_hash, proof, difficulty):
guess = f'{proof}'
guess = (str(last_hash)+str(guess)).encode() #there was no ref to last hash in valid proof
guess_hash = hashlib.sha256(guess).hexdigest()
#guess_hash = hash(guess)#.hexdigest()
if difficulty is not None:
leading_zeros = "0" * difficulty
else:
leading_zeros = "0" * 6
#return guess_hash[0:difficulty] == leading_zeros
return str(guess_hash)[0:difficulty] == leading_zeros