From 1007e20035f2c0d924cc9b8d186d62a9e817ba6b Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Thu, 12 Jun 2014 15:01:14 +1000 Subject: [PATCH] Create a json string based on parameters we'll be sending to the database on every work update to be submitted once it's hooked in --- src/bitcoin.c | 9 ++++++++- src/bitcoin.h | 1 + src/stratifier.c | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/bitcoin.c b/src/bitcoin.c index eb6d8c78..8a7e2f23 100644 --- a/src/bitcoin.c +++ b/src/bitcoin.c @@ -88,6 +88,7 @@ static bool gbt_merkle_bins(gbtbase_t *gbt, json_t *transaction_arr) uchar *hashbin; dealloc(gbt->txn_data); + dealloc(gbt->txn_hashes); gbt->transactions = 0; gbt->merkles = 0; gbt->transactions = json_array_size(transaction_arr); @@ -110,6 +111,8 @@ static bool gbt_merkle_bins(gbtbase_t *gbt, json_t *transaction_arr) } gbt->txn_data = ckzalloc(len + 1); + gbt->txn_hashes = ckzalloc(gbt->transactions * 65 + 1); + memset(gbt->txn_hashes, 0x20, gbt->transactions * 65); // Spaces for (i = 0; i < gbt->transactions; i++) { char binswap[32]; @@ -140,6 +143,7 @@ static bool gbt_merkle_bins(gbtbase_t *gbt, json_t *transaction_arr) LOGERR("Failed to hex2bin hash in gbt_merkle_bins"); return false; } + memcpy(gbt->txn_hashes + i * 65, hash, 64); bswap_256(hashbin + 32 + 32 * i, binswap); } } @@ -256,8 +260,10 @@ bool gen_gbtbase(connsock_t *cs, gbtbase_t *gbt) gbt_merkle_bins(gbt, transaction_arr); json_object_set_new_nocheck(gbt->json, "transactions", json_integer(gbt->transactions)); - if (gbt->transactions) + if (gbt->transactions) { json_object_set_new_nocheck(gbt->json, "txn_data", json_string_nocheck(gbt->txn_data)); + json_object_set_new_nocheck(gbt->json, "txn_hashes", json_string_nocheck(gbt->txn_hashes)); + } json_object_set_new_nocheck(gbt->json, "merkles", json_integer(gbt->merkles)); if (gbt->merkles) { array = json_array(); @@ -276,6 +282,7 @@ void clear_gbtbase(gbtbase_t *gbt) { dealloc(gbt->flags); dealloc(gbt->txn_data); + dealloc(gbt->txn_hashes); json_decref(gbt->json); gbt->json = NULL; memset(gbt, 0, sizeof(gbtbase_t)); diff --git a/src/bitcoin.h b/src/bitcoin.h index 0b3805fb..cb51457f 100644 --- a/src/bitcoin.h +++ b/src/bitcoin.h @@ -24,6 +24,7 @@ struct gbtbase { char *flags; int transactions; char *txn_data; + char *txn_hashes; int merkles; char merklehash[16][68]; json_t *json; diff --git a/src/stratifier.c b/src/stratifier.c index 378fd3f9..d5d8e9ae 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -105,6 +105,7 @@ struct workbase { char *flags; int transactions; char *txn_data; + char *txn_hashes; int merkles; char merklehash[16][68]; char merklebin[16][32]; @@ -361,6 +362,7 @@ static void clear_workbase(workbase_t *wb) { free(wb->flags); free(wb->txn_data); + free(wb->txn_hashes); free(wb->logdir); free(wb->coinb2bin); free(wb->coinb2); @@ -387,6 +389,37 @@ static void purge_share_hashtable(uint64_t wb_id) LOGINFO("Cleared %d shares from share hashtable", purged); } +/* This message will be sent to the database once it's hooked in */ +static void send_workinfo(ckpool_t *ckp, workbase_t *wb) +{ + uint64_t createdate; + char *coinb1a, *s; + json_t *val; + + createdate = wb->gentime.tv_sec; + createdate *= 1000000000ull; + createdate += wb->gentime.tv_nsec; + coinb1a = bin2hex(wb->coinb1a, wb->coinb1alen); + ASPRINTF(&s, "%016lx", createdate); + + val = json_pack("{ss,si,ss,ss,ss,ss,ss,ss,ss,ss,ss,ss}", + "method", "workinfo", + "workinfoid", wb->id, + "poolinstance", ckp->name, + "transactiontree", wb->txn_hashes, + "prevhash", wb->prevhash, + "coinbase1a", coinb1a, + "version", wb->bbversion, + "bits", wb->nbit, + "createdate", s, + "createby", "code", + "createcode", __func__, + "createinet", "127.0.0.1"); + free(coinb1a); + free(s); + json_decref(val); +} + static void add_base(ckpool_t *ckp, workbase_t *wb, bool *new_block) { workbase_t *tmp, *tmpa; @@ -434,6 +467,8 @@ static void add_base(ckpool_t *ckp, workbase_t *wb, bool *new_block) if (*new_block) purge_share_hashtable(wb->id); + + send_workinfo(ckp, wb); } /* This function assumes it will only receive a valid json gbt base template @@ -474,8 +509,10 @@ static void update_base(ckpool_t *ckp) json_intcpy(&wb->height, val, "height"); json_strdup(&wb->flags, val, "flags"); json_intcpy(&wb->transactions, val, "transactions"); - if (wb->transactions) + if (wb->transactions) { json_strdup(&wb->txn_data, val, "txn_data"); + json_strdup(&wb->txn_hashes, val, "txn_hashes"); + } json_intcpy(&wb->merkles, val, "merkles"); wb->merkle_array = json_array(); if (wb->merkles) {