Make target on stratum scale to any size by clearing sequential bits according to diff.
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
diff --git a/cgminer.c b/cgminer.c
index 3b36946..a2c3392 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -4477,12 +4477,13 @@ static void gen_hash(unsigned char *data, unsigned char *hash, int len)
static void gen_stratum_work(struct pool *pool, struct work *work)
{
- char header[257], hash1[129], *nonce2, *buf;
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;
uint32_t *data32, *swap32;
- uint64_t diff, diff64;
- char target[65];
- int len, cb1_len, n1_len, cb2_len, i;
+ uint8_t *data8;
+ int diff;
mutex_lock(&pool->pool_lock);
@@ -4551,18 +4552,25 @@ 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");
- /* Generate target as hex where 0x00000000FFFFFFFF is diff 1 */
- diff64 = (1Ull << (31 + diff)) - 1;
- diff64 = ~htobe64(diff64);
- sprintf(target, "ffffffffffffffffffffffffffffffffffffffffffffffff");
- buf = bin2hex((const unsigned char *)&diff64, 8);
- if (unlikely(!buf))
- quit(1, "Failed to convert diff64 to buf in gen_stratum_work");
- strcat(target, buf);
- free(buf);
- applog(LOG_DEBUG, "Generated target %s", target);
- if (unlikely(!hex2bin(work->target, target, 32)))
- quit(1, "Failed to convert target to bin 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] &= ~(8 >> 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);
work->pool = pool;
work->stratum = true;
diff --git a/miner.h b/miner.h
index 9143ebc..78565f7 100644
--- a/miner.h
+++ b/miner.h
@@ -511,6 +511,21 @@ static inline void swap256(void *dest_p, const void *src_p)
dest[7] = src[0];
}
+static inline void swab256(void *dest_p, const void *src_p)
+{
+ uint32_t *dest = dest_p;
+ const uint32_t *src = src_p;
+
+ dest[0] = swab32(src[7]);
+ dest[1] = swab32(src[6]);
+ dest[2] = swab32(src[5]);
+ dest[3] = swab32(src[4]);
+ dest[4] = swab32(src[3]);
+ dest[5] = swab32(src[2]);
+ dest[6] = swab32(src[1]);
+ dest[7] = swab32(src[0]);
+}
+
extern void quit(int status, const char *format, ...);
static inline void mutex_lock(pthread_mutex_t *lock)