cpu-miner.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Copyright 2010 Jeff Garzik
  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 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "cpuminer-config.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <unistd.h>
  16. #include <sys/time.h>
  17. #include <time.h>
  18. #ifndef WIN32
  19. #include <sys/resource.h>
  20. #endif
  21. #include <pthread.h>
  22. #include <getopt.h>
  23. #include <jansson.h>
  24. #include <curl/curl.h>
  25. #include "compat.h"
  26. #include "miner.h"
  27. #define PROGRAM_NAME "minerd"
  28. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  29. #define DEF_RPC_USERPASS "rpcuser:rpcpass"
  30. enum sha256_algos {
  31. ALGO_C, /* plain C */
  32. ALGO_4WAY, /* parallel SSE2 */
  33. ALGO_VIA, /* VIA padlock */
  34. ALGO_CRYPTOPP, /* Crypto++ (C) */
  35. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  36. ALGO_SSE2_64, /* SSE2 for x86_64 */
  37. };
  38. static const char *algo_names[] = {
  39. [ALGO_C] = "c",
  40. #ifdef WANT_SSE2_4WAY
  41. [ALGO_4WAY] = "4way",
  42. #endif
  43. #ifdef WANT_VIA_PADLOCK
  44. [ALGO_VIA] = "via",
  45. #endif
  46. [ALGO_CRYPTOPP] = "cryptopp",
  47. #ifdef WANT_CRYPTOPP_ASM32
  48. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  49. #endif
  50. #ifdef WANT_X8664_SSE2
  51. [ALGO_SSE2_64] = "sse2_64",
  52. #endif
  53. };
  54. bool opt_debug = false;
  55. bool opt_protocol = false;
  56. static bool opt_quiet = false;
  57. static int opt_retries = 10;
  58. static int opt_fail_pause = 30;
  59. static int opt_scantime = 5;
  60. static json_t *opt_config;
  61. static const bool opt_time = true;
  62. static enum sha256_algos opt_algo = ALGO_C;
  63. static int opt_n_threads = 1;
  64. static char *rpc_url;
  65. static char *userpass;
  66. struct option_help {
  67. const char *name;
  68. const char *helptext;
  69. };
  70. static struct option_help options_help[] = {
  71. { "help",
  72. "(-h) Display this help text" },
  73. { "config FILE",
  74. "(-c FILE) JSON-format configuration file (default: none)\n"
  75. "See example-cfg.json for an example configuration." },
  76. { "algo XXX",
  77. "(-a XXX) Specify sha256 implementation:\n"
  78. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  79. #ifdef WANT_SSE2_4WAY
  80. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  81. #endif
  82. #ifdef WANT_VIA_PADLOCK
  83. "\n\tvia\t\tVIA padlock implementation"
  84. #endif
  85. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  86. #ifdef WANT_CRYPTOPP_ASM32
  87. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  88. #endif
  89. #ifdef WANT_X8664_SSE2
  90. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  91. #endif
  92. },
  93. { "quiet",
  94. "(-q) Disable per-thread hashmeter output (default: off)" },
  95. { "debug",
  96. "(-D) Enable debug output (default: off)" },
  97. { "protocol-dump",
  98. "(-P) Verbose dump of protocol-level activities (default: off)" },
  99. { "retries N",
  100. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  101. "\t(default: 10; use -1 for \"never\")" },
  102. { "retry-pause N",
  103. "(-R N) Number of seconds to pause, between retries\n"
  104. "\t(default: 30)" },
  105. { "scantime N",
  106. "(-s N) Upper bound on time spent scanning current work,\n"
  107. "\tin seconds. (default: 5)" },
  108. { "threads N",
  109. "(-t N) Number of miner threads (default: 1)" },
  110. { "url URL",
  111. "URL for bitcoin JSON-RPC server "
  112. "(default: " DEF_RPC_URL ")" },
  113. { "userpass USERNAME:PASSWORD",
  114. "Username:Password pair for bitcoin JSON-RPC server "
  115. "(default: " DEF_RPC_USERPASS ")" },
  116. };
  117. static struct option options[] = {
  118. { "help", 0, NULL, 'h' },
  119. { "algo", 1, NULL, 'a' },
  120. { "config", 1, NULL, 'c' },
  121. { "quiet", 0, NULL, 'q' },
  122. { "debug", 0, NULL, 'D' },
  123. { "protocol-dump", 0, NULL, 'P' },
  124. { "threads", 1, NULL, 't' },
  125. { "retries", 1, NULL, 'r' },
  126. { "retry-pause", 1, NULL, 'R' },
  127. { "scantime", 1, NULL, 's' },
  128. { "url", 1, NULL, 1001 },
  129. { "userpass", 1, NULL, 1002 },
  130. { }
  131. };
  132. struct work {
  133. unsigned char data[128];
  134. unsigned char hash1[64];
  135. unsigned char midstate[32];
  136. unsigned char target[32];
  137. unsigned char hash[32];
  138. };
  139. static bool jobj_binary(const json_t *obj, const char *key,
  140. void *buf, size_t buflen)
  141. {
  142. const char *hexstr;
  143. json_t *tmp;
  144. tmp = json_object_get(obj, key);
  145. if (!tmp) {
  146. fprintf(stderr, "JSON key '%s' not found\n", key);
  147. return false;
  148. }
  149. hexstr = json_string_value(tmp);
  150. if (!hexstr) {
  151. fprintf(stderr, "JSON key '%s' is not a string\n", key);
  152. return false;
  153. }
  154. if (!hex2bin(buf, hexstr, buflen))
  155. return false;
  156. return true;
  157. }
  158. static bool work_decode(const json_t *val, struct work *work)
  159. {
  160. if (!jobj_binary(val, "midstate",
  161. work->midstate, sizeof(work->midstate))) {
  162. fprintf(stderr, "JSON inval midstate\n");
  163. goto err_out;
  164. }
  165. if (!jobj_binary(val, "data", work->data, sizeof(work->data))) {
  166. fprintf(stderr, "JSON inval data\n");
  167. goto err_out;
  168. }
  169. if (!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1))) {
  170. fprintf(stderr, "JSON inval hash1\n");
  171. goto err_out;
  172. }
  173. if (!jobj_binary(val, "target", work->target, sizeof(work->target))) {
  174. fprintf(stderr, "JSON inval target\n");
  175. goto err_out;
  176. }
  177. memset(work->hash, 0, sizeof(work->hash));
  178. return true;
  179. err_out:
  180. return false;
  181. }
  182. static void submit_work(CURL *curl, struct work *work)
  183. {
  184. char *hexstr = NULL;
  185. json_t *val, *res;
  186. char s[345], timestr[64];
  187. time_t now;
  188. struct tm *tm;
  189. now = time(NULL);
  190. /* build hex string */
  191. hexstr = bin2hex(work->data, sizeof(work->data));
  192. if (!hexstr) {
  193. fprintf(stderr, "submit_work OOM\n");
  194. goto out;
  195. }
  196. /* build JSON-RPC request */
  197. sprintf(s,
  198. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  199. hexstr);
  200. if (opt_debug)
  201. fprintf(stderr, "DBG: sending RPC call:\n%s", s);
  202. /* issue JSON-RPC request */
  203. val = json_rpc_call(curl, rpc_url, userpass, s);
  204. if (!val) {
  205. fprintf(stderr, "submit_work json_rpc_call failed\n");
  206. goto out;
  207. }
  208. res = json_object_get(val, "result");
  209. tm = localtime(&now);
  210. strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
  211. printf("[%s] PROOF OF WORK RESULT: %s\n",
  212. timestr, json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  213. json_decref(val);
  214. out:
  215. free(hexstr);
  216. }
  217. static bool get_work(CURL *curl, struct work *work)
  218. {
  219. static const char *rpc_req =
  220. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  221. json_t *val;
  222. bool rc;
  223. val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
  224. if (!val)
  225. return false;
  226. rc = work_decode(json_object_get(val, "result"), work);
  227. json_decref(val);
  228. return rc;
  229. }
  230. static void hashmeter(int thr_id, const struct timeval *diff,
  231. unsigned long hashes_done)
  232. {
  233. double khashes, secs;
  234. khashes = hashes_done / 1000.0;
  235. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  236. if (!opt_quiet)
  237. printf("HashMeter(%d): %lu hashes, %.2f khash/sec\n",
  238. thr_id, hashes_done,
  239. khashes / secs);
  240. }
  241. static void *miner_thread(void *thr_id_int)
  242. {
  243. int thr_id = (unsigned long) thr_id_int;
  244. int failures = 0;
  245. uint32_t max_nonce = 0xffffff;
  246. CURL *curl;
  247. curl = curl_easy_init();
  248. if (!curl) {
  249. fprintf(stderr, "CURL initialization failed\n");
  250. return NULL;
  251. }
  252. while (1) {
  253. struct work work __attribute__((aligned(128)));
  254. unsigned long hashes_done;
  255. struct timeval tv_start, tv_end, diff;
  256. bool rc;
  257. /* obtain new work from bitcoin */
  258. if (!get_work(curl, &work)) {
  259. fprintf(stderr, "json_rpc_call failed, ");
  260. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  261. fprintf(stderr, "terminating thread\n");
  262. return NULL; /* exit thread */
  263. }
  264. /* pause, then restart work loop */
  265. fprintf(stderr, "retry after %d seconds\n",
  266. opt_fail_pause);
  267. sleep(opt_fail_pause);
  268. continue;
  269. }
  270. hashes_done = 0;
  271. gettimeofday(&tv_start, NULL);
  272. /* scan nonces for a proof-of-work hash */
  273. switch (opt_algo) {
  274. case ALGO_C:
  275. rc = scanhash_c(work.midstate, work.data + 64,
  276. work.hash1, work.hash, work.target,
  277. max_nonce, &hashes_done);
  278. break;
  279. #ifdef WANT_X8664_SSE2
  280. case ALGO_SSE2_64: {
  281. unsigned int rc5 =
  282. scanhash_sse2_64(work.midstate, work.data + 64,
  283. work.hash1, work.hash,
  284. work.target,
  285. max_nonce, &hashes_done);
  286. rc = (rc5 == -1) ? false : true;
  287. }
  288. break;
  289. #endif
  290. #ifdef WANT_SSE2_4WAY
  291. case ALGO_4WAY: {
  292. unsigned int rc4 =
  293. ScanHash_4WaySSE2(work.midstate, work.data + 64,
  294. work.hash1, work.hash,
  295. work.target,
  296. max_nonce, &hashes_done);
  297. rc = (rc4 == -1) ? false : true;
  298. }
  299. break;
  300. #endif
  301. #ifdef WANT_VIA_PADLOCK
  302. case ALGO_VIA:
  303. rc = scanhash_via(work.data, work.target,
  304. max_nonce, &hashes_done);
  305. break;
  306. #endif
  307. case ALGO_CRYPTOPP:
  308. rc = scanhash_cryptopp(work.midstate, work.data + 64,
  309. work.hash1, work.hash, work.target,
  310. max_nonce, &hashes_done);
  311. break;
  312. #ifdef WANT_CRYPTOPP_ASM32
  313. case ALGO_CRYPTOPP_ASM32:
  314. rc = scanhash_asm32(work.midstate, work.data + 64,
  315. work.hash1, work.hash, work.target,
  316. max_nonce, &hashes_done);
  317. break;
  318. #endif
  319. default:
  320. /* should never happen */
  321. return NULL;
  322. }
  323. /* record scanhash elapsed time */
  324. gettimeofday(&tv_end, NULL);
  325. timeval_subtract(&diff, &tv_end, &tv_start);
  326. hashmeter(thr_id, &diff, hashes_done);
  327. /* adjust max_nonce to meet target scan time */
  328. if (diff.tv_sec > (opt_scantime * 2))
  329. max_nonce /= 2; /* large decrease */
  330. else if ((diff.tv_sec > opt_scantime) &&
  331. (max_nonce > 1500000))
  332. max_nonce -= 1000000; /* small decrease */
  333. else if ((diff.tv_sec < opt_scantime) &&
  334. (max_nonce < 0xffffec76))
  335. max_nonce += 100000; /* small increase */
  336. /* if nonce found, submit work */
  337. if (rc)
  338. submit_work(curl, &work);
  339. failures = 0;
  340. }
  341. curl_easy_cleanup(curl);
  342. return NULL;
  343. }
  344. static void show_usage(void)
  345. {
  346. int i;
  347. printf("minerd version %s\n\n", VERSION);
  348. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  349. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  350. struct option_help *h;
  351. h = &options_help[i];
  352. printf("--%s\n%s\n\n", h->name, h->helptext);
  353. }
  354. exit(1);
  355. }
  356. static void parse_arg (int key, char *arg)
  357. {
  358. int v, i;
  359. switch(key) {
  360. case 'a':
  361. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  362. if (algo_names[i] &&
  363. !strcmp(arg, algo_names[i])) {
  364. opt_algo = i;
  365. break;
  366. }
  367. }
  368. if (i == ARRAY_SIZE(algo_names))
  369. show_usage();
  370. break;
  371. case 'c': {
  372. json_error_t err;
  373. if (opt_config)
  374. json_decref(opt_config);
  375. opt_config = json_load_file(arg, &err);
  376. if (!json_is_object(opt_config)) {
  377. fprintf(stderr, "JSON decode of %s failed\n", arg);
  378. show_usage();
  379. }
  380. break;
  381. }
  382. case 'q':
  383. opt_quiet = true;
  384. break;
  385. case 'D':
  386. opt_debug = true;
  387. break;
  388. case 'P':
  389. opt_protocol = true;
  390. break;
  391. case 'r':
  392. v = atoi(arg);
  393. if (v < -1 || v > 9999) /* sanity check */
  394. show_usage();
  395. opt_retries = v;
  396. break;
  397. case 'R':
  398. v = atoi(arg);
  399. if (v < 1 || v > 9999) /* sanity check */
  400. show_usage();
  401. opt_fail_pause = v;
  402. break;
  403. case 's':
  404. v = atoi(arg);
  405. if (v < 1 || v > 9999) /* sanity check */
  406. show_usage();
  407. opt_scantime = v;
  408. break;
  409. case 't':
  410. v = atoi(arg);
  411. if (v < 1 || v > 9999) /* sanity check */
  412. show_usage();
  413. opt_n_threads = v;
  414. break;
  415. case 1001: /* --url */
  416. if (strncmp(arg, "http://", 7) &&
  417. strncmp(arg, "https://", 8))
  418. show_usage();
  419. free(rpc_url);
  420. rpc_url = strdup(arg);
  421. break;
  422. case 1002: /* --userpass */
  423. if (!strchr(arg, ':'))
  424. show_usage();
  425. free(userpass);
  426. userpass = strdup(arg);
  427. break;
  428. default:
  429. show_usage();
  430. }
  431. }
  432. static void parse_config(void)
  433. {
  434. int i;
  435. json_t *val;
  436. if (!json_is_object(opt_config))
  437. return;
  438. for (i = 0; i < ARRAY_SIZE(options); i++) {
  439. if (!options[i].name)
  440. break;
  441. if (!strcmp(options[i].name, "config"))
  442. continue;
  443. val = json_object_get(opt_config, options[i].name);
  444. if (!val)
  445. continue;
  446. if (options[i].has_arg && json_is_string(val)) {
  447. char *s = strdup(json_string_value(val));
  448. if (!s)
  449. break;
  450. parse_arg(options[i].val, s);
  451. free(s);
  452. } else if (!options[i].has_arg && json_is_true(val))
  453. parse_arg(options[i].val, "");
  454. else
  455. fprintf(stderr, "JSON option %s invalid\n",
  456. options[i].name);
  457. }
  458. }
  459. static void parse_cmdline(int argc, char *argv[])
  460. {
  461. int key;
  462. while (1) {
  463. key = getopt_long(argc, argv, "a:c:qDPr:s:t:h?", options, NULL);
  464. if (key < 0)
  465. break;
  466. parse_arg(key, optarg);
  467. }
  468. parse_config();
  469. }
  470. int main (int argc, char *argv[])
  471. {
  472. int i;
  473. pthread_t *t_all;
  474. rpc_url = strdup(DEF_RPC_URL);
  475. userpass = strdup(DEF_RPC_USERPASS);
  476. /* parse command line */
  477. parse_cmdline(argc, argv);
  478. /* set our priority to the highest (aka "nicest, least intrusive") */
  479. if (setpriority(PRIO_PROCESS, 0, 19))
  480. perror("setpriority");
  481. t_all = calloc(opt_n_threads, sizeof(pthread_t));
  482. if (!t_all)
  483. return 1;
  484. /* start mining threads */
  485. for (i = 0; i < opt_n_threads; i++) {
  486. if (pthread_create(&t_all[i], NULL, miner_thread,
  487. (void *)(unsigned long) i)) {
  488. fprintf(stderr, "thread %d create failed\n", i);
  489. return 1;
  490. }
  491. sleep(1); /* don't pound RPC server all at once */
  492. }
  493. fprintf(stderr, "%d miner threads started, "
  494. "using SHA256 '%s' algorithm.\n",
  495. opt_n_threads,
  496. algo_names[opt_algo]);
  497. /* main loop - simply wait for all threads to exit */
  498. for (i = 0; i < opt_n_threads; i++)
  499. pthread_join(t_all[i], NULL);
  500. fprintf(stderr, "all threads dead, fred. exiting.\n");
  501. return 0;
  502. }