cpu-miner.c 8.3 KB

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