Browse Source

Abstract code from stratumsrv_mining_submit into new work2d_submit_nonce function

Luke Dashjr 12 years ago
parent
commit
4df39b914b
3 changed files with 29 additions and 10 deletions
  1. 6 10
      driver-stratum.c
  2. 22 0
      work2d.c
  3. 1 0
      work2d.h

+ 6 - 10
driver-stratum.c

@@ -358,7 +358,6 @@ static
 void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
 void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
 {
 {
 	uint32_t * const xnonce1_p = &conn->xnonce1_le;
 	uint32_t * const xnonce1_p = &conn->xnonce1_le;
-	struct work _work, *work;
 	struct stratumsrv_job *ssj;
 	struct stratumsrv_job *ssj;
 	struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
 	struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
 	struct cgpu_info *cgpu;
 	struct cgpu_info *cgpu;
@@ -368,7 +367,8 @@ void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const cha
 	const char * const ntime = __json_array_string(params, 3);
 	const char * const ntime = __json_array_string(params, 3);
 	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 nonce_n;
+	uint32_t ntime_n, nonce_n;
+	bool is_stale;
 	
 	
 	if (unlikely(!client))
 	if (unlikely(!client))
 		return_stratumsrv_failure(20, "Failed creating new cgpu");
 		return_stratumsrv_failure(20, "Failed creating new cgpu");
@@ -389,25 +389,21 @@ void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const cha
 	if (!ssj)
 	if (!ssj)
 		return_stratumsrv_failure(21, "Job not found");
 		return_stratumsrv_failure(21, "Job not found");
 	
 	
-	// Generate dummy work
-	work = &_work;
 	hex2bin(xnonce2, extranonce2, work2d_xnonce2sz);
 	hex2bin(xnonce2, extranonce2, work2d_xnonce2sz);
-	work2d_gen_dummy_work(work, &ssj->swork, &ssj->tv_prepared, xnonce2, *xnonce1_p);
 	
 	
 	// Submit nonce
 	// Submit nonce
-	hex2bin(&work->data[68], ntime, 4);
+	hex2bin((void*)&ntime_n, ntime, 4);
+	ntime_n = be32toh(ntime_n);
 	hex2bin((void*)&nonce_n, nonce, 4);
 	hex2bin((void*)&nonce_n, nonce, 4);
 	nonce_n = le32toh(nonce_n);
 	nonce_n = le32toh(nonce_n);
-	if (!submit_nonce(thr, work, nonce_n))
+	if (!work2d_submit_nonce(thr, &ssj->swork, &ssj->tv_prepared, xnonce2, *xnonce1_p, nonce_n, ntime_n, &is_stale))
 		_stratumsrv_failure(bev, idstr, 23, "H-not-zero");
 		_stratumsrv_failure(bev, idstr, 23, "H-not-zero");
 	else
 	else
-	if (stale_work(work, true))
+	if (is_stale)
 		_stratumsrv_failure(bev, idstr, 21, "stale");
 		_stratumsrv_failure(bev, idstr, 21, "stale");
 	else
 	else
 		_stratumsrv_success(bev, idstr);
 		_stratumsrv_success(bev, idstr);
 	
 	
-	clean_work(work);
-	
 	if (!conn->hashes_done_ext)
 	if (!conn->hashes_done_ext)
 	{
 	{
 		struct timeval tv_now, tv_delta;
 		struct timeval tv_now, tv_delta;

+ 22 - 0
work2d.c

@@ -72,3 +72,25 @@ void work2d_gen_dummy_work(struct work * const work, struct stratum_work * const
 		memset(s, '\xbb', p - s);
 		memset(s, '\xbb', p - s);
 	gen_stratum_work2(work, swork);
 	gen_stratum_work2(work, swork);
 }
 }
+
+bool work2d_submit_nonce(struct thr_info * const thr, struct stratum_work * const swork, const struct timeval * const tvp_prepared, const void * const xnonce2, const uint32_t xnonce1, const uint32_t nonce, const uint32_t ntime, bool * const out_is_stale)
+{
+	struct work _work, *work;
+	bool rv;
+	
+	// Generate dummy work
+	work = &_work;
+	work2d_gen_dummy_work(work, swork, tvp_prepared, xnonce2, xnonce1);
+	*(uint32_t *)&work->data[68] = htobe32(ntime);
+	
+	// Check if it's stale, if desired
+	if (out_is_stale)
+		*out_is_stale = stale_work(work, true);
+	
+	// Submit nonce
+	rv = submit_nonce(thr, work, nonce);
+	
+	clean_work(work);
+	
+	return rv;
+}

+ 1 - 0
work2d.h

@@ -12,5 +12,6 @@ extern bool reserve_work2d_(uint32_t *xnonce1_p);
 extern void release_work2d_(uint32_t xnonce1);
 extern void release_work2d_(uint32_t xnonce1);
 
 
 extern void work2d_gen_dummy_work(struct work *, struct stratum_work *, const struct timeval *tvp_prepared, const void *xnonce2, uint32_t xnonce1);
 extern void work2d_gen_dummy_work(struct work *, struct stratum_work *, const struct timeval *tvp_prepared, const void *xnonce2, uint32_t xnonce1);
+extern bool work2d_submit_nonce(struct thr_info *, struct stratum_work *, const struct timeval *tvp_prepared, const void *xnonce2, uint32_t xnonce1, uint32_t nonce, uint32_t ntime, bool *out_is_stale);
 
 
 #endif
 #endif