From e37ae9022bc7e0db66334e0523c7ecc2f7ef2463 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 26 Jul 2016 02:59:38 +0000 Subject: [PATCH 1/3] Implement blkmk_count_sigops --- blkmaker.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/blkmaker.c b/blkmaker.c index cf247a2..67e7136 100644 --- a/blkmaker.c +++ b/blkmaker.c @@ -78,6 +78,37 @@ char varintEncode(unsigned char *out, uint64_t n) { return L; } +static +int16_t blkmk_count_sigops(const uint8_t * const script, const size_t scriptsz) { + int16_t sigops = 0; + for (size_t i = 0; i < scriptsz; ++i) { + if (script[i] <= 0x4c /* OP_PUSHDATA1 */) { + if (script[i] == 0x4c) { + if (i + 1 >= scriptsz) { + break; + } + ++i; + } + i += script[i]; + } else if (script[i] == 0x4d /* OP_PUSHDATA2 */) { + if (i + 2 >= scriptsz) { + break; + } + i += 2 + upk_u16le(script, i + 1); + } else if (script[i] == 0x4e /* OP_PUSHDATA4 */) { + if (i + 4 >= scriptsz) { + break; + } + i += 4 + upk_u32le(script, i + 1); + } else if (script[i] == 0xac /* OP_CHECKSIG */ || script[i] == 0xad /* OP_CHECKSIGVERIFY */) { + ++sigops; + } else if (script[i] == 0xae /* OP_CHECKMULTISIG */ || script[i] == 0xaf /* OP_CHECKMULTISIGVERIFY */) { + sigops += 20; + } + } + return sigops; +} + uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const script, const size_t scriptsz, bool * const inout_newcb) { if (tmpl->cbtxn && !(*inout_newcb && (tmpl->mutations & BMM_GENERATE))) { From 3c7889fa23e30cc90ed567029dd9986028e2e41d Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 26 Jul 2016 03:08:03 +0000 Subject: [PATCH 2/3] Keep a tally of total sigops in blktmpl_t->txns (if known) --- blkmaker_jansson.c | 9 +++++++++ blktemplate.h | 1 + 2 files changed, 10 insertions(+) diff --git a/blkmaker_jansson.c b/blkmaker_jansson.c index 390b08a..f2d4ad5 100644 --- a/blkmaker_jansson.c +++ b/blkmaker_jansson.c @@ -283,6 +283,7 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t size_t txns = tmpl->txncount = json_array_size(v); tmpl->txns = malloc(txns * sizeof(*tmpl->txns)); tmpl->txns_datasz = 0; + tmpl->txns_sigops = 0; for (size_t i = 0; i < txns; ++i) { struct blktxn_t * const txn = &tmpl->txns[i]; @@ -290,6 +291,14 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t return s; } tmpl->txns_datasz += txn->datasz; + if (tmpl->txns_sigops == -1) { + ; // Impossible to tally the unknown + } else if (txn->sigops_ == -1) { + tmpl->txns_sigops = -1; + } else { + tmpl->txns_sigops += txn->sigops_; + } + tmpl->txns_sigops += txn->sigops_; } if ((v = json_object_get(json, "coinbasetxn")) && json_is_object(v)) diff --git a/blktemplate.h b/blktemplate.h index e675bee..8f3f7af 100644 --- a/blktemplate.h +++ b/blktemplate.h @@ -139,6 +139,7 @@ typedef struct { struct blkaux_t *auxs; unsigned long txns_datasz; + signed long txns_sigops; } blktemplate_t; extern void blktxn_init(struct blktxn_t *); From df9c3e93039f55bcda7f82914c16933ff2b022b5 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 26 Jul 2016 03:11:46 +0000 Subject: [PATCH 3/3] blkmk_init_generation*: If possible, check we are not overflowing sigoplimit --- blkmaker.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blkmaker.c b/blkmaker.c index 67e7136..d51934d 100644 --- a/blkmaker.c +++ b/blkmaker.c @@ -180,7 +180,8 @@ uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const s memset(&data[off], 0, 4); // lock time off += 4; - if (tmpl->txns_datasz + off > tmpl->sizelimit) { + if (tmpl->txns_datasz + off > tmpl->sizelimit + || (tmpl->txns_sigops >= 0 && tmpl->txns_sigops + blkmk_count_sigops(script, scriptsz) > tmpl->sigoplimit)) { free(data); return 0; }