From b65c555d87a6c212431efcf2b45144380deb9247 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 22 Apr 2014 12:48:17 +1000 Subject: [PATCH] Decode the generator response and store the variables in the stratifier --- src/bitcoin.h | 2 +- src/libckpool.c | 1 + src/stratifier.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/bitcoin.h b/src/bitcoin.h index 8c891e51..dd291dc2 100644 --- a/src/bitcoin.h +++ b/src/bitcoin.h @@ -21,8 +21,8 @@ struct gbtbase { char nbit[12]; int coinbasevalue; int height; - char *txn_data; int transactions; + char *txn_data; int merkles; char merklehash[16][68]; json_t *json; diff --git a/src/libckpool.c b/src/libckpool.c index b958da6e..ab3f8e99 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -828,6 +828,7 @@ out: return val; } + /* Align a size_t to 4 byte boundaries for fussy arches */ void align_len(size_t *len) { diff --git a/src/stratifier.c b/src/stratifier.c index 3d50881e..30b89a68 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -17,13 +17,94 @@ #include "libckpool.h" #include "bitcoin.h" +struct workbase { + /* GBT/shared variables */ + char target[68]; + double diff; + uint32_t version; + uint32_t curtime; + char prevhash[68]; + char ntime[12]; + char bbversion[12]; + char nbit[12]; + int coinbasevalue; + int height; + int transactions; + char *txn_data; + int merkles; + char merklehash[16][68]; + + /* Work variables */ +}; + +typedef struct workbase workbase_t; + +/* No error checking with these, make sure we know they're valid already! */ +static inline void json_strcpy(char *buf, json_t *val, const char *key) +{ + strcpy(buf, json_string_value(json_object_get(val, key))); +} + +static inline void json_dblcpy(double *dbl, json_t *val, const char *key) +{ + *dbl = json_real_value(json_object_get(val, key)); +} + +static inline void json_uintcpy(uint32_t *u32, json_t *val, const char *key) +{ + *u32 = (uint32_t)json_integer_value(json_object_get(val, key)); +} + +static inline void json_intcpy(int *i, json_t *val, const char *key) +{ + *i = json_integer_value(json_object_get(val, key)); +} + +static inline void json_strdup(char **buf, json_t *val, const char *key) +{ + *buf = strdup(json_string_value(json_object_get(val, key))); +} + +/* This function assumes it will only receive a valid json gbt base template + * since checking should have been done earlier, and creates the base template + * for generating work templates. */ static void update_base(ckpool_t *ckp) { + workbase_t wb; + json_t *val; char *buf; + memset(&wb, 0, sizeof(wb)); buf = send_recv_proc(&ckp->generator, "getbase"); - /* Do something with this buffer here */ + if (unlikely(!buf)) { + LOGWARNING("Failed to get base from generator in update_base"); + return; + } + val = json_loads(buf, 0, NULL); dealloc(buf); + + json_strcpy(wb.target, val, "target"); + json_dblcpy(&wb.diff, val, "diff"); + json_uintcpy(&wb.version, val, "version"); + json_uintcpy(&wb.curtime, val, "curtime"); + json_strcpy(wb.prevhash, val, "prevhash"); + json_strcpy(wb.ntime, val, "ntime"); + json_strcpy(wb.bbversion, val, "bbversion"); + json_intcpy(&wb.coinbasevalue, val, "coinbasevalue"); + json_intcpy(&wb.height, val, "height"); + json_intcpy(&wb.transactions, val, "transactions"); + if (wb.transactions) + json_strdup(&wb.txn_data, val, "txn_data"); + json_intcpy(&wb.merkles, val, "merkles"); + if (wb.merkles) { + json_t *arr; + int i; + + arr = json_object_get(val, "merklehash"); + for (i = 0; i < wb.merkles; i++) + strcpy(&wb.merklehash[i][0], json_string_value(json_array_get(arr, i))); + } + json_decref(val); } static int strat_loop(ckpool_t *ckp, proc_instance_t *pi)