clean up target_to_hex code

This is now more readable IMO. The nodejs pool has a very ugly approach
to achieve the same result. We also now warn about any larger than 32bit
target, which the nodejs pool just blindly ignores currently.
This commit is contained in:
Jethro Grassie 2019-04-04 19:35:10 -04:00
parent 5f253f4827
commit 3970973b89
No known key found for this signature in database
GPG Key ID: DE8ED755616565BB
1 changed files with 10 additions and 23 deletions

View File

@ -976,8 +976,11 @@ pool_clients_send_job()
static void
target_to_hex(uint64_t target, char *target_hex)
{
memset(target_hex, '0', 8);
BIGNUM *res = BN_new();
if (target & 0xFFFFFFFF00000000)
{
log_warn("Target too high: %"PRIu64, target);
}
BIGNUM *diff = BN_new();
BIGNUM *bnt = BN_new();
#ifdef SIXTY_FOUR_BIT_LONG
BN_set_word(bnt, target);
@ -986,28 +989,12 @@ target_to_hex(uint64_t target, char *target_hex)
snprintf(st, 24, "%"PRIu64, target);
BN_dec2bn(&bnt, st);
#endif
BN_div(res, NULL, base_diff, bnt, bn_ctx);
unsigned char bnb[32];
BN_bn2bin(res, &bnb[0]);
size_t cc = 4 - (32 - BN_num_bytes(res));
BN_div(diff, NULL, base_diff, bnt, bn_ctx);
BN_rshift(diff, diff, 224);
uint32_t w = BN_get_word(diff);
bin_to_hex((const char*)&w, 4, &target_hex[0]);
BN_free(bnt);
BN_free(res);
if (cc > 4 || cc == 0)
{
log_warn("Requested target too big/small: %"PRIu64, target);
target_hex[1] = '1';
return;
}
char *ph = &target_hex[0];
while (cc)
{
char c = bnb[--cc];
bin_to_hex((const char*)&c, 1, ph++);
ph++;
}
BN_free(diff);
}
static char *