Extract get-work logic into separate function.

This commit is contained in:
Jeff Garzik 2011-02-17 01:21:16 -05:00 committed by Jeff Garzik
parent 6d2882937f
commit fa7e80b8e5

View file

@ -245,6 +245,24 @@ out:
free(hexstr); 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, static void hashmeter(int thr_id, const struct timeval *diff,
unsigned long hashes_done) 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 thr_id = (unsigned long) thr_id_int;
int failures = 0; int failures = 0;
static const char *rpc_req =
"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
uint32_t max_nonce = 0xffffff; uint32_t max_nonce = 0xffffff;
CURL *curl; CURL *curl;
@ -278,12 +294,10 @@ static void *miner_thread(void *thr_id_int)
struct work work __attribute__((aligned(128))); struct work work __attribute__((aligned(128)));
unsigned long hashes_done; unsigned long hashes_done;
struct timeval tv_start, tv_end, diff; struct timeval tv_start, tv_end, diff;
json_t *val;
bool rc; bool rc;
/* obtain new work from bitcoin */ /* obtain new work from bitcoin */
val = json_rpc_call(curl, rpc_url, userpass, rpc_req); if (!get_work(curl, &work)) {
if (!val) {
fprintf(stderr, "json_rpc_call failed, "); fprintf(stderr, "json_rpc_call failed, ");
if ((opt_retries >= 0) && (++failures > opt_retries)) { if ((opt_retries >= 0) && (++failures > opt_retries)) {
@ -298,25 +312,6 @@ static void *miner_thread(void *thr_id_int)
continue; 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; hashes_done = 0;
gettimeofday(&tv_start, NULL); gettimeofday(&tv_start, NULL);