From 23102d1417067ed03b4cf886c942e53a739e90a2 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 6 Feb 2018 17:13:17 +1100 Subject: [PATCH] Implement basics of vmask support for ckproxy. --- src/ckpool.h | 2 ++ src/generator.c | 15 ++++++++++++-- src/stratifier.c | 52 ++++++++++++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/ckpool.h b/src/ckpool.h index e519176e..1594f6cc 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -302,6 +302,7 @@ enum stratum_msgtype { SM_WORKERSTATS, SM_REQTXNS, SM_CONFIGURE, + SM_VERSIONMASK, SM_NONE }; @@ -329,6 +330,7 @@ static const char __maybe_unused *stratum_msgs[] = { "workerstats", "reqtxns", "mining.configure", + "vmask", "" }; diff --git a/src/generator.c b/src/generator.c index 17c1130f..b89abc3d 100644 --- a/src/generator.c +++ b/src/generator.c @@ -1892,7 +1892,7 @@ static void *proxy_send(void *arg) int64_t client_id = 0, id; notify_instance_t *ni; json_t *jobid = NULL; - json_t *val; + json_t *val, *vmask; if (unlikely(msg)) { json_decref(msg->json_msg); @@ -1961,12 +1961,23 @@ static void *proxy_send(void *arg) continue; } - JSON_CPACK(val, "{s[soooo]soss}", "params", subproxy->auth, jobid, + vmask = json_object_get(msg->json_msg, "vmask"); + if (vmask) { + JSON_CPACK(val, "{s[sooooo]soss}", "params", subproxy->auth, jobid, json_object_dup(msg->json_msg, "nonce2"), json_object_dup(msg->json_msg, "ntime"), json_object_dup(msg->json_msg, "nonce"), + json_copy(vmask), "id", json_object_dup(msg->json_msg, "id"), "method", "mining.submit"); + } else { + JSON_CPACK(val, "{s[soooo]soss}", "params", subproxy->auth, jobid, + json_object_dup(msg->json_msg, "nonce2"), + json_object_dup(msg->json_msg, "ntime"), + json_object_dup(msg->json_msg, "nonce"), + "id", json_object_dup(msg->json_msg, "id"), + "method", "mining.submit"); + } add_json_msgq(&csmsgq, subproxy, &val); send_json_msgq(gdata, &csmsgq); } diff --git a/src/stratifier.c b/src/stratifier.c index 0beea46a..9e25b837 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -236,6 +236,9 @@ struct stratum_instance { * or other problem and should be dropped lazily if * this is set to 2 */ + bool vmask; /* Requested vmask */ + uint32_t version_mask; /* Mask to use for this client */ + int latency; /* Latency when on a mining node */ bool reconnect; /* This client really needs to reconnect */ @@ -3055,7 +3058,7 @@ void stratum_set_proxy_vmask(ckpool_t *ckp, int id, int subid, uint32_t version_ proxy = existing_subproxy(ckp->sdata, id, subid); proxy->version_mask = version_mask; - LOGWARNING("Stratum Proxy %d:%d had version mask set to %x", id, subid, version_mask); + LOGWARNING("Stratum Proxy %d:%d had version mask set to %08x", id, subid, version_mask); } static void stratum_send_diff(sdata_t *sdata, const stratum_instance_t *client); @@ -5719,20 +5722,6 @@ static void stratum_send_diff(sdata_t *sdata, const stratum_instance_t *client) stratum_add_send(sdata, json_msg, client->id, SM_DIFF); } -#if 0 -/* Needs to be entered with client holding a ref count. */ -static void stratum_send_version_mask(sdata_t *sdata, const stratum_instance_t *client) -{ - char version_str[12]; - json_t *json_msg; - - sprintf(version_str, "%08x", client->ckp->version_mask); - JSON_CPACK(json_msg, "{s[s]soss}", "params", version_str, "id", json_null(), - "method", "mining.set_version_mask"); - stratum_add_send(sdata, json_msg, client->id, SM_VERSIONMASK); -} -#endif - /* Needs to be entered with client holding a ref count. */ static void stratum_send_message(sdata_t *sdata, const stratum_instance_t *client, const char *msg) { @@ -6027,7 +6016,7 @@ static void update_client(const stratum_instance_t *client, const int64_t client /* Submit a share in proxy mode to the parent pool. workbase_lock is held. * Needs to be entered with client holding a ref count. */ static void submit_share(stratum_instance_t *client, const int64_t jobid, const char *nonce2, - const char *ntime, const char *nonce) + const char *ntime, const char *nonce, const char *version_mask) { ckpool_t *ckp = client->ckp; json_t *json_msg; @@ -6037,6 +6026,8 @@ static void submit_share(stratum_instance_t *client, const int64_t jobid, const JSON_CPACK(json_msg, "{sIsssssssIsIsi}", "jobid", jobid, "nonce2", enonce2, "ntime", ntime, "nonce", nonce, "client_id", client->id, "proxy", client->proxyid, "subproxy", client->subproxyid); + if (version_mask) + json_set_string(json_msg, "vmask", version_mask); generator_add_send(ckp, json_msg); } @@ -6135,7 +6126,7 @@ static json_t *parse_submit(stratum_instance_t *client, json_t *json_msg, if (version_mask && strlen(version_mask) && validhex(version_mask)) { sscanf(version_mask, "%x", &version_mask32); // check version mask - if (version_mask32 && ((~ckp->version_mask) & version_mask32) != 0) { + if (version_mask32 && ((~client->version_mask) & version_mask32) != 0) { // means client changed some bits which server doesn't allow to change err = SE_INVALID_VERSION_MASK; *err_val = JSON_ERR(err); @@ -6262,7 +6253,7 @@ out_nowb: * stale shares and filter out the rest. */ if (wb && wb->proxy && submit) { LOGINFO("Submitting share upstream: %s", hexhash); - submit_share(client, id, nonce2, ntime, nonce); + submit_share(client, id, nonce2, ntime, nonce, version_mask); } add_submit(ckp, client, diff, result, submit); @@ -6486,11 +6477,30 @@ static void suggest_diff(ckpool_t *ckp, stratum_instance_t *client, const char * stratum_send_diff(ckp->sdata, client); } +/* Needs to be entered with client holding a ref count */ +static void stratum_send_version_mask(sdata_t *sdata, stratum_instance_t *client) +{ + char version_str[12]; + json_t *json_msg; + + if (unlikely(!client->proxy)) { + LOGERR("stratum_send_version_mask called on a non proxied client"); + return; + } + client->version_mask = client->proxy->version_mask; + sprintf(version_str, "%08x", client->version_mask); + JSON_CPACK(json_msg, "{s[s]soss}", "params", version_str, "id", json_null(), + "method", "mining.set_version_mask"); + stratum_add_send(sdata, json_msg, client->id, SM_VERSIONMASK); +} + /* Send diff first when sending the first stratum template after subscribing */ -static void init_client(const stratum_instance_t *client, const int64_t client_id) +static void init_client(stratum_instance_t *client, const int64_t client_id) { sdata_t *sdata = client->sdata; + if (client->vmask) + stratum_send_version_mask(client->sdata, client); stratum_send_diff(sdata, client); stratum_send_update(sdata, client_id, true); } @@ -6699,7 +6709,11 @@ static void parse_method(ckpool_t *ckp, sdata_t *sdata, stratum_instance_t *clie LOGINFO("Mining configure requested from %s %s", client->identity, client->address); + client->vmask = true; + /* Send a temporary vmask in proxy mode till we know what the + * real vmask will be for the upstream pool. */ sprintf(version_str, "%08x", ckp->version_mask); + client->version_mask = ckp->version_mask; val = json_object(); JSON_CPACK(result_val, "{sbss}", "version-rolling", json_true(), "version-rolling.mask", version_str);