Browse Source

stats: compute work difficulty from target

master
Tanguy Pruvot 10 years ago
parent
commit
cdc29336f7
  1. 30
      cpu-miner.c
  2. 26
      miner.h
  3. 2
      util.c

30
cpu-miner.c

@ -370,6 +370,8 @@ struct work {
uint64_t u64[1]; uint64_t u64[1];
} noncerange; } noncerange;
double difficulty;
uint32_t scanned_from; uint32_t scanned_from;
uint32_t scanned_to; uint32_t scanned_to;
}; };
@ -450,6 +452,27 @@ err_out:
return false; return false;
} }
/**
* Calculate the work difficulty as double
*/
static void calc_diff(struct work *work, int known)
{
// sample for diff 32.53 : 00000007de5f0000
const uint64_t diffone = 0xFFFF000000000000ull;
uint64_t *data64, d64;
char rtarget[32];
swab256(rtarget, work->target);
data64 = (uint64_t *)(rtarget + 3); /* todo: index (3) can be tuned here */
d64 = swab64(*data64);
if (unlikely(!d64))
d64 = 1;
work->difficulty = (double)diffone / d64;
if (opt_difficulty > 0.) {
work->difficulty /= opt_difficulty;
}
}
static int share_result(int result, const char *reason) static int share_result(int result, const char *reason)
{ {
char s[345]; char s[345];
@ -502,6 +525,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
applog(LOG_DEBUG, "DEBUG: stale work detected, discarding"); applog(LOG_DEBUG, "DEBUG: stale work detected, discarding");
return true; return true;
} }
calc_diff(work, 0);
pthread_mutex_unlock(&g_work_lock); pthread_mutex_unlock(&g_work_lock);
if (have_stratum) { if (have_stratum) {
@ -1000,13 +1024,13 @@ static void *miner_thread(void *userdata)
} }
if (memcmp(work.target, g_work.target, sizeof(work.target))) { if (memcmp(work.target, g_work.target, sizeof(work.target))) {
calc_diff(&g_work, 0);
if (opt_debug) { if (opt_debug) {
uint64_t target64 = g_work.target[7] * 0x100000000ULL + g_work.target[6]; uint64_t target64 = g_work.target[7] * 0x100000000ULL + g_work.target[6];
applog(LOG_DEBUG, "job %s target change: %llx", g_work.job_id, target64); applog(LOG_DEBUG, "job %s target change: %llx (%.1f)", g_work.job_id, target64, g_work.difficulty);
applog_hash((uint8_t*) work.target);
applog_compare_hash((uint8_t*) g_work.target, (uint8_t*) work.target);
} }
memcpy(work.target, g_work.target, sizeof(work.target)); memcpy(work.target, g_work.target, sizeof(work.target));
work.difficulty = g_work.difficulty;
(*nonceptr) = (0xffffffffUL / opt_n_threads) * thr_id; // 0 if single thr (*nonceptr) = (0xffffffffUL / opt_n_threads) * thr_id; // 0 if single thr
/* on new target, ignoring nonce, clear sent data (hashlog) */ /* on new target, ignoring nonce, clear sent data (hashlog) */
if (memcmp(work.target, g_work.target, sizeof(work.target))) { if (memcmp(work.target, g_work.target, sizeof(work.target))) {

26
miner.h

@ -85,6 +85,8 @@ enum {
#else #else
#define bswap_32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) \ #define bswap_32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) \
| (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu)) | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu))
#define bswap_64(x) (((uint64_t) bswap_32((uint32_t)((x) & 0xffffffffu)) << 32) \
| (uint64_t) bswap_32((uint32_t)((x) >> 32)))
#endif #endif
static inline uint32_t swab32(uint32_t v) static inline uint32_t swab32(uint32_t v)
@ -96,6 +98,30 @@ static inline uint32_t swab32(uint32_t v)
#endif #endif
} }
static inline uint64_t swab64(uint64_t v)
{
#ifdef WANT_BUILTIN_BSWAP
return __builtin_bswap64(v);
#else
return bswap_64(v);
#endif
}
static inline void swab256(void *dest_p, const void *src_p)
{
uint32_t *dest = (uint32_t *) dest_p;
const uint32_t *src = (const uint32_t *) 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]);
}
#ifdef HAVE_SYS_ENDIAN_H #ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h> #include <sys/endian.h>
#endif #endif

2
util.c

@ -388,7 +388,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
upload_data.len = strlen(rpc_req); upload_data.len = strlen(rpc_req);
upload_data.pos = 0; upload_data.pos = 0;
sprintf(len_hdr, "Content-Length: %lu", (unsigned long) upload_data.len); sprintf(len_hdr, "Content-Length: %lu", (unsigned long) upload_data.len);
sprintf(hashrate_hdr, "X-Mining-Hashrate: %llu", global_hashrate); sprintf(hashrate_hdr, "X-Mining-Hashrate: %llu", (unsigned long long) global_hashrate);
headers = curl_slist_append(headers, "Content-Type: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, len_hdr); headers = curl_slist_append(headers, len_hdr);

Loading…
Cancel
Save