-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine.py
68 lines (59 loc) · 2.01 KB
/
mine.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
"""
* Blockchain
* Author: Taha Canturk
* Author: Taha Canturk
* Github: kibnakamoto
* Project: Blockchain
* Date: Jan 31 - 2023
* Software Version: 1.0
"""
import multiprocessing
from collections import deque
import constants
import block
import merkletree
class Mine:
def __init__(self):
self.hashes, pubks, signatures, m_hashes, verified = merkletree.get_mempool_verification_data()
self.nonce = None
self.block_hash = None
# eliminate transactions if their signatures aren't verified
i = 0
while self.hashes and i < len(self.hashes):
if not verified[i]:
del verified[i]
del m_hashes[i]
del signatures[i]
del pubks[i]
del self.hashes[i]
i += 1
# TODO: verify if previous transaction exists
def verify_prevs() -> NotImplemented:
pass
# does the actual mining by calculating the nonce and block header hash
def find_nonce(self) -> None:
while True:
self.nonce = block.gen_nonce()
self.blck = block.Block(self.nonce, self.hashes)
if(int(self.blck.block_hash, 16) < constants.target):
break
self.block_hash = self.blck.block_hash
# find nonce using multi-threading
# t: thread count
def threading_find_nonce(self, t:int=1) -> None:
self.threads = deque(maxlen=t)
count = multiprocessing.cpu_count()
if t >= count:
t = count-1
for i in range(t):
thread = multiprocessing.Process(target=self.find_nonce, name=i)
self.threads.append(thread)
thread.start()
terminate_signal = False
while not terminate_signal:
for thread in self.threads:
thread.join(timeout=0)
if self.block_hash != None:
thread.terminate()
terminate_signal = True
self.blck.add_block(transactions=self.hashes)