cpu-miner.c 13 KB

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