Clean up proof-of-work checking, attempting to address find-solution-too-fast problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
diff --git a/cpu-miner.c b/cpu-miner.c
index a78e13f..d785101 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -16,8 +16,12 @@
#include "sha256_generic.c"
-static const bool opt_verbose = true;
-static const bool opt_debug = true;
+enum {
+ POW_SLEEP_INTERVAL = 5,
+};
+
+static const bool opt_verbose = false;
+static const bool opt_debug = false;
struct data_buffer {
void *buf;
@@ -32,7 +36,7 @@ struct upload_buffer {
struct work {
unsigned char midstate[32];
unsigned char data[128];
- unsigned char hash[64];
+ unsigned char hash[32];
unsigned char hash1[64];
BIGNUM *target;
};
@@ -169,6 +173,19 @@ err_out:
return NULL;
}
+static char *bin2hex(unsigned char *p, size_t len)
+{
+ int i;
+ char *s = malloc((len * 2) + 1);
+ if (!s)
+ return NULL;
+
+ for (i = 0; i < len; i++)
+ sprintf(s + (i * 2), "%02x", p[i]);
+
+ return s;
+}
+
static bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
{
while (*hexstr && len) {
@@ -270,6 +287,17 @@ err_out:
return NULL;
}
+#define ___constant_swab32(x) ((u32)( \
+ (((u32)(x) & (u32)0x000000ffUL) << 24) | \
+ (((u32)(x) & (u32)0x0000ff00UL) << 8) | \
+ (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \
+ (((u32)(x) & (u32)0xff000000UL) >> 24)))
+
+static inline uint32_t swab32(uint32_t v)
+{
+ return ___constant_swab32(v);
+}
+
static void runhash(void *state, void *input, const void *init)
{
memcpy(state, init, 32);
@@ -285,6 +313,7 @@ static const uint32_t init_state[8] = {
static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash)
{
+ uint32_t *hash32 = (uint32_t *) hash;
uint32_t *nonce = (uint32_t *)(data + 12);
uint32_t n;
@@ -296,14 +325,21 @@ static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
runhash(hash1, data, midstate);
runhash(hash, hash1, init_state);
- if (((uint16_t *)hash)[14] == 0) {
- if (opt_debug)
- fprintf(stderr, "DBG: found zeroes in hash\n");
+ if (hash32[7] == 0) {
+ if (1) {
+ char *hexstr;
+
+ hexstr = bin2hex(hash, 32);
+ fprintf(stderr,
+ "DBG: found zeroes in hash:\n%s\n",
+ hexstr);
+ free(hexstr);
+ }
return n;
}
if ((n & 0xffffff) == 0) {
- if (opt_debug)
+ if (1)
fprintf(stderr, "DBG: end of nonce range\n");
return 0;
}
@@ -315,22 +351,63 @@ static const char *userpass = "pretzel:smooth";
static void submit_work(struct work *work)
{
- char hexstr[256 + 1], *s;
+ char *hexstr = NULL, *s = NULL;
+ json_t *val, *res;
int i;
- json_t *val;
+ unsigned char hash_rev[32];
+ BIGNUM *hashnum;
+ char *s_hash, *s_target;
- printf("PROOF OF WORK FOUND\n");
+ printf("PROOF OF WORK FOUND? submitting...\n");
+
+ for (i = 0; i < 32/4; i++)
+ ((uint32_t *)hash_rev)[i] =
+ swab32(((uint32_t *)work->hash)[i]);
+
+ hashnum = BN_bin2bn(hash_rev, sizeof(hash_rev), NULL);
+ if (!hashnum) {
+ fprintf(stderr, "BN_bin2bn failed\n");
+ return;
+ }
+
+ s_hash = BN_bn2hex(hashnum);
+ s_target = BN_bn2hex(work->target);
+ fprintf(stderr, " hash:%s\n hashTarget:%s\n",
+ s_hash, s_target);
+ free(s_hash);
+ free(s_target);
+
+#if 0
+ i = BN_cmp(hashnum, work->target);
+#endif
+
+ BN_free(hashnum);
+
+#if 0
+ if (i >= 0) {
+ fprintf(stderr, "---INVALID--- proof of work found.\n");
+ return;
+ }
+#endif
+
+#if 0
+ /* byte reverse data */
+ for (i = 0; i < 128/4; i ++)
+ ((uint32_t *)work->data)[i] =
+ swab32(((uint32_t *)work->data)[i]);
+#endif
/* build hex string */
- for (i = 0; i < sizeof(work->data); i++)
- sprintf(hexstr + (i * 2), "%02x", work->data[i]);
+ hexstr = bin2hex(work->data, sizeof(work->data));
+ if (!hexstr)
+ goto out;
/* build JSON-RPC request */
if (asprintf(&s,
"{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
hexstr) < 0) {
fprintf(stderr, "asprintf failed\n");
- return;
+ goto out;
}
if (opt_debug)
@@ -340,11 +417,19 @@ static void submit_work(struct work *work)
val = json_rpc_call(url, userpass, s);
if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n");
- return;
+ goto out;
}
- free(s);
+ res = json_object_get(val, "result");
+
+ printf("PROOF OF WORK RESULT: %s\n",
+ json_is_true(res) ? "true (yay!!!)" : "false (booooo)");
+
json_decref(val);
+
+out:
+ free(s);
+ free(hexstr);
}
static int main_loop(void)
@@ -388,7 +473,7 @@ static int main_loop(void)
submit_work(work);
fprintf(stderr, "sleeping, after proof-of-work...\n");
- sleep(20);
+ sleep(POW_SLEEP_INTERVAL);
}
work_free(work);