Commit 4a72a1574f88fce9d20b1a2cbbb3c8b332092791

Con Kolivas 2012-11-01T22:34:08

Create a function that generates a GBT coinbase from the existing pool variables.

diff --git a/cgminer.c b/cgminer.c
index 5f1fc08..384a9a9 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -1357,6 +1357,27 @@ static void calc_midstate(struct work *work)
 #endif
 }
 
+/* Generate a GBT coinbase from the existing GBT variables stored. Must be
+ * entered under gbt_lock */
+static void __build_gbt_coinbase(struct pool *pool)
+{
+	unsigned char *coinbase;
+	uint8_t *extra_len;
+	int cbt_len;
+
+	cbt_len = strlen(pool->coinbasetxn) / 2;
+	/* We add 4 bytes of extra data corresponding to nonce2 of stratum */
+	coinbase = calloc(cbt_len + 4, 1);
+	hex2bin(coinbase, pool->coinbasetxn, 42);
+	extra_len = (uint8_t *)(coinbase + 41);
+	*extra_len += 4;
+	hex2bin(coinbase + 42, pool->coinbasetxn + 84, cbt_len - 42);
+	pool->nonce2++;
+	memcpy(coinbase + cbt_len, &pool->nonce2, 4);
+	free(pool->gbt_coinbase);
+	pool->gbt_coinbase = coinbase;
+}
+
 static bool gbt_decode(struct pool *pool, json_t *res_val)
 {
 	const char *previousblockhash;
@@ -1394,7 +1415,7 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)
 	applog(LOG_DEBUG, "curtime: %d", curtime);
 	applog(LOG_DEBUG, "submitold: %s", submitold ? "true" : "false");
 	applog(LOG_DEBUG, "bits: %s", bits);
-	
+
 	mutex_lock(&pool->gbt_lock);
 	free(pool->previousblockhash);
 	free(pool->gbt_target);
@@ -1410,6 +1431,7 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)
 	pool->curtime = htobe32(curtime);
 	pool->gbt_submitold = submitold;
 	pool->gbt_bits = strdup(bits);
+	__build_gbt_coinbase(pool);
 	mutex_unlock(&pool->gbt_lock);
 
 	return true;
diff --git a/miner.h b/miner.h
index 0b20984..e21228a 100644
--- a/miner.h
+++ b/miner.h
@@ -896,6 +896,7 @@ struct pool {
 	uint32_t curtime;
 	bool gbt_submitold;
 	char *gbt_bits;
+	unsigned char *gbt_coinbase;
 
 };