Browse Source

Merge commit 'b561773' into stratum

Luke Dashjr 13 years ago
parent
commit
7e5df6075d
3 changed files with 59 additions and 24 deletions
  1. 31 5
      miner.c
  2. 27 18
      util.c
  3. 1 1
      util.h

+ 31 - 5
miner.c

@@ -2360,7 +2360,7 @@ static bool submit_upstream_work(const struct work *work, CURL *curl, bool resub
 	int rolltime;
 	uint32_t *hash32;
 	struct timeval tv_submit, tv_submit_reply;
-	char hashshow[64+1] = "";
+	char hashshow[64 +1 ] = "";
 	char worktime[200] = "";
 
 	if (work->tmpl) {
@@ -3387,7 +3387,9 @@ next_submit:
 			pool->rpc_user, work->job_id, work->nonce2, work->ntime, noncehex, sshare->id);
 		free(noncehex);
 
-		sock_send(pool->sock, s, strlen(s));
+		applog(LOG_INFO, "Submitting share %08lx to pool %d", (unsigned long)(hash32[6]), pool->pool_no);
+
+		stratum_send(pool, s, strlen(s));
 
 		goto out;
 	}
@@ -4856,6 +4858,21 @@ out_unlock:
 	}
 }
 
+static void stratum_share_result(json_t *val, json_t *res_val,
+				 struct stratum_share *sshare)
+{
+	struct work *work = &sshare->work;
+	char hashshow[65];
+	uint32_t *hash32;
+	int intdiff;
+
+	hash32 = (uint32_t *)(work->hash);
+	intdiff = round(work->work_difficulty);
+	sprintf(hashshow, "%08lx Diff %d%s", (unsigned long)(hash32[6]), intdiff,
+		work->block? " BLOCK!" : "");
+	share_result(val, res_val, work, hashshow, false, "");
+}
+
 /* Parses stratum json responses and tries to find the id that the request
  * matched to and treat it accordingly. */
 static bool parse_stratum_response(char *s)
@@ -4868,7 +4885,7 @@ static bool parse_stratum_response(char *s)
 
 	val = JSON_LOADS(s, &err);
 	if (!val) {
-		applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text);;
+		applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text);
 		goto out;
 	}
 
@@ -4885,20 +4902,29 @@ static bool parse_stratum_response(char *s)
 		else
 			ss = strdup("(unknown reason)");
 
-		applog(LOG_INFO, "JSON-RPC decode failed: %s", ss);
+		applog(LOG_INFO, "JSON-RPC non method decode failed: %s", ss);
 
 		free(ss);
 
 		goto out;
 	}
 
+	id = json_integer_value(id_val);
 	mutex_lock(&sshare_lock);
 	HASH_FIND_INT(stratum_shares, &id, sshare);
 	if (sshare)
 		HASH_DEL(stratum_shares, sshare);
 	mutex_unlock(&sshare_lock);
-	if (!sshare)
+	if (!sshare) {
+		if (json_is_true(res_val))
+			applog(LOG_NOTICE, "Accepted untracked stratum share");
+		else
+			applog(LOG_NOTICE, "Rejected untracked stratum share");
 		goto out;
+	}
+	stratum_share_result(val, res_val, sshare);
+	free(sshare);
+
 	ret = true;
 out:
 	if (val)

+ 27 - 18
util.c

@@ -821,9 +821,11 @@ bool extract_sockaddr(struct pool *pool, char *url)
 }
 
 /* Send a single command across a socket, appending \n to it */
-bool sock_send(int sock, char *s, ssize_t len)
+bool stratum_send(struct pool *pool, char *s, ssize_t len)
 {
+	SOCKETTYPE sock = pool->sock;
 	ssize_t sent = 0;
+	bool ret = false;
 
 	if (opt_protocol)
 		applog(LOG_DEBUG, "SEND: %s", s);
@@ -831,15 +833,20 @@ bool sock_send(int sock, char *s, ssize_t len)
 	strcat(s, "\n");
 	len++;
 
+	mutex_lock(&pool->pool_lock);
 	while (len > 0 ) {
 		sent = send(sock, s + sent, len, 0);
-		if (SOCKETFAIL(sent))
-			return false;
+		if (SOCKETFAIL(sent)) {
+			ret = false;
+			goto out_unlock;
+		}
 		len -= sent;
 	}
+	ret = true;
 	fsync(sock);
-
-	return true;
+out_unlock:
+	mutex_unlock(&pool->pool_lock);
+	return ret;;
 }
 
 #define RECVSIZE 8192
@@ -873,8 +880,8 @@ static bool sock_full(SOCKETTYPE sock, bool wait)
  * from the socket and returns that as a malloced char */
 char *recv_line(SOCKETTYPE sock)
 {
-	char *sret = NULL, *s;
-	ssize_t len;
+	char *sret = NULL, *s, c;
+	ssize_t offset = 0;
 
 	s = alloca(RECVSIZE);
 	if (SOCKETFAIL(recv(sock, s, RECVSIZE, MSG_PEEK))) {
@@ -886,12 +893,13 @@ char *recv_line(SOCKETTYPE sock)
 		applog(LOG_DEBUG, "Failed to parse a \\n terminated string in recv_line");
 		goto out;
 	}
-	len = strlen(sret) + 1;
-	/* We know how much data is in the buffer so this read should not fail */
-	if (SOCKETFAIL(recv(sock, s, len, 0)))
-		goto out;
-	if (s)
-		sret = strdup(strtok(s, "\n"));
+
+	do {
+		read(sock, &c, 1);
+		memcpy(s + offset++, &c, 1);
+	} while (strncmp(&c, "\n", 1));
+	sret = strdup(s);
+	strcpy(sret + offset - 1, "\0");
 out:
 	if (!sret)
 		clear_sock(sock);
@@ -1042,11 +1050,12 @@ bool parse_method(struct pool *pool, char *s)
 	}
 
 	method = json_object_get(val, "method");
+	if (!method)
+		goto out;
 	err_val = json_object_get(val, "error");
 	params = json_object_get(val, "params");
 
-	if (!method || json_is_null(method) ||
-	    (err_val && !json_is_null(err_val))) {
+	if (err_val && !json_is_null(err_val)) {
 		char *ss;
 
 		if (err_val)
@@ -1054,7 +1063,7 @@ bool parse_method(struct pool *pool, char *s)
 		else
 			ss = strdup("(unknown reason)");
 
-		applog(LOG_INFO, "JSON-RPC decode failed: %s", ss);
+		applog(LOG_INFO, "JSON-RPC method decode failed: %s", ss);
 
 		free(ss);
 
@@ -1105,7 +1114,7 @@ bool auth_stratum(struct pool *pool)
 		free(sret);
 	}
 
-	if (!sock_send(pool->sock, s, strlen(s)))
+	if (!stratum_send(pool, s, strlen(s)))
 		goto out;
 
 	sret = recv_line(pool->sock);
@@ -1160,7 +1169,7 @@ bool initiate_stratum(struct pool *pool)
 		goto out;
 	}
 
-	if (!sock_send(pool->sock, s, strlen(s))) {
+	if (!stratum_send(pool, s, strlen(s))) {
 		applog(LOG_DEBUG, "Failed to send s in initiate_stratum");
 		goto out;
 	}

+ 1 - 1
util.h

@@ -116,7 +116,7 @@
 #endif
 
 struct pool;
-bool sock_send(int sock, char *s, ssize_t len);
+bool stratum_send(struct pool *pool, char *s, ssize_t len);
 char *recv_line(SOCKETTYPE sock);
 bool parse_method(struct pool *pool, char *s);
 bool extract_sockaddr(struct pool *pool, char *url);