diff --git a/src/bitcoin.c b/src/bitcoin.c index e94d5b97..ba4c82c1 100644 --- a/src/bitcoin.c +++ b/src/bitcoin.c @@ -262,12 +262,20 @@ bool gen_gbtbase(connsock_t *cs, gbtbase_t *gbt) json_array_append_new(array, json_string_nocheck(&gbt->merklehash[i][0])); json_object_set_new_nocheck(gbt->json, "merklehash", array); } + ret = true; out: json_decref(val); return ret; } +void clear_gbtbase(gbtbase_t *gbt) +{ + dealloc(gbt->txn_data); + json_decref(gbt->json); + gbt->json = NULL; +} + static const char *blockcount_req = "{\"method\": \"getblockcount\"}\n"; /* Request getblockcount from bitcoind, returning the count or -1 if the call diff --git a/src/bitcoin.h b/src/bitcoin.h index 83cd916e..8c891e51 100644 --- a/src/bitcoin.h +++ b/src/bitcoin.h @@ -32,6 +32,7 @@ typedef struct gbtbase gbtbase_t; bool validate_address(connsock_t *cs, const char *address); bool gen_gbtbase(connsock_t *cs, gbtbase_t *gbt); +void clear_gbtbase(gbtbase_t *gbt); int get_blockcount(connsock_t *cs); bool get_blockhash(connsock_t *cs, int height, char *hash); bool get_bestblockhash(connsock_t *cs, char *hash); diff --git a/src/generator.c b/src/generator.c index 3583279a..ff75400b 100644 --- a/src/generator.c +++ b/src/generator.c @@ -14,15 +14,19 @@ #include "ckpool.h" #include "libckpool.h" #include "generator.h" +#include "bitcoin.h" int generator(proc_instance_t *pi) { ckpool_t *ckp = pi->ckp; char *userpass = NULL; + gbtbase_t gbt; connsock_t cs; int ret = 0; memset(&cs, 0, sizeof(cs)); + memset(&gbt, 0, sizeof(gbt)); + if (!ckp->btcdurl) ckp->btcdurl = strdup("localhost:8332"); if (!ckp->btcdauth) @@ -35,6 +39,7 @@ int generator(proc_instance_t *pi) goto out; } userpass = strdup(ckp->btcdauth); + realloc_strcat(&userpass, ":"); realloc_strcat(&userpass, ckp->btcdpass); cs.auth = http_base64(userpass); if (!cs.auth) { @@ -49,7 +54,16 @@ int generator(proc_instance_t *pi) ret = 1; goto out; } + keep_sockalive(cs.fd); + block_socket(cs.fd); + /* Test we can connect, authorise and get a block template */ + if (!gen_gbtbase(&cs, &gbt)) { + LOGWARNING("Failed to get test block template from %s:%s auth %s", + cs.url, cs.port, userpass); + goto out; + } + clear_gbtbase(&gbt); out: /* Clean up here */ dealloc(cs.url); diff --git a/src/libckpool.c b/src/libckpool.c index ceadd0af..9fa345ef 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -468,10 +468,12 @@ retry: int extralen; ret = recv(cs->fd, readbuf, bufsiz - 2, MSG_PEEK); - if (ret < 1) { - LOGNOTICE("Failed to recv in read_socket_line"); + if (ret < 0) { + LOGERR("Failed to recv in read_socket_line"); goto out; } + if (!ret) + continue; eom = strchr(readbuf, '\n'); if (eom) extralen = eom - readbuf + 1; @@ -483,9 +485,8 @@ retry: if (unlikely(!cs->buf)) quit(1, "Failed to alloc buf of %d bytes in read_socket_line", (int)buflen); ret = recv(cs->fd, cs->buf + bufofs, extralen, 0); - if (ret != extralen) { - LOGNOTICE("Failed to recv %d bytes in read_socket_line", (int)buflen); - ret = -1; + if (ret < 0) { + LOGERR("Failed to recv %d bytes in read_socket_line", (int)buflen); goto out; } bufofs += ret;