cpu-miner.c 9.4 KB

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