Skip to content

Commit

Permalink
stratum: store server time offset in context
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Sep 3, 2014
1 parent 1a4391d commit e2cf49a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
9 changes: 6 additions & 3 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1695,6 +1695,9 @@ int main(int argc, char *argv[])
pthread_mutex_init(&stratum.sock_lock, NULL);
pthread_mutex_init(&stratum.work_lock, NULL);

/* init stratum data.. */
memset(&stratum.url, 0, sizeof(stratum));

flags = !opt_benchmark && strncmp(rpc_url, "https:", 6)
? (CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL)
: CURL_GLOBAL_ALL;
Expand Down
2 changes: 2 additions & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 18 additions & 10 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <unistd.h>
#include <jansson.h>
#include <curl/curl.h>
#include <sys/time.h>
#include <time.h>
#ifdef WIN32
#include "compat/winansi.h"
Expand Down Expand Up @@ -1012,12 +1011,13 @@ bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *p

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));
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e2cf49a

Please sign in to comment.