driver-stratum.c 17 KB

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