cpu-miner.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. #define _GNU_SOURCE
  10. #include "cpuminer-config.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #include <unistd.h>
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18. #include <pthread.h>
  19. #include <argp.h>
  20. #include <jansson.h>
  21. #include <curl/curl.h>
  22. #include <openssl/bn.h>
  23. #define PROGRAM_NAME "minerd"
  24. #include "sha256_generic.c"
  25. enum {
  26. STAT_SLEEP_INTERVAL = 10,
  27. STAT_CTR_INTERVAL = 10000000,
  28. };
  29. static bool opt_debug;
  30. static bool opt_protocol;
  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 = "http://127.0.0.1:8332/";
  37. static char *userpass = "rpcuser:rpcpass";
  38. static struct argp_option options[] = {
  39. { "debug", 'D', NULL, 0,
  40. "Enable debug output" },
  41. { "protocol-dump", 'P', NULL, 0,
  42. "Verbose dump of protocol-level activities" },
  43. { "threads", 't', "N", 0,
  44. "Number of miner threads (default: 1)" },
  45. { "url", 1001, "URL", 0,
  46. "URL for bitcoin JSON-RPC server "
  47. "(default: http://127.0.0.1:8332/)" },
  48. { "userpass", 1002, "USER:PASS", 0,
  49. "Username:Password pair for bitcoin JSON-RPC server "
  50. "(default: rpcuser:rpcpass)" },
  51. { }
  52. };
  53. static const char doc[] =
  54. PROGRAM_NAME " - CPU miner for bitcoin";
  55. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  56. static const struct argp argp = { options, parse_opt, NULL, doc };
  57. struct data_buffer {
  58. void *buf;
  59. size_t len;
  60. };
  61. struct upload_buffer {
  62. const void *buf;
  63. size_t len;
  64. };
  65. struct work {
  66. unsigned char midstate[32];
  67. unsigned char data[128];
  68. unsigned char hash[32];
  69. unsigned char hash1[64];
  70. BIGNUM *target;
  71. };
  72. static void databuf_free(struct data_buffer *db)
  73. {
  74. if (!db)
  75. return;
  76. free(db->buf);
  77. memset(db, 0, sizeof(*db));
  78. }
  79. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  80. void *user_data)
  81. {
  82. struct data_buffer *db = user_data;
  83. size_t len = size * nmemb;
  84. size_t oldlen, newlen;
  85. void *newmem;
  86. static const unsigned char zero;
  87. oldlen = db->len;
  88. newlen = oldlen + len;
  89. newmem = realloc(db->buf, newlen + 1);
  90. if (!newmem)
  91. return 0;
  92. db->buf = newmem;
  93. db->len = newlen;
  94. memcpy(db->buf + oldlen, ptr, len);
  95. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  96. return len;
  97. }
  98. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  99. void *user_data)
  100. {
  101. struct upload_buffer *ub = user_data;
  102. int len = size * nmemb;
  103. if (len > ub->len)
  104. len = ub->len;
  105. if (len) {
  106. memcpy(ptr, ub->buf, len);
  107. ub->buf += len;
  108. ub->len -= len;
  109. }
  110. return len;
  111. }
  112. static json_t *json_rpc_call(const char *url, const char *userpass,
  113. const char *rpc_req)
  114. {
  115. CURL *curl;
  116. json_t *val;
  117. int rc;
  118. struct data_buffer all_data = { };
  119. struct upload_buffer upload_data;
  120. json_error_t err = { };
  121. struct curl_slist *headers = NULL;
  122. char len_hdr[64];
  123. curl = curl_easy_init();
  124. if (!curl)
  125. return NULL;
  126. if (opt_protocol)
  127. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  128. curl_easy_setopt(curl, CURLOPT_URL, url);
  129. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  130. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  131. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  132. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  133. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  134. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  135. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  136. if (userpass) {
  137. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  138. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  139. }
  140. curl_easy_setopt(curl, CURLOPT_POST, 1);
  141. if (opt_protocol)
  142. printf("JSON protocol request:\n%s\n", rpc_req);
  143. upload_data.buf = rpc_req;
  144. upload_data.len = strlen(rpc_req);
  145. sprintf(len_hdr, "Content-Length: %lu",
  146. (unsigned long) upload_data.len);
  147. headers = curl_slist_append(headers,
  148. "Content-type: application/json");
  149. headers = curl_slist_append(headers, len_hdr);
  150. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  151. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  152. rc = curl_easy_perform(curl);
  153. if (rc)
  154. goto err_out;
  155. val = json_loads(all_data.buf, &err);
  156. if (!val) {
  157. fprintf(stderr, "JSON failed(%d): %s\n", err.line, err.text);
  158. goto err_out;
  159. }
  160. if (opt_protocol) {
  161. char *s = json_dumps(val, JSON_INDENT(3));
  162. printf("JSON protocol response:\n%s\n", s);
  163. free(s);
  164. }
  165. databuf_free(&all_data);
  166. curl_slist_free_all(headers);
  167. curl_easy_cleanup(curl);
  168. return val;
  169. err_out:
  170. databuf_free(&all_data);
  171. curl_slist_free_all(headers);
  172. curl_easy_cleanup(curl);
  173. return NULL;
  174. }
  175. static char *bin2hex(unsigned char *p, size_t len)
  176. {
  177. int i;
  178. char *s = malloc((len * 2) + 1);
  179. if (!s)
  180. return NULL;
  181. for (i = 0; i < len; i++)
  182. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  183. return s;
  184. }
  185. static bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  186. {
  187. while (*hexstr && len) {
  188. char hex_byte[3];
  189. unsigned int v;
  190. if (!hexstr[1]) {
  191. fprintf(stderr, "hex2bin str truncated\n");
  192. return false;
  193. }
  194. hex_byte[0] = hexstr[0];
  195. hex_byte[1] = hexstr[1];
  196. hex_byte[2] = 0;
  197. if (sscanf(hex_byte, "%x", &v) != 1) {
  198. fprintf(stderr, "hex2bin sscanf '%s' failed\n",
  199. hex_byte);
  200. return false;
  201. }
  202. *p = (unsigned char) v;
  203. p++;
  204. hexstr += 2;
  205. len--;
  206. }
  207. return (len == 0 && *hexstr == 0) ? true : false;
  208. }
  209. static bool jobj_binary(const json_t *obj, const char *key,
  210. void *buf, size_t buflen)
  211. {
  212. const char *hexstr;
  213. json_t *tmp;
  214. tmp = json_object_get(obj, key);
  215. if (!tmp) {
  216. fprintf(stderr, "JSON key '%s' not found\n", key);
  217. return false;
  218. }
  219. hexstr = json_string_value(tmp);
  220. if (!hexstr) {
  221. fprintf(stderr, "JSON key '%s' is not a string\n", key);
  222. return false;
  223. }
  224. if (!hex2bin(buf, hexstr, buflen))
  225. return false;
  226. return true;
  227. }
  228. static void work_free(struct work *work)
  229. {
  230. if (!work)
  231. return;
  232. if (work->target)
  233. BN_free(work->target);
  234. free(work);
  235. }
  236. static struct work *work_decode(const json_t *val)
  237. {
  238. struct work *work;
  239. work = calloc(1, sizeof(*work));
  240. if (!work)
  241. return NULL;
  242. if (!jobj_binary(val, "midstate",
  243. work->midstate, sizeof(work->midstate))) {
  244. fprintf(stderr, "JSON inval midstate\n");
  245. goto err_out;
  246. }
  247. if (!jobj_binary(val, "data", work->data, sizeof(work->data))) {
  248. fprintf(stderr, "JSON inval data\n");
  249. goto err_out;
  250. }
  251. if (!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1))) {
  252. fprintf(stderr, "JSON inval hash1\n");
  253. goto err_out;
  254. }
  255. if (!BN_hex2bn(&work->target,
  256. json_string_value(json_object_get(val, "target")))) {
  257. fprintf(stderr, "JSON inval target\n");
  258. goto err_out;
  259. }
  260. return work;
  261. err_out:
  262. work_free(work);
  263. return NULL;
  264. }
  265. static void inc_stats(uint64_t n_hashes)
  266. {
  267. pthread_mutex_lock(&stats_mutex);
  268. hash_ctr += n_hashes;
  269. pthread_mutex_unlock(&stats_mutex);
  270. }
  271. static void runhash(void *state, void *input, const void *init)
  272. {
  273. memcpy(state, init, 32);
  274. sha256_transform(state, input);
  275. }
  276. static const uint32_t init_state[8] = {
  277. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  278. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  279. };
  280. /* suspiciously similar to ScanHash* from bitcoin */
  281. static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
  282. unsigned char *hash1, unsigned char *hash)
  283. {
  284. uint32_t *hash32 = (uint32_t *) hash;
  285. uint32_t *nonce = (uint32_t *)(data + 12);
  286. uint32_t n;
  287. unsigned long stat_ctr = 0;
  288. while (1) {
  289. n = *nonce;
  290. n++;
  291. *nonce = n;
  292. runhash(hash1, data, midstate);
  293. runhash(hash, hash1, init_state);
  294. if (hash32[7] == 0) {
  295. char *hexstr;
  296. hexstr = bin2hex(hash, 32);
  297. fprintf(stderr,
  298. "DBG: found zeroes in hash:\n%s\n",
  299. hexstr);
  300. free(hexstr);
  301. return n;
  302. }
  303. stat_ctr++;
  304. if (stat_ctr >= STAT_CTR_INTERVAL) {
  305. inc_stats(STAT_CTR_INTERVAL);
  306. stat_ctr = 0;
  307. }
  308. if ((n & 0xffffff) == 0) {
  309. inc_stats(stat_ctr);
  310. if (opt_debug)
  311. fprintf(stderr, "DBG: end of nonce range\n");
  312. return 0;
  313. }
  314. }
  315. }
  316. static void submit_work(struct work *work)
  317. {
  318. char *hexstr = NULL, *s = NULL;
  319. json_t *val, *res;
  320. printf("PROOF OF WORK FOUND? submitting...\n");
  321. /* build hex string */
  322. hexstr = bin2hex(work->data, sizeof(work->data));
  323. if (!hexstr)
  324. goto out;
  325. /* build JSON-RPC request */
  326. if (asprintf(&s,
  327. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  328. hexstr) < 0) {
  329. fprintf(stderr, "asprintf failed\n");
  330. goto out;
  331. }
  332. if (opt_debug)
  333. fprintf(stderr, "DBG: sending RPC call:\n%s", s);
  334. /* issue JSON-RPC request */
  335. val = json_rpc_call(rpc_url, userpass, s);
  336. if (!val) {
  337. fprintf(stderr, "submit_work json_rpc_call failed\n");
  338. goto out;
  339. }
  340. res = json_object_get(val, "result");
  341. printf("PROOF OF WORK RESULT: %s\n",
  342. json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
  343. json_decref(val);
  344. out:
  345. free(s);
  346. free(hexstr);
  347. }
  348. static void *miner_thread(void *dummy)
  349. {
  350. static const char *rpc_req =
  351. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  352. while (1) {
  353. json_t *val;
  354. struct work *work;
  355. uint32_t nonce;
  356. /* obtain new work from bitcoin */
  357. val = json_rpc_call(rpc_url, userpass, rpc_req);
  358. if (!val) {
  359. fprintf(stderr, "json_rpc_call failed\n");
  360. return NULL;
  361. }
  362. /* decode result into work state struct */
  363. work = work_decode(json_object_get(val, "result"));
  364. if (!work) {
  365. fprintf(stderr, "work decode failed\n");
  366. return NULL;
  367. }
  368. json_decref(val);
  369. /* scan nonces for a proof-of-work hash */
  370. nonce = scanhash(work->midstate, work->data + 64,
  371. work->hash1, work->hash);
  372. /* if nonce found, submit work */
  373. if (nonce)
  374. submit_work(work);
  375. work_free(work);
  376. }
  377. return NULL;
  378. }
  379. static error_t parse_opt (int key, char *arg, struct argp_state *state)
  380. {
  381. int v;
  382. switch(key) {
  383. case 'D':
  384. opt_debug = true;
  385. break;
  386. case 'P':
  387. opt_protocol = true;
  388. break;
  389. case 't':
  390. v = atoi(arg);
  391. if (v < 1 || v > 9999) /* sanity check */
  392. argp_usage(state);
  393. opt_n_threads = v;
  394. break;
  395. case 1001: /* --url */
  396. if (strncmp(arg, "http://", 7) &&
  397. strncmp(arg, "https://", 8))
  398. argp_usage(state);
  399. rpc_url = arg;
  400. break;
  401. case 1002: /* --userpass */
  402. if (!strchr(arg, ':'))
  403. argp_usage(state);
  404. userpass = arg;
  405. break;
  406. case ARGP_KEY_ARG:
  407. argp_usage(state); /* too many args */
  408. break;
  409. case ARGP_KEY_END:
  410. break;
  411. default:
  412. return ARGP_ERR_UNKNOWN;
  413. }
  414. return 0;
  415. }
  416. static void calc_stats(void)
  417. {
  418. uint64_t hashes;
  419. long double hd, sd;
  420. pthread_mutex_lock(&stats_mutex);
  421. hashes = hash_ctr;
  422. hash_ctr = 0;
  423. pthread_mutex_unlock(&stats_mutex);
  424. hashes = hashes / 1000;
  425. hd = hashes;
  426. sd = STAT_SLEEP_INTERVAL;
  427. fprintf(stderr, "wildly inaccurate HashMeter: %.2Lf khash/sec\n", hd / sd);
  428. }
  429. int main (int argc, char *argv[])
  430. {
  431. error_t aprc;
  432. int i;
  433. /* parse command line */
  434. aprc = argp_parse(&argp, argc, argv, 0, NULL, NULL);
  435. if (aprc) {
  436. fprintf(stderr, "argp_parse failed: %s\n", strerror(aprc));
  437. return 1;
  438. }
  439. /* set our priority to the highest (aka "nicest, least intrusive") */
  440. if (setpriority(PRIO_PROCESS, 0, 19))
  441. perror("setpriority");
  442. /* start mining threads */
  443. for (i = 0; i < opt_n_threads; i++) {
  444. pthread_t t;
  445. if (pthread_create(&t, NULL, miner_thread, NULL)) {
  446. fprintf(stderr, "thread %d create failed\n", i);
  447. return 1;
  448. }
  449. sleep(1); /* don't pound RPC server all at once */
  450. }
  451. fprintf(stderr, "%d miner threads started.\n", opt_n_threads);
  452. /* main loop */
  453. while (program_running) {
  454. sleep(STAT_SLEEP_INTERVAL);
  455. calc_stats();
  456. }
  457. return 0;
  458. }