|
|
@@ -5347,15 +5347,39 @@ static void gen_hash(unsigned char *data, unsigned char *hash, int len)
|
|
|
sha2(hash1, 32, hash, false);
|
|
|
}
|
|
|
|
|
|
+static void set_work_target(struct work *work, int diff)
|
|
|
+{
|
|
|
+ unsigned char rtarget[33], target[33];
|
|
|
+ uint8_t *data8;
|
|
|
+ int i, j;
|
|
|
+
|
|
|
+ /* Scale to any diff by setting number of bits according to diff */
|
|
|
+ hex2bin(rtarget, "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32);
|
|
|
+ data8 = (uint8_t *)(rtarget + 4);
|
|
|
+ for (i = 1, j = 0; i < diff; i++, j++) {
|
|
|
+ int byte = j / 8;
|
|
|
+ int bit = j % 8;
|
|
|
+
|
|
|
+ data8[byte] &= ~(128 >> bit);
|
|
|
+ }
|
|
|
+ swab256(target, rtarget);
|
|
|
+ if (opt_debug) {
|
|
|
+ char *htarget = bin2hex(target, 32);
|
|
|
+
|
|
|
+ if (likely(htarget)) {
|
|
|
+ applog(LOG_DEBUG, "Generated target %s", htarget);
|
|
|
+ free(htarget);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ memcpy(work->target, target, 256);
|
|
|
+}
|
|
|
+
|
|
|
static void gen_stratum_work(struct pool *pool, struct work *work)
|
|
|
{
|
|
|
unsigned char *coinbase, merkle_root[33], merkle_sha[65], *merkle_hash;
|
|
|
- int len, cb1_len, n1_len, cb2_len, i, j;
|
|
|
- unsigned char rtarget[33], target[33];
|
|
|
char header[257], hash1[129], *nonce2;
|
|
|
+ int len, cb1_len, n1_len, cb2_len, i;
|
|
|
uint32_t *data32, *swap32;
|
|
|
- uint8_t *data8;
|
|
|
- int diff;
|
|
|
|
|
|
mutex_lock(&pool->pool_lock);
|
|
|
|
|
|
@@ -5401,7 +5425,9 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
|
|
|
strcat(header, "00000000"); /* nonce */
|
|
|
strcat(header, "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000");
|
|
|
|
|
|
- diff = pool->swork.diff;
|
|
|
+ /* Store the stratum work diff to check it still matches the pool's
|
|
|
+ * stratum diff when submitting shares */
|
|
|
+ work->sdiff = pool->swork.diff;
|
|
|
|
|
|
/* Copy parameters required for share submission */
|
|
|
sprintf(work->job_id, "%s", pool->swork.job_id);
|
|
|
@@ -5424,25 +5450,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
|
|
|
if (unlikely(!hex2bin(work->hash1, hash1, 64)))
|
|
|
quit(1, "Failed to convert hash1 in gen_stratum_work");
|
|
|
|
|
|
- /* Scale to any diff by setting number of bits according to diff */
|
|
|
- hex2bin(rtarget, "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32);
|
|
|
- data8 = (uint8_t *)(rtarget + 4);
|
|
|
- for (i = 1, j = 0; i < diff; i++, j++) {
|
|
|
- int byte = j / 8;
|
|
|
- int bit = j % 8;
|
|
|
-
|
|
|
- data8[byte] &= ~(128 >> bit);
|
|
|
- }
|
|
|
- swab256(target, rtarget);
|
|
|
- if (opt_debug) {
|
|
|
- char *htarget = bin2hex(target, 32);
|
|
|
-
|
|
|
- if (likely(htarget)) {
|
|
|
- applog(LOG_DEBUG, "Generated target %s", htarget);
|
|
|
- free(htarget);
|
|
|
- }
|
|
|
- }
|
|
|
- memcpy(work->target, target, 256);
|
|
|
+ set_work_target(work, work->sdiff);
|
|
|
|
|
|
work->pool = pool;
|
|
|
work->stratum = true;
|
|
|
@@ -5450,7 +5458,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
|
|
|
work->id = total_work++;
|
|
|
work->longpoll = false;
|
|
|
work->getwork_mode = GETWORK_MODE_STRATUM;
|
|
|
- calc_diff(work, diff);
|
|
|
+ calc_diff(work, work->sdiff);
|
|
|
|
|
|
gettimeofday(&work->tv_staged, NULL);
|
|
|
}
|
|
|
@@ -5572,7 +5580,7 @@ err_out:
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-enum test_nonce2_result hashtest2(const struct work *work, bool checktarget)
|
|
|
+enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
|
|
|
{
|
|
|
uint32_t *data32 = (uint32_t *)(work->data);
|
|
|
unsigned char swap[128];
|
|
|
@@ -5580,6 +5588,8 @@ enum test_nonce2_result hashtest2(const struct work *work, bool checktarget)
|
|
|
unsigned char hash1[32];
|
|
|
unsigned char hash2[32];
|
|
|
uint32_t *hash2_32 = (uint32_t *)hash2;
|
|
|
+ struct pool *pool = work->pool;
|
|
|
+ int diff;
|
|
|
|
|
|
swap32yes(swap32, data32, 80 / 4);
|
|
|
|
|
|
@@ -5596,6 +5606,17 @@ enum test_nonce2_result hashtest2(const struct work *work, bool checktarget)
|
|
|
|
|
|
memcpy((void*)work->hash, hash2, 32);
|
|
|
|
|
|
+ if (work->stratum) {
|
|
|
+ mutex_lock(&pool->pool_lock);
|
|
|
+ diff = pool->swork.diff;
|
|
|
+ mutex_unlock(&pool->pool_lock);
|
|
|
+
|
|
|
+ if (unlikely(work->sdiff != diff)) {
|
|
|
+ applog(LOG_DEBUG, "Share needs retargetting to match pool");
|
|
|
+ set_work_target(work, diff);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!fulltest(work->hash, work->target))
|
|
|
return TNR_HIGH;
|
|
|
|