driver-stratum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. _ssm_gen_dummy_work(&_ssm_cur_job_work, ssj, NULL, 0);
  133. _ssm_notify_sz = p - buf;
  134. assert(_ssm_notify_sz <= bufsz);
  135. _ssm_notify = buf;
  136. LL_FOREACH(_ssm_connections, conn)
  137. {
  138. if (unlikely(!conn->xnonce1_le))
  139. continue;
  140. bufferevent_write(conn->bev, _ssm_notify, _ssm_notify_sz);
  141. }
  142. return true;
  143. }
  144. static
  145. void _ssj_free(struct stratumsrv_job * const ssj)
  146. {
  147. free(ssj->my_job_id);
  148. stratum_work_clean(&ssj->swork);
  149. free(ssj->nonce1);
  150. free(ssj);
  151. }
  152. static
  153. void stratumsrv_job_pruner()
  154. {
  155. struct stratumsrv_job *ssj, *tmp_ssj;
  156. struct timeval tv_now;
  157. timer_set_now(&tv_now);
  158. HASH_ITER(hh, _ssm_jobs, ssj, tmp_ssj)
  159. {
  160. if (timer_elapsed(&ssj->tv_prepared, &tv_now) <= opt_expiry)
  161. break;
  162. HASH_DEL(_ssm_jobs, ssj);
  163. applog(LOG_DEBUG, "SSM: Pruning job_id %s", ssj->my_job_id);
  164. _ssj_free(ssj);
  165. }
  166. }
  167. static void stratumsrv_client_close(struct stratumsrv_conn *);
  168. static
  169. void stratumsrv_conn_close_completion_cb(struct bufferevent *bev, void *p)
  170. {
  171. struct evbuffer * const output = bufferevent_get_output(bev);
  172. if (evbuffer_get_length(output))
  173. // Still have more data to write...
  174. return;
  175. stratumsrv_client_close(p);
  176. }
  177. static void stratumsrv_event(struct bufferevent *, short, void *);
  178. static
  179. void stratumsrv_boot(struct stratumsrv_conn * const conn, const char * const msg)
  180. {
  181. struct bufferevent * const bev = conn->bev;
  182. char buf[58 + strlen(msg)];
  183. int bufsz = sprintf(buf, "{\"params\":[\"%s\"],\"method\":\"client.show_message\",\"id\":null}\n", msg);
  184. bufferevent_write(bev, buf, bufsz);
  185. bufferevent_setcb(bev, NULL, stratumsrv_conn_close_completion_cb, stratumsrv_event, conn);
  186. }
  187. static
  188. void stratumsrv_boot_all_subscribed(const char * const msg)
  189. {
  190. struct stratumsrv_conn *conn, *tmp_conn;
  191. _ssm_notify = NULL;
  192. // Boot all connections
  193. LL_FOREACH_SAFE(_ssm_connections, conn, tmp_conn)
  194. {
  195. if (!conn->xnonce1_le)
  196. continue;
  197. stratumsrv_boot(conn, msg);
  198. }
  199. }
  200. static
  201. void _stratumsrv_update_notify(evutil_socket_t fd, short what, __maybe_unused void *p)
  202. {
  203. struct pool *pool = current_pool();
  204. bool clean;
  205. if (fd == _ssm_update_notifier[0])
  206. {
  207. evtimer_del(ev_notify);
  208. notifier_read(_ssm_update_notifier);
  209. applog(LOG_DEBUG, "SSM: Update triggered by notifier");
  210. }
  211. clean = _ssm_cur_job_work.pool ? stale_work(&_ssm_cur_job_work, true) : true;
  212. if (clean)
  213. {
  214. struct stratumsrv_job *ssj, *tmp;
  215. applog(LOG_DEBUG, "SSM: Current replacing job stale, pruning all jobs");
  216. HASH_ITER(hh, _ssm_jobs, ssj, tmp)
  217. {
  218. HASH_DEL(_ssm_jobs, ssj);
  219. _ssj_free(ssj);
  220. }
  221. }
  222. else
  223. stratumsrv_job_pruner();
  224. if (!pool->stratum_notify)
  225. {
  226. applog(LOG_WARNING, "SSM: Not using a stratum server upstream!");
  227. if (clean)
  228. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  229. goto out;
  230. }
  231. if (!stratumsrv_update_notify_str(pool, clean))
  232. {
  233. applog(LOG_WARNING, "SSM: Failed to subdivide upstream stratum notify!");
  234. if (clean)
  235. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  236. }
  237. out: ;
  238. struct timeval tv_scantime = {
  239. .tv_sec = opt_scantime,
  240. };
  241. evtimer_add(ev_notify, &tv_scantime);
  242. }
  243. static struct proxy_client *_stratumsrv_find_or_create_client(const char *);
  244. static
  245. struct proxy_client *(*stratumsrv_find_or_create_client)(const char *) = _stratumsrv_find_or_create_client;
  246. static
  247. struct proxy_client *_stratumsrv_find_or_create_client(const char *user)
  248. {
  249. struct proxy_client * const client = proxy_find_or_create_client(user);
  250. struct cgpu_info *cgpu;
  251. struct thr_info *thr;
  252. if (!client)
  253. return NULL;
  254. cgpu = client->cgpu;
  255. thr = cgpu->thr[0];
  256. memcpy(thr->work_restart_notifier, _ssm_update_notifier, sizeof(thr->work_restart_notifier));
  257. stratumsrv_find_or_create_client = proxy_find_or_create_client;
  258. return client;
  259. }
  260. static
  261. void _stratumsrv_failure(struct bufferevent * const bev, const char * const idstr, const int e, const char * const emsg)
  262. {
  263. if (!idstr)
  264. return;
  265. char buf[0x100];
  266. size_t bufsz = snprintf(buf, sizeof(buf), "{\"error\":[%d,\"%s\",null],\"id\":%s,\"result\":null}\n", e, emsg, idstr);
  267. bufferevent_write(bev, buf, bufsz);
  268. }
  269. #define return_stratumsrv_failure(e, emsg) do{ \
  270. _stratumsrv_failure(bev, idstr, e, emsg); \
  271. return; \
  272. }while(0)
  273. static
  274. void _stratumsrv_success(struct bufferevent * const bev, const char * const idstr)
  275. {
  276. if (!idstr)
  277. return;
  278. size_t bufsz = 36 + strlen(idstr);
  279. char buf[bufsz];
  280. bufsz = sprintf(buf, "{\"result\":true,\"id\":%s,\"error\":null}\n", idstr);
  281. bufferevent_write(bev, buf, bufsz);
  282. }
  283. static
  284. void stratumsrv_mining_subscribe(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  285. {
  286. char buf[90 + strlen(idstr) + (_ssm_client_octets * 2 * 2) + 0x10];
  287. char xnonce1x[(_ssm_client_octets * 2) + 1];
  288. int bufsz;
  289. if (!_ssm_notify)
  290. {
  291. evtimer_del(ev_notify);
  292. _stratumsrv_update_notify(-1, 0, NULL);
  293. if (!_ssm_notify)
  294. return_stratumsrv_failure(20, "No notify set (upstream not stratum?)");
  295. }
  296. if (!*xnonce1_p)
  297. {
  298. uint32_t xnonce1;
  299. for (xnonce1 = MAX_CLIENTS; _ssm_xnonce1s[xnonce1]; --xnonce1)
  300. if (!xnonce1)
  301. return_stratumsrv_failure(20, "Maximum clients already connected");
  302. *xnonce1_p = htole32(xnonce1);
  303. }
  304. bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
  305. bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
  306. bufferevent_write(bev, buf, bufsz);
  307. bufferevent_write(bev, "{\"params\":[0.9999847412109375],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 75);
  308. bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
  309. }
  310. static
  311. void stratumsrv_mining_authorize(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  312. {
  313. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  314. if (unlikely(!client))
  315. return_stratumsrv_failure(20, "Failed creating new cgpu");
  316. _stratumsrv_success(bev, idstr);
  317. }
  318. static
  319. void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
  320. {
  321. uint32_t * const xnonce1_p = &conn->xnonce1_le;
  322. struct work _work, *work;
  323. struct stratumsrv_job *ssj;
  324. struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  325. struct cgpu_info *cgpu;
  326. struct thr_info *thr;
  327. const char * const job_id = __json_array_string(params, 1);
  328. const char * const extranonce2 = __json_array_string(params, 2);
  329. const char * const ntime = __json_array_string(params, 3);
  330. const char * const nonce = __json_array_string(params, 4);
  331. uint32_t nonce_n;
  332. if (unlikely(!client))
  333. return_stratumsrv_failure(20, "Failed creating new cgpu");
  334. if (unlikely(!(job_id && extranonce2 && ntime && nonce)))
  335. return_stratumsrv_failure(20, "Couldn't understand parameters");
  336. if (unlikely(strlen(nonce) < 8))
  337. return_stratumsrv_failure(20, "nonce too short");
  338. if (unlikely(strlen(ntime) < 8))
  339. return_stratumsrv_failure(20, "ntime too short");
  340. if (unlikely(strlen(extranonce2) < _ssm_client_xnonce2sz * 2))
  341. return_stratumsrv_failure(20, "extranonce2 too short");
  342. cgpu = client->cgpu;
  343. thr = cgpu->thr[0];
  344. // Lookup job_id
  345. HASH_FIND_STR(_ssm_jobs, job_id, ssj);
  346. if (!ssj)
  347. return_stratumsrv_failure(21, "Job not found");
  348. // Generate dummy work
  349. work = &_work;
  350. _ssm_gen_dummy_work(work, ssj, extranonce2, *xnonce1_p);
  351. // Submit nonce
  352. hex2bin(&work->data[68], ntime, 4);
  353. hex2bin((void*)&nonce_n, nonce, 4);
  354. nonce_n = le32toh(nonce_n);
  355. if (!submit_nonce(thr, work, nonce_n))
  356. _stratumsrv_failure(bev, idstr, 23, "H-not-zero");
  357. else
  358. if (stale_work(work, true))
  359. _stratumsrv_failure(bev, idstr, 21, "stale");
  360. else
  361. _stratumsrv_success(bev, idstr);
  362. clean_work(work);
  363. if (!conn->hashes_done_ext)
  364. {
  365. struct timeval tv_now, tv_delta;
  366. timer_set_now(&tv_now);
  367. timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
  368. conn->tv_hashes_done = tv_now;
  369. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  370. }
  371. }
  372. static
  373. void stratumsrv_mining_hashes_done(struct bufferevent * const bev, json_t * const params, const char * const idstr, struct stratumsrv_conn * const conn)
  374. {
  375. double f;
  376. struct timeval tv_delta;
  377. struct cgpu_info *cgpu;
  378. struct thr_info *thr;
  379. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  380. json_t *jduration = json_array_get(params, 1);
  381. json_t *jhashcount = json_array_get(params, 2);
  382. if (!(json_is_number(jduration) && json_is_number(jhashcount)))
  383. return_stratumsrv_failure(20, "mining.hashes_done(String username, Number duration-in-seconds, Number hashcount)");
  384. cgpu = client->cgpu;
  385. thr = cgpu->thr[0];
  386. f = json_number_value(jduration);
  387. tv_delta.tv_sec = f;
  388. tv_delta.tv_usec = (f - tv_delta.tv_sec) * 1e6;
  389. f = json_number_value(jhashcount);
  390. hashes_done(thr, f, &tv_delta, NULL);
  391. conn->hashes_done_ext = true;
  392. }
  393. static
  394. bool stratumsrv_process_line(struct bufferevent * const bev, const char * const ln, void * const p)
  395. {
  396. struct stratumsrv_conn *conn = p;
  397. json_error_t jerr;
  398. json_t *json, *params, *j2;
  399. const char *method;
  400. char *idstr;
  401. json = JSON_LOADS(ln, &jerr);
  402. if (!json)
  403. {
  404. applog(LOG_ERR, "SSM: JSON parse error: %s", ln);
  405. return false;
  406. }
  407. method = bfg_json_obj_string(json, "method", NULL);
  408. if (!method)
  409. {
  410. applog(LOG_ERR, "SSM: JSON missing method: %s", ln);
  411. return false;
  412. }
  413. params = json_object_get(json, "params");
  414. if (!params)
  415. {
  416. applog(LOG_ERR, "SSM: JSON missing params: %s", ln);
  417. return false;
  418. }
  419. applog(LOG_DEBUG, "SSM: RECV: %s", ln);
  420. j2 = json_object_get(json, "id");
  421. idstr = (j2 && !json_is_null(j2)) ? json_dumps_ANY(j2, 0) : NULL;
  422. if (!strcasecmp(method, "mining.submit"))
  423. stratumsrv_mining_submit(bev, params, idstr, conn);
  424. else
  425. if (!strcasecmp(method, "mining.hashes_done"))
  426. stratumsrv_mining_hashes_done(bev, params, idstr, conn);
  427. else
  428. if (!strcasecmp(method, "mining.authorize"))
  429. stratumsrv_mining_authorize(bev, params, idstr, &conn->xnonce1_le);
  430. else
  431. if (!strcasecmp(method, "mining.subscribe"))
  432. stratumsrv_mining_subscribe(bev, params, idstr, &conn->xnonce1_le);
  433. else
  434. _stratumsrv_failure(bev, idstr, -3, "Method not supported");
  435. free(idstr);
  436. return true;
  437. }
  438. static
  439. void stratumsrv_client_close(struct stratumsrv_conn * const conn)
  440. {
  441. struct bufferevent * const bev = conn->bev;
  442. bufferevent_free(bev);
  443. LL_DELETE(_ssm_connections, conn);
  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. for (uint64_t n = MAX_CLIENTS; n; n >>= 8)
  515. ++_ssm_client_octets;
  516. _ssm_client_xnonce2sz = 2;
  517. struct event_base *evbase = event_base_new();
  518. _smm_evbase = evbase;
  519. {
  520. ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
  521. _stratumsrv_update_notify(-1, 0, NULL);
  522. }
  523. {
  524. notifier_init(_ssm_update_notifier);
  525. struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
  526. event_add(ev_update_notifier, NULL);
  527. }
  528. stratumsrv_change_port();
  529. event_base_dispatch(evbase);
  530. return NULL;
  531. }
  532. void stratumsrv_start()
  533. {
  534. _smm_running = true;
  535. pthread_t pth;
  536. if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
  537. quit(1, "stratumsrv thread create failed");
  538. }