-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpools.cpp
executable file
·345 lines (297 loc) · 8.46 KB
/
pools.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Functions which handle multiple pools data
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "miner.h"
#include "compat.h"
#include "algos.h"
// to move in miner.h
extern bool check_dups;
extern double opt_max_diff;
extern double opt_max_rate;
extern int opt_scantime;
extern int opt_shares_limit;
extern int opt_time_limit;
extern char* rpc_url;
extern char* rpc_user;
extern char* rpc_pass;
extern char* short_url;
extern struct work _ALIGN(64) g_work;
extern struct stratum_ctx stratum;
extern pthread_mutex_t stratum_work_lock;
extern pthread_mutex_t g_work_lock;
extern pthread_mutex_t g_work_time_lock;
extern pthread_mutex_t stats_lock;
extern bool stratum_need_reset;
extern time_t firstwork_time;
extern volatile time_t g_work_time;
extern volatile int pool_switch_count;
extern volatile bool pool_is_switching;
extern uint8_t conditional_state[MAX_GPUS];
extern double thr_hashrates[MAX_GPUS];
extern int dev_pool_id;
extern struct option options[];
#define CFG_NULL 0
#define CFG_POOL 1
struct opt_config_array {
int cat;
const char *name; // json key
const char *longname; // global opt name if different
} cfg_array_keys[] = {
{ CFG_POOL, "url", NULL }, /* let this key first, increment pools */
{ CFG_POOL, "user", NULL },
{ CFG_POOL, "pass", NULL },
{ CFG_POOL, "userpass", NULL },
{ CFG_POOL, "name", "pool-name" },
{ CFG_POOL, "algo", "pool-algo" },
{ CFG_POOL, "scantime", "pool-scantime" },
{ CFG_POOL, "max-diff", "pool-max-diff" },
{ CFG_POOL, "max-rate", "pool-max-rate" },
{ CFG_POOL, "disabled", "pool-disabled" },
{ CFG_POOL, "time-limit", "pool-time-limit" },
{ CFG_NULL, NULL, NULL }
};
// store current credentials in pools container, operates on a single pool instance
void pool_set_creds(struct pool_infos *p, char *full_rpc_url, char *short_rpc_url, char *rpc_username, char *rpc_password)
{
snprintf(p->url, sizeof(p->url), "%s", full_rpc_url);
snprintf(p->short_url, sizeof(p->short_url), "%s", short_rpc_url);
snprintf(p->user, sizeof(p->user), "%s", rpc_username);
snprintf(p->pass, sizeof(p->pass), "%s", rpc_password);
if (!(p->status & POOL_ST_DEFINED)) {
p->status |= POOL_ST_DEFINED;
// init pool options as "unset"
// until cmdline is fully parsed...
p->algo = -1;
p->max_diff = -1.;
p->max_rate = -1.;
p->scantime = -1;
p->shares_limit = -1;
p->time_limit = -1;
p->check_dups = check_dups;
p->status |= POOL_ST_DEFINED;
}
if (strlen(rpc_url)) {
p->type = POOL_STRATUM;
p->status |= POOL_ST_VALID;
}
}
// fill the unset pools options with cmdline ones
void pool_init_defaults(struct pool_infos *poolinfos, int number_of_pools)
{
for (int i=0; i<number_of_pools; i++) {
poolinfos[i].id = i;
if (poolinfos[i].algo == -1) poolinfos[i].algo = (int) opt_algo;
if (poolinfos[i].max_diff == -1.) poolinfos[i].max_diff = opt_max_diff;
if (poolinfos[i].max_rate == -1.) poolinfos[i].max_rate = opt_max_rate;
if (poolinfos[i].scantime == -1) poolinfos[i].scantime = opt_scantime;
if (poolinfos[i].shares_limit == -1) poolinfos[i].shares_limit = opt_shares_limit;
if (poolinfos[i].time_limit == -1) poolinfos[i].time_limit = opt_time_limit;
}
}
// attributes only set by a json pools config
void pool_set_attr(struct pool_infos *p, const char* key, char* arg)
{
if (!strcasecmp(key, "name")) {
snprintf(p->name, sizeof(p->name), "%s", arg);
return;
}
if (!strcasecmp(key, "algo")) {
p->algo = algo_to_int(arg);
return;
}
if (!strcasecmp(key, "scantime")) {
p->scantime = atoi(arg);
return;
}
if (!strcasecmp(key, "max-diff")) {
p->max_diff = atof(arg);
return;
}
if (!strcasecmp(key, "max-rate")) {
p->max_rate = atof(arg);
return;
}
if (!strcasecmp(key, "shares-limit")) {
p->shares_limit = atoi(arg);
return;
}
if (!strcasecmp(key, "time-limit")) {
p->time_limit = atoi(arg);
printf("p->time_limit = %d\n", p->time_limit);
return;
}
if (!strcasecmp(key, "disabled")) {
int removed = atoi(arg);
if (removed) {
p->status |= POOL_ST_REMOVED;
}
return;
}
}
// pool switching code
bool pool_switch(int thr_id, int pooln)
{
int prevn = cur_pooln;
bool algo_switch = false;
struct pool_infos *prev = &pools[cur_pooln];
struct pool_infos* p = NULL;
stratum_free_job(&stratum);
prev->stratum = stratum;
if (pooln < (num_pools+1)) {
cur_pooln = pooln;
p = &pools[cur_pooln];
} else {
applog(LOG_ERR, "Switch to inexistant pool %d!", pooln);
return false;
}
// save global attributes
prev->check_dups = check_dups;
pthread_mutex_lock(&stratum_work_lock);
free(rpc_user); rpc_user = strdup(p->user);
free(rpc_pass); rpc_pass = strdup(p->pass);
free(rpc_url); rpc_url = strdup(p->url);
short_url = p->short_url; // just a pointer, no alloc
opt_scantime = p->scantime;
opt_max_diff = p->max_diff;
opt_max_rate = p->max_rate;
opt_shares_limit = p->shares_limit;
opt_time_limit = p->time_limit;
// yiimp stats reporting
opt_stratum_stats = (strstr(p->pass, "stats") != NULL) || (strcmp(p->user, "benchmark") == 0);
pthread_mutex_unlock(&stratum_work_lock);
if (prevn != cur_pooln) {
pool_switch_count++;
net_diff = 0;
pthread_mutex_lock(&g_work_lock);
pthread_mutex_lock(&g_work_time_lock);
g_work_time = 0;
g_work.data[0] = 0;
pthread_mutex_unlock(&g_work_lock);
pthread_mutex_unlock(&g_work_time_lock);
pool_is_switching = true;
stratum_need_reset = true;
// used to get the pool uptime
firstwork_time = time(NULL);
printf("%s restarting all threads\n", __func__);
restart_threads();
// reset wait states
for (int n=0; n<opt_n_threads; n++)
conditional_state[n] = false;
// restore flags
check_dups = p->check_dups;
// temporary... until stratum code cleanup
stratum = p->stratum;
stratum.pooln = cur_pooln;
// unlock the stratum thread
tq_push(thr_info[stratum_thr_id].q, strdup(rpc_url));
applog(LOG_BLUE, "Switch to stratum pool %d: %s", cur_pooln,
strlen(p->name) ? p->name : p->short_url);
}
return true;
}
// search available pool
int pool_get_first_valid(struct pool_infos *infos, int startfrom)
{
int next = 0;
for (int i=0; i<num_pools; i++) {
int pooln = (startfrom + i) % num_pools;
if (!(infos[pooln].status & POOL_ST_VALID))
continue;
if (infos[pooln].status & (POOL_ST_DISABLED | POOL_ST_REMOVED))
continue;
next = pooln;
break;
}
return next;
}
// switch to next available pool
bool pool_switch_next(struct pool_infos *infos, int thr_id)
{
if (num_pools > 1) {
int pooln = pool_get_first_valid(infos, cur_pooln+1);
return pool_switch(thr_id, pooln);
} else {
// no switch possible
if (!opt_quiet)
applog(LOG_DEBUG, "No other pools to try...");
return false;
}
}
// Parse pools array in json config
bool parse_pool_array(json_t *obj)
{
size_t idx;
json_t *p, *val;
if (!json_is_array(obj))
return false;
// array of objects [ {}, {} ]
json_array_foreach(obj, idx, p)
{
if (!json_is_object(p))
continue;
for (int i = 0; i < ARRAY_SIZE(cfg_array_keys); i++)
{
int opt = -1;
char *s = NULL;
if (cfg_array_keys[i].cat != CFG_POOL)
continue;
val = json_object_get(p, cfg_array_keys[i].name);
if (!val)
continue;
for (int k = 0; k < options_count(); k++)
{
const char *alias = cfg_array_keys[i].longname;
if (alias && !strcasecmp(options[k].name, alias)) {
opt = k;
break;
}
if (!alias && !strcasecmp(options[k].name, cfg_array_keys[i].name)) {
opt = k;
break;
}
}
if (opt == -1)
continue;
if (json_is_string(val)) {
s = strdup(json_string_value(val));
if (!s)
continue;
// applog(LOG_DEBUG, "pool key %s '%s'", options[opt].name, s);
parse_arg(options[opt].val, s);
free(s);
} else {
// numeric or bool
char buf[32] = { 0 };
double d = 0.;
if (json_is_true(val)) d = 1.;
else if (json_is_integer(val))
d = 1.0 * json_integer_value(val);
else if (json_is_real(val))
d = json_real_value(val);
snprintf(buf, sizeof(buf)-1, "%f", d);
// applog(LOG_DEBUG, "pool key %s '%f'", options[opt].name, d);
parse_arg(options[opt].val, buf);
}
}
}
return true;
}
// debug stuff
void pool_dump_infos(struct pool_infos *p)
{
struct pool_infos *start = POOL_INFOS_HEAD(p);
if (opt_benchmark) return;
for (int i=0; i<num_pools; i++) {
applog(LOG_DEBUG, "POOL %01d: %s USER %s -s %d", i,
start[i].short_url, start[i].user, start[i].scantime);
}
}
void pool_dump_info(struct pool_infos *p)
{
if (opt_benchmark) return;
applog(LOG_BLUE, "POOL %01d: %s USER %s -s %d", p->id, p->short_url, p->user, p->scantime);
}