cpu-miner.c 7.9 KB

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