cpu-miner.c 7.9 KB

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