Commit 7415d7aaa0b3a08b4863886adcfe209c102faed4

Con Kolivas 2012-09-29T16:10:13

Begin implementing a hash database of submissions and attempt sending results.

diff --git a/cgminer.c b/cgminer.c
index 285cd53..55a2ca0 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -170,6 +170,7 @@ static pthread_mutex_t *stgd_lock;
 pthread_mutex_t console_lock;
 pthread_mutex_t ch_lock;
 static pthread_rwlock_t blk_lock;
+static pthread_mutex_t sshare_lock;
 
 pthread_rwlock_t netacc_lock;
 
@@ -225,6 +226,21 @@ struct block {
 
 static struct block *blocks = NULL;
 
+
+int swork_id;
+
+/* For creating a hash database of stratum shares submitted that have not had
+ * a response yet */
+struct stratum_share {
+	struct pool *pool;
+	char hash6[8];
+	UT_hash_handle hh;
+	bool block;
+	int id;
+};
+
+static struct stratum_share *stratum_shares = NULL;
+
 char *opt_socks_proxy = NULL;
 
 static const char def_conf[] = "cgminer.conf";
@@ -2712,6 +2728,28 @@ static void *submit_work_thread(void *userdata)
 		work->stale = true;
 	}
 
+	if (work->stratum) {
+		struct stratum_share *sshare = calloc(sizeof(struct stratum_share), 1);
+		uint32_t *hash32 = (uint32_t *)work->hash;
+		char *s = alloca(1024);
+
+		sprintf(sshare->hash6, "%08lx", (unsigned long)hash32[6]);
+		sshare->block = work->block;
+		sshare->pool = pool;
+		/* Give the stratum share a unique id */
+		mutex_lock(&sshare_lock);
+		sshare->id = swork_id++;
+		HASH_ADD_INT(stratum_shares, id, sshare);
+		mutex_unlock(&sshare_lock);
+
+		sprintf(s, "{\"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%08lx\"], \"id\": %d, \"method\": \"mining.submit\"}",
+			pool->rpc_user, work->job_id, work->nonce2, work->ntime, (unsigned long)work->blk.nonce, sshare->id);
+
+		sock_send(pool->sock, s, strlen(s));
+
+		goto out;
+	}
+
 	ce = pop_curl_entry(pool);
 	/* submit solution to bitcoin via JSON-RPC */
 	while (!submit_upstream_work(work, ce->curl, resubmit)) {
@@ -5650,6 +5688,7 @@ int main(int argc, char *argv[])
 	mutex_init(&control_lock);
 	mutex_init(&sharelog_lock);
 	mutex_init(&ch_lock);
+	mutex_init(&sshare_lock);
 	rwlock_init(&blk_lock);
 	rwlock_init(&netacc_lock);
 
diff --git a/miner.h b/miner.h
index c220b9c..e216be6 100644
--- a/miner.h
+++ b/miner.h
@@ -595,6 +595,7 @@ extern bool opt_worktime;
 #ifdef USE_BITFORCE
 extern bool opt_bfl_noncerange;
 #endif
+extern int swork_id;
 
 extern pthread_rwlock_t netacc_lock;
 
@@ -752,9 +753,6 @@ enum pool_enable {
 };
 
 struct stratum_work {
-	/* id we sent to receive this work */
-	int id;
-
 	char *job_id;
 	char *prev_hash;
 	char *coinbase1;
diff --git a/util.c b/util.c
index 0c4b736..b5888ea 100644
--- a/util.c
+++ b/util.c
@@ -848,7 +848,7 @@ bool extract_sockaddr(struct pool *pool, char *url)
 }
 
 /* Send a single command across a socket, appending \n to it */
-static bool sock_send(int sock, char *s, ssize_t len)
+bool sock_send(int sock, char *s, ssize_t len)
 {
 	ssize_t sent = 0;
 
@@ -1115,7 +1115,7 @@ bool auth_stratum(struct pool *pool)
 
 	s = alloca(RECVSIZE);
 	sprintf(s, "{\"id\": %d, \"method\": \"mining.authorize\", \"params\": [\"%s\", \"%s\"]}",
-		pool->swork.id++, pool->rpc_user, pool->rpc_pass);
+		swork_id++, pool->rpc_user, pool->rpc_pass);
 
 	/* Parse all data prior sending auth request */
 	while (sock_full(pool->sock, false)) {
@@ -1171,7 +1171,7 @@ bool initiate_stratum(struct pool *pool)
 	bool ret = false;
 
 	s = alloca(RECVSIZE);
-	sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": []}", pool->swork.id++);
+	sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": []}", swork_id++);
 
 	pool->sock = socket(AF_INET, SOCK_STREAM, 0);
 	if (pool->sock == INVSOCK)
diff --git a/util.h b/util.h
index 4eb51a7..2746e10 100644
--- a/util.h
+++ b/util.h
@@ -109,6 +109,7 @@
 	#endif
 #endif
 struct pool;
+bool sock_send(int sock, char *s, ssize_t len);
 char *recv_line(SOCKETTYPE sock);
 bool parse_stratum(struct pool *pool, char *s);
 bool extract_sockaddr(struct pool *pool, char *url);