Extract get-work logic into separate function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
diff --git a/cpu-miner.c b/cpu-miner.c
index 3486b20..30f1808 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -245,6 +245,24 @@ out:
free(hexstr);
}
+static bool get_work(CURL *curl, struct work *work)
+{
+ static const char *rpc_req =
+ "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
+ json_t *val;
+ bool rc;
+
+ val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
+ if (!val)
+ return false;
+
+ rc = work_decode(json_object_get(val, "result"), work);
+
+ json_decref(val);
+
+ return rc;
+}
+
static void hashmeter(int thr_id, const struct timeval *diff,
unsigned long hashes_done)
{
@@ -263,8 +281,6 @@ static void *miner_thread(void *thr_id_int)
{
int thr_id = (unsigned long) thr_id_int;
int failures = 0;
- static const char *rpc_req =
- "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
uint32_t max_nonce = 0xffffff;
CURL *curl;
@@ -278,12 +294,10 @@ static void *miner_thread(void *thr_id_int)
struct work work __attribute__((aligned(128)));
unsigned long hashes_done;
struct timeval tv_start, tv_end, diff;
- json_t *val;
bool rc;
/* obtain new work from bitcoin */
- val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
- if (!val) {
+ if (!get_work(curl, &work)) {
fprintf(stderr, "json_rpc_call failed, ");
if ((opt_retries >= 0) && (++failures > opt_retries)) {
@@ -298,25 +312,6 @@ static void *miner_thread(void *thr_id_int)
continue;
}
- /* decode result into work state struct */
- rc = work_decode(json_object_get(val, "result"), &work);
- if (!rc) {
- fprintf(stderr, "JSON-decode of work failed, ");
-
- if ((opt_retries >= 0) && (++failures > opt_retries)) {
- fprintf(stderr, "terminating thread\n");
- return NULL; /* exit thread */
- }
-
- /* pause, then restart work loop */
- fprintf(stderr, "retry after %d seconds\n",
- opt_fail_pause);
- sleep(opt_fail_pause);
- continue;
- }
-
- json_decref(val);
-
hashes_done = 0;
gettimeofday(&tv_start, NULL);