Browse Source

stratum: store server time offset in context

master
Tanguy Pruvot 10 years ago
parent
commit
b1f5df374d
  1. 9
      cpu-miner.c
  2. 2
      miner.h
  3. 28
      util.c

9
cpu-miner.c

@ -179,7 +179,7 @@ static bool submit_old = false;
bool use_syslog = false; bool use_syslog = false;
bool use_colors = false; bool use_colors = false;
static bool opt_background = false; static bool opt_background = false;
static bool opt_quiet = false; bool opt_quiet = false;
static int opt_retries = -1; static int opt_retries = -1;
static int opt_fail_pause = 30; static int opt_fail_pause = 30;
int opt_timeout = 270; 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++); for (i = 0; i < (int)sctx->xnonce2_size && !++sctx->job.xnonce2[i]; i++);
/* Assemble block header */ /* Assemble block header */
memset(work->data, 0, 128); memset(work->data, 0, sizeof(work->data));
work->data[0] = le32dec(sctx->job.version); work->data[0] = le32dec(sctx->job.version);
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
work->data[1 + i] = le32dec((uint32_t *)sctx->job.prevhash + 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); pthread_mutex_unlock(&sctx->work_lock);
if (opt_debug) { 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); char *xnonce2str = bin2hex(work->xnonce2, sctx->xnonce2_size);
applog(LOG_DEBUG, "DEBUG: job_id=%s xnonce2=%s time=%s", applog(LOG_DEBUG, "DEBUG: job_id=%s xnonce2=%s time=%s",
work->job_id, xnonce2str, tm); work->job_id, xnonce2str, tm);
@ -1690,6 +1690,9 @@ int main(int argc, char *argv[])
sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass); 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(&stats_lock, NULL);
pthread_mutex_init(&g_work_lock, NULL); pthread_mutex_init(&g_work_lock, NULL);
pthread_mutex_init(&stratum.sock_lock, NULL); pthread_mutex_init(&stratum.sock_lock, NULL);

2
miner.h

@ -377,6 +377,8 @@ struct stratum_ctx {
size_t xnonce2_size; size_t xnonce2_size;
struct stratum_job job; struct stratum_job job;
pthread_mutex_t work_lock; pthread_mutex_t work_lock;
int srvtime_diff;
}; };
bool stratum_socket_full(struct stratum_ctx *sctx, int timeout); bool stratum_socket_full(struct stratum_ctx *sctx, int timeout);

28
util.c

@ -21,7 +21,6 @@
#include <unistd.h> #include <unistd.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <sys/time.h>
#include <time.h> #include <time.h>
#ifdef WIN32 #ifdef WIN32
#include "compat/winansi.h" #include "compat/winansi.h"
@ -1012,12 +1011,13 @@ out:
static bool stratum_notify(struct stratum_ctx *sctx, json_t *params) 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; size_t coinb1_size, coinb2_size;
bool clean, ret = false; bool clean, ret = false;
int merkle_count, i; int merkle_count, i;
json_t *merkle_arr; json_t *merkle_arr;
unsigned char **merkle; unsigned char **merkle;
int ntime;
job_id = json_string_value(json_array_get(params, 0)); job_id = json_string_value(json_array_get(params, 0));
prevhash = json_string_value(json_array_get(params, 1)); 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); merkle_count = json_array_size(merkle_arr);
version = json_string_value(json_array_get(params, 5)); version = json_string_value(json_array_get(params, 5));
nbits = json_string_value(json_array_get(params, 6)); 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)); clean = json_is_true(json_array_get(params, 8));
nreward = json_string_value(json_array_get(params, 9)); 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(prevhash) != 64 || strlen(version) != 8 ||
strlen(nbits) != 8 || strlen(ntime) != 8) { strlen(nbits) != 8 || strlen(stime) != 8) {
applog(LOG_ERR, "Stratum notify: invalid parameters"); applog(LOG_ERR, "Stratum notify: invalid parameters");
goto out; 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 *)); merkle = (unsigned char**)malloc(merkle_count * sizeof(char *));
for (i = 0; i < merkle_count; i++) { for (i = 0; i < merkle_count; i++) {
const char *s = json_string_value(json_array_get(merkle_arr, 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.version, version, 4);
hex2bin(sctx->job.nbits, nbits, 4); hex2bin(sctx->job.nbits, nbits, 4);
hex2bin(sctx->job.ntime, ntime, 4); hex2bin(sctx->job.ntime, stime, 4);
if(nreward != NULL) if(nreward != NULL)
{ {
if(strlen(nreward) == 4) if(strlen(nreward) == 4)
@ -1368,11 +1378,9 @@ size_t time2str(char* buf, time_t timer)
*/ */
char* atime2str(time_t timer) char* atime2str(time_t timer)
{ {
struct tm* tm_info; char* buf = (char*) malloc(16);
char* buf = malloc(16);
memset(buf, 0, 16); memset(buf, 0, 16);
tm_info = localtime(&timer); time2str(buf, timer);
strftime(buf, 19, "%H:%M:%S", tm_info);
return buf; return buf;
} }

Loading…
Cancel
Save