cpu-miner.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. printf("PROOF OF WORK FOUND? submitting...\n");
  172. /* build hex string */
  173. hexstr = bin2hex(work->data, sizeof(work->data));
  174. if (!hexstr)
  175. goto out;
  176. /* build JSON-RPC request */
  177. sprintf(s,
  178. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  179. hexstr);
  180. if (opt_debug)
  181. fprintf(stderr, "DBG: sending RPC call:\n%s", s);
  182. /* issue JSON-RPC request */
  183. val = json_rpc_call(rpc_url, userpass, s);
  184. if (!val) {
  185. fprintf(stderr, "submit_work json_rpc_call failed\n");
  186. goto out;
  187. }
  188. res = json_object_get(val, "result");
  189. printf("PROOF OF WORK RESULT: %s\n",
  190. json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  191. json_decref(val);
  192. out:
  193. free(hexstr);
  194. }
  195. static void hashmeter(int thr_id, const struct timeval *diff,
  196. unsigned long hashes_done)
  197. {
  198. double khashes, secs;
  199. khashes = hashes_done / 1000.0;
  200. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  201. if (!opt_quiet)
  202. printf("HashMeter(%d): %lu hashes, %.2f khash/sec\n",
  203. thr_id, hashes_done,
  204. khashes / secs);
  205. }
  206. static void *miner_thread(void *thr_id_int)
  207. {
  208. int thr_id = (unsigned long) thr_id_int;
  209. int failures = 0;
  210. static const char *rpc_req =
  211. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  212. while (1) {
  213. struct work work __attribute__((aligned(128)));
  214. unsigned long hashes_done;
  215. struct timeval tv_start, tv_end, diff;
  216. uint32_t max_nonce = 0xffffff;
  217. json_t *val;
  218. bool rc;
  219. /* obtain new work from bitcoin */
  220. val = json_rpc_call(rpc_url, userpass, rpc_req);
  221. if (!val) {
  222. fprintf(stderr, "json_rpc_call failed, ");
  223. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  224. fprintf(stderr, "terminating thread\n");
  225. return NULL; /* exit thread */
  226. }
  227. /* pause, then restart work loop */
  228. fprintf(stderr, "retry after %d seconds\n",
  229. FAILURE_INTERVAL);
  230. sleep(FAILURE_INTERVAL);
  231. continue;
  232. }
  233. /* decode result into work state struct */
  234. rc = work_decode(json_object_get(val, "result"), &work);
  235. if (!rc) {
  236. fprintf(stderr, "JSON-decode of work failed, ");
  237. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  238. fprintf(stderr, "terminating thread\n");
  239. return NULL; /* exit thread */
  240. }
  241. /* pause, then restart work loop */
  242. fprintf(stderr, "retry after %d seconds\n",
  243. FAILURE_INTERVAL);
  244. sleep(FAILURE_INTERVAL);
  245. continue;
  246. }
  247. json_decref(val);
  248. hashes_done = 0;
  249. gettimeofday(&tv_start, NULL);
  250. /* scan nonces for a proof-of-work hash */
  251. switch (opt_algo) {
  252. case ALGO_C:
  253. rc = scanhash_c(work.midstate, work.data + 64,
  254. work.hash1, work.hash,
  255. max_nonce, &hashes_done);
  256. break;
  257. #ifdef WANT_SSE2_4WAY
  258. case ALGO_4WAY: {
  259. unsigned int rc4 =
  260. ScanHash_4WaySSE2(work.midstate, work.data + 64,
  261. work.hash1, work.hash,
  262. max_nonce, &hashes_done);
  263. rc = (rc4 == -1) ? false : true;
  264. }
  265. break;
  266. #endif
  267. #ifdef WANT_VIA_PADLOCK
  268. case ALGO_VIA:
  269. rc = scanhash_via(work.data, max_nonce, &hashes_done);
  270. break;
  271. #endif
  272. case ALGO_CRYPTOPP:
  273. rc = scanhash_cryptopp(work.midstate, work.data + 64,
  274. work.hash1, work.hash,
  275. max_nonce, &hashes_done);
  276. break;
  277. #ifdef WANT_CRYPTOPP_ASM32
  278. case ALGO_CRYPTOPP_ASM32:
  279. rc = scanhash_asm32(work.midstate, work.data + 64,
  280. work.hash1, work.hash,
  281. max_nonce, &hashes_done);
  282. break;
  283. #endif
  284. default:
  285. /* should never happen */
  286. return NULL;
  287. }
  288. /* record scanhash elapsed time */
  289. gettimeofday(&tv_end, NULL);
  290. timeval_subtract(&diff, &tv_end, &tv_start);
  291. hashmeter(thr_id, &diff, hashes_done);
  292. /* adjust max_nonce to meet target scan time */
  293. if (diff.tv_sec > (opt_scantime * 2))
  294. max_nonce /= 2; /* large decrease */
  295. else if (diff.tv_sec > opt_scantime)
  296. max_nonce -= 1000; /* small decrease */
  297. else if (diff.tv_sec < (opt_scantime - 1))
  298. max_nonce += 1000; /* small increase */
  299. /* catch stupidly slow cases, such as simulators */
  300. if (max_nonce < 1000)
  301. max_nonce = 1000;
  302. /* if nonce found, submit work */
  303. if (rc)
  304. submit_work(&work);
  305. failures = 0;
  306. }
  307. return NULL;
  308. }
  309. static void show_usage(void)
  310. {
  311. int i;
  312. printf("minerd version %s\n\n", VERSION);
  313. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  314. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  315. struct option_help *h;
  316. h = &options_help[i];
  317. printf("--%s\n%s\n\n", h->name, h->helptext);
  318. }
  319. exit(1);
  320. }
  321. static void parse_arg (int key, char *arg)
  322. {
  323. int v, i;
  324. switch(key) {
  325. case 'a':
  326. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  327. if (algo_names[i] &&
  328. !strcmp(arg, algo_names[i])) {
  329. opt_algo = i;
  330. break;
  331. }
  332. }
  333. if (i == ARRAY_SIZE(algo_names))
  334. show_usage();
  335. break;
  336. case 'q':
  337. opt_quiet = true;
  338. break;
  339. case 'D':
  340. opt_debug = true;
  341. break;
  342. case 'P':
  343. opt_protocol = true;
  344. break;
  345. case 'r':
  346. v = atoi(arg);
  347. if (v < -1 || v > 9999) /* sanity check */
  348. show_usage();
  349. opt_retries = v;
  350. break;
  351. case 's':
  352. v = atoi(arg);
  353. if (v < 1 || v > 9999) /* sanity check */
  354. show_usage();
  355. opt_scantime = v;
  356. break;
  357. case 't':
  358. v = atoi(arg);
  359. if (v < 1 || v > 9999) /* sanity check */
  360. show_usage();
  361. opt_n_threads = v;
  362. break;
  363. case 1001: /* --url */
  364. if (strncmp(arg, "http://", 7) &&
  365. strncmp(arg, "https://", 8))
  366. show_usage();
  367. rpc_url = arg;
  368. break;
  369. case 1002: /* --userpass */
  370. if (!strchr(arg, ':'))
  371. show_usage();
  372. userpass = arg;
  373. break;
  374. default:
  375. show_usage();
  376. }
  377. }
  378. static void parse_cmdline(int argc, char *argv[])
  379. {
  380. int key;
  381. while (1) {
  382. key = getopt_long(argc, argv, "a:qDPr:s:t:h?", options, NULL);
  383. if (key < 0)
  384. break;
  385. parse_arg(key, optarg);
  386. }
  387. }
  388. int main (int argc, char *argv[])
  389. {
  390. int i;
  391. pthread_t *t_all;
  392. /* parse command line */
  393. parse_cmdline(argc, argv);
  394. /* set our priority to the highest (aka "nicest, least intrusive") */
  395. if (setpriority(PRIO_PROCESS, 0, 19))
  396. perror("setpriority");
  397. t_all = calloc(opt_n_threads, sizeof(pthread_t));
  398. if (!t_all)
  399. return 1;
  400. /* start mining threads */
  401. for (i = 0; i < opt_n_threads; i++) {
  402. if (pthread_create(&t_all[i], NULL, miner_thread,
  403. (void *)(unsigned long) i)) {
  404. fprintf(stderr, "thread %d create failed\n", i);
  405. return 1;
  406. }
  407. sleep(1); /* don't pound RPC server all at once */
  408. }
  409. fprintf(stderr, "%d miner threads started, "
  410. "using SHA256 '%s' algorithm.\n",
  411. opt_n_threads,
  412. algo_names[opt_algo]);
  413. /* main loop - simply wait for all threads to exit */
  414. for (i = 0; i < opt_n_threads; i++)
  415. pthread_join(t_all[i], NULL);
  416. fprintf(stderr, "all threads dead, fred. exiting.\n");
  417. return 0;
  418. }