From 26c890c291424c992d109722c6553fcad0634f5a Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 8 Feb 2015 09:02:22 +1100 Subject: [PATCH] Send the diff from the proxy instance as soon as we get it and differentiate which proxy it comes from --- src/generator.c | 51 +++++++++++++++++++++++------------------------- src/stratifier.c | 36 +++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/generator.c b/src/generator.c index e2d0460a..e2c58fb8 100644 --- a/src/generator.c +++ b/src/generator.c @@ -101,7 +101,6 @@ struct proxy_instance { bool no_params; /* Doesn't want any parameters on subscribe */ bool notified; /* Received new template for work */ - bool diffed; /* Received new diff */ bool reconnect; /* We need to drop and reconnect */ bool alive; @@ -838,7 +837,6 @@ static bool parse_diff(proxy_instance_t *proxi, json_t *val) if (diff == 0 || diff == proxi->diff) return true; proxi->diff = diff; - proxi->diffed = true; return true; } @@ -963,7 +961,23 @@ out: 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_error_t err; @@ -1018,6 +1032,8 @@ static bool parse_method(proxy_instance_t *proxi, const char *msg) if (cmdmatch(buf, "mining.set_difficulty")) { ret = parse_diff(proxi, params); + if (likely(ret)) + send_diff(ckp, proxi); goto out; } @@ -1046,7 +1062,7 @@ out: 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; char *buf = NULL; @@ -1076,7 +1092,7 @@ static bool auth_stratum(connsock_t *cs, proxy_instance_t *proxi) ret = false; goto out; } - ret = parse_method(proxi, buf); + ret = parse_method(ckp, proxi, buf); } while (ret); val = json_msg_result(buf, &res_val, &err_val); @@ -1114,7 +1130,7 @@ out: buf = cached_proxy_line(proxi); if (!buf) break; - parse_method(proxi, buf); + parse_method(ckp, proxi, buf); }; } return ret; @@ -1185,19 +1201,6 @@ static void send_notify(ckpool_t *ckp, proxy_instance_t *proxi) 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) { stratum_msg_t *msg; @@ -1394,7 +1397,7 @@ static bool proxy_alive(ckpool_t *ckp, server_instance_t *si, proxy_instance_t * } goto out; } - if (!auth_stratum(cs, proxi)) { + if (!auth_stratum(ckp, cs, proxi)) { if (!pinging) { LOGWARNING("Failed initial authorise to %s:%s with %s:%s !", cs->url, cs->port, si->auth, si->pass); @@ -1562,15 +1565,11 @@ static void *proxy_recv(void *arg) } continue; } - if (parse_method(proxi, cs->buf)) { + if (parse_method(ckp, proxi, cs->buf)) { if (proxi->notified && proxi == current_proxy(gdata)) { send_notify(ckp, proxi); proxi->notified = false; } - if (proxi->diffed) { - send_proc(ckp->stratifier, "diff"); - proxi->diffed = false; - } if (proxi->reconnect) { /* Call this proxy dead to allow us to fail * over to a backup pool until the reconnect @@ -1718,8 +1717,6 @@ retry: if (cmdmatch(buf, "shutdown")) { ret = 0; goto out; - } else if (cmdmatch(buf, "getdiff")) { - send_diff(proxi, &sockd); } else if (cmdmatch(buf, "reconnect")) { goto reconnect; } else if (cmdmatch(buf, "submitblock:")) { diff --git a/src/stratifier.c b/src/stratifier.c index da2759aa..bf819694 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1097,8 +1097,6 @@ static void update_subscribe(ckpool_t *ckp, const char *cmd) json_decref(val); } -static void update_diff(ckpool_t *ckp); - static void update_notify(ckpool_t *ckp, const char *cmd) { 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"); return; } - buf = cmd + 7; + buf = cmd + 7; /* "notify=" */ LOGDEBUG("Update notify: %s", buf); 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); wb->txn_hashes = ckzalloc(1); - /* Check diff on each notify */ - update_diff(ckp); - ck_rlock(&sdata->workbase_lock); strcpy(wb->enonce1const, proxy->enonce1); wb->enonce1constlen = proxy->enonce1constlen; @@ -1201,31 +1196,44 @@ out: 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; sdata_t *sdata = ckp->data; double old_diff, diff; + const char *buf; + proxy_t *proxy; json_t *val; - char *buf; + int id = 0; if (unlikely(!sdata->current_workbase)) { LOGINFO("No current workbase to update diff yet"); return; } - buf = send_recv_proc(ckp->generator, "getdiff"); - if (unlikely(!buf)) { - LOGWARNING("Failed to get diff from generator in update_diff"); + if (unlikely(strlen(cmd) < 6)) { + LOGWARNING("Zero length string passed to update_diff"); return; } - + buf = cmd + 5; /* "diff=" */ LOGDEBUG("Update diff: %s", buf); + 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_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 * 1 or it will round down to zero. */ if (unlikely(diff < 1)) @@ -1919,7 +1927,7 @@ retry: /* Proxifier has a new notify ready */ update_notify(ckp, buf); } else if (cmdmatch(buf, "diff")) { - update_diff(ckp); + update_diff(ckp, buf); } else if (cmdmatch(buf, "dropclient")) { int64_t client_id;