cpu-miner.c 7.9 KB

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