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. STAT_SLEEP_INTERVAL = 100,
  30. STAT_CTR_INTERVAL = 10000000,
  31. FAILURE_INTERVAL = 30,
  32. };
  33. enum sha256_algos {
  34. ALGO_C, /* plain C */
  35. ALGO_4WAY, /* parallel SSE2 */
  36. ALGO_VIA, /* VIA padlock */
  37. ALGO_CRYPTOPP, /* Crypto++ (C) */
  38. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  39. };
  40. static const char *algo_names[] = {
  41. [ALGO_C] = "c",
  42. #ifdef WANT_SSE2_4WAY
  43. [ALGO_4WAY] = "4way",
  44. #endif
  45. #ifdef WANT_VIA_PADLOCK
  46. [ALGO_VIA] = "via",
  47. #endif
  48. [ALGO_CRYPTOPP] = "cryptopp",
  49. #ifdef WANT_CRYPTOPP_ASM32
  50. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  51. #endif
  52. };
  53. bool opt_debug = false;
  54. bool opt_protocol = false;
  55. bool opt_quiet = false;
  56. static int opt_retries = 10;
  57. static bool program_running = true;
  58. static const bool opt_time = true;
  59. static enum sha256_algos opt_algo = ALGO_C;
  60. static int opt_n_threads = 1;
  61. static char *rpc_url = DEF_RPC_URL;
  62. static char *userpass = DEF_RPC_USERPASS;
  63. struct option_help {
  64. const char *name;
  65. const char *helptext;
  66. };
  67. static struct option_help options_help[] = {
  68. { "help",
  69. "(-h) Display this help text" },
  70. { "algo XXX",
  71. "(-a XXX) Specify sha256 implementation:\n"
  72. "\tc\t\tLinux kernel sha256, implemented in C (default)"
  73. #ifdef WANT_SSE2_4WAY
  74. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  75. #endif
  76. #ifdef WANT_VIA_PADLOCK
  77. "\n\tvia\t\tVIA padlock implementation"
  78. #endif
  79. "\n\tcryptopp\tCrypto++ library implementation"
  80. #ifdef WANT_CRYPTOPP_ASM32
  81. "\n\tcryptopp_asm32\tCrypto++ library implementation"
  82. #endif
  83. },
  84. { "quiet",
  85. "(-q) Disable per-thread hashmeter output (default: off)" },
  86. { "debug",
  87. "(-D) Enable debug output (default: off)" },
  88. { "protocol-dump",
  89. "(-P) Verbose dump of protocol-level activities (default: off)" },
  90. { "retries N",
  91. "(-r N) Number of times to retry, if JSON-RPC call fails\n"
  92. "\t(default: 10; use -1 for \"never\")" },
  93. { "threads N",
  94. "(-t N) Number of miner threads (default: 1)" },
  95. { "url URL",
  96. "URL for bitcoin JSON-RPC server "
  97. "(default: " DEF_RPC_URL ")" },
  98. { "userpass USERNAME:PASSWORD",
  99. "Username:Password pair for bitcoin JSON-RPC server "
  100. "(default: " DEF_RPC_USERPASS ")" },
  101. };
  102. static struct option options[] = {
  103. { "help", 0, NULL, 'h' },
  104. { "algo", 1, NULL, 'a' },
  105. { "quiet", 0, NULL, 'q' },
  106. { "debug", 0, NULL, 'D' },
  107. { "protocol-dump", 0, NULL, 'P' },
  108. { "threads", 1, NULL, 't' },
  109. { "retries", 1, NULL, 'r' },
  110. { "url", 1, NULL, 1001 },
  111. { "userpass", 1, NULL, 1002 },
  112. { }
  113. };
  114. struct work {
  115. unsigned char data[128];
  116. unsigned char hash1[64];
  117. unsigned char midstate[32];
  118. unsigned char target[32];
  119. unsigned char hash[32];
  120. };
  121. static bool jobj_binary(const json_t *obj, const char *key,
  122. void *buf, size_t buflen)
  123. {
  124. const char *hexstr;
  125. json_t *tmp;
  126. tmp = json_object_get(obj, key);
  127. if (!tmp) {
  128. fprintf(stderr, "JSON key '%s' not found\n", key);
  129. return false;
  130. }
  131. hexstr = json_string_value(tmp);
  132. if (!hexstr) {
  133. fprintf(stderr, "JSON key '%s' is not a string\n", key);
  134. return false;
  135. }
  136. if (!hex2bin(buf, hexstr, buflen))
  137. return false;
  138. return true;
  139. }
  140. static bool work_decode(const json_t *val, struct work *work)
  141. {
  142. if (!jobj_binary(val, "midstate",
  143. work->midstate, sizeof(work->midstate))) {
  144. fprintf(stderr, "JSON inval midstate\n");
  145. goto err_out;
  146. }
  147. if (!jobj_binary(val, "data", work->data, sizeof(work->data))) {
  148. fprintf(stderr, "JSON inval data\n");
  149. goto err_out;
  150. }
  151. if (!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1))) {
  152. fprintf(stderr, "JSON inval hash1\n");
  153. goto err_out;
  154. }
  155. if (!jobj_binary(val, "target", work->target, sizeof(work->target))) {
  156. fprintf(stderr, "JSON inval target\n");
  157. goto err_out;
  158. }
  159. memset(work->hash, 0, sizeof(work->hash));
  160. return true;
  161. err_out:
  162. return false;
  163. }
  164. static void submit_work(struct work *work)
  165. {
  166. char *hexstr = NULL;
  167. json_t *val, *res;
  168. char s[345];
  169. printf("PROOF OF WORK FOUND? submitting...\n");
  170. /* build hex string */
  171. hexstr = bin2hex(work->data, sizeof(work->data));
  172. if (!hexstr)
  173. goto out;
  174. /* build JSON-RPC request */
  175. sprintf(s,
  176. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  177. hexstr);
  178. if (opt_debug)
  179. fprintf(stderr, "DBG: sending RPC call:\n%s", s);
  180. /* issue JSON-RPC request */
  181. val = json_rpc_call(rpc_url, userpass, s);
  182. if (!val) {
  183. fprintf(stderr, "submit_work json_rpc_call failed\n");
  184. goto out;
  185. }
  186. res = json_object_get(val, "result");
  187. printf("PROOF OF WORK RESULT: %s\n",
  188. json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  189. json_decref(val);
  190. out:
  191. free(hexstr);
  192. }
  193. static void hashmeter(int thr_id, struct timeval *tv_start,
  194. unsigned long hashes_done)
  195. {
  196. struct timeval tv_end, diff;
  197. double khashes, secs;
  198. gettimeofday(&tv_end, NULL);
  199. timeval_subtract(&diff, &tv_end, tv_start);
  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;
  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, &hashes_done);
  255. break;
  256. #ifdef WANT_SSE2_4WAY
  257. case ALGO_4WAY: {
  258. unsigned int rc4 =
  259. ScanHash_4WaySSE2(work.midstate, work.data + 64,
  260. work.hash1, work.hash,
  261. &hashes_done);
  262. rc = (rc4 == -1) ? false : true;
  263. }
  264. break;
  265. #endif
  266. #ifdef WANT_VIA_PADLOCK
  267. case ALGO_VIA:
  268. rc = scanhash_via(work.data, &hashes_done);
  269. break;
  270. #endif
  271. case ALGO_CRYPTOPP:
  272. rc = scanhash_cryptopp(work.midstate, work.data + 64,
  273. work.hash1, work.hash, &hashes_done);
  274. break;
  275. #ifdef WANT_CRYPTOPP_ASM32
  276. case ALGO_CRYPTOPP_ASM32:
  277. rc = scanhash_asm32(work.midstate, work.data + 64,
  278. work.hash1, work.hash, &hashes_done);
  279. break;
  280. #endif
  281. default:
  282. /* should never happen */
  283. return NULL;
  284. }
  285. hashmeter(thr_id, &tv_start, hashes_done);
  286. /* if nonce found, submit work */
  287. if (rc)
  288. submit_work(&work);
  289. failures = 0;
  290. }
  291. return NULL;
  292. }
  293. static void show_usage(void)
  294. {
  295. int i;
  296. printf("minerd version %s\n\n", VERSION);
  297. printf("Usage:\tminerd [options]\n\nSupported options:\n");
  298. for (i = 0; i < ARRAY_SIZE(options_help); i++) {
  299. struct option_help *h;
  300. h = &options_help[i];
  301. printf("--%s\n%s\n\n", h->name, h->helptext);
  302. }
  303. exit(1);
  304. }
  305. static void parse_arg (int key, char *arg)
  306. {
  307. int v, i;
  308. switch(key) {
  309. case 'a':
  310. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  311. if (algo_names[i] &&
  312. !strcmp(arg, algo_names[i])) {
  313. opt_algo = i;
  314. break;
  315. }
  316. }
  317. if (i == ARRAY_SIZE(algo_names))
  318. show_usage();
  319. break;
  320. case 'q':
  321. opt_quiet = true;
  322. break;
  323. case 'D':
  324. opt_debug = true;
  325. break;
  326. case 'P':
  327. opt_protocol = true;
  328. break;
  329. case 'r':
  330. v = atoi(arg);
  331. if (v < -1 || v > 9999) /* sanity check */
  332. show_usage();
  333. opt_retries = v;
  334. break;
  335. case 't':
  336. v = atoi(arg);
  337. if (v < 1 || v > 9999) /* sanity check */
  338. show_usage();
  339. opt_n_threads = v;
  340. break;
  341. case 1001: /* --url */
  342. if (strncmp(arg, "http://", 7) &&
  343. strncmp(arg, "https://", 8))
  344. show_usage();
  345. rpc_url = arg;
  346. break;
  347. case 1002: /* --userpass */
  348. if (!strchr(arg, ':'))
  349. show_usage();
  350. userpass = arg;
  351. break;
  352. default:
  353. show_usage();
  354. }
  355. }
  356. static void parse_cmdline(int argc, char *argv[])
  357. {
  358. int key;
  359. while (1) {
  360. key = getopt_long(argc, argv, "a:DPt:h?", options, NULL);
  361. if (key < 0)
  362. break;
  363. parse_arg(key, optarg);
  364. }
  365. }
  366. int main (int argc, char *argv[])
  367. {
  368. int i;
  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. /* start mining threads */
  375. for (i = 0; i < opt_n_threads; i++) {
  376. pthread_t t;
  377. if (pthread_create(&t, 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 */
  389. while (program_running) {
  390. sleep(STAT_SLEEP_INTERVAL);
  391. /* do nothing */
  392. }
  393. return 0;
  394. }