cpu-miner.c 10 KB

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