cpu-miner.c 11 KB

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