From 5307b027f616601b3ee3f7effd0897499ef9a47e Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 26 Jul 2016 04:36:22 +0000 Subject: [PATCH] Implement weightlimit --- blkmaker.c | 18 ++++++++++++++++++ blkmaker_jansson.c | 9 +++++++++ blktemplate.h | 2 ++ 3 files changed, 29 insertions(+) diff --git a/blkmaker.c b/blkmaker.c index 315ff7b..97f8e5a 100644 --- a/blkmaker.c +++ b/blkmaker.c @@ -224,7 +224,9 @@ uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const s off += 4; const int16_t sigops_counted = blkmk_count_sigops(script, scriptsz, tmpl->_bip141_sigops); + const int64_t gentx_weight = (off * 4) + 2 /* marker & flag */ + 1 /* witness stack count */ + 1 /* stack item size */ + 32 /* stack item: nonce */ ; if (tmpl->txns_datasz + off > tmpl->sizelimit + || (tmpl->txns_weight >= 0 && tmpl->txns_weight + gentx_weight > tmpl->weightlimit) || (tmpl->txns_sigops >= 0 && tmpl->txns_sigops + sigops_counted > tmpl->sigoplimit)) { free(data); return 0; @@ -241,6 +243,7 @@ uint64_t blkmk_init_generation3(blktemplate_t * const tmpl, const void * const s txn->data = data; txn->datasz = off; txn->sigops_ = sigops_counted; + txn->weight = gentx_weight; if (tmpl->cbtxn) { @@ -442,6 +445,10 @@ bool _blkmk_append_cb(blktemplate_t * const tmpl, void * const vout, const void return false; } + if (tmpl->cbtxn->weight + tmpl->txns_weight + (appendsz * 4) > tmpl->weightlimit) { + return false; + } + const int16_t orig_scriptSig_sigops = blkmk_count_sigops(&in[cbScriptSigLen + 1], in[cbScriptSigLen], tmpl->_bip141_sigops); int cbPostScriptSig = cbScriptSigLen + 1 + in[cbScriptSigLen]; if (appended_at_offset) @@ -504,6 +511,16 @@ ssize_t blkmk_append_coinbase_safe2(blktemplate_t * const tmpl, const void * con availsz = availsz2; } } + { + const size_t current_blockweight = tmpl->cbtxn->weight + tmpl->txns_weight; + if (current_blockweight > tmpl->weightlimit) { + return false; + } + const size_t availsz2 = (tmpl->weightlimit - current_blockweight) / 4; + if (availsz2 < availsz) { + availsz = availsz2; + } + } if (appendsz > availsz) return availsz; @@ -515,6 +532,7 @@ ssize_t blkmk_append_coinbase_safe2(blktemplate_t * const tmpl, const void * con if (!_blkmk_append_cb(tmpl, newp, append, appendsz, NULL, &tmpl->cbtxn->sigops_)) return -3; tmpl->cbtxn->datasz += appendsz; + tmpl->cbtxn->weight += appendsz * 4; return availsz; } diff --git a/blkmaker_jansson.c b/blkmaker_jansson.c index b24a7f8..ceedca1 100644 --- a/blkmaker_jansson.c +++ b/blkmaker_jansson.c @@ -306,6 +306,7 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t my_flip(tmpl->prevblk, 32); GETNUM_O(sigoplimit, unsigned short); GETNUM_O(sizelimit, unsigned long); + GETNUM_O(weightlimit, int64_t); GETNUM(version, uint32_t); if ((v = json_object_get(json, "mutable")) && json_is_array(v)) @@ -408,6 +409,7 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t tmpl->txns = calloc(txns, sizeof(*tmpl->txns)); tmpl->txns_datasz = 0; tmpl->txns_sigops = 0; + tmpl->txns_weight = 0; for (size_t i = 0; i < txns; ++i) { struct blktxn_t * const txn = &tmpl->txns[i]; @@ -422,6 +424,13 @@ const char *blktmpl_add_jansson(blktemplate_t *tmpl, const json_t *json, time_t } else { tmpl->txns_sigops += txn->sigops_; } + if (tmpl->txns_weight == -1) { + ; // Impossible to tally the unknown + } else if (txn->weight == -1) { + tmpl->txns_weight = -1; + } else { + tmpl->txns_weight += txn->weight; + } } if ((v = json_object_get(json, "coinbasetxn")) && json_is_object(v)) diff --git a/blktemplate.h b/blktemplate.h index 563ae7f..5c19fc0 100644 --- a/blktemplate.h +++ b/blktemplate.h @@ -156,6 +156,8 @@ typedef struct { bool _bip141_sigops; bool _calculated_witness; libblkmaker_hash_t *_witnessmrklroot; + int64_t weightlimit; + int64_t txns_weight; } blktemplate_t; extern void blktxn_init(struct blktxn_t *);