cpu-miner.c 9.3 KB

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