Browse Source

Fix gbt base generation, be more flexible with reading lines from socket, and add gbtbase clearing function

master
Con Kolivas 11 years ago
parent
commit
0da6336602
  1. 8
      src/bitcoin.c
  2. 1
      src/bitcoin.h
  3. 14
      src/generator.c
  4. 11
      src/libckpool.c

8
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

1
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);

14
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);

11
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;

Loading…
Cancel
Save