cpu-miner.c 11 KB

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