From b806916201607441ee07bfede3967c34af52d8e4 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 6 Feb 2018 10:12:22 +1100 Subject: [PATCH] Choose the generation transaction type and return the transaction length within libckpool. --- src/ckpool.h | 2 +- src/libckpool.c | 22 ++++++++++++++++++++-- src/libckpool.h | 5 ++--- src/stratifier.c | 40 ++++++++++++++-------------------------- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/ckpool.h b/src/ckpool.h index c8b7f5e8..bc1cd564 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 Con Kolivas + * Copyright 2014-2018 Con Kolivas * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free diff --git a/src/libckpool.c b/src/libckpool.c index 694444ed..e828c4a9 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1730,7 +1730,9 @@ char *http_base64(const char *src) return (str); } -void address_to_pubkeytxn(char *pkh, const char *addr) +/* It's assumed that there is no chance of sending invalid chars to these + * functions as they should have been checked beforehand. */ +static void address_to_pubkeytxn(char *pkh, const char *addr) { char b58bin[25] = {}; @@ -1743,7 +1745,7 @@ void address_to_pubkeytxn(char *pkh, const char *addr) pkh[24] = 0xac; } -void address_to_scripttxn(char *psh, const char *addr) +static void address_to_scripttxn(char *psh, const char *addr) { char b58bin[25] = {}; @@ -1754,6 +1756,22 @@ void address_to_scripttxn(char *psh, const char *addr) psh[22] = 0x87; } +/* Convert an address to a transaction and return the length of the transaction */ +int address_to_txn(char *p2h, const char *addr, const bool script, const bool segwit) +{ + if (unlikely(segwit)) { + /* It should be impossible to hit this for now */ + LOGEMERG("Segwit bech32 address passed to address_to_txn while unsupported."); + return 0; + } + if (script) { + address_to_scripttxn(p2h, addr); + return 23; + } + address_to_pubkeytxn(p2h, addr); + return 25; +} + /* For encoding nHeight into coinbase, return how many bytes were used */ int ser_number(uchar *s, int32_t val) { diff --git a/src/libckpool.h b/src/libckpool.h index 306f1c6c..3d29995c 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 Con Kolivas + * Copyright 2014-2018 Con Kolivas * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -561,8 +561,7 @@ void b58tobin(char *b58bin, const char *b58); int safecmp(const char *a, const char *b); bool cmdmatch(const char *buf, const char *cmd); -void address_to_pubkeytxn(char *pkh, const char *addr); -void address_to_scripttxn(char *pkh, const char *addr); +int address_to_txn(char *p2h, const char *addr, const bool script, const bool segwit); int ser_number(uchar *s, int32_t val); int get_sernumber(uchar *s); bool fulltest(const uchar *hash, const uchar *target); diff --git a/src/stratifier.c b/src/stratifier.c index 06b81d3f..6ffcac10 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -389,10 +389,10 @@ static const char *ckdb_seq_names[] = { struct stratifier_data { ckpool_t *ckp; - char pubkeytxnbin[25]; - int pubkeytxnlen; - char donkeytxnbin[25]; - int donkeytxnlen; + char txnbin[40]; + int txnlen; + char dontxnbin[40]; + int dontxnlen; pool_stats_t stats; /* Protects changes to pool stats */ @@ -615,9 +615,9 @@ static void generate_coinbase(const ckpool_t *ckp, workbase_t *wb) *u64 = htole64(g64); wb->coinb2len += 8; - wb->coinb2bin[wb->coinb2len++] = sdata->pubkeytxnlen; - memcpy(wb->coinb2bin + wb->coinb2len, sdata->pubkeytxnbin, sdata->pubkeytxnlen); - wb->coinb2len += sdata->pubkeytxnlen; + wb->coinb2bin[wb->coinb2len++] = sdata->txnlen; + memcpy(wb->coinb2bin + wb->coinb2len, sdata->txnbin, sdata->txnlen); + wb->coinb2len += sdata->txnlen; if (wb->insert_witness) { // 0 value @@ -636,9 +636,9 @@ static void generate_coinbase(const ckpool_t *ckp, workbase_t *wb) *u64 = htole64(d64); wb->coinb2len += 8; - wb->coinb2bin[wb->coinb2len++] = sdata->donkeytxnlen; - memcpy(wb->coinb2bin + wb->coinb2len, sdata->donkeytxnbin, sdata->donkeytxnlen); - wb->coinb2len += sdata->donkeytxnlen; + wb->coinb2bin[wb->coinb2len++] = sdata->dontxnlen; + memcpy(wb->coinb2bin + wb->coinb2len, sdata->dontxnbin, sdata->dontxnlen); + wb->coinb2len += sdata->dontxnlen; } wb->coinb2len += 4; // Blank lock @@ -2354,8 +2354,8 @@ static sdata_t *duplicate_sdata(const sdata_t *sdata) dsdata->ckp = sdata->ckp; /* Copy the transaction binaries for workbase creation */ - memcpy(dsdata->pubkeytxnbin, sdata->pubkeytxnbin, 25); - memcpy(dsdata->donkeytxnbin, sdata->donkeytxnbin, 25); + memcpy(dsdata->txnbin, sdata->txnbin, 40); + memcpy(dsdata->dontxnbin, sdata->dontxnbin, 40); /* Use the same work queues for all subproxies */ dsdata->ssends = sdata->ssends; @@ -8597,23 +8597,11 @@ void *stratifier(void *arg) /* Store this for use elsewhere */ hex2bin(scriptsig_header_bin, scriptsig_header, 41); - if (ckp->script) { - address_to_scripttxn(sdata->pubkeytxnbin, ckp->btcaddress); - sdata->pubkeytxnlen = 23; - } else { - address_to_pubkeytxn(sdata->pubkeytxnbin, ckp->btcaddress); - sdata->pubkeytxnlen = 25; - } + sdata->txnlen = address_to_txn(sdata->txnbin, ckp->btcaddress, ckp->script, ckp->segwit); if (generator_checkaddr(ckp, ckp->donaddress, &ckp->donscript, &ckp->donsegwit)) { ckp->donvalid = true; - if (ckp->donscript) { - sdata->donkeytxnlen = 23; - address_to_scripttxn(sdata->donkeytxnbin, ckp->donaddress); - } else { - sdata->donkeytxnlen = 25; - address_to_pubkeytxn(sdata->donkeytxnbin, ckp->donaddress); - } + sdata->dontxnlen = address_to_txn(sdata->dontxnbin, ckp->donaddress, ckp->script, ckp->segwit); } }