Use the sessionid as passed on stratum connect to attempt to resume a connection once and then clear it if it fails, to use a new connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
diff --git a/cgminer.c b/cgminer.c
index fc18ed9..1ad5a1c 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3194,7 +3194,7 @@ static void *submit_work_thread(void *userdata)
}
mutex_lock(&pool->pool_lock);
- sessionid_match = !strcmp(work->nonce1, pool->nonce1);
+ sessionid_match = (pool->nonce1 && !strcmp(work->nonce1, pool->nonce1));
mutex_unlock(&pool->pool_lock);
if (!sessionid_match) {
diff --git a/util.c b/util.c
index 2384542..ca57c5b 100644
--- a/util.c
+++ b/util.c
@@ -1426,14 +1426,16 @@ static bool setup_stratum_curl(struct pool *pool)
bool initiate_stratum(struct pool *pool)
{
+ char s[RBUFSIZE], *sret = NULL, *nonce1, *sessionid;
json_t *val = NULL, *res_val, *err_val;
- char s[RBUFSIZE], *sret = NULL;
+ bool ret = false, recvd = false;
json_error_t err;
- bool ret = false;
+ int n2size;
if (!setup_stratum_curl(pool))
goto out;
+resend:
if (pool->sessionid)
sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"%s\"]}", swork_id++, pool->sessionid);
else
@@ -1453,6 +1455,8 @@ bool initiate_stratum(struct pool *pool)
if (!sret)
goto out;
+ recvd = true;
+
val = JSON_LOADS(sret, &err);
free(sret);
if (!val) {
@@ -1479,22 +1483,33 @@ bool initiate_stratum(struct pool *pool)
goto out;
}
- pool->nonce1 = json_array_string(res_val, 1);
- if (!pool->nonce1) {
+ sessionid = json_array_string(json_array_get(res_val, 0), 1);
+ if (!sessionid) {
+ applog(LOG_INFO, "Failed to get sessionid in initiate_stratum");
+ goto out;
+ }
+ nonce1 = json_array_string(res_val, 1);
+ if (!nonce1) {
applog(LOG_INFO, "Failed to get nonce1 in initiate_stratum");
+ free(sessionid);
goto out;
}
- pool->n1_len = strlen(pool->nonce1) / 2;
- pool->n2size = json_integer_value(json_array_get(res_val, 2));
- if (!pool->n2size) {
+ n2size = json_integer_value(json_array_get(res_val, 2));
+ if (!n2size) {
applog(LOG_INFO, "Failed to get n2size in initiate_stratum");
+ free(sessionid);
+ free(nonce1);
goto out;
}
- //pool->sessionid = json_array_string(res_val, 3);
- if (pool->sessionid)
- applog(LOG_DEBUG, "Pool %d stratum session id: %s", pool->pool_no, pool->sessionid);
- else
- applog(LOG_DEBUG, "Pool %d stratum session id does not exist", pool->pool_no);
+
+ mutex_lock(&pool->pool_lock);
+ pool->sessionid = sessionid;
+ pool->nonce1 = nonce1;
+ pool->n1_len = strlen(nonce1) / 2;
+ pool->n2size = n2size;
+ mutex_unlock(&pool->pool_lock);
+
+ applog(LOG_DEBUG, "Pool %d stratum session id: %s", pool->pool_no, pool->sessionid);
ret = true;
out:
@@ -1510,8 +1525,21 @@ out:
applog(LOG_DEBUG, "Pool %d confirmed mining.subscribe with extranonce1 %s extran2size %d",
pool->pool_no, pool->nonce1, pool->n2size);
}
- } else
+ } else {
+ if (recvd && pool->sessionid) {
+ /* Reset the sessionid used for stratum resuming in case the pool
+ * does not support it, or does not know how to respond to the
+ * presence of the sessionid parameter. */
+ mutex_lock(&pool->pool_lock);
+ free(pool->sessionid);
+ free(pool->nonce1);
+ pool->sessionid = pool->nonce1 = NULL;
+ mutex_unlock(&pool->pool_lock);
+ applog(LOG_DEBUG, "Failed to resume stratum, trying afresh");
+ goto resend;
+ }
applog(LOG_DEBUG, "Initiate stratum failed");
+ }
return ret;
}