diff --git a/cpu-miner.c b/cpu-miner.c index 85d4d2b..2b04c5c 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -460,11 +460,14 @@ static bool submit_upstream_work(CURL *curl, struct work *work) bool rc = false; /* pass if the previous hash is not the current previous hash */ + pthread_mutex_lock(&g_work_lock); if (memcmp(work->data + 1, g_work.data + 1, 32)) { + pthread_mutex_unlock(&g_work_lock); if (opt_debug) applog(LOG_DEBUG, "DEBUG: stale work detected, discarding"); return true; } + pthread_mutex_unlock(&g_work_lock); if (have_stratum) { uint32_t sent; @@ -894,6 +897,7 @@ static void *miner_thread(void *userdata) struct timeval tv_start, tv_end, diff; int64_t max64; uint64_t umax64; + bool extrajob = false; int rc; // &work.data[19] @@ -901,13 +905,24 @@ static void *miner_thread(void *userdata) uint32_t *nonceptr = (uint32_t*) (((char*)work.data) + wcmplen); if (have_stratum) { - while (time(NULL) >= (g_work_time + opt_scantime) && !work_done) - usleep(500*1000); - work_done = false; - pthread_mutex_lock(&g_work_lock); + uint32_t sleeptime = 0; + while (!work_done && time(NULL) >= (g_work_time + opt_scantime)) { + sleeptime++; + usleep(50*1000); + if (sleeptime > 5) { + extrajob = true; + break; + } + } + if (sleeptime) + applog(LOG_DEBUG, "sleeptime: %u ms", sleeptime*100); nonceptr = (uint32_t*) (((char*)work.data) + wcmplen); - if ((*nonceptr) >= end_nonce) + pthread_mutex_lock(&g_work_lock); + extrajob |= work_done; + if ((*nonceptr) >= end_nonce || extrajob) { + work_done = false; stratum_gen_work(&stratum, &g_work); + } } else { int min_scantime = have_longpoll ? LP_SCANTIME : opt_scantime; /* obtain new work from internal workio thread */ @@ -946,11 +961,11 @@ static void *miner_thread(void *userdata) (*nonceptr) = (0xffffffffUL / opt_n_threads) * thr_id; // 0 if single thr } else (*nonceptr)++; //?? - pthread_mutex_unlock(&g_work_lock); work_restart[thr_id].restart = 0; if (opt_debug) applog(LOG_WARNING, "job %s %08x", g_work.job_id, (*nonceptr)); + pthread_mutex_unlock(&g_work_lock); /* adjust max_nonce to meet target scan time */ if (have_stratum) @@ -962,15 +977,18 @@ static void *miner_thread(void *userdata) max64 *= (int64_t)thr_hashrates[thr_id]; if (max64 <= 0) { + /* should not be set too high, + else you can miss multiple nounces */ switch (opt_algo) { case ALGO_JACKPOT: max64 = 0x1fffLL; break; case ALGO_BLAKECOIN: max64 = 0x3ffffffLL; + break; case ALGO_BLAKE: /* based on the 750Ti hashrate (100kH) */ - max64 = 0x3ffffffLL; + max64 = 0x1ffffffLL; break; default: max64 = 0xfffffLL; @@ -1008,7 +1026,7 @@ static void *miner_thread(void *userdata) work_restart[thr_id].restart = 1; hashlog_purge_old(); // wait a bit for a new job... - sleep(1); + usleep(500*1000); (*nonceptr) = end_nonce + 1; work_done = true; continue; diff --git a/util.c b/util.c index 04209e0..6ed44c0 100644 --- a/util.c +++ b/util.c @@ -1020,7 +1020,7 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) int merkle_count, i; json_t *merkle_arr; unsigned char **merkle; - int ntime; + int ntime, hoffset; job_id = json_string_value(json_array_get(params, 0)); prevhash = json_string_value(json_array_get(params, 1)); @@ -1078,7 +1078,8 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) hex2bin(sctx->job.coinbase, coinb1, coinb1_size); memcpy(sctx->job.coinbase + coinb1_size, sctx->xnonce1, sctx->xnonce1_size); - sctx->bloc_height = le16dec((uint8_t*) sctx->job.coinbase + 43); + hoffset = coinb1_size - 15; // 43; + sctx->bloc_height = le16dec((uint8_t*) sctx->job.coinbase + hoffset); if (!sctx->job.job_id || strcmp(sctx->job.job_id, job_id)) memset(sctx->job.xnonce2, 0, sctx->xnonce2_size); hex2bin(sctx->job.xnonce2 + sctx->xnonce2_size, coinb2, coinb2_size); @@ -1125,7 +1126,7 @@ static bool stratum_set_difficulty(struct stratum_ctx *sctx, json_t *params) sctx->next_diff = diff; pthread_mutex_unlock(&sctx->work_lock); - applog(LOG_INFO, "Stratum difficulty set to %g", diff); + applog(LOG_WARNING, "Stratum difficulty set to %g", diff); return true; }