-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.cpp
315 lines (272 loc) · 8.9 KB
/
main.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// VeriBlock PoW GPU Miner
// Copyright 2017-2018 VeriBlock, Inc.
// All rights reserved.
// https://www.veriblock.org
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <thread>
#include "Miner.h"
#ifdef _WIN32
#include <VersionHelpers.h>
#elif __linux__
#include <sys/socket.h>
#include <netdb.h>
#endif
// TODO(mks): Rework logging.
bool verboseOutput = false;
char outputBuffer[8192];
void vprintf(char* toprint) {
if (verboseOutput) {
printf(toprint);
}
}
void promptExit(int exitCode) {
std::cout << "Exiting in 10 seconds..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
exit(exitCode);
}
#ifdef _WIN32
static WSADATA g_wsa_data;
char net_init(void) {
return (WSAStartup(MAKEWORD(2, 2), &g_wsa_data) == NO_ERROR);
}
void net_deinit(void) { WSACleanup(); }
#else
char net_init(void) { return 1; }
void net_deinit(void) {}
#endif
string net_dns_resolve(const char* hostname) {
struct addrinfo hints, *results, *item;
int status;
char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; /* AF_INET6 to force version */
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(hostname, NULL, &hints, &results)) != 0) {
fprintf(stderr, "failed to resolve hostname \"%s\": %s", hostname,
gai_strerror(status));
return "invalid hostname";
}
printf("IP addresses for %s:\n\n", hostname);
string ret;
for (item = results; item != NULL; item = item->ai_next) {
void* addr;
char* ipver;
/* get pointer to the address itself */
/* different fields in IPv4 and IPv6 */
if (item->ai_family == AF_INET) /* address is IPv4 */
{
struct sockaddr_in* ipv4 = (struct sockaddr_in*)item->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else /* address is IPv6 */
{
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)item->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
/* convert IP to a string and print it */
inet_ntop(item->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
ret = ipstr;
}
freeaddrinfo(results);
return ret;
}
void printHelpAndExit() {
printf("VeriBlock vBlake GPU CUDA Miner v1.0\n");
printf("Required Arguments:\n");
printf(
"-o <poolAddress> The pool address to mine to in the format "
"host:port\n");
printf(
"-u <username> The username (often an address) used at the "
"pool\n");
printf("Optional Arguments:\n");
printf(
"-p <password> The miner/worker password to use on the "
"pool\n");
printf(
"-d <deviceList> Comma-separated list of device numbers to "
"use (default all).\n");
printf(
"-tpb <threadPerBlock> The threads per block to use with the Blake "
"kernel (default %d)\n",
DEFAULT_THREADS_PER_BLOCK);
printf(
"-bs <blockSize> The blocksize to use with the vBlake kernel "
"(default %d)\n",
DEFAULT_BLOCK_SIZE);
printf(
"-l <enableLogging> Whether to log to a file (default true)\n");
printf(
"-v <enableVerboseOutput> Whether to enable verbose output for "
"debugging (default false)\n");
printf("\n");
printf("Example command line:\n");
printf(
"VeriBlock-NodeCore-PoW-CUDA -u VHT36jJyoVFN7ap5Gu77Crua2BMv5j -o "
"testnet-pool-gpu.veriblock.org:8501 -l false\n");
promptExit(0);
}
int main(int argc, char* argv[]) {
try {
// Check for help argument (only -h)
for (int i = 1; i < argc; i++) {
char* argument = argv[i];
if (!strcmp(argument, "-h")) {
printHelpAndExit();
}
}
if (argc % 2 != 1) {
snprintf(outputBuffer, sizeof(outputBuffer),
"GPU miner must be provided valid argument pairs!");
std::cerr << outputBuffer << std::endl;
printHelpAndExit();
}
string hostAndPort = ""; // "94.130.64.18:8501";
string username = ""; // "VGX71bcRsEh4HZzhbA9Nj7GQNH5jGw";
string password = "";
int threadsPerBlock = DEFAULT_THREADS_PER_BLOCK;
int blockSize = DEFAULT_BLOCK_SIZE;
std::set<int> deviceList;
if (argc > 1) {
for (int i = 1; i < argc; i += 2) {
char* argument = argv[i];
printf("%s\n", argument);
if (argument[0] == '-' && argument[1] == 'd') {
char* pch = strtok(argv[i + 1], ",");
while (pch != NULL) {
int device = atoi(pch);
std::cout << device << "\n";
deviceList.insert(device);
pch = strtok(NULL, ",");
}
} else if (!strcmp(argument, "-o")) {
hostAndPort = string(argv[i + 1]);
} else if (!strcmp(argument, "-u")) {
username = string(argv[i + 1]);
} else if (!strcmp(argument, "-p")) {
password = string(argv[i + 1]);
} else if (!strcmp(argument, "-tpb")) {
threadsPerBlock = std::stoi(argv[i + 1]);
} else if (!strcmp(argument, "-bs")) {
blockSize = std::stoi(argv[i + 1]);
} else if (!strcmp(argument, "-l")) {
// to lower case conversion
for (int j = 0; j < strlen(argv[i + 1]); j++) {
argv[i + 1][j] = tolower(argv[i + 1][j]);
}
if (!strcmp(argv[i + 1], "true") || !strcmp(argv[i + 1], "t")) {
Log::setEnabled(true);
} else {
Log::setEnabled(false);
}
} else if (!strcmp(argument, "-v")) {
// to lower case conversion
for (int j = 0; j < strlen(argv[i + 1]); j++) {
argv[i + 1][j] = tolower(argv[i + 1][j]);
}
if (!strcmp(argv[i + 1], "true") || !strcmp(argv[i + 1], "t")) {
verboseOutput = true;
} else {
verboseOutput = false;
}
}
}
} else {
printHelpAndExit();
}
if (HIGH_RESOURCE) {
snprintf(outputBuffer, sizeof(outputBuffer), "Resource Utilization: HIGH");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
} else {
snprintf(outputBuffer, sizeof(outputBuffer), "Resource Utilization: LOW");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
}
if (NVML) {
snprintf(outputBuffer, sizeof(outputBuffer), "NVML Status: ENABLED");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
} else {
snprintf(outputBuffer, sizeof(outputBuffer), "NVML Status: DISABLED");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
}
if (CPU_SHARES) {
snprintf(outputBuffer, sizeof(outputBuffer), "Share Type: CPU");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
} else {
snprintf(outputBuffer, sizeof(outputBuffer), "Share Type: GPU");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
}
if (BENCHMARK) {
snprintf(outputBuffer, sizeof(outputBuffer), "Benchmark Mode: ENABLED");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
} else {
snprintf(outputBuffer, sizeof(outputBuffer), "Benchmark Mode: DISABLED");
std::cerr << outputBuffer << std::endl;
Log::info(outputBuffer);
}
#ifdef _WIN32
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
#else
#endif
if (hostAndPort.compare("") == 0) {
string error =
"You must specify a host in the command line arguments! Example: \n-o "
"127.0.0.1:8501 or localhost:8501";
std::cerr << error << std::endl;
Log::error(error);
promptExit(-1);
}
if (username.compare("") == 0) {
string error =
"You must specify a username in the command line arguments! Example: "
"\n-u V5bLSbCqj9VzQR3MNANqL13YC2tUep";
std::cerr << error << std::endl;
Log::error(error);
promptExit(-1);
}
string host = hostAndPort.substr(0, hostAndPort.find(":"));
// GetHostByName
net_init();
host = net_dns_resolve(host.c_str());
net_deinit();
string portString = hostAndPort.substr(hostAndPort.find(":") + 1);
// Ensure that port is numeric
if (portString.find_first_not_of("1234567890") != string::npos) {
string error =
"You must specify a host in the command line arguments! Example: \n-o "
"127.0.0.1:8501 or localhost:8501";
std::cerr << error << std::endl;
Log::error(error);
promptExit(-1);
}
int port = std::stoi(portString);
snprintf(
outputBuffer, sizeof(outputBuffer),
"Attempting to mine to pool %s:%d with username %s and password %s...",
host.c_str(), port, username.c_str(), password.c_str());
std::cout << outputBuffer << std::endl;
Log::info(outputBuffer);
try {
UCPClient ucpClient(host, port, username, password);
startMining(ucpClient, deviceList, threadsPerBlock, blockSize);
} catch(std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
}
} catch(std::exception& e) {
std::cout << "exception: " << e.what() << std::endl;
}
return 0;
}