driver-stratum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include "config.h"
  2. #ifndef WIN32
  3. #include <arpa/inet.h>
  4. #else
  5. #include <winsock2.h>
  6. #endif
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <event2/buffer.h>
  11. #include <event2/bufferevent.h>
  12. #include <event2/event.h>
  13. #include <event2/listener.h>
  14. #include <jansson.h>
  15. #include "deviceapi.h"
  16. #include "driver-proxy.h"
  17. #include "miner.h"
  18. #include "util.h"
  19. #define MAX_CLIENTS 255
  20. static bool _ssm_xnonce1s[MAX_CLIENTS + 1] = { true };
  21. static uint8_t _ssm_client_octets;
  22. static uint8_t _ssm_client_xnonce2sz;
  23. static char *_ssm_notify;
  24. static int _ssm_notify_sz;
  25. static struct event *ev_notify;
  26. static notifier_t _ssm_update_notifier;
  27. struct stratumsrv_job {
  28. char *my_job_id;
  29. struct pool *pool;
  30. uint8_t work_restart_id;
  31. uint8_t n2size;
  32. struct timeval tv_prepared;
  33. struct stratum_work swork;
  34. char *nonce1;
  35. UT_hash_handle hh;
  36. };
  37. static struct stratumsrv_job *_ssm_jobs;
  38. static struct work _ssm_cur_job_work;
  39. static uint64_t _ssm_jobid;
  40. static struct event_base *_smm_evbase;
  41. static bool _smm_running;
  42. static struct evconnlistener *_smm_listener;
  43. struct stratumsrv_conn {
  44. struct bufferevent *bev;
  45. uint32_t xnonce1_le;
  46. struct timeval tv_hashes_done;
  47. bool hashes_done_ext;
  48. struct stratumsrv_conn *next;
  49. };
  50. static struct stratumsrv_conn *_ssm_connections;
  51. static
  52. void _ssm_gen_dummy_work(struct work *work, struct stratumsrv_job *ssj, const char * const extranonce2, uint32_t xnonce1)
  53. {
  54. uint8_t *p, *s;
  55. *work = (struct work){
  56. .pool = ssj->pool,
  57. .work_restart_id = ssj->work_restart_id,
  58. .tv_staged = ssj->tv_prepared,
  59. };
  60. bytes_resize(&work->nonce2, ssj->n2size);
  61. s = bytes_buf(&work->nonce2);
  62. p = &s[ssj->n2size - _ssm_client_xnonce2sz];
  63. if (extranonce2)
  64. hex2bin(p, extranonce2, _ssm_client_xnonce2sz);
  65. #ifndef __OPTIMIZE__
  66. else
  67. memset(p, '\0', _ssm_client_xnonce2sz);
  68. #endif
  69. p -= _ssm_client_octets;
  70. memcpy(p, &xnonce1, _ssm_client_octets);
  71. if (p != s)
  72. memset(s, '\xbb', p - s);
  73. gen_stratum_work2(work, &ssj->swork, ssj->nonce1);
  74. }
  75. static
  76. bool stratumsrv_update_notify_str(struct pool * const pool, bool clean)
  77. {
  78. cg_rlock(&pool->data_lock);
  79. struct stratumsrv_conn *conn;
  80. const struct stratum_work * const swork = &pool->swork;
  81. const int n2size = pool->n2size;
  82. char my_job_id[33];
  83. int i;
  84. struct stratumsrv_job *ssj;
  85. ssize_t n2pad = n2size - _ssm_client_octets - _ssm_client_xnonce2sz;
  86. if (n2pad < 0)
  87. return false;
  88. size_t coinb1in_lenx = swork->nonce2_offset * 2;
  89. size_t n2padx = n2pad * 2;
  90. size_t coinb1_lenx = coinb1in_lenx + n2padx;
  91. size_t coinb2_len = bytes_len(&swork->coinbase) - swork->nonce2_offset - n2size;
  92. size_t coinb2_lenx = coinb2_len * 2;
  93. sprintf(my_job_id, "%"PRIx64"-%"PRIx64, (uint64_t)time(NULL), _ssm_jobid++);
  94. size_t bufsz = 166 + strlen(my_job_id) + coinb1_lenx + coinb2_lenx + (swork->merkles * 67);
  95. char * const buf = malloc(bufsz);
  96. char *p = buf;
  97. char prevhash[65], coinb1[coinb1_lenx + 1], coinb2[coinb2_lenx], version[9], nbits[9], ntime[9];
  98. uint32_t ntime_n;
  99. bin2hex(prevhash, &swork->header1[4], 32);
  100. bin2hex(coinb1, bytes_buf(&swork->coinbase), swork->nonce2_offset);
  101. memset(&coinb1[coinb1in_lenx], 'B', n2padx);
  102. coinb1[coinb1_lenx] = '\0';
  103. bin2hex(coinb2, &bytes_buf(&swork->coinbase)[swork->nonce2_offset + n2size], coinb2_len);
  104. p += sprintf(p, "{\"params\":[\"%s\",\"%s\",\"%s\",\"%s\",[", my_job_id, prevhash, coinb1, coinb2);
  105. for (i = 0; i < swork->merkles; ++i)
  106. {
  107. if (i)
  108. *p++ = ',';
  109. *p++ = '"';
  110. bin2hex(p, &bytes_buf(&swork->merkle_bin)[i * 32], 32);
  111. p += 64;
  112. *p++ = '"';
  113. }
  114. bin2hex(version, swork->header1, 4);
  115. bin2hex(nbits, swork->diffbits, 4);
  116. ntime_n = htobe32(swork->ntime + timer_elapsed(&swork->tv_received, NULL));
  117. bin2hex(ntime, &ntime_n, 4);
  118. p += sprintf(p, "],\"%s\",\"%s\",\"%s\",%s],\"method\":\"mining.notify\",\"id\":null}\n", version, nbits, ntime, clean ? "true" : "false");
  119. ssj = malloc(sizeof(*ssj));
  120. *ssj = (struct stratumsrv_job){
  121. .my_job_id = strdup(my_job_id),
  122. .pool = pool,
  123. .work_restart_id = pool->work_restart_id,
  124. .n2size = n2size,
  125. .nonce1 = strdup(pool->nonce1),
  126. };
  127. timer_set_now(&ssj->tv_prepared);
  128. stratum_work_cpy(&ssj->swork, swork);
  129. cg_runlock(&pool->data_lock);
  130. ssj->swork.data_lock_p = NULL;
  131. HASH_ADD_KEYPTR(hh, _ssm_jobs, ssj->my_job_id, strlen(ssj->my_job_id), ssj);
  132. if (likely(_ssm_cur_job_work.pool))
  133. clean_work(&_ssm_cur_job_work);
  134. _ssm_gen_dummy_work(&_ssm_cur_job_work, ssj, NULL, 0);
  135. _ssm_notify_sz = p - buf;
  136. assert(_ssm_notify_sz <= bufsz);
  137. free(_ssm_notify);
  138. _ssm_notify = buf;
  139. LL_FOREACH(_ssm_connections, conn)
  140. {
  141. if (unlikely(!conn->xnonce1_le))
  142. continue;
  143. bufferevent_write(conn->bev, _ssm_notify, _ssm_notify_sz);
  144. }
  145. return true;
  146. }
  147. static
  148. void _ssj_free(struct stratumsrv_job * const ssj)
  149. {
  150. free(ssj->my_job_id);
  151. stratum_work_clean(&ssj->swork);
  152. free(ssj->nonce1);
  153. free(ssj);
  154. }
  155. static
  156. void stratumsrv_job_pruner()
  157. {
  158. struct stratumsrv_job *ssj, *tmp_ssj;
  159. struct timeval tv_now;
  160. timer_set_now(&tv_now);
  161. HASH_ITER(hh, _ssm_jobs, ssj, tmp_ssj)
  162. {
  163. if (timer_elapsed(&ssj->tv_prepared, &tv_now) <= opt_expiry)
  164. break;
  165. HASH_DEL(_ssm_jobs, ssj);
  166. applog(LOG_DEBUG, "SSM: Pruning job_id %s", ssj->my_job_id);
  167. _ssj_free(ssj);
  168. }
  169. }
  170. static void stratumsrv_client_close(struct stratumsrv_conn *);
  171. static
  172. void stratumsrv_conn_close_completion_cb(struct bufferevent *bev, void *p)
  173. {
  174. struct evbuffer * const output = bufferevent_get_output(bev);
  175. if (evbuffer_get_length(output))
  176. // Still have more data to write...
  177. return;
  178. stratumsrv_client_close(p);
  179. }
  180. static void stratumsrv_event(struct bufferevent *, short, void *);
  181. static
  182. void stratumsrv_boot(struct stratumsrv_conn * const conn, const char * const msg)
  183. {
  184. struct bufferevent * const bev = conn->bev;
  185. char buf[58 + strlen(msg)];
  186. int bufsz = sprintf(buf, "{\"params\":[\"%s\"],\"method\":\"client.show_message\",\"id\":null}\n", msg);
  187. bufferevent_write(bev, buf, bufsz);
  188. bufferevent_setcb(bev, NULL, stratumsrv_conn_close_completion_cb, stratumsrv_event, conn);
  189. }
  190. static
  191. void stratumsrv_boot_all_subscribed(const char * const msg)
  192. {
  193. struct stratumsrv_conn *conn, *tmp_conn;
  194. free(_ssm_notify);
  195. _ssm_notify = NULL;
  196. // Boot all connections
  197. LL_FOREACH_SAFE(_ssm_connections, conn, tmp_conn)
  198. {
  199. if (!conn->xnonce1_le)
  200. continue;
  201. stratumsrv_boot(conn, msg);
  202. }
  203. }
  204. static
  205. void _stratumsrv_update_notify(evutil_socket_t fd, short what, __maybe_unused void *p)
  206. {
  207. struct pool *pool = current_pool();
  208. bool clean;
  209. if (fd == _ssm_update_notifier[0])
  210. {
  211. evtimer_del(ev_notify);
  212. notifier_read(_ssm_update_notifier);
  213. applog(LOG_DEBUG, "SSM: Update triggered by notifier");
  214. }
  215. clean = _ssm_cur_job_work.pool ? stale_work(&_ssm_cur_job_work, true) : true;
  216. if (clean)
  217. {
  218. struct stratumsrv_job *ssj, *tmp;
  219. applog(LOG_DEBUG, "SSM: Current replacing job stale, pruning all jobs");
  220. HASH_ITER(hh, _ssm_jobs, ssj, tmp)
  221. {
  222. HASH_DEL(_ssm_jobs, ssj);
  223. _ssj_free(ssj);
  224. }
  225. }
  226. else
  227. stratumsrv_job_pruner();
  228. if (!pool->stratum_notify)
  229. {
  230. applog(LOG_WARNING, "SSM: Not using a stratum server upstream!");
  231. if (clean)
  232. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  233. goto out;
  234. }
  235. if (!stratumsrv_update_notify_str(pool, clean))
  236. {
  237. applog(LOG_WARNING, "SSM: Failed to subdivide upstream stratum notify!");
  238. if (clean)
  239. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  240. }
  241. out: ;
  242. struct timeval tv_scantime = {
  243. .tv_sec = opt_scantime,
  244. };
  245. evtimer_add(ev_notify, &tv_scantime);
  246. }
  247. static struct proxy_client *_stratumsrv_find_or_create_client(const char *);
  248. static
  249. struct proxy_client *(*stratumsrv_find_or_create_client)(const char *) = _stratumsrv_find_or_create_client;
  250. static
  251. struct proxy_client *_stratumsrv_find_or_create_client(const char *user)
  252. {
  253. struct proxy_client * const client = proxy_find_or_create_client(user);
  254. struct cgpu_info *cgpu;
  255. struct thr_info *thr;
  256. if (!client)
  257. return NULL;
  258. cgpu = client->cgpu;
  259. thr = cgpu->thr[0];
  260. memcpy(thr->work_restart_notifier, _ssm_update_notifier, sizeof(thr->work_restart_notifier));
  261. stratumsrv_find_or_create_client = proxy_find_or_create_client;
  262. return client;
  263. }
  264. static
  265. void _stratumsrv_failure(struct bufferevent * const bev, const char * const idstr, const int e, const char * const emsg)
  266. {
  267. if (!idstr)
  268. return;
  269. char buf[0x100];
  270. size_t bufsz = snprintf(buf, sizeof(buf), "{\"error\":[%d,\"%s\",null],\"id\":%s,\"result\":null}\n", e, emsg, idstr);
  271. bufferevent_write(bev, buf, bufsz);
  272. }
  273. #define return_stratumsrv_failure(e, emsg) do{ \
  274. _stratumsrv_failure(bev, idstr, e, emsg); \
  275. return; \
  276. }while(0)
  277. static
  278. void _stratumsrv_success(struct bufferevent * const bev, const char * const idstr)
  279. {
  280. if (!idstr)
  281. return;
  282. size_t bufsz = 36 + strlen(idstr);
  283. char buf[bufsz];
  284. bufsz = sprintf(buf, "{\"result\":true,\"id\":%s,\"error\":null}\n", idstr);
  285. bufferevent_write(bev, buf, bufsz);
  286. }
  287. static
  288. void stratumsrv_mining_subscribe(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  289. {
  290. char buf[90 + strlen(idstr) + (_ssm_client_octets * 2 * 2) + 0x10];
  291. char xnonce1x[(_ssm_client_octets * 2) + 1];
  292. int bufsz;
  293. if (!_ssm_notify)
  294. {
  295. evtimer_del(ev_notify);
  296. _stratumsrv_update_notify(-1, 0, NULL);
  297. if (!_ssm_notify)
  298. return_stratumsrv_failure(20, "No notify set (upstream not stratum?)");
  299. }
  300. if (!*xnonce1_p)
  301. {
  302. uint32_t xnonce1;
  303. for (xnonce1 = MAX_CLIENTS; _ssm_xnonce1s[xnonce1]; --xnonce1)
  304. if (!xnonce1)
  305. return_stratumsrv_failure(20, "Maximum clients already connected");
  306. *xnonce1_p = htole32(xnonce1);
  307. }
  308. bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
  309. bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
  310. bufferevent_write(bev, buf, bufsz);
  311. bufferevent_write(bev, "{\"params\":[0.9999847412109375],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 75);
  312. bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
  313. }
  314. static
  315. void stratumsrv_mining_authorize(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  316. {
  317. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  318. if (unlikely(!client))
  319. return_stratumsrv_failure(20, "Failed creating new cgpu");
  320. _stratumsrv_success(bev, idstr);
  321. }
  322. static
  323. void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
  324. {
  325. uint32_t * const xnonce1_p = &conn->xnonce1_le;
  326. struct work _work, *work;
  327. struct stratumsrv_job *ssj;
  328. struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  329. struct cgpu_info *cgpu;
  330. struct thr_info *thr;
  331. const char * const job_id = __json_array_string(params, 1);
  332. const char * const extranonce2 = __json_array_string(params, 2);
  333. const char * const ntime = __json_array_string(params, 3);
  334. const char * const nonce = __json_array_string(params, 4);
  335. uint32_t nonce_n;
  336. if (unlikely(!client))
  337. return_stratumsrv_failure(20, "Failed creating new cgpu");
  338. if (unlikely(!(job_id && extranonce2 && ntime && nonce)))
  339. return_stratumsrv_failure(20, "Couldn't understand parameters");
  340. if (unlikely(strlen(nonce) < 8))
  341. return_stratumsrv_failure(20, "nonce too short");
  342. if (unlikely(strlen(ntime) < 8))
  343. return_stratumsrv_failure(20, "ntime too short");
  344. if (unlikely(strlen(extranonce2) < _ssm_client_xnonce2sz * 2))
  345. return_stratumsrv_failure(20, "extranonce2 too short");
  346. cgpu = client->cgpu;
  347. thr = cgpu->thr[0];
  348. // Lookup job_id
  349. HASH_FIND_STR(_ssm_jobs, job_id, ssj);
  350. if (!ssj)
  351. return_stratumsrv_failure(21, "Job not found");
  352. // Generate dummy work
  353. work = &_work;
  354. _ssm_gen_dummy_work(work, ssj, extranonce2, *xnonce1_p);
  355. // Submit nonce
  356. hex2bin(&work->data[68], ntime, 4);
  357. hex2bin((void*)&nonce_n, nonce, 4);
  358. nonce_n = le32toh(nonce_n);
  359. if (!submit_nonce(thr, work, nonce_n))
  360. _stratumsrv_failure(bev, idstr, 23, "H-not-zero");
  361. else
  362. if (stale_work(work, true))
  363. _stratumsrv_failure(bev, idstr, 21, "stale");
  364. else
  365. _stratumsrv_success(bev, idstr);
  366. clean_work(work);
  367. if (!conn->hashes_done_ext)
  368. {
  369. struct timeval tv_now, tv_delta;
  370. timer_set_now(&tv_now);
  371. timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
  372. conn->tv_hashes_done = tv_now;
  373. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  374. }
  375. }
  376. static
  377. void stratumsrv_mining_hashes_done(struct bufferevent * const bev, json_t * const params, const char * const idstr, struct stratumsrv_conn * const conn)
  378. {
  379. double f;
  380. struct timeval tv_delta;
  381. struct cgpu_info *cgpu;
  382. struct thr_info *thr;
  383. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  384. json_t *jduration = json_array_get(params, 1);
  385. json_t *jhashcount = json_array_get(params, 2);
  386. if (!(json_is_number(jduration) && json_is_number(jhashcount)))
  387. return_stratumsrv_failure(20, "mining.hashes_done(String username, Number duration-in-seconds, Number hashcount)");
  388. cgpu = client->cgpu;
  389. thr = cgpu->thr[0];
  390. f = json_number_value(jduration);
  391. tv_delta.tv_sec = f;
  392. tv_delta.tv_usec = (f - tv_delta.tv_sec) * 1e6;
  393. f = json_number_value(jhashcount);
  394. hashes_done(thr, f, &tv_delta, NULL);
  395. conn->hashes_done_ext = true;
  396. }
  397. static
  398. bool stratumsrv_process_line(struct bufferevent * const bev, const char * const ln, void * const p)
  399. {
  400. struct stratumsrv_conn *conn = p;
  401. json_error_t jerr;
  402. json_t *json, *params, *j2;
  403. const char *method;
  404. char *idstr;
  405. json = JSON_LOADS(ln, &jerr);
  406. if (!json)
  407. {
  408. applog(LOG_ERR, "SSM: JSON parse error: %s", ln);
  409. return false;
  410. }
  411. method = bfg_json_obj_string(json, "method", NULL);
  412. if (!method)
  413. {
  414. applog(LOG_ERR, "SSM: JSON missing method: %s", ln);
  415. return false;
  416. }
  417. params = json_object_get(json, "params");
  418. if (!params)
  419. {
  420. applog(LOG_ERR, "SSM: JSON missing params: %s", ln);
  421. return false;
  422. }
  423. applog(LOG_DEBUG, "SSM: RECV: %s", ln);
  424. j2 = json_object_get(json, "id");
  425. idstr = (j2 && !json_is_null(j2)) ? json_dumps_ANY(j2, 0) : NULL;
  426. if (!strcasecmp(method, "mining.submit"))
  427. stratumsrv_mining_submit(bev, params, idstr, conn);
  428. else
  429. if (!strcasecmp(method, "mining.hashes_done"))
  430. stratumsrv_mining_hashes_done(bev, params, idstr, conn);
  431. else
  432. if (!strcasecmp(method, "mining.authorize"))
  433. stratumsrv_mining_authorize(bev, params, idstr, &conn->xnonce1_le);
  434. else
  435. if (!strcasecmp(method, "mining.subscribe"))
  436. stratumsrv_mining_subscribe(bev, params, idstr, &conn->xnonce1_le);
  437. else
  438. _stratumsrv_failure(bev, idstr, -3, "Method not supported");
  439. free(idstr);
  440. return true;
  441. }
  442. static
  443. void stratumsrv_client_close(struct stratumsrv_conn * const conn)
  444. {
  445. struct bufferevent * const bev = conn->bev;
  446. bufferevent_free(bev);
  447. LL_DELETE(_ssm_connections, conn);
  448. free(conn);
  449. }
  450. static
  451. void stratumsrv_read(struct bufferevent *bev, void *p)
  452. {
  453. struct evbuffer *input = bufferevent_get_input(bev);
  454. char *ln;
  455. bool rv;
  456. while ( (ln = evbuffer_readln(input, NULL, EVBUFFER_EOL_ANY)) )
  457. {
  458. rv = stratumsrv_process_line(bev, ln, p);
  459. free(ln);
  460. if (unlikely(!rv))
  461. {
  462. stratumsrv_client_close(p);
  463. break;
  464. }
  465. }
  466. }
  467. static
  468. void stratumsrv_event(struct bufferevent *bev, short events, void *p)
  469. {
  470. if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR))
  471. {
  472. if (events & BEV_EVENT_ERROR)
  473. applog(LOG_ERR, "Error from bufferevent");
  474. if (events & BEV_EVENT_EOF)
  475. applog(LOG_DEBUG, "EOF from bufferevent");
  476. stratumsrv_client_close(p);
  477. }
  478. }
  479. static
  480. void stratumlistener(struct evconnlistener *listener, evutil_socket_t sock, struct sockaddr *addr, int len, void *p)
  481. {
  482. struct stratumsrv_conn *conn;
  483. struct event_base *evbase = evconnlistener_get_base(listener);
  484. struct bufferevent *bev = bufferevent_socket_new(evbase, sock, BEV_OPT_CLOSE_ON_FREE);
  485. conn = malloc(sizeof(*conn));
  486. *conn = (struct stratumsrv_conn){
  487. .bev = bev,
  488. };
  489. LL_PREPEND(_ssm_connections, conn);
  490. bufferevent_setcb(bev, stratumsrv_read, NULL, stratumsrv_event, conn);
  491. bufferevent_enable(bev, EV_READ | EV_WRITE);
  492. }
  493. void stratumsrv_start();
  494. void stratumsrv_change_port()
  495. {
  496. struct event_base * const evbase = _smm_evbase;
  497. if (_smm_listener)
  498. evconnlistener_free(_smm_listener);
  499. if (!_smm_running)
  500. {
  501. stratumsrv_start();
  502. return;
  503. }
  504. struct sockaddr_in sin = {
  505. .sin_family = AF_INET,
  506. .sin_addr.s_addr = INADDR_ANY,
  507. .sin_port = htons(stratumsrv_port),
  508. };
  509. _smm_listener = evconnlistener_new_bind(evbase, stratumlistener, NULL, (
  510. LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
  511. ), 0x10, (void*)&sin, sizeof(sin));
  512. }
  513. static
  514. void *stratumsrv_thread(__maybe_unused void *p)
  515. {
  516. pthread_detach(pthread_self());
  517. RenameThread("stratumsrv");
  518. for (uint64_t n = MAX_CLIENTS; n; n >>= 8)
  519. ++_ssm_client_octets;
  520. _ssm_client_xnonce2sz = 2;
  521. struct event_base *evbase = event_base_new();
  522. _smm_evbase = evbase;
  523. {
  524. ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
  525. _stratumsrv_update_notify(-1, 0, NULL);
  526. }
  527. {
  528. notifier_init(_ssm_update_notifier);
  529. struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
  530. event_add(ev_update_notifier, NULL);
  531. }
  532. stratumsrv_change_port();
  533. event_base_dispatch(evbase);
  534. return NULL;
  535. }
  536. void stratumsrv_start()
  537. {
  538. _smm_running = true;
  539. pthread_t pth;
  540. if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
  541. quit(1, "stratumsrv thread create failed");
  542. }