cpu-miner.c 9.9 KB

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