Commit 3858e991f0bc015191dfe3bd0bb3f00f2899c960

Paul Sheppard 2012-11-01T12:02:26

Merge branch 'master' of https://github.com/ckolivas/cgminer.git

diff --git a/NEWS b/NEWS
index a696a7e..4c270f3 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,11 @@
+Version 2.8.7 - October 29, 2012
+
+- Fail on select() failing in stratum thread without needing to attempt
+recv_line.
+- Add share to stratum database before sending it again in case we get a
+response from the pool before it's added.
+
+
 Version 2.8.6 - October 29, 2012
 
 - Shorten the initiate stratum connect timeout to 30 seconds.
diff --git a/cgminer.c b/cgminer.c
index 92de983..0ac54da 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2915,36 +2915,38 @@ static void *submit_work_thread(void *userdata)
 	}
 
 	if (work->stratum) {
+		struct stratum_share *sshare = calloc(sizeof(struct stratum_share), 1);
 		uint32_t *hash32 = (uint32_t *)work->hash, nonce;
 		char *noncehex;
 		char s[1024];
 
+		memcpy(&sshare->work, work, sizeof(struct work));
+		mutex_lock(&sshare_lock);
 		/* Give the stratum share a unique id */
-		swork_id++;
+		sshare->id = swork_id++;
+		HASH_ADD_INT(stratum_shares, id, sshare);
+		mutex_unlock(&sshare_lock);
+
 		nonce = *((uint32_t *)(work->data + 76));
 		noncehex = bin2hex((const unsigned char *)&nonce, 4);
-
 		memset(s, 0, 1024);
 		sprintf(s, "{\"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\": %d, \"method\": \"mining.submit\"}",
-			pool->rpc_user, work->job_id, work->nonce2, work->ntime, noncehex, swork_id);
+			pool->rpc_user, work->job_id, work->nonce2, work->ntime, noncehex, sshare->id);
 		free(noncehex);
 
 		applog(LOG_INFO, "Submitting share %08lx to pool %d", (unsigned long)(hash32[6]), pool->pool_no);
 
 		if (likely(stratum_send(pool, s, strlen(s)))) {
-			struct stratum_share *sshare = calloc(sizeof(struct stratum_share), 1);
-
 			if (pool_tclear(pool, &pool->submit_fail))
 					applog(LOG_WARNING, "Pool %d communication resumed, submitting work", pool->pool_no);
 			applog(LOG_DEBUG, "Successfully submitted, adding to stratum_shares db");
-			memcpy(&sshare->work, work, sizeof(struct work));
-
-			mutex_lock(&sshare_lock);
-			sshare->id = swork_id;
-			HASH_ADD_INT(stratum_shares, id, sshare);
-			mutex_unlock(&sshare_lock);
 		} else {
 			applog(LOG_INFO, "Failed to submit stratum share");
+			mutex_lock(&sshare_lock);
+			HASH_DEL(stratum_shares, sshare);
+			mutex_unlock(&sshare_lock);
+			free(sshare);
+
 			if (!pool_tset(pool, &pool->submit_fail)) {
 				total_ro++;
 				pool->remotefail_occasions++;
@@ -4330,8 +4332,10 @@ static void *stratum_thread(void *userdata)
 		 * every minute so if we fail to receive any for 90 seconds we
 		 * assume the connection has been dropped and treat this pool
 		 * as dead */
-		select(pool->sock + 1, &rd, NULL, NULL, &timeout);
-		s = recv_line(pool);
+		if (unlikely(select(pool->sock + 1, &rd, NULL, NULL, &timeout) < 1))
+			s = NULL;
+		else
+			s = recv_line(pool);
 		if (!s) {
 			applog(LOG_INFO, "Stratum connection to pool %d interrupted", pool->pool_no);
 			pool->getfail_occasions++;
@@ -4357,6 +4361,7 @@ static void *stratum_thread(void *userdata)
 		free(s);
 		if (pool->swork.clean) {
 			struct work work;
+			memset(&work, 0, sizeof(work));
 
 			/* Generate a single work item to update the current
 			 * block database */
diff --git a/configure.ac b/configure.ac
index 13fb246..a88f2b7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2,7 +2,7 @@
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_maj], [2])
 m4_define([v_min], [8])
-m4_define([v_mic], [6])
+m4_define([v_mic], [7])
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_ver], [v_maj.v_min.v_mic])
 m4_define([lt_rev], m4_eval(v_maj + v_min))
diff --git a/miner.h b/miner.h
index 16dcbe3..4f79da2 100644
--- a/miner.h
+++ b/miner.h
@@ -872,7 +872,6 @@ struct pool {
 	CURL *stratum_curl;
 	SOCKETTYPE sock;
 	char sockbuf[RBUFSIZE];
-	struct sockaddr_in *server, client;
 	char *sockaddr_url; /* stripped url used for sockaddr */
 	char *nonce1;
 	uint32_t nonce2;
diff --git a/util.c b/util.c
index 02de173..439c603 100644
--- a/util.c
+++ b/util.c
@@ -836,7 +836,6 @@ bool extract_sockaddr(struct pool *pool, char *url)
 {
 	char *url_begin, *url_end, *port_start = NULL;
 	char url_address[256], port[6];
-	struct addrinfo hints, *res;
 	int url_len, port_len = 0;
 
 	pool->sockaddr_url = url;
@@ -866,18 +865,6 @@ bool extract_sockaddr(struct pool *pool, char *url)
 		strcpy(port, "80");
 
 	pool->stratum_port = strdup(port);
-
-	memset(&hints, 0, sizeof(struct addrinfo));
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
-	hints.ai_protocol = IPPROTO_TCP;
-
-	if (getaddrinfo(url_address, port, &hints, &res)) {
-		applog(LOG_DEBUG, "Failed to extract sock addr");
-		return false;
-	}
-
-	pool->server = (struct sockaddr_in *)res->ai_addr;
 	pool->sockaddr_url = strdup(url_address);
 
 	return true;
@@ -1097,6 +1084,13 @@ static bool parse_notify(struct pool *pool, json_t *val)
 	}
 
 	mutex_lock(&pool->pool_lock);
+	free(pool->swork.job_id);
+	free(pool->swork.prev_hash);
+	free(pool->swork.coinbase1);
+	free(pool->swork.coinbase2);
+	free(pool->swork.bbversion);
+	free(pool->swork.nbit);
+	free(pool->swork.ntime);
 	pool->swork.job_id = job_id;
 	pool->swork.prev_hash = prev_hash;
 	pool->swork.coinbase1 = coinbase1;