driver-stratum.c 17 KB

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