cpu-miner.c 8.8 KB

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