From 75d4da0d9bcf65fd0117e3c053085af6e23d46c1 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Sun, 24 Aug 2014 19:35:36 +0000 Subject: [PATCH] Move submission block assembly code to base libblkmaker --- blkmaker.c | 64 +++++++++++++++++++++++++++++++++++++++++++ blkmaker_jansson.c | 67 +++++----------------------------------------- private.h | 3 +++ 3 files changed, 73 insertions(+), 61 deletions(-) diff --git a/blkmaker.c b/blkmaker.c index 2a6eaf3..5ec6935 100644 --- a/blkmaker.c +++ b/blkmaker.c @@ -13,6 +13,12 @@ #include #include +#ifndef WIN32 +#include +#else +#include +#endif + #include #include @@ -416,3 +422,61 @@ unsigned long blkmk_work_left(const blktemplate_t *tmpl) { return UINT_MAX - tmpl->next_dataid; return BLKMK_UNLIMITED_WORK_COUNT; } + +static +char varintEncode(unsigned char *out, uint64_t n) { + if (n < 0xfd) + { + out[0] = n; + return 1; + } + char L; + if (n <= 0xffff) + { + out[0] = '\xfd'; + L = 3; + } + else + if (n <= 0xffffffff) + { + out[0] = '\xfe'; + L = 5; + } + else + { + out[0] = '\xff'; + L = 9; + } + for (unsigned char i = 1; i < L; ++i) + out[i] = (n >> ((i - 1) * 8)) % 256; + return L; +} + +char *blkmk_assemble_submission_(blktemplate_t * const tmpl, const unsigned char * const data, const unsigned int dataid, blknonce_t nonce, const bool foreign) +{ + unsigned char blk[80 + 8 + 1000000]; + memcpy(blk, data, 76); + nonce = htonl(nonce); + memcpy(&blk[76], &nonce, 4); + size_t offs = 80; + + if (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid))) + { + offs += varintEncode(&blk[offs], 1 + tmpl->txncount); + + if (!_blkmk_extranonce(tmpl, &blk[offs], dataid, &offs)) + return NULL; + + if (foreign || !(tmpl->mutations & BMAb_COINBASE)) + for (unsigned long i = 0; i < tmpl->txncount; ++i) + { + memcpy(&blk[offs], tmpl->txns[i].data, tmpl->txns[i].datasz); + offs += tmpl->txns[i].datasz; + } + } + + char *blkhex = malloc((offs * 2) + 1); + _blkmk_bin2hex(blkhex, blk, offs); + + return blkhex; +} diff --git a/blkmaker_jansson.c b/blkmaker_jansson.c index 2144846..8687cd1 100644 --- a/blkmaker_jansson.c +++ b/blkmaker_jansson.c @@ -10,12 +10,6 @@ #include #include -#ifndef WIN32 -#include -#else -#include -#endif - #include #include @@ -253,66 +247,17 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t return NULL; } -static -char varintEncode(unsigned char *out, uint64_t n) { - if (n < 0xfd) - { - out[0] = n; - return 1; - } - char L; - if (n <= 0xffff) - { - out[0] = '\xfd'; - L = 3; - } - else - if (n <= 0xffffffff) - { - out[0] = '\xfe'; - L = 5; - } - else - { - out[0] = '\xff'; - L = 9; - } - for (unsigned char i = 1; i < L; ++i) - out[i] = (n >> ((i - 1) * 8)) % 256; - return L; -} - -#define my_bin2hex _blkmk_bin2hex - static json_t *_blkmk_submit_jansson(blktemplate_t *tmpl, const unsigned char *data, unsigned int dataid, blknonce_t nonce, bool foreign) { - unsigned char blk[80 + 8 + 1000000]; - memcpy(blk, data, 76); - nonce = htonl(nonce); - memcpy(&blk[76], &nonce, 4); - size_t offs = 80; - - if (foreign || (!(tmpl->mutations & BMAb_TRUNCATE && !dataid))) - { - offs += varintEncode(&blk[offs], 1 + tmpl->txncount); - - if (!_blkmk_extranonce(tmpl, &blk[offs], dataid, &offs)) - return NULL; - - if (foreign || !(tmpl->mutations & BMAb_COINBASE)) - for (unsigned long i = 0; i < tmpl->txncount; ++i) - { - memcpy(&blk[offs], tmpl->txns[i].data, tmpl->txns[i].datasz); - offs += tmpl->txns[i].datasz; - } - } - - char blkhex[(offs * 2) + 1]; - my_bin2hex(blkhex, blk, offs); + char *blkhex = blkmk_assemble_submission_(tmpl, data, dataid, nonce, foreign); + if (!blkhex) + return NULL; json_t *rv = json_array(), *ja, *jb; jb = NULL; - if (!(ja = json_string(blkhex))) + ja = json_string(blkhex); + free(blkhex); + if (!ja) goto err; if (json_array_append_new(rv, ja)) goto err; diff --git a/private.h b/private.h index f0e437a..7bec518 100644 --- a/private.h +++ b/private.h @@ -4,8 +4,11 @@ #include #include +#include + // blkmaker.c extern bool _blkmk_dblsha256(void *hash, const void *data, size_t datasz); +extern char *blkmk_assemble_submission_(blktemplate_t *, const unsigned char *data, unsigned int dataid, blknonce_t nonce, bool foreign); // blktemplate.c extern void _blktxn_free(struct blktxn_t *);