cpu-miner.c 11 KB

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