Browse Source

work_ntime_range helper function

Luke Dashjr 11 years ago
parent
commit
aca0b2a006
2 changed files with 27 additions and 0 deletions
  1. 26 0
      miner.c
  2. 1 0
      miner.h

+ 26 - 0
miner.c

@@ -2856,6 +2856,32 @@ void work_set_simple_ntime_roll_limit(struct work * const work, const int ntime_
 	set_simple_ntime_roll_limit(&work->ntime_roll_limits, upk_u32be(work->data, 0x44), ntime_roll, tvp_ref);
 }
 
+int work_ntime_range(struct work * const work, const struct timeval * const tvp_earliest, const struct timeval * const tvp_latest, const int desired_roll)
+{
+	const struct ntime_roll_limits * const nrl = &work->ntime_roll_limits;
+	const uint32_t ref_ntime = work_get_ntime(work);
+	const int earliest_elapsed = timer_elapsed(&nrl->tv_ref, tvp_earliest);
+	const int   latest_elapsed = timer_elapsed(&nrl->tv_ref, tvp_latest);
+	// minimum ntime is the latest possible result (add a second to spare) adjusted for minimum offset (or fixed minimum ntime)
+	uint32_t min_ntime = max(nrl->min, ref_ntime + latest_elapsed+1 + nrl->minoff);
+	// maximum ntime is the earliest possible result adjusted for maximum offset (or fixed maximum ntime)
+	uint32_t max_ntime = min(nrl->max, ref_ntime + earliest_elapsed + nrl->maxoff);
+	if (max_ntime < min_ntime)
+		return -1;
+	
+	if (max_ntime - min_ntime > desired_roll)
+	{
+		// Adjust min_ntime upward for accuracy, when possible
+		const int mid_elapsed = ((latest_elapsed - earliest_elapsed) / 2) + earliest_elapsed;
+		uint32_t ideal_ntime = ref_ntime + mid_elapsed;
+		if (ideal_ntime > min_ntime)
+			min_ntime = min(ideal_ntime, max_ntime - desired_roll);
+	}
+	
+	work_set_ntime(work, min_ntime);
+	return max_ntime - min_ntime;
+}
+
 #if BLKMAKER_VERSION > 1
 static
 void refresh_bitcoind_address(const bool fresh)

+ 1 - 0
miner.h

@@ -1527,6 +1527,7 @@ extern double share_diff(const struct work *);
 extern const char *bfg_workpadding_bin;
 extern void set_simple_ntime_roll_limit(struct ntime_roll_limits *, uint32_t ntime_base, int ntime_roll, const struct timeval *tvp_ref);
 extern void work_set_simple_ntime_roll_limit(struct work *, int ntime_roll, const struct timeval *tvp_ref);
+extern int work_ntime_range(struct work *, const struct timeval *tvp_earliest, const struct timeval *tvp_latest, int desired_roll);
 extern void work_hash(struct work *);
 
 #define NTIME_DATA_OFFSET  0x44