cpu-miner.c 7.6 KB

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