Abstract out the test for stale work and test for it in the actual mining threads as well.
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 87 88 89 90 91 92 93 94 95 96 97
diff --git a/main.c b/main.c
index 55200f1..ed2ac41 100644
--- a/main.c
+++ b/main.c
@@ -865,36 +865,48 @@ static bool workio_get_work(struct workio_cmd *wc)
return true;
}
+static bool stale_work(struct work *work)
+{
+ bool ret = false;
+ char *hexstr;
+
+ hexstr = bin2hex(work->data, 36);
+ if (unlikely(!hexstr)) {
+ applog(LOG_ERR, "submit_work_thread OOM");
+ return false;
+ }
+
+ if (strncmp(hexstr, current_block, 36))
+ ret = true;
+
+ free(hexstr);
+ return ret;
+}
+
static void *submit_work_thread(void *userdata)
{
struct workio_cmd *wc = (struct workio_cmd *)userdata;
int failures = 0;
- char *hexstr;
pthread_detach(pthread_self());
- hexstr = bin2hex(wc->u.work->data, 36);
- if (unlikely(!hexstr)) {
- applog(LOG_ERR, "submit_work_thread OOM");
- goto out;
- }
- if (unlikely(strncmp(hexstr, current_block, 36))) {
- applog(LOG_WARNING, "Stale work detected, discarding");
+ if (stale_work(wc->u.work)) {
+ applog(LOG_WARNING, "Stale share detected, discarding");
stale_shares++;
- goto out_free;
+ goto out;
}
/* submit solution to bitcoin via JSON-RPC */
while (!submit_upstream_work(wc->u.work)) {
- if (unlikely(strncmp(hexstr, current_block, 36))) {
- applog(LOG_WARNING, "Stale work detected, discarding");
+ if (stale_work(wc->u.work)) {
+ applog(LOG_WARNING, "Stale share detected, discarding");
stale_shares++;
- goto out_free;
+ break;
}
if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
kill_work();
- goto out_free;
+ break;
}
/* pause, then restart work-request loop */
@@ -902,8 +914,6 @@ static void *submit_work_thread(void *userdata)
opt_fail_pause);
sleep(opt_fail_pause);
}
-out_free:
- free(hexstr);
out:
workio_cmd_free(wc);
return NULL;
@@ -1508,7 +1518,8 @@ static void *miner_thread(void *userdata)
}
if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
- work.blk.nonce >= MAXTHREADS - hashes_done)
+ work.blk.nonce >= MAXTHREADS - hashes_done ||
+ stale_work(&work))
needs_work = true;
}
@@ -1668,7 +1679,10 @@ static void *gpuminer_thread(void *userdata)
}
}
- if (diff.tv_sec > opt_scantime || work->blk.nonce >= MAXTHREADS - hashes || work_restart[thr_id].restart) {
+ if (diff.tv_sec > opt_scantime ||
+ work->blk.nonce >= MAXTHREADS - hashes ||
+ work_restart[thr_id].restart ||
+ stale_work(work)) {
/* Ignore any reads since we're getting new work and queue a clean buffer */
status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
BUFFERSIZE, blank_res, 0, NULL, NULL);