Commit 5237bf350d7b393f89840d6aa9eabd60f8b2872f

Con Kolivas 2013-08-12T11:36:28

Perform merkle bin hex2bin on stratum notify to avoid doing it on each work generation.

diff --git a/cgminer.c b/cgminer.c
index fa0d174..42f58b7 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -5605,10 +5605,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	gen_hash(pool->coinbase, merkle_root, pool->swork.cb_len);
 	memcpy(merkle_sha, merkle_root, 32);
 	for (i = 0; i < pool->swork.merkles; i++) {
-		unsigned char merkle_bin[32];
-
-		hex2bin(merkle_bin, pool->swork.merkle[i], 32);
-		memcpy(merkle_sha + 32, merkle_bin, 32);
+		memcpy(merkle_sha + 32, pool->swork.merkle_bin[i], 32);
 		gen_hash(merkle_sha, merkle_root, 64);
 		memcpy(merkle_sha, merkle_root, 32);
 	}
diff --git a/miner.h b/miner.h
index fd15e20..2b0a541 100644
--- a/miner.h
+++ b/miner.h
@@ -1088,7 +1088,7 @@ enum pool_enable {
 struct stratum_work {
 	char *job_id;
 	char *prev_hash;
-	char **merkle;
+	unsigned char **merkle_bin;
 	char *bbversion;
 	char *nbit;
 	char *ntime;
diff --git a/util.c b/util.c
index 93c2054..cd813e4 100644
--- a/util.c
+++ b/util.c
@@ -1258,11 +1258,19 @@ static bool parse_notify(struct pool *pool, json_t *val)
 	pool->nonce2_offset = cb1_len + pool->n1_len;
 
 	for (i = 0; i < pool->swork.merkles; i++)
-		free(pool->swork.merkle[i]);
+		free(pool->swork.merkle_bin[i]);
 	if (merkles) {
-		pool->swork.merkle = realloc(pool->swork.merkle, sizeof(char *) * merkles + 1);
-		for (i = 0; i < merkles; i++)
-			pool->swork.merkle[i] = json_array_string(arr, i);
+		pool->swork.merkle_bin = realloc(pool->swork.merkle_bin,
+						 sizeof(char *) * merkles + 1);
+		for (i = 0; i < merkles; i++) {
+			char *merkle = json_array_string(arr, i);
+
+			pool->swork.merkle_bin[i] = malloc(32);
+			if (unlikely(!pool->swork.merkle_bin[i]))
+				quit(1, "Failed to malloc pool swork merkle_bin");
+			hex2bin(pool->swork.merkle_bin[i], merkle, 32);
+			free(merkle);
+		}
 	}
 	pool->swork.merkles = merkles;
 	if (clean)
@@ -1300,8 +1308,6 @@ static bool parse_notify(struct pool *pool, json_t *val)
 		applog(LOG_DEBUG, "prev_hash: %s", prev_hash);
 		applog(LOG_DEBUG, "coinbase1: %s", coinbase1);
 		applog(LOG_DEBUG, "coinbase2: %s", coinbase2);
-		for (i = 0; i < merkles; i++)
-			applog(LOG_DEBUG, "merkle%d: %s", i, pool->swork.merkle[i]);
 		applog(LOG_DEBUG, "bbversion: %s", bbversion);
 		applog(LOG_DEBUG, "nbit: %s", nbit);
 		applog(LOG_DEBUG, "ntime: %s", ntime);