From 281bcd75e92cd9824a3b62f90821c6c5ddb89083 Mon Sep 17 00:00:00 2001 From: elbandi Date: Thu, 24 Apr 2014 16:34:00 +0200 Subject: [PATCH] Send subscribe request before auth --- sgminer.c | 2 +- util.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/sgminer.c b/sgminer.c index 0765cba8..4b3bf7cf 100644 --- a/sgminer.c +++ b/sgminer.c @@ -5686,7 +5686,7 @@ retry_stratum: bool init = pool_tset(pool, &pool->stratum_init); if (!init) { - bool ret = initiate_stratum(pool) && auth_stratum(pool) && (!opt_extranonce_subscribe || subscribe_extranonce(pool)); + bool ret = initiate_stratum(pool) && (!opt_extranonce_subscribe || subscribe_extranonce(pool)) && auth_stratum(pool); if (ret) init_stratum_threads(pool); diff --git a/util.c b/util.c index 5de9669c..c6179c1f 100644 --- a/util.c +++ b/util.c @@ -1878,7 +1878,9 @@ bool parse_method(struct pool *pool, char *s) bool subscribe_extranonce(struct pool *pool) { - char s[RBUFSIZE]; + json_t *val = NULL, *res_val, *err_val; + char s[RBUFSIZE], *sret = NULL; + json_error_t err; bool ret = false; sprintf(s, "{\"id\": %d, \"method\": \"mining.extranonce.subscribe\", \"params\": []}", @@ -1887,8 +1889,54 @@ bool subscribe_extranonce(struct pool *pool) if (!stratum_send(pool, s, strlen(s))) return ret; + /* Parse all data in the queue and anything left should be auth */ + while (42) { + if (!socket_full(pool, DEFAULT_SOCKWAIT)) { + applog(LOG_DEBUG, "Timed out waiting for response extranonce.subscribe"); + /* some pool doesnt send anything, so this is normal */ + ret = true; + goto out; + } + + sret = recv_line(pool); + if (!sret) + return ret; + if (parse_method(pool, sret)) + free(sret); + else + break; + } + + val = JSON_LOADS(sret, &err); + free(sret); + res_val = json_object_get(val, "result"); + err_val = json_object_get(val, "error"); + + if (!res_val || json_is_false(res_val) || (err_val && !json_is_null(err_val))) { + char *ss; + + if (err_val) { + ss = (char *)json_string_value(json_array_get(err_val, 1)); + if (opt_extranonce_subscribe && strcmp(ss, "Method 'subscribe' not found for service 'mining.extranonce'") == 0) { + applog(LOG_INFO, "Cannot subscribe to mining.extranonce on %s", get_pool_name(pool)); + ret = true; + goto out; + } + ss = json_dumps(err_val, JSON_INDENT(3)); + } + else + ss = strdup("(unknown reason)"); + applog(LOG_INFO, "%s JSON stratum auth failed: %s", get_pool_name(pool), ss); + free(ss); + + goto out; + } + ret = true; applog(LOG_INFO, "Stratum extranonce subscribe for %s", get_pool_name(pool)); + +out: + json_decref(val); return ret; } @@ -2520,10 +2568,10 @@ bool restart_stratum(struct pool *pool) suspend_stratum(pool); if (!initiate_stratum(pool)) return false; - if (!auth_stratum(pool)) - return false; if (opt_extranonce_subscribe && !subscribe_extranonce(pool)) return false; + if (!auth_stratum(pool)) + return false; return true; }