-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiner.py
83 lines (65 loc) · 2.52 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import jsonschema
from time import sleep
from threading import Thread
from queue import Queue
from utility.json_validation import block_schema
from object.Object import Object
NONCE_OFFSET = 10**18
class Miner:
@staticmethod
def mine(block_json, start_nonce=0, thread_num=4):
try:
jsonschema.validate(block_json, block_schema)
except jsonschema.exceptions.ValidationError as e:
return False
# create a queue to receive results from each thread
result_queue = Queue()
# one queue to receive the nonce from each thread
nonce_queues = [Queue() for _ in range(thread_num)]
start_nonces = [start_nonce + i * NONCE_OFFSET for i in range(thread_num)]
# create a list of threads
threads = [
Thread(
target=Miner.mine_thread,
args=(block_json, start_nonces[i], result_queue, nonce_queues[i]),
)
for i in range(thread_num)
]
print("Start nonces are: ", start_nonces)
print("Start mining...")
# start all threads
for thread in threads:
thread.start()
sleep(0.5)
# print current nonce
# get the nonce from each thread, print them in one line
# make sure they are in the right order
while True:
# always just get the last nonce, clear the queue
nonces = [int(nonce_queues[i].get()) for i in range(thread_num)]
# sort nonces
nonces.sort()
# print(f"\x1b[KCurrent nonces: {nonces}", end="\n")
if not result_queue.empty():
break
# wait for first thread to finish
result = result_queue.get()
# force stop all threads
for thread in threads:
thread.join()
return result
@staticmethod
def mine_thread(block_json, start_nonce, result_queue, nonce_queue):
block_json["nonce"] = str(start_nonce)
hash = Object.get_id_from_json(block_json)
print("Nonce shall be ", str(start_nonce))
print("Start with nonce ", block_json["nonce"])
sleep(1)
while int(hash, 16) > int(block_json["T"], 16):
block_json["nonce"] = str(int(block_json["nonce"]) + 1)
hash = Object.get_id_from_json(block_json)
print(block_json["nonce"], end="\n")
nonce_queue.put(block_json["nonce"])
sleep(0.1)
print("Found nonce ", block_json["nonce"])
result_queue.put(block_json)