cpu-miner.c 8.7 KB

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