cpu-miner.c 12 KB

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