From b5fcff0bcb8afb4297ab4133ef1af9fb30a6c707 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 1 Feb 2015 17:54:55 +1100 Subject: [PATCH] Optimise method parsing for the common case of shares first and do not allow clients to try and subscribe or authorise twice --- src/stratifier.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index ebe453fa..6bdba364 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -3285,12 +3285,25 @@ static void parse_method(sdata_t *sdata, stratum_instance_t *client, const int64 const char *method; char buf[256]; - /* Random broken clients send something not an integer as the id so we copy - * the json item for id_val as is for the response. */ + /* Random broken clients send something not an integer as the id so we + * copy the json item for id_val as is for the response. By far the + * most common messages will be shares so look for those first */ method = json_string_value(method_val); + if (likely(cmdmatch(method, "mining.submit") && client->authorised)) { + json_params_t *jp = create_json_params(client_id, method_val, params_val, id_val, address); + + ckmsgq_add(sdata->sshareq, jp); + return; + } + if (cmdmatch(method, "mining.subscribe")) { - json_t *val, *result_val = parse_subscribe(client, client_id, params_val); + json_t *val, *result_val; + if (unlikely(client->subscribed)) { + LOGNOTICE("Client %ld trying to subscribe twice", client_id); + return; + } + result_val = parse_subscribe(client, client_id, params_val); /* Shouldn't happen, sanity check */ if (unlikely(!result_val)) { LOGWARNING("parse_subscribe returned NULL result_val"); @@ -3323,9 +3336,22 @@ static void parse_method(sdata_t *sdata, stratum_instance_t *client, const int64 return; } - if (cmdmatch(method, "mining.auth") && client->subscribed) { - json_params_t *jp = create_json_params(client_id, method_val, params_val, id_val, address); + /* We should only accept subscribed requests from here on */ + if (!client->subscribed) { + LOGINFO("Dropping unsubscribed client %ld", client_id); + snprintf(buf, 255, "dropclient=%ld", client_id); + send_proc(client->ckp->connector, buf); + return; + } + + if (cmdmatch(method, "mining.auth")) { + json_params_t *jp; + if (unlikely(client->authorised)) { + LOGNOTICE("Client %ld trying to authorise twice", client_id); + return; + } + jp = create_json_params(client_id, method_val, params_val, id_val, address); ckmsgq_add(sdata->sauthq, jp); return; } @@ -3341,13 +3367,6 @@ static void parse_method(sdata_t *sdata, stratum_instance_t *client, const int64 return; } - if (cmdmatch(method, "mining.submit")) { - json_params_t *jp = create_json_params(client_id, method_val, params_val, id_val, address); - - ckmsgq_add(sdata->sshareq, jp); - return; - } - if (cmdmatch(method, "mining.suggest")) { suggest_diff(client, method, params_val); return;