Browse Source

Merge branch 'stratumsrv_scrypt' into bfgminer

Conflicts:
	miner.c
Luke Dashjr 11 years ago
parent
commit
578ff0d6e2
4 changed files with 45 additions and 55 deletions
  1. 9 3
      driver-stratum.c
  2. 31 25
      miner.c
  3. 4 18
      scrypt.c
  4. 1 9
      scrypt.h

+ 9 - 3
driver-stratum.c

@@ -339,7 +339,12 @@ void stratumsrv_mining_subscribe(struct bufferevent *bev, json_t *params, const
 	bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
 	bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
 	bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
 	bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
 	bufferevent_write(bev, buf, bufsz);
 	bufferevent_write(bev, buf, bufsz);
-	bufferevent_write(bev, "{\"params\":[0.9999847412109375],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 75);
+	bufferevent_write(bev, "{\"params\":[", 11);
+	if (opt_scrypt)
+		bufferevent_write(bev, "0.000015258556232", 17);
+	else
+		bufferevent_write(bev, "0.9999847412109375", 18);
+	bufferevent_write(bev, "],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 46);
 	bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
 	bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
 }
 }
 
 
@@ -368,7 +373,7 @@ void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const cha
 	const char * const nonce = __json_array_string(params, 4);
 	const char * const nonce = __json_array_string(params, 4);
 	uint8_t xnonce2[work2d_xnonce2sz];
 	uint8_t xnonce2[work2d_xnonce2sz];
 	uint32_t ntime_n, nonce_n;
 	uint32_t ntime_n, nonce_n;
-	const float nonce_diff = 1;
+	const float nonce_diff = opt_scrypt ? (1./0x10000) : 1.;
 	bool is_stale;
 	bool is_stale;
 	
 	
 	if (unlikely(!client))
 	if (unlikely(!client))
@@ -411,7 +416,8 @@ void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const cha
 		timer_set_now(&tv_now);
 		timer_set_now(&tv_now);
 		timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
 		timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
 		conn->tv_hashes_done = tv_now;
 		conn->tv_hashes_done = tv_now;
-		hashes_done(thr, 0x100000000, &tv_delta, NULL);
+		const uint64_t hashes = opt_scrypt ? 0x10000 : 0x100000000;
+		hashes_done(thr, hashes, &tv_delta, NULL);
 	}
 	}
 }
 }
 
 

+ 31 - 25
miner.c

@@ -9274,19 +9274,39 @@ void inc_hw_errors3(struct thr_info *thr, const struct work *work, const uint32_
 		thr->cgpu->drv->hw_error(thr);
 		thr->cgpu->drv->hw_error(thr);
 }
 }
 
 
-enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
+static
+bool test_hash(const void * const phash, const float diff)
 {
 {
-	uint32_t *hash2_32 = (uint32_t *)&work->hash[0];
+	const uint32_t * const hash = phash;
+	if (diff >= 1.)
+		// FIXME: > 1 should check more
+		return !hash[7];
+	
+	const uint32_t Htarg = (uint32_t)(0x100000000 * diff) - 1;
+	const uint32_t tmp_hash7 = le32toh(hash[7]);
+	
+	applog(LOG_DEBUG, "htarget %08lx hash %08lx",
+				(long unsigned int)Htarg,
+				(long unsigned int)tmp_hash7);
+	return (tmp_hash7 <= Htarg);
+}
 
 
-	hash_data(work->hash, work->data);
+enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool checktarget)
+{
+	uint32_t *work_nonce = (uint32_t *)(work->data + 64 + 12);
+	*work_nonce = htole32(nonce);
 
 
-	if (hash2_32[7] != 0)
+#ifdef USE_SCRYPT
+	if (opt_scrypt)
+		scrypt_hash_data(work->hash, work->data);
+	else
+#endif
+		hash_data(work->hash, work->data);
+	
+	if (!test_hash(work->hash, work->nonce_diff))
 		return TNR_BAD;
 		return TNR_BAD;
-
-	if (!checktarget)
-		return TNR_GOOD;
-
-	if (!hash_target_check_v(work->hash, work->target))
+	
+	if (checktarget && !hash_target_check_v(work->hash, work->target))
 	{
 	{
 		bool high_hash = true;
 		bool high_hash = true;
 		struct pool * const pool = work->pool;
 		struct pool * const pool = work->pool;
@@ -9304,24 +9324,10 @@ enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
 		if (high_hash)
 		if (high_hash)
 			return TNR_HIGH;
 			return TNR_HIGH;
 	}
 	}
-
+	
 	return TNR_GOOD;
 	return TNR_GOOD;
 }
 }
 
 
-enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool checktarget)
-{
-	uint32_t *work_nonce = (uint32_t *)(work->data + 64 + 12);
-	*work_nonce = htole32(nonce);
-
-#ifdef USE_SCRYPT
-	if (opt_scrypt)
-		// NOTE: Depends on scrypt_test return matching enum values
-		return scrypt_test(work->data, work->target, nonce);
-#endif
-
-	return hashtest2(work, checktarget);
-}
-
 /* Returns true if nonce for work was a valid share */
 /* Returns true if nonce for work was a valid share */
 bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 {
 {
@@ -9350,7 +9356,7 @@ bool submit_noffset_nonce(struct thr_info *thr, struct work *work_in, uint32_t n
 	work->thr_id = thr->id;
 	work->thr_id = thr->id;
 
 
 	/* Do one last check before attempting to submit the work */
 	/* Do one last check before attempting to submit the work */
-	/* Side effect: sets work->data for us */
+	/* Side effect: sets work->data and work->hash for us */
 	res = test_nonce2(work, nonce);
 	res = test_nonce2(work, nonce);
 	
 	
 	if (unlikely(res == TNR_BAD))
 	if (unlikely(res == TNR_BAD))

+ 4 - 18
scrypt.c

@@ -420,30 +420,16 @@ void scrypt_regenhash(struct work *work)
 	flip32(ohash, ohash);
 	flip32(ohash, ohash);
 }
 }
 
 
-static const uint32_t diff1targ = 0x0000ffff;
-
-/* Used externally as confirmation of correct OCL code */
-int scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t nonce)
+/* Used by test_nonce functions */
+void scrypt_hash_data(unsigned char * const out_hash, const unsigned char * const pdata)
 {
 {
-	uint32_t tmp_hash7, Htarg = le32toh(((const uint32_t *)ptarget)[7]);
 	uint32_t data[20], ohash[8];
 	uint32_t data[20], ohash[8];
 	char *scratchbuf;
 	char *scratchbuf;
 
 
-	be32enc_vect(data, (const uint32_t *)pdata, 19);
-	data[19] = htobe32(nonce);
+	be32enc_vect(data, (const uint32_t *)pdata, 20);
 	scratchbuf = alloca(SCRATCHBUF_SIZE);
 	scratchbuf = alloca(SCRATCHBUF_SIZE);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
-	tmp_hash7 = be32toh(ohash[7]);
-
-	applog(LOG_DEBUG, "htarget %08lx diff1 %08lx hash %08lx",
-				(long unsigned int)Htarg,
-				(long unsigned int)diff1targ,
-				(long unsigned int)tmp_hash7);
-	if (tmp_hash7 > diff1targ)
-		return -1;
-	if (tmp_hash7 > Htarg)
-		return 0;
-	return 1;
+	swap32yes(out_hash, ohash, 32/4);
 }
 }
 
 
 bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
 bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,

+ 1 - 9
scrypt.h

@@ -6,18 +6,10 @@
 #include "miner.h"
 #include "miner.h"
 
 
 #ifdef USE_SCRYPT
 #ifdef USE_SCRYPT
-extern int scrypt_test(unsigned char *pdata, const unsigned char *ptarget,
-			uint32_t nonce);
+extern void scrypt_hash_data(unsigned char *out_hash, const unsigned char *data);
 extern void scrypt_regenhash(struct work *work);
 extern void scrypt_regenhash(struct work *work);
 
 
 #else /* USE_SCRYPT */
 #else /* USE_SCRYPT */
-static inline int scrypt_test(__maybe_unused unsigned char *pdata,
-			       __maybe_unused const unsigned char *ptarget,
-			       __maybe_unused uint32_t nonce)
-{
-	return 0;
-}
-
 static inline void scrypt_regenhash(__maybe_unused struct work *work)
 static inline void scrypt_regenhash(__maybe_unused struct work *work)
 {
 {
 }
 }