From b1f5df374db13c597cd90fd3f8f4802f6b7b5f61 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Wed, 3 Sep 2014 12:54:13 +0200 Subject: [PATCH] stratum: store server time offset in context --- cpu-miner.c | 9 ++++++--- miner.h | 2 ++ util.c | 28 ++++++++++++++++++---------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cpu-miner.c b/cpu-miner.c index 6da1465..20bac21 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -179,7 +179,7 @@ static bool submit_old = false; bool use_syslog = false; bool use_colors = false; static bool opt_background = false; -static bool opt_quiet = false; +bool opt_quiet = false; static int opt_retries = -1; static int opt_fail_pause = 30; int opt_timeout = 270; @@ -789,7 +789,7 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work) for (i = 0; i < (int)sctx->xnonce2_size && !++sctx->job.xnonce2[i]; i++); /* Assemble block header */ - memset(work->data, 0, 128); + memset(work->data, 0, sizeof(work->data)); work->data[0] = le32dec(sctx->job.version); for (i = 0; i < 8; i++) work->data[1 + i] = le32dec((uint32_t *)sctx->job.prevhash + i); @@ -822,7 +822,7 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work) pthread_mutex_unlock(&sctx->work_lock); if (opt_debug) { - char *tm = atime2str(swab32(work->data[17])); + char *tm = atime2str(swab32(work->data[17]) - sctx->srvtime_diff); char *xnonce2str = bin2hex(work->xnonce2, sctx->xnonce2_size); applog(LOG_DEBUG, "DEBUG: job_id=%s xnonce2=%s time=%s", work->job_id, xnonce2str, tm); @@ -1690,6 +1690,9 @@ int main(int argc, char *argv[]) sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass); } + /* init stratum data.. */ + memset(&stratum.url, 0, sizeof(stratum)); + pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&g_work_lock, NULL); pthread_mutex_init(&stratum.sock_lock, NULL); diff --git a/miner.h b/miner.h index 3100371..9101c61 100644 --- a/miner.h +++ b/miner.h @@ -377,6 +377,8 @@ struct stratum_ctx { size_t xnonce2_size; struct stratum_job job; pthread_mutex_t work_lock; + + int srvtime_diff; }; bool stratum_socket_full(struct stratum_ctx *sctx, int timeout); diff --git a/util.c b/util.c index 275abf7..73a1847 100644 --- a/util.c +++ b/util.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #ifdef WIN32 #include "compat/winansi.h" @@ -1012,12 +1011,13 @@ out: static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) { - const char *job_id, *prevhash, *coinb1, *coinb2, *version, *nbits, *ntime, *nreward; + const char *job_id, *prevhash, *coinb1, *coinb2, *version, *nbits, *stime, *nreward; size_t coinb1_size, coinb2_size; bool clean, ret = false; int merkle_count, i; json_t *merkle_arr; unsigned char **merkle; + int ntime; job_id = json_string_value(json_array_get(params, 0)); prevhash = json_string_value(json_array_get(params, 1)); @@ -1029,16 +1029,26 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) merkle_count = json_array_size(merkle_arr); version = json_string_value(json_array_get(params, 5)); nbits = json_string_value(json_array_get(params, 6)); - ntime = json_string_value(json_array_get(params, 7)); + stime = json_string_value(json_array_get(params, 7)); clean = json_is_true(json_array_get(params, 8)); nreward = json_string_value(json_array_get(params, 9)); - if (!job_id || !prevhash || !coinb1 || !coinb2 || !version || !nbits || !ntime || + if (!job_id || !prevhash || !coinb1 || !coinb2 || !version || !nbits || !stime || strlen(prevhash) != 64 || strlen(version) != 8 || - strlen(nbits) != 8 || strlen(ntime) != 8) { + strlen(nbits) != 8 || strlen(stime) != 8) { applog(LOG_ERR, "Stratum notify: invalid parameters"); goto out; } + + /* store stratum server time diff */ + hex2bin((unsigned char *)&ntime, stime, 4); + ntime = swab32(ntime) - time(0); + if (ntime > sctx->srvtime_diff) { + sctx->srvtime_diff = ntime; + if (!opt_quiet) + applog(LOG_DEBUG, "stratum time is at least %ds in the future", ntime); + } + merkle = (unsigned char**)malloc(merkle_count * sizeof(char *)); for (i = 0; i < merkle_count; i++) { const char *s = json_string_value(json_array_get(merkle_arr, i)); @@ -1079,7 +1089,7 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) hex2bin(sctx->job.version, version, 4); hex2bin(sctx->job.nbits, nbits, 4); - hex2bin(sctx->job.ntime, ntime, 4); + hex2bin(sctx->job.ntime, stime, 4); if(nreward != NULL) { if(strlen(nreward) == 4) @@ -1368,11 +1378,9 @@ size_t time2str(char* buf, time_t timer) */ char* atime2str(time_t timer) { - struct tm* tm_info; - char* buf = malloc(16); + char* buf = (char*) malloc(16); memset(buf, 0, 16); - tm_info = localtime(&timer); - strftime(buf, 19, "%H:%M:%S", tm_info); + time2str(buf, timer); return buf; }