Browse Source

Merge branch 'cg_merges_20121226' into bfgminer

Luke Dashjr 13 years ago
parent
commit
9153c71be3
10 changed files with 241 additions and 134 deletions
  1. 33 0
      NEWS
  2. 6 0
      README
  3. 2 2
      driver-ztex.c
  4. 170 84
      miner.c
  5. 3 2
      miner.h
  6. 9 7
      scrypt.c
  7. 12 31
      sha2.c
  8. 4 7
      sha2.h
  9. 1 1
      util.c
  10. 1 0
      util.h

+ 33 - 0
NEWS

@@ -1,3 +1,36 @@
+BFGMiner Version 2.10.2 - Future
+
+- Do not give the share submission failure message on planned stratum
+disconnects.
+- Parse anything in the stratum socket if it's full without waiting. Empty the
+socket even if a connection is not needed in case there are share returns.
+- Provide a mechanism to zero all the statistics from the menu.
+- Display the current pool diff in the status line.
+- Display block diff in status line.
+- Generalise the code for solving a block to enable block solve detection with
+scrypt mining.
+- Generate the output hash for scrypt as well and use the one function to set
+share_diff.
+- Use the flip80 function in regeneratehash and the correct sized hash array.
+- Use one size for scratchbuf as a macro in scrypt.c
+- Stage work outside of the stgd lock to prevent attempted recursive locking in
+clone_available.
+- share_diff needs to be performed on a BE version of the output hash to work,
+leading to false best_share values as spotted by luke-Jr.
+- Remove the unused sha224 functions.
+- Use the flip functions in hashtest.
+- Simplify the setting of the nonce data field in work on submitting nonces.
+- Scrypt code does not enter the hashtest function.
+- Go back to cloning available work under staged lock.
+- Updated links to AMD APP SDK
+- Updated link to ADL SDK
+- scrypt_diff uses a uint64_t as well.
+- Correct target for stratum support with scrypt mining.
+- libztex: fixed a typo
+- libztex: check returnvalue of libusb_claim_interface() and release the
+interface in case of early exit
+
+
 BFGMiner Version 2.10.1 - Future
 BFGMiner Version 2.10.1 - Future
 
 
 - libztex: fixed a typo
 - libztex: fixed a typo

+ 6 - 0
README

@@ -993,6 +993,12 @@ average to find one 8 difficulty share, per 8 single difficulty shares found.
 However, the number is actually random and converges over time, it is an average,
 However, the number is actually random and converges over time, it is an average,
 not an exact value, thus you may find more or less than the expected average.
 not an exact value, thus you may find more or less than the expected average.
 
 
+Q: Why do the scrypt diffs not match with the current difficulty target?
+A: The current scrypt block difficulty is expressed in terms of how many
+multiples of the BTC difficulty it currently is (eg 28) whereas the shares of
+"difficulty 1" are actually 65536 times smaller than the BTC ones. The diff
+expressed by cgminer is as multiples of difficulty 1 shares.
+
 ---
 ---
 
 
 This code is provided entirely free of charge by the programmer in his spare
 This code is provided entirely free of charge by the programmer in his spare

+ 2 - 2
driver-ztex.c

@@ -166,8 +166,8 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 
 
 	swap32yes(swap32, data32, 80 / 4);
 	swap32yes(swap32, data32, 80 / 4);
 
 
-	sha2(swap, 80, hash1, false);
-	sha2(hash1, 32, hash2, false);
+	sha2(swap, 80, hash1);
+	sha2(hash1, 32, hash2);
 	if (htobe32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
 	if (htobe32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
 		dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
 		dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
 		applog(LOG_DEBUG, "%s: checkNonce failed for %08x", ztex->repr, hdata->nonce);
 		applog(LOG_DEBUG, "%s: checkNonce failed for %08x", ztex->repr, hdata->nonce);

+ 170 - 84
miner.c

@@ -262,6 +262,7 @@ static char datestamp[40];
 static char blocktime[32];
 static char blocktime[32];
 struct timeval block_timeval;
 struct timeval block_timeval;
 static char best_share[8] = "0";
 static char best_share[8] = "0";
+static char block_diff[8];
 uint64_t best_diff = 0;
 uint64_t best_diff = 0;
 
 
 struct block {
 struct block {
@@ -1626,8 +1627,8 @@ static void calc_midstate(struct work *work)
 
 
 	swap32yes(&data.i[0], work->data, 16);
 	swap32yes(&data.i[0], work->data, 16);
 	sha2_context ctx;
 	sha2_context ctx;
-	sha2_starts( &ctx, 0 );
-	sha2_update( &ctx, data.c, 64 );
+	sha2_starts(&ctx);
+	sha2_update(&ctx, data.c, 64);
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
 	swap32tole(work->midstate, work->midstate, 8);
 	swap32tole(work->midstate, work->midstate, 8);
 }
 }
@@ -2149,14 +2150,15 @@ static void curses_print_status(void)
 		mvwprintw(statuswin, 4, 0, " Connected to multiple pools with%s LP",
 		mvwprintw(statuswin, 4, 0, " Connected to multiple pools with%s LP",
 			have_longpoll ? "": "out");
 			have_longpoll ? "": "out");
 	} else if (pool->has_stratum) {
 	} else if (pool->has_stratum) {
-		mvwprintw(statuswin, 4, 0, " Connected to %s with stratum as user %s",
-			pool->sockaddr_url, pool->rpc_user);
+		mvwprintw(statuswin, 4, 0, " Connected to %s diff %s with stratum as user %s",
+			pool->sockaddr_url, pool->diff, pool->rpc_user);
 	} else {
 	} else {
-		mvwprintw(statuswin, 4, 0, " Connected to %s with%s LP as user %s",
-			pool->sockaddr_url, have_longpoll ? "": "out", pool->rpc_user);
+		mvwprintw(statuswin, 4, 0, " Connected to %s diff %s with%s LP as user %s",
+			pool->sockaddr_url, pool->diff, have_longpoll ? "": "out", pool->rpc_user);
 	}
 	}
 	wclrtoeol(statuswin);
 	wclrtoeol(statuswin);
-	mvwprintw(statuswin, 5, 0, " Block: %s...  Started: %s  Best share: %s   ", current_hash, blocktime, best_share);
+	mvwprintw(statuswin, 5, 0, " Block: %s...  Diff:%s  Started: %s  Best share: %s   ",
+		  current_hash, block_diff, blocktime, best_share);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
@@ -2370,12 +2372,11 @@ void clear_logwin(void)
 }
 }
 #endif
 #endif
 
 
-/* regenerate the full work->hash value and also return true if it's a block */
-bool regeneratehash(struct work *work)
+/* Returns true if the regenerated work->hash solves a block */
+static bool solves_block(const struct work *work)
 {
 {
 	unsigned char target[32];
 	unsigned char target[32];
 
 
-	hash_data(work->hash, work->data);
 	real_block_target(target, work->data);
 	real_block_target(target, work->data);
 	return hash_target_check(work->hash, target);
 	return hash_target_check(work->hash, target);
 }
 }
@@ -2552,7 +2553,10 @@ static uint64_t share_diff(const struct work *work)
 	uint64_t ret;
 	uint64_t ret;
 
 
 	swab256(rhash, work->hash);
 	swab256(rhash, work->hash);
-	data64 = (uint64_t *)(rhash + 4);
+	if (opt_scrypt)
+		data64 = (uint64_t *)(rhash + 2);
+	else
+		data64 = (uint64_t *)(rhash + 4);
 	d64 = be64toh(*data64);
 	d64 = be64toh(*data64);
 	if (unlikely(!d64))
 	if (unlikely(!d64))
 		d64 = 1;
 		d64 = 1;
@@ -2566,23 +2570,6 @@ static uint64_t share_diff(const struct work *work)
 	return ret;
 	return ret;
 }
 }
 
 
-static uint64_t scrypt_diff(const struct work *work)
-{
-	const uint64_t scrypt_diffone = 0x0000ffff00000000ul;
-	uint64_t d64 = work->outputhash, ret;
-
-	if (unlikely(!d64))
-		d64 = 1;
-	ret = scrypt_diffone / d64;
-	mutex_lock(&control_lock);
-	if (ret > best_diff) {
-		best_diff = ret;
-		suffix_string(best_diff, best_share, 0);
-	}
-	mutex_unlock(&control_lock);
-	return ret;
-}
-
 static char *submit_upstream_work_request(struct work *work)
 static char *submit_upstream_work_request(struct work *work)
 {
 {
 	char *hexstr = NULL;
 	char *hexstr = NULL;
@@ -2626,7 +2613,6 @@ static bool submit_upstream_work_completed(struct work *work, bool resubmit, str
 	int thr_id = work->thr_id;
 	int thr_id = work->thr_id;
 	struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
 	struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
 	struct pool *pool = work->pool;
 	struct pool *pool = work->pool;
-	uint32_t *hash32;
 	struct timeval tv_submit_reply;
 	struct timeval tv_submit_reply;
 	char hashshow[64 + 4] = "";
 	char hashshow[64 + 4] = "";
 	char worktime[200] = "";
 	char worktime[200] = "";
@@ -2649,27 +2635,20 @@ static bool submit_upstream_work_completed(struct work *work, bool resubmit, str
 
 
 	if (!QUIET) {
 	if (!QUIET) {
 		int intdiff = floor(work->work_difficulty);
 		int intdiff = floor(work->work_difficulty);
-		char diffdisp[16];
-
-		hash32 = (uint32_t *)(work->hash);
-		if (opt_scrypt) {
-			uint32_t sharediff;
-			uint64_t outhash;
+		char diffdisp[16], *outhash;
+		unsigned char rhash[32];
+		uint64_t sharediff;
 
 
-			scrypt_outputhash(work);
-			sharediff = scrypt_diff(work);
-			suffix_string(sharediff, diffdisp, 0);
-
-			outhash = work->outputhash >> 16;
-			sprintf(hashshow, "%08lx Diff %s/%d", (unsigned long)outhash, diffdisp, intdiff);
-		} else {
-			uint64_t sharediff = share_diff(work);
-
-			suffix_string(sharediff, diffdisp, 0);
-
-			sprintf(hashshow, "%08lx Diff %s/%d%s", (unsigned long)(hash32[6]), diffdisp, intdiff,
-				work->block? " BLOCK!" : "");
-		}
+		swab256(rhash, work->hash);
+		if (opt_scrypt)
+			outhash = bin2hex(rhash + 2, 4);
+		else
+			outhash = bin2hex(rhash + 4, 4);
+		sharediff = share_diff(work);
+		suffix_string(sharediff, diffdisp, 0);
+		sprintf(hashshow, "%s Diff %s/%d%s", outhash, diffdisp, intdiff,
+			work->block? " BLOCK!" : "");
+		free(outhash);
 
 
 		if (opt_worktime) {
 		if (opt_worktime) {
 			char workclone[20];
 			char workclone[20];
@@ -2795,6 +2774,7 @@ static double DIFFEXACTONE = 269599466671506397946670150870196306736371444225405
 static void calc_diff(struct work *work, int known)
 static void calc_diff(struct work *work, int known)
 {
 {
 	struct cgminer_pool_stats *pool_stats = &(work->pool->cgminer_pool_stats);
 	struct cgminer_pool_stats *pool_stats = &(work->pool->cgminer_pool_stats);
+	double difficulty;
 
 
 	if (opt_scrypt) {
 	if (opt_scrypt) {
 		uint64_t *data64, d64;
 		uint64_t *data64, d64;
@@ -2818,20 +2798,22 @@ static void calc_diff(struct work *work, int known)
 		work->work_difficulty = DIFFEXACTONE / (targ ? : DIFFEXACTONE);
 		work->work_difficulty = DIFFEXACTONE / (targ ? : DIFFEXACTONE);
 	} else
 	} else
 		work->work_difficulty = known;
 		work->work_difficulty = known;
+	difficulty = work->work_difficulty;
 
 
-	pool_stats->last_diff = work->work_difficulty;
+	pool_stats->last_diff = difficulty;
+	suffix_string((uint64_t)difficulty, work->pool->diff, 0);
 
 
-	if (work->work_difficulty == pool_stats->min_diff)
+	if (difficulty == pool_stats->min_diff)
 		pool_stats->min_diff_count++;
 		pool_stats->min_diff_count++;
-	else if (work->work_difficulty < pool_stats->min_diff || pool_stats->min_diff == 0) {
-		pool_stats->min_diff = work->work_difficulty;
+	else if (difficulty < pool_stats->min_diff || pool_stats->min_diff == 0) {
+		pool_stats->min_diff = difficulty;
 		pool_stats->min_diff_count = 1;
 		pool_stats->min_diff_count = 1;
 	}
 	}
 
 
-	if (work->work_difficulty == pool_stats->max_diff)
+	if (difficulty == pool_stats->max_diff)
 		pool_stats->max_diff_count++;
 		pool_stats->max_diff_count++;
-	else if (work->work_difficulty > pool_stats->max_diff) {
-		pool_stats->max_diff = work->work_difficulty;
+	else if (difficulty > pool_stats->max_diff) {
+		pool_stats->max_diff = difficulty;
 		pool_stats->max_diff_count = 1;
 		pool_stats->max_diff_count = 1;
 	}
 	}
 }
 }
@@ -3340,31 +3322,29 @@ static void stage_work(struct work *work);
 
 
 static bool clone_available(void)
 static bool clone_available(void)
 {
 {
-	struct work *work, *tmp;
-	struct work *work_clone;
+	struct work *work_clone = NULL, *work, *tmp;
 	bool cloned = false;
 	bool cloned = false;
 
 
+	mutex_lock(stgd_lock);
 	if (!staged_rollable)
 	if (!staged_rollable)
-		goto out;
+		goto out_unlock;
 
 
-	mutex_lock(stgd_lock);
 	HASH_ITER(hh, staged_work, work, tmp) {
 	HASH_ITER(hh, staged_work, work, tmp) {
 		if (can_roll(work) && should_roll(work)) {
 		if (can_roll(work) && should_roll(work)) {
 			roll_work(work);
 			roll_work(work);
 			work_clone = make_clone(work);
 			work_clone = make_clone(work);
 			roll_work(work);
 			roll_work(work);
+			applog(LOG_DEBUG, "Pushing cloned available work to stage thread");
 			cloned = true;
 			cloned = true;
 			break;
 			break;
 		}
 		}
 	}
 	}
+
+out_unlock:
 	mutex_unlock(stgd_lock);
 	mutex_unlock(stgd_lock);
 
 
-	if (cloned) {
-		applog(LOG_DEBUG, "Pushing cloned available work to stage thread");
+	if (cloned)
 		stage_work(work_clone);
 		stage_work(work_clone);
-	}
-
-out:
 	return cloned;
 	return cloned;
 }
 }
 
 
@@ -3481,9 +3461,19 @@ static bool stale_work(struct work *work, bool share)
 	return false;
 	return false;
 }
 }
 
 
+static void regen_hash(struct work *work)
+{
+	hash_data(work->hash, work->data);
+}
+
 static void check_solve(struct work *work)
 static void check_solve(struct work *work)
 {
 {
-	work->block = regeneratehash(work);
+	if (opt_scrypt)
+		scrypt_outputhash(work);
+	else
+		regen_hash(work);
+
+	work->block = solves_block(work);
 	if (unlikely(work->block)) {
 	if (unlikely(work->block)) {
 		work->pool->solved++;
 		work->pool->solved++;
 		found_blocks++;
 		found_blocks++;
@@ -4185,7 +4175,7 @@ static void set_curblock(char *hexstr, unsigned char *hash)
 	mutex_lock(&ch_lock);
 	mutex_lock(&ch_lock);
 	gettimeofday(&block_timeval, NULL);
 	gettimeofday(&block_timeval, NULL);
 	old_hash = current_hash;
 	old_hash = current_hash;
-	current_hash = bin2hex(hash_swap + 4, 12);
+	current_hash = bin2hex(hash_swap + 4, 8);
 	free(old_hash);
 	free(old_hash);
 	old_hash = current_fullhash;
 	old_hash = current_fullhash;
 	current_fullhash = bin2hex(hash_swap, 32);
 	current_fullhash = bin2hex(hash_swap, 32);
@@ -4194,7 +4184,7 @@ static void set_curblock(char *hexstr, unsigned char *hash)
 
 
 	get_timestamp(blocktime, &block_timeval);
 	get_timestamp(blocktime, &block_timeval);
 
 
-	applog(LOG_INFO, "New block: %s...", current_hash);
+	applog(LOG_INFO, "New block: %s... diff %s", current_hash, block_diff);
 }
 }
 
 
 /* Search to see if this string is from a block that has been seen before */
 /* Search to see if this string is from a block that has been seen before */
@@ -4226,6 +4216,45 @@ static int block_sort(struct block *blocka, struct block *blockb)
 	return blocka->block_no - blockb->block_no;
 	return blocka->block_no - blockb->block_no;
 }
 }
 
 
+static void set_blockdiff(const struct work *work)
+{
+	uint64_t *data64, d64, diff64;
+	uint32_t diffhash[8];
+	uint32_t difficulty;
+	uint32_t diffbytes;
+	uint32_t diffvalue;
+	char rhash[32];
+	int diffshift;
+
+	difficulty = swab32(*((uint32_t *)(work->data + 72)));
+
+	diffbytes = ((difficulty >> 24) & 0xff) - 3;
+	diffvalue = difficulty & 0x00ffffff;
+
+	diffshift = (diffbytes % 4) * 8;
+	if (diffshift == 0) {
+		diffshift = 32;
+		diffbytes--;
+	}
+
+	memset(diffhash, 0, 32);
+	diffhash[(diffbytes >> 2) + 1] = diffvalue >> (32 - diffshift);
+	diffhash[diffbytes >> 2] = diffvalue << diffshift;
+
+	swab256(rhash, diffhash);
+
+	if (opt_scrypt)
+		data64 = (uint64_t *)(rhash + 2);
+	else
+		data64 = (uint64_t *)(rhash + 4);
+	d64 = be64toh(*data64);
+	if (unlikely(!d64))
+		d64 = 1;
+
+	diff64 = diffone / d64;
+	suffix_string(diff64, block_diff, 0);
+}
+
 static bool test_work_current(struct work *work)
 static bool test_work_current(struct work *work)
 {
 {
 	bool ret = true;
 	bool ret = true;
@@ -4266,6 +4295,7 @@ static bool test_work_current(struct work *work)
 			free(oldblock);
 			free(oldblock);
 		}
 		}
 		HASH_ADD_STR(blocks, hash, s);
 		HASH_ADD_STR(blocks, hash, s);
+		set_blockdiff(work);
 		wr_unlock(&blk_lock);
 		wr_unlock(&blk_lock);
 		work->pool->block_id = block_id;
 		work->pool->block_id = block_id;
 		if (deleted_block)
 		if (deleted_block)
@@ -4705,6 +4735,51 @@ void write_config(FILE *fcfg)
 	json_escape_free();
 	json_escape_free();
 }
 }
 
 
+void zero_stats(void)
+{
+	int i;
+
+	gettimeofday(&total_tv_start, NULL);
+	total_mhashes_done = 0;
+	total_getworks = 0;
+	total_accepted = 0;
+	total_rejected = 0;
+	hw_errors = 0;
+	total_stale = 0;
+	total_discarded = 0;
+	local_work = 0;
+	total_go = 0;
+	total_ro = 0;
+	total_secs = 1.0;
+	best_diff = 0;
+	total_diff1 = 0;
+	suffix_string(best_diff, best_share, 0);
+
+	for (i = 0; i < total_pools; i++) {
+		struct pool *pool = pools[i];
+
+		pool->getwork_requested = 0;
+		pool->accepted = 0;
+		pool->rejected = 0;
+		pool->stale_shares = 0;
+		pool->discarded_work = 0;
+		pool->getfail_occasions = 0;
+		pool->remotefail_occasions = 0;
+	}
+
+	mutex_lock(&hash_lock);
+	for (i = 0; i < total_devices; ++i) {
+		struct cgpu_info *cgpu = devices[i];
+
+		cgpu->total_mhashes = 0;
+		cgpu->accepted = 0;
+		cgpu->rejected = 0;
+		cgpu->hw_errors = 0;
+		cgpu->utility = 0.0;
+	}
+	mutex_unlock(&hash_lock);
+}
+
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES
 static void display_pools(void)
 static void display_pools(void)
 {
 {
@@ -4908,7 +4983,7 @@ retry:
 	wlogprint("[N]ormal [C]lear [S]ilent mode (disable all output)\n");
 	wlogprint("[N]ormal [C]lear [S]ilent mode (disable all output)\n");
 	wlogprint("[D]ebug:%s\n[P]er-device:%s\n[Q]uiet:%s\n[V]erbose:%s\n"
 	wlogprint("[D]ebug:%s\n[P]er-device:%s\n[Q]uiet:%s\n[V]erbose:%s\n"
 		  "[R]PC debug:%s\n[W]orkTime details:%s\nco[M]pact: %s\n"
 		  "[R]PC debug:%s\n[W]orkTime details:%s\nco[M]pact: %s\n"
-		  "[L]og interval:%d\n",
+		  "[L]og interval:%d\n[Z]ero statistics\n",
 		opt_debug_console ? "on" : "off",
 		opt_debug_console ? "on" : "off",
 	        want_per_device_stats? "on" : "off",
 	        want_per_device_stats? "on" : "off",
 		opt_quiet ? "on" : "off",
 		opt_quiet ? "on" : "off",
@@ -4980,6 +5055,9 @@ retry:
 		opt_worktime ^= true;
 		opt_worktime ^= true;
 		wlogprint("WorkTime details %s\n", opt_worktime ? "enabled" : "disabled");
 		wlogprint("WorkTime details %s\n", opt_worktime ? "enabled" : "disabled");
 		goto retry;
 		goto retry;
+	} else if (!strncasecmp(&input, "z", 1)) {
+		zero_stats();
+		goto retry;
 	} else
 	} else
 		clear_logwin();
 		clear_logwin();
 
 
@@ -5554,7 +5632,7 @@ static void *stratum_thread(void *userdata)
 		/* Check to see whether we need to maintain this connection
 		/* Check to see whether we need to maintain this connection
 		 * indefinitely or just bring it up when we switch to this
 		 * indefinitely or just bring it up when we switch to this
 		 * pool */
 		 * pool */
-		if (!cnx_needed(pool)) {
+		if (!sock_full(pool, false) && !cnx_needed(pool)) {
 			suspend_stratum(pool);
 			suspend_stratum(pool);
 			clear_stratum_shares(pool);
 			clear_stratum_shares(pool);
 			clear_pool_work(pool);
 			clear_pool_work(pool);
@@ -5578,7 +5656,7 @@ static void *stratum_thread(void *userdata)
 		/* If we fail to receive any notify messages for 2 minutes we
 		/* If we fail to receive any notify messages for 2 minutes we
 		 * assume the connection has been dropped and treat this pool
 		 * assume the connection has been dropped and treat this pool
 		 * as dead */
 		 * as dead */
-		if (unlikely(select(pool->sock + 1, &rd, NULL, NULL, &timeout) < 1))
+		if (!sock_full(pool, false) && select(pool->sock + 1, &rd, NULL, NULL, &timeout) < 1)
 			s = NULL;
 			s = NULL;
 		else
 		else
 			s = recv_line(pool);
 			s = recv_line(pool);
@@ -5957,8 +6035,8 @@ void gen_hash(unsigned char *data, unsigned char *hash, int len)
 {
 {
 	unsigned char hash1[32];
 	unsigned char hash1[32];
 
 
-	sha2(data, len, hash1, false);
-	sha2(hash1, 32, hash, false);
+	sha2(data, len, hash1);
+	sha2(hash1, 32, hash);
 }
 }
 
 
 /* Diff 1 is a 256 bit unsigned integer of
 /* Diff 1 is a 256 bit unsigned integer of
@@ -5967,24 +6045,32 @@ void gen_hash(unsigned char *data, unsigned char *hash, int len)
  * cover a huge range of difficulty targets, though not all 256 bits' worth */
  * cover a huge range of difficulty targets, though not all 256 bits' worth */
 static void set_work_target(struct work *work, double diff)
 static void set_work_target(struct work *work, double diff)
 {
 {
-	unsigned char rtarget[32], target[32];
-	double d64;
+	unsigned char target[32];
 	uint64_t *data64, h64;
 	uint64_t *data64, h64;
+	double d64;
 
 
 	d64 = diffone;
 	d64 = diffone;
 	d64 /= diff;
 	d64 /= diff;
 	d64 = ceil(d64);
 	d64 = ceil(d64);
+	h64 = d64;
 
 
+	memset(target, 0, 32);
 	if (d64 < 18446744073709551616.0) {
 	if (d64 < 18446744073709551616.0) {
-		h64 = d64;
+		unsigned char rtarget[32];
+
 		memset(rtarget, 0, 32);
 		memset(rtarget, 0, 32);
-		data64 = (uint64_t *)(rtarget + 4);
+		if (opt_scrypt)
+			data64 = (uint64_t *)(rtarget + 2);
+		else
+			data64 = (uint64_t *)(rtarget + 4);
 		*data64 = htobe64(h64);
 		*data64 = htobe64(h64);
 		swab256(target, rtarget);
 		swab256(target, rtarget);
 	} else {
 	} else {
 		/* Support for the classic all FFs just-below-1 diff */
 		/* Support for the classic all FFs just-below-1 diff */
-		memset(target, 0xff, 28);
-		memset(&target[28], 0, 4);
+		if (opt_scrypt)
+			memset(target, 0xff, 30);
+		else
+			memset(target, 0xff, 28);
 	}
 	}
 
 
 	if (opt_debug) {
 	if (opt_debug) {
@@ -6162,8 +6248,11 @@ enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool che
 
 
 void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 {
 {
+	uint32_t *work_nonce = (uint32_t *)(work->data + 64 + 12);
 	struct timeval tv_work_found;
 	struct timeval tv_work_found;
+
 	gettimeofday(&tv_work_found, NULL);
 	gettimeofday(&tv_work_found, NULL);
+	*work_nonce = htole32(nonce);
 
 
 	mutex_lock(&stats_lock);
 	mutex_lock(&stats_lock);
 	total_diff1++;
 	total_diff1++;
@@ -6192,10 +6281,7 @@ void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 			// Share above target, normal
 			// Share above target, normal
 			/* Check the diff of the share, even if it didn't reach the
 			/* Check the diff of the share, even if it didn't reach the
 			 * target, just to set the best share value if it's higher. */
 			 * target, just to set the best share value if it's higher. */
-			if (opt_scrypt)
-				scrypt_diff(work);
-			else
-				share_diff(work);
+			share_diff(work);
 			return;
 			return;
 		case TNR_GOOD:
 		case TNR_GOOD:
 			break;
 			break;
@@ -7405,7 +7491,7 @@ bool add_cgpu(struct cgpu_info*cgpu)
 
 
 static bool my_blkmaker_sha256_callback(void *digest, const void *buffer, size_t length)
 static bool my_blkmaker_sha256_callback(void *digest, const void *buffer, size_t length)
 {
 {
-	sha2(buffer, length, digest, false);
+	sha2(buffer, length, digest);
 	return true;
 	return true;
 }
 }
 
 

+ 3 - 2
miner.h

@@ -625,6 +625,8 @@ static inline void swab256(void *dest_p, const void *src_p)
 	dest[7] = swab32(src[0]);
 	dest[7] = swab32(src[0]);
 }
 }
 
 
+#define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
+
 extern void quit(int status, const char *format, ...);
 extern void quit(int status, const char *format, ...);
 
 
 static inline void mutex_lock(pthread_mutex_t *lock)
 static inline void mutex_lock(pthread_mutex_t *lock)
@@ -917,6 +919,7 @@ struct pool {
 	int seq_getfails;
 	int seq_getfails;
 	int solved;
 	int solved;
 	int diff1;
 	int diff1;
+	char diff[8];
 
 
 	double diff_accepted;
 	double diff_accepted;
 	double diff_rejected;
 	double diff_rejected;
@@ -1011,8 +1014,6 @@ struct work {
 	unsigned char	target[32];
 	unsigned char	target[32];
 	unsigned char	hash[32];
 	unsigned char	hash[32];
 
 
-	uint64_t	outputhash;
-
 	int		rolls;
 	int		rolls;
 
 
 	dev_blk_ctx	blk;
 	dev_blk_ctx	blk;

+ 9 - 7
scrypt.c

@@ -402,18 +402,21 @@ static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint
 	PBKDF2_SHA256_80_128_32(input, X, ostate);
 	PBKDF2_SHA256_80_128_32(input, X, ostate);
 }
 }
 
 
+/* 131583 rounded up to 4 byte alignment */
+#define SCRATCHBUF_SIZE	(131584)
+
 void scrypt_outputhash(struct work *work)
 void scrypt_outputhash(struct work *work)
 {
 {
-	uint32_t data[20], ohash[8], rhash[8];
+	uint32_t data[20];
 	char *scratchbuf;
 	char *scratchbuf;
 	uint32_t *nonce = (uint32_t *)(work->data + 76);
 	uint32_t *nonce = (uint32_t *)(work->data + 76);
+	uint32_t *ohash = (uint32_t *)(work->hash);
 
 
 	be32enc_vect(data, (const uint32_t *)work->data, 19);
 	be32enc_vect(data, (const uint32_t *)work->data, 19);
 	data[19] = htobe32(*nonce);
 	data[19] = htobe32(*nonce);
-	scratchbuf = alloca(131584);
+	scratchbuf = alloca(SCRATCHBUF_SIZE);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
 	scrypt_1024_1_1_256_sp(data, scratchbuf, ohash);
-	swap256(rhash, ohash);
-	work->outputhash = be64toh(*((uint64_t *)rhash));
+	flip32(ohash, ohash);
 }
 }
 
 
 /* Used externally as confirmation of correct OCL code */
 /* Used externally as confirmation of correct OCL code */
@@ -425,7 +428,7 @@ bool scrypt_test(unsigned char *pdata, const unsigned char *ptarget, uint32_t no
 
 
 	be32enc_vect(data, (const uint32_t *)pdata, 19);
 	be32enc_vect(data, (const uint32_t *)pdata, 19);
 	data[19] = htobe32(nonce);
 	data[19] = htobe32(nonce);
-	scratchbuf = alloca(131584);
+	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]);
 	tmp_hash7 = be32toh(ohash[7]);
 
 
@@ -446,7 +449,7 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p
 
 
 	be32enc_vect(data, (const uint32_t *)pdata, 19);
 	be32enc_vect(data, (const uint32_t *)pdata, 19);
 
 
-	scratchbuf = malloc(131583);
+	scratchbuf = malloc(SCRATCHBUF_SIZE);
 	if (unlikely(!scratchbuf)) {
 	if (unlikely(!scratchbuf)) {
 		applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
 		applog(LOG_ERR, "Failed to malloc scratchbuf in scanhash_scrypt");
 		return ret;
 		return ret;
@@ -476,4 +479,3 @@ bool scanhash_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *p
 	free(scratchbuf);;
 	free(scratchbuf);;
 	return ret;
 	return ret;
 }
 }
-

+ 12 - 31
sha2.c

@@ -60,37 +60,19 @@
 /*
 /*
  * SHA-256 context setup
  * SHA-256 context setup
  */
  */
-void sha2_starts( sha2_context *ctx, int is224 )
+void sha2_starts( sha2_context *ctx )
 {
 {
     ctx->total[0] = 0;
     ctx->total[0] = 0;
     ctx->total[1] = 0;
     ctx->total[1] = 0;
 
 
-    if( is224 == 0 )
-    {
-        /* SHA-256 */
-        ctx->state[0] = 0x6A09E667;
-        ctx->state[1] = 0xBB67AE85;
-        ctx->state[2] = 0x3C6EF372;
-        ctx->state[3] = 0xA54FF53A;
-        ctx->state[4] = 0x510E527F;
-        ctx->state[5] = 0x9B05688C;
-        ctx->state[6] = 0x1F83D9AB;
-        ctx->state[7] = 0x5BE0CD19;
-    }
-    else
-    {
-        /* SHA-224 */
-        ctx->state[0] = 0xC1059ED8;
-        ctx->state[1] = 0x367CD507;
-        ctx->state[2] = 0x3070DD17;
-        ctx->state[3] = 0xF70E5939;
-        ctx->state[4] = 0xFFC00B31;
-        ctx->state[5] = 0x68581511;
-        ctx->state[6] = 0x64F98FA7;
-        ctx->state[7] = 0xBEFA4FA4;
-    }
-
-    ctx->is224 = is224;
+    ctx->state[0] = 0x6A09E667;
+    ctx->state[1] = 0xBB67AE85;
+    ctx->state[2] = 0x3C6EF372;
+    ctx->state[3] = 0xA54FF53A;
+    ctx->state[4] = 0x510E527F;
+    ctx->state[5] = 0x9B05688C;
+    ctx->state[6] = 0x1F83D9AB;
+    ctx->state[7] = 0x5BE0CD19;
 }
 }
 
 
 static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
 static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
@@ -306,19 +288,18 @@ void sha2_finish( sha2_context *ctx, unsigned char output[32] )
     PUT_ULONG_BE( ctx->state[5], output, 20 );
     PUT_ULONG_BE( ctx->state[5], output, 20 );
     PUT_ULONG_BE( ctx->state[6], output, 24 );
     PUT_ULONG_BE( ctx->state[6], output, 24 );
 
 
-    if( ctx->is224 == 0 )
-        PUT_ULONG_BE( ctx->state[7], output, 28 );
+    PUT_ULONG_BE( ctx->state[7], output, 28 );
 }
 }
 
 
 /*
 /*
  * output = SHA-256( input buffer )
  * output = SHA-256( input buffer )
  */
  */
 void sha2( const unsigned char *input, int ilen,
 void sha2( const unsigned char *input, int ilen,
-           unsigned char output[32], int is224 )
+           unsigned char output[32] )
 {
 {
     sha2_context ctx;
     sha2_context ctx;
 
 
-    sha2_starts( &ctx, is224 );
+    sha2_starts( &ctx );
     sha2_update( &ctx, input, ilen );
     sha2_update( &ctx, input, ilen );
     sha2_finish( &ctx, output );
     sha2_finish( &ctx, output );
 
 

+ 4 - 7
sha2.h

@@ -39,7 +39,6 @@ typedef struct
 
 
     unsigned char ipad[64];     /*!< HMAC: inner padding        */
     unsigned char ipad[64];     /*!< HMAC: inner padding        */
     unsigned char opad[64];     /*!< HMAC: outer padding        */
     unsigned char opad[64];     /*!< HMAC: outer padding        */
-    int is224;                  /*!< 0 => SHA-256, else SHA-224 */
 }
 }
 sha2_context;
 sha2_context;
 
 
@@ -51,9 +50,8 @@ extern "C" {
  * \brief          SHA-256 context setup
  * \brief          SHA-256 context setup
  *
  *
  * \param ctx      context to be initialized
  * \param ctx      context to be initialized
- * \param is224    0 = use SHA256, 1 = use SHA224
  */
  */
-void sha2_starts( sha2_context *ctx, int is224 );
+void sha2_starts( sha2_context *ctx);
 
 
 /**
 /**
  * \brief          SHA-256 process buffer
  * \brief          SHA-256 process buffer
@@ -68,7 +66,7 @@ void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
  * \brief          SHA-256 final digest
  * \brief          SHA-256 final digest
  *
  *
  * \param ctx      SHA-256 context
  * \param ctx      SHA-256 context
- * \param output   SHA-224/256 checksum result
+ * \param output   SHA-256 checksum result
  */
  */
 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
 
 
@@ -77,11 +75,10 @@ void sha2_finish( sha2_context *ctx, unsigned char output[32] );
  *
  *
  * \param input    buffer holding the  data
  * \param input    buffer holding the  data
  * \param ilen     length of the input data
  * \param ilen     length of the input data
- * \param output   SHA-224/256 checksum result
- * \param is224    0 = use SHA256, 1 = use SHA224
+ * \param output   SHA-256 checksum result
  */
  */
 void sha2( const unsigned char *input, int ilen,
 void sha2( const unsigned char *input, int ilen,
-           unsigned char output[32], int is224 );
+           unsigned char output[32]);
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }

+ 1 - 1
util.c

@@ -1024,7 +1024,7 @@ bool stratum_send(struct pool *pool, char *s, ssize_t len)
 }
 }
 
 
 /* Check to see if Santa's been good to you */
 /* Check to see if Santa's been good to you */
-static bool sock_full(struct pool *pool, bool wait)
+bool sock_full(struct pool *pool, bool wait)
 {
 {
 	SOCKETTYPE sock = pool->sock;
 	SOCKETTYPE sock = pool->sock;
 	struct timeval timeout;
 	struct timeval timeout;

+ 1 - 0
util.h

@@ -59,6 +59,7 @@ extern bool hash_target_check(const unsigned char *hash, const unsigned char *ta
 extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
 extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
 
 
 bool stratum_send(struct pool *pool, char *s, ssize_t len);
 bool stratum_send(struct pool *pool, char *s, ssize_t len);
+bool sock_full(struct pool *pool, bool wait);
 char *recv_line(struct pool *pool);
 char *recv_line(struct pool *pool);
 bool parse_method(struct pool *pool, char *s);
 bool parse_method(struct pool *pool, char *s);
 bool extract_sockaddr(struct pool *pool, char *url);
 bool extract_sockaddr(struct pool *pool, char *url);