cpu-miner.c 7.7 KB

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