Browse Source

Send the diff from the proxy instance as soon as we get it and differentiate which proxy it comes from

master
Con Kolivas 10 years ago
parent
commit
26c890c291
  1. 51
      src/generator.c
  2. 36
      src/stratifier.c

51
src/generator.c

@ -101,7 +101,6 @@ struct proxy_instance {
bool no_params; /* Doesn't want any parameters on subscribe */ bool no_params; /* Doesn't want any parameters on subscribe */
bool notified; /* Received new template for work */ bool notified; /* Received new template for work */
bool diffed; /* Received new diff */
bool reconnect; /* We need to drop and reconnect */ bool reconnect; /* We need to drop and reconnect */
bool alive; bool alive;
@ -838,7 +837,6 @@ static bool parse_diff(proxy_instance_t *proxi, json_t *val)
if (diff == 0 || diff == proxi->diff) if (diff == 0 || diff == proxi->diff)
return true; return true;
proxi->diff = diff; proxi->diff = diff;
proxi->diffed = true;
return true; return true;
} }
@ -963,7 +961,23 @@ out:
return ret; return ret;
} }
static bool parse_method(proxy_instance_t *proxi, const char *msg) static void send_diff(ckpool_t *ckp, proxy_instance_t *proxi)
{
json_t *json_msg;
char *msg, *buf;
JSON_CPACK(json_msg, "{sisf}",
"proxy", proxi->id,
"diff", proxi->diff);
msg = json_dumps(json_msg, JSON_NO_UTF8);
json_decref(json_msg);
ASPRINTF(&buf, "diff=%s", msg);
free(msg);
send_proc(ckp->stratifier, buf);
free(buf);
}
static bool parse_method(ckpool_t *ckp, proxy_instance_t *proxi, const char *msg)
{ {
json_t *val = NULL, *method, *err_val, *params; json_t *val = NULL, *method, *err_val, *params;
json_error_t err; json_error_t err;
@ -1018,6 +1032,8 @@ static bool parse_method(proxy_instance_t *proxi, const char *msg)
if (cmdmatch(buf, "mining.set_difficulty")) { if (cmdmatch(buf, "mining.set_difficulty")) {
ret = parse_diff(proxi, params); ret = parse_diff(proxi, params);
if (likely(ret))
send_diff(ckp, proxi);
goto out; goto out;
} }
@ -1046,7 +1062,7 @@ out:
return ret; return ret;
} }
static bool auth_stratum(connsock_t *cs, proxy_instance_t *proxi) static bool auth_stratum(ckpool_t *ckp, connsock_t *cs, proxy_instance_t *proxi)
{ {
json_t *val = NULL, *res_val, *req, *err_val; json_t *val = NULL, *res_val, *req, *err_val;
char *buf = NULL; char *buf = NULL;
@ -1076,7 +1092,7 @@ static bool auth_stratum(connsock_t *cs, proxy_instance_t *proxi)
ret = false; ret = false;
goto out; goto out;
} }
ret = parse_method(proxi, buf); ret = parse_method(ckp, proxi, buf);
} while (ret); } while (ret);
val = json_msg_result(buf, &res_val, &err_val); val = json_msg_result(buf, &res_val, &err_val);
@ -1114,7 +1130,7 @@ out:
buf = cached_proxy_line(proxi); buf = cached_proxy_line(proxi);
if (!buf) if (!buf)
break; break;
parse_method(proxi, buf); parse_method(ckp, proxi, buf);
}; };
} }
return ret; return ret;
@ -1185,19 +1201,6 @@ static void send_notify(ckpool_t *ckp, proxy_instance_t *proxi)
free(buf); free(buf);
} }
static void send_diff(proxy_instance_t *proxi, int *sockd)
{
json_t *json_msg;
char *msg;
JSON_CPACK(json_msg, "{sf}", "diff", proxi->diff);
msg = json_dumps(json_msg, JSON_NO_UTF8);
json_decref(json_msg);
send_unix_msg(*sockd, msg);
free(msg);
_Close(sockd);
}
static void submit_share(proxy_instance_t *proxi, json_t *val) static void submit_share(proxy_instance_t *proxi, json_t *val)
{ {
stratum_msg_t *msg; stratum_msg_t *msg;
@ -1394,7 +1397,7 @@ static bool proxy_alive(ckpool_t *ckp, server_instance_t *si, proxy_instance_t *
} }
goto out; goto out;
} }
if (!auth_stratum(cs, proxi)) { if (!auth_stratum(ckp, cs, proxi)) {
if (!pinging) { if (!pinging) {
LOGWARNING("Failed initial authorise to %s:%s with %s:%s !", LOGWARNING("Failed initial authorise to %s:%s with %s:%s !",
cs->url, cs->port, si->auth, si->pass); cs->url, cs->port, si->auth, si->pass);
@ -1562,15 +1565,11 @@ static void *proxy_recv(void *arg)
} }
continue; continue;
} }
if (parse_method(proxi, cs->buf)) { if (parse_method(ckp, proxi, cs->buf)) {
if (proxi->notified && proxi == current_proxy(gdata)) { if (proxi->notified && proxi == current_proxy(gdata)) {
send_notify(ckp, proxi); send_notify(ckp, proxi);
proxi->notified = false; proxi->notified = false;
} }
if (proxi->diffed) {
send_proc(ckp->stratifier, "diff");
proxi->diffed = false;
}
if (proxi->reconnect) { if (proxi->reconnect) {
/* Call this proxy dead to allow us to fail /* Call this proxy dead to allow us to fail
* over to a backup pool until the reconnect * over to a backup pool until the reconnect
@ -1718,8 +1717,6 @@ retry:
if (cmdmatch(buf, "shutdown")) { if (cmdmatch(buf, "shutdown")) {
ret = 0; ret = 0;
goto out; goto out;
} else if (cmdmatch(buf, "getdiff")) {
send_diff(proxi, &sockd);
} else if (cmdmatch(buf, "reconnect")) { } else if (cmdmatch(buf, "reconnect")) {
goto reconnect; goto reconnect;
} else if (cmdmatch(buf, "submitblock:")) { } else if (cmdmatch(buf, "submitblock:")) {

36
src/stratifier.c

@ -1097,8 +1097,6 @@ static void update_subscribe(ckpool_t *ckp, const char *cmd)
json_decref(val); json_decref(val);
} }
static void update_diff(ckpool_t *ckp);
static void update_notify(ckpool_t *ckp, const char *cmd) static void update_notify(ckpool_t *ckp, const char *cmd)
{ {
bool new_block = false, clean; bool new_block = false, clean;
@ -1114,7 +1112,7 @@ static void update_notify(ckpool_t *ckp, const char *cmd)
LOGWARNING("Zero length string passed to update_notify"); LOGWARNING("Zero length string passed to update_notify");
return; return;
} }
buf = cmd + 7; buf = cmd + 7; /* "notify=" */
LOGDEBUG("Update notify: %s", buf); LOGDEBUG("Update notify: %s", buf);
val = json_loads(buf, 0, NULL); val = json_loads(buf, 0, NULL);
@ -1180,9 +1178,6 @@ static void update_notify(ckpool_t *ckp, const char *cmd)
hex2bin(wb->headerbin, header, 112); hex2bin(wb->headerbin, header, 112);
wb->txn_hashes = ckzalloc(1); wb->txn_hashes = ckzalloc(1);
/* Check diff on each notify */
update_diff(ckp);
ck_rlock(&sdata->workbase_lock); ck_rlock(&sdata->workbase_lock);
strcpy(wb->enonce1const, proxy->enonce1); strcpy(wb->enonce1const, proxy->enonce1);
wb->enonce1constlen = proxy->enonce1constlen; wb->enonce1constlen = proxy->enonce1constlen;
@ -1201,31 +1196,44 @@ out:
static void stratum_send_diff(sdata_t *sdata, const stratum_instance_t *client); static void stratum_send_diff(sdata_t *sdata, const stratum_instance_t *client);
static void update_diff(ckpool_t *ckp) static void update_diff(ckpool_t *ckp, const char *cmd)
{ {
stratum_instance_t *client, *tmp; stratum_instance_t *client, *tmp;
sdata_t *sdata = ckp->data; sdata_t *sdata = ckp->data;
double old_diff, diff; double old_diff, diff;
const char *buf;
proxy_t *proxy;
json_t *val; json_t *val;
char *buf; int id = 0;
if (unlikely(!sdata->current_workbase)) { if (unlikely(!sdata->current_workbase)) {
LOGINFO("No current workbase to update diff yet"); LOGINFO("No current workbase to update diff yet");
return; return;
} }
buf = send_recv_proc(ckp->generator, "getdiff"); if (unlikely(strlen(cmd) < 6)) {
if (unlikely(!buf)) { LOGWARNING("Zero length string passed to update_diff");
LOGWARNING("Failed to get diff from generator in update_diff");
return; return;
} }
buf = cmd + 5; /* "diff=" */
LOGDEBUG("Update diff: %s", buf); LOGDEBUG("Update diff: %s", buf);
val = json_loads(buf, 0, NULL); val = json_loads(buf, 0, NULL);
dealloc(buf); if (unlikely(!val)) {
LOGWARNING("Failed to json decode in update_diff");
return;
}
json_get_int(&id, val, "proxy");
json_dblcpy(&diff, val, "diff"); json_dblcpy(&diff, val, "diff");
json_decref(val); json_decref(val);
LOGNOTICE("Got updated diff for proxy %d", id);
proxy = proxy_by_id(sdata, id);
if (proxy != current_proxy(sdata)) {
LOGINFO("Diff from backup proxy");
return;
}
/* We only really care about integer diffs so clamp the lower limit to /* We only really care about integer diffs so clamp the lower limit to
* 1 or it will round down to zero. */ * 1 or it will round down to zero. */
if (unlikely(diff < 1)) if (unlikely(diff < 1))
@ -1919,7 +1927,7 @@ retry:
/* Proxifier has a new notify ready */ /* Proxifier has a new notify ready */
update_notify(ckp, buf); update_notify(ckp, buf);
} else if (cmdmatch(buf, "diff")) { } else if (cmdmatch(buf, "diff")) {
update_diff(ckp); update_diff(ckp, buf);
} else if (cmdmatch(buf, "dropclient")) { } else if (cmdmatch(buf, "dropclient")) {
int64_t client_id; int64_t client_id;

Loading…
Cancel
Save