Commit 6a0882f05b84f6866d6af86c34456f382c9a9bc1

ckolivas 2013-02-22T16:55:39

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.

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;
 }