cpu-miner.c 11 KB

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