From 3e954e225f8d2053c8a29e4d9e8d532116b56661 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 4 Jan 2017 17:29:33 +1100 Subject: [PATCH] Parse upstream requests for transactions, returning any found in the remote's transaction table. --- src/connector.c | 2 ++ src/stratifier.c | 44 +++++++++++++++++++++++++++++++++----------- src/stratifier.h | 1 + 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/connector.c b/src/connector.c index 51054bd3..96051eaf 100644 --- a/src/connector.c +++ b/src/connector.c @@ -1202,6 +1202,8 @@ static void *urecv_process(void *arg) parse_upstream_workinfo(ckp, val); else if (!safecmp(method, stratum_msgs[SM_BLOCK])) parse_upstream_block(ckp, val); + else if (!safecmp(method, stratum_msgs[SM_REQTXNS])) + parse_upstream_reqtxns(ckp, val); else if (!safecmp(method, "pong")) LOGDEBUG("Received upstream pong"); else diff --git a/src/stratifier.c b/src/stratifier.c index cca34992..76bbbb8e 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -7046,27 +7046,49 @@ static json_t *get_hash_transactions(sdata_t *sdata, const json_t *hashes) return txn_array; } -static void parse_remote_reqtxns(sdata_t *sdata, json_t *val, const int64_t client_id) +static json_t *get_reqtxns(sdata_t *sdata, json_t *val, bool downstream) { json_t *hashes = json_object_get(val, "hash"); + json_t *txns, *ret = NULL; int requested, found; - json_t *txns; if (unlikely(!hashes) || !json_is_array(hashes)) - return; + goto out; requested = json_array_size(hashes); if (unlikely(!requested)) - return; + goto out; txns = get_hash_transactions(sdata, hashes); found = json_array_size(txns); - if (!found) - return json_decref(txns); - /* Reuse val variable */ - JSON_CPACK(val, "{ssso}", "method", stratum_msgs[SM_TRANSACTIONS], "transaction", txns); - stratum_add_send(sdata, val, client_id, SM_TRANSACTIONS); - LOGINFO("Sending %d found of %d requested txns to remote client %"PRId64, - found, requested, client_id); + if (found) { + JSON_CPACK(ret, "{ssso}", "method", stratum_msgs[SM_TRANSACTIONS], "transaction", txns); + LOGINFO("Sending %d found of %d requested txns %s", found, requested, + downstream ? "downstream" : "upstream"); + } else + json_decref(txns); +out: + return ret; +} + +static void parse_remote_reqtxns(sdata_t *sdata, json_t *val, const int64_t client_id) +{ + json_t *ret = get_reqtxns(sdata, val, true); + + if (!ret) + return; + stratum_add_send(sdata, ret, client_id, SM_TRANSACTIONS); +} + +void parse_upstream_reqtxns(ckpool_t *ckp, json_t *val) +{ + json_t *ret = get_reqtxns(ckp->sdata, val, false); + char *msg; + + if (!ret) + return; + msg = json_dumps(ret, JSON_NO_UTF8 | JSON_PRESERVE_ORDER | JSON_COMPACT | JSON_EOL); + json_decref(ret); + connector_upstream_msg(ckp, msg); } static void parse_trusted_msg(ckpool_t *ckp, sdata_t *sdata, json_t *val, stratum_instance_t *client) diff --git a/src/stratifier.h b/src/stratifier.h index ca2d1482..aaa8c080 100644 --- a/src/stratifier.h +++ b/src/stratifier.h @@ -15,6 +15,7 @@ void parse_remote_txns(ckpool_t *ckp, const json_t *val); void parse_upstream_auth(ckpool_t *ckp, json_t *val); void parse_upstream_workinfo(ckpool_t *ckp, json_t *val); void parse_upstream_block(ckpool_t *ckp, json_t *val); +void parse_upstream_reqtxns(ckpool_t *ckp, json_t *val); char *stratifier_stats(ckpool_t *ckp, void *data); void stratifier_add_recv(ckpool_t *ckp, json_t *val); void stratifier_block_solve(ckpool_t *ckp, const char *blockhash);