cpu-miner.c 10 KB

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