cpu-miner.c 8.6 KB

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