-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.py
216 lines (178 loc) · 7.93 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import datetime
import logging
import os
import zipfile
import psutil
import subprocess
import urllib.request
import logging
from stratum import Stratum
from statistical import Statistical
class GPUMiner:
def __init__(self, json_data: dict, wallet: str, device_type: str):
self.stats = Statistical()
self.running = False
self.share = 0
self.process = None
self.name = json_data['name']
self.version = json_data['version']
self.url_win = json_data['url_win']
self.url_linux = json_data['url_linux']
self.exe = json_data['exe']
self.algos = json_data['algos']
self.stratums = json_data['stratums']
self.args = json_data['args']
self.wallet = wallet
self.device_type = device_type
self.folder_extracted = os.path.join('miners', f'{self.name}_{self.version}', 'extracted')
self.fd_log = None
if self.name == 'srbminer':
self.folder_extracted = os.path.join(self.folder_extracted,
f'SRBMiner-Multi-{self.version.replace(".", "-")}')
elif self.name == 'bzminer':
self.folder_extracted = os.path.join(self.folder_extracted,
f'bzminer_v{self.version}_{"windows" if os.name == "nt" else "linux"}')
elif self.name == 'pickminer':
if os.name == 'nt':
self.folder_extracted = os.path.join(os.environ["ProgramFiles(x86)"], 'pickminer')
else:
self.folder_extracted = os.path.join(self.folder_extracted,
f'pickminer_{self.version}')
elif self.name == 'teamredminer':
self.folder_extracted = os.path.join(self.folder_extracted,
f'teamredminer-v{self.version}-{"win" if os.name == "nt" else "linux"}')
elif self.name == 'lolminer':
self.folder_extracted = os.path.join(self.folder_extracted,
f'{self.version}')
elif self.name == "nbminer":
self.folder_extracted = os.path.join(self.folder_extracted,
f'NBMiner_{"Win" if os.name == "nt" else "Linux"}')
elif self.name == 'etcminer':
self.folder_extracted = os.path.join(self.folder_extracted, 'etcminer')
elif self.name == 'ethminer':
self.folder_extracted = os.path.join(self.folder_extracted, 'bin')
elif self.name == 'rigel':
self.folder_extracted = os.path.join(self.folder_extracted, f'rigel-{self.version}-{"win" if os.name == "nt" else "linux"}')
def is_running(self) -> bool:
try:
psutil.Process(self.process.pid)
except psutil.NoSuchProcess:
self.running = False
return self.running
def get_name(self) -> str:
return self.name
def get_url(self) -> str:
return self.url_win if os.name == 'nt' else self.url_linux
def get_algos(self, algo_name) -> str:
return self.algos[algo_name]
def get_device_type(self) -> str:
return self.device_type
def get_args(self) -> str:
return self.args
def get_wallet(self) -> str:
return self.wallet
def get_version(self) -> str:
return self.version
def get_folder_extracted(self) -> str:
return self.folder_extracted
def get_shares(self) -> int:
return self.share
def get_stratum_protocol(self, algo_name: str) -> str:
return self.stratums[algo_name]
def download(self) -> bool:
try:
logging.info(f'Downloading {self.get_name()}')
logging.info(f'\tVersion: {self.get_version()}')
logging.info(f'\tFolder: {self.get_folder_extracted()}')
url = self.get_url()
if self.get_name() == 'srbminer':
url = url\
.replace('download/<VERSION>', f'download/{self.get_version()}')\
.replace('<VERSION>', self.get_version().replace('.', '_'))
elif self.get_name() == 'gminer':
url = url\
.replace('download/<VERSION>', f'download/{self.get_version()}')\
.replace('<VERSION>', self.get_version().replace('.', '_'))
else:
url = url.replace('<VERSION>', self.get_version())
logging.info(f'\tURL: {url}')
folder_miner = os.path.join('miners', f'{self.name}_{self.version}')
if os.path.exists(folder_miner):
logging.info(f'Folder {folder_miner} already exist.')
return True
os.makedirs(folder_miner)
miner_filename = 'miner.' + ('zip' if os.name == 'nt' else 'gz')
miner_package = os.path.join(folder_miner, miner_filename)
# pickminer use a setup.exe to install the software.
if os.name == 'nt':
if self.name == 'pickminer':
miner_package = os.path.join(folder_miner, 'setup.exe')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
output_file, _ = urllib.request.urlretrieve(url, miner_package)
logging.info(f'\tOutput: {output_file}')
if os.name == 'nt':
if self.name == 'pickminer':
cmd = f'{output_file} /SILENT'
subprocess.check_output(cmd, shell=True)
else:
with zipfile.ZipFile(miner_package, 'r') as zip_ref:
zip_ref.extractall(os.path.join(folder_miner, 'extracted'))
else:
import tarfile
file = tarfile.open(output_file)
file.extractall(os.path.join(folder_miner, 'extracted'))
file.close()
except Exception as error:
logging.error(f'{error}')
return False
return True
def __set_fd_log(self, algo_name: str):
log_file = os.path.join('results', f'{self.device_type}_{self.name}_{algo_name}.log')
logging.info(f'{self.name} is writing LOG => {log_file}.')
self.fd_log = open(log_file, 'w')
def run(self, stratum: Stratum, algo_name: str):
exe = f'{"./" if os.name != "nt" else ""}{self.exe}{".exe" if os.name == "nt" else ""}'
params_args = self.get_args()
if '<HOST>' not in params_args:
logging.error(f'<HOST> not found !')
return
if '<PORT>' not in params_args:
logging.error(f'<PORT> not found !')
return
if '<WALLET>' not in params_args:
logging.error(f'<WALLET> not found !')
return
parameters = self.get_args() \
.replace('<HOST>', stratum.get_host()) \
.replace('<PORT>', str(stratum.get_port()))\
.replace('<WALLET>', self.get_wallet())\
.replace('<STRATUM>', self.get_stratum_protocol(algo_name))
cmd_algo = self.get_algos(algo_name)
cmd_exe = f'{exe} {cmd_algo} {parameters}'
cmd = f'cd {self.get_folder_extracted()} && {cmd_exe}'
logging.info(cmd_exe)
self.running = True
self.__set_fd_log(algo_name)
self.process = subprocess.Popen(
cmd,
stdout=self.fd_log,
stderr=self.fd_log,
shell=True)
def kill(self):
try:
self.running = False
system_process = psutil.Process(self.process.pid)
for proc in system_process.children(recursive=True):
proc.kill()
system_process.kill()
try:
if self.fd_log is not None:
self.fd_log.close()
except Exception:
# I dont know why Windows have an error here.
pass
except psutil.NoSuchProcess:
pass
def increase_share(self):
self.share += 1