cpu-miner.c 9.0 KB

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