From 33a8e1e511011e278792bc25af717b4c7f31bd8e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 13 Dec 2016 16:52:31 -0800 Subject: [PATCH] block/tx: sigops cost/size/weight. --- lib/blockchain/chain.js | 4 ++-- lib/http/rpc.js | 11 +++++------ lib/http/server.js | 2 ++ lib/mempool/mempool.js | 2 +- lib/mempool/mempoolentry.js | 2 +- lib/mining/miner.js | 6 +++--- lib/mining/minerblock.js | 6 +++--- lib/primitives/block.js | 2 +- lib/primitives/tx.js | 4 ++-- lib/protocol/constants.js | 5 +++-- test/block-test.js | 2 +- 11 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 30785105..9420c969 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -653,9 +653,9 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) { } // Count sigops (legacy + scripthash? + witness?) - sigops += tx.getSigopsWeight(view, state.flags); + sigops += tx.getSigopsCost(view, state.flags); - if (sigops > constants.block.MAX_SIGOPS_WEIGHT) { + if (sigops > constants.block.MAX_SIGOPS_COST) { throw new VerifyError(block, 'invalid', 'bad-blk-sigops', diff --git a/lib/http/rpc.js b/lib/http/rpc.js index d20ca21f..976dee1e 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1548,6 +1548,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) { var vbrules = []; var attempt = yield this._getAttempt(false); var block = attempt.block; + var scale = attempt.witness ? 1 : constants.WITNESS_SCALE_FACTOR; var i, j, entry, tx, deps, input, dep, output, raw, rwhash; var template, name, deployment, state; @@ -1576,7 +1577,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) { hash: tx.rwhash(), depends: deps, fee: entry.fee, - sigops: entry.sigops, + sigops: entry.sigops / scale | 0, weight: tx.getWeight() }); } @@ -1629,10 +1630,8 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) { maxtime: this.network.now() + 2 * 60 * 60, mutable: mutable, noncerange: '00000000ffffffff', - sigoplimit: attempt.witness - ? constants.block.MAX_SIGOPS_WEIGHT - : constants.block.MAX_SIGOPS, - sizelimit: constants.block.MAX_SIZE, + sigoplimit: constants.block.MAX_SIGOPS_COST / scale | 0, + sizelimit: constants.block.MAX_RAW_SIZE, weightlimit: constants.block.MAX_WEIGHT, curtime: block.ts, bits: util.hex32(block.bits), @@ -1661,7 +1660,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) { hash: rwhash, depends: [], fee: 0, - sigops: tx.getSigopsWeight(), + sigops: tx.getSigopsCost() / scale | 0, weight: tx.getWeight() }; } else { diff --git a/lib/http/server.js b/lib/http/server.js index 17365078..c2588556 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -552,6 +552,8 @@ HTTPServer.prototype._init = function _init() { req.body.method = 'getworklp'; } + this.logger.info(req.body); + try { json = yield this.rpc.execute(req.body); } catch (err) { diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 37dcde2e..81f78cfe 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -902,7 +902,7 @@ Mempool.prototype.verify = co(function* verify(entry, view) { } // Annoying process known as sigops counting. - if (tx.getSigopsWeight(view, flags) > constants.tx.MAX_SIGOPS_WEIGHT) { + if (tx.getSigopsCost(view, flags) > constants.tx.MAX_SIGOPS_COST) { throw new VerifyError(tx, 'nonstandard', 'bad-txns-too-many-sigops', diff --git a/lib/mempool/mempoolentry.js b/lib/mempool/mempoolentry.js index f78992d5..92654b45 100644 --- a/lib/mempool/mempoolentry.js +++ b/lib/mempool/mempoolentry.js @@ -84,7 +84,7 @@ MempoolEntry.fromOptions = function fromOptions(options) { MempoolEntry.prototype.fromTX = function fromTX(tx, view, height) { var priority = tx.getPriority(view, height); var value = tx.getChainValue(view, height); - var sigops = tx.getSigopsWeight(view); + var sigops = tx.getSigopsCost(view); var dependencies = false; var size = tx.getVirtualSize(); var fee = tx.getFee(view); diff --git a/lib/mining/miner.js b/lib/mining/miner.js index d1ff5920..ba96d00f 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -54,7 +54,7 @@ function Miner(options) { this.minWeight = 0; this.maxWeight = 750000 * constants.WITNESS_SCALE_FACTOR; - this.maxSigops = constants.block.MAX_SIGOPS_WEIGHT; + this.maxSigops = constants.block.MAX_SIGOPS_COST; this.priorityWeight = 50000 * constants.WITNESS_SCALE_FACTOR; this.minPriority = constants.tx.FREE_THRESHOLD; @@ -106,7 +106,7 @@ Miner.prototype._initOptions = function _initOptions(options) { if (options.maxSigops != null) { assert(util.isNumber(options.maxSigops)); - assert(options.maxSigops <= constants.block.MAX_SIGOPS_WEIGHT); + assert(options.maxSigops <= constants.block.MAX_SIGOPS_COST); this.maxSigops = options.maxSigops; } @@ -462,7 +462,7 @@ Miner.prototype.build = function build(attempt) { sigops += item.sigops; // If we had a view we could do: - // sigops += tx.getSigopsWeight(view, attempt.flags); + // sigops += tx.getSigopsCost(view, attempt.flags); if (sigops > this.maxSigops) continue; diff --git a/lib/mining/minerblock.js b/lib/mining/minerblock.js index 5816e762..4e4664ea 100644 --- a/lib/mining/minerblock.js +++ b/lib/mining/minerblock.js @@ -190,7 +190,7 @@ MinerBlock.prototype._init = function _init() { this.weight += 8 * scale; // Initialize sigops weight. - this.sigops = cb.getSigopsWeight(null, this.flags); + this.sigops = cb.getSigopsCost(null, this.flags); }; /** @@ -288,7 +288,7 @@ MinerBlock.prototype.addTX = function addTX(tx, view) { if (this.weight + weight > constants.block.MAX_WEIGHT) return false; - if (this.sigops + sigops > constants.block.MAX_SIGOPS_WEIGHT) + if (this.sigops + sigops > constants.block.MAX_SIGOPS_COST) return false; if (!this.witness && tx.hasWitness()) @@ -522,7 +522,7 @@ BlockEntry.fromTX = function fromTX(tx, view, attempt) { entry.rate = tx.getRate(view); entry.priority = tx.getPriority(view, attempt.height); entry.free = false; - entry.sigops = tx.getSigopsWeight(view, attempt.flags); + entry.sigops = tx.getSigopsCost(view, attempt.flags); return entry; }; diff --git a/lib/primitives/block.js b/lib/primitives/block.js index 9aff63f2..056c8da3 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -484,7 +484,7 @@ Block.prototype._verify = function _verify(ret) { // Count legacy sigops (do not count scripthash or witness). sigops += tx.getLegacySigops(); - if (sigops * scale > constants.block.MAX_SIGOPS_WEIGHT) { + if (sigops * scale > constants.block.MAX_SIGOPS_COST) { ret.reason = 'bad-blk-sigops'; ret.score = 100; return false; diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index d4ee9a9f..e8b9285f 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -1177,7 +1177,7 @@ TX.prototype.getScripthashSigops = function getScripthashSigops(view) { * @returns {Number} sigop weight */ -TX.prototype.getSigopsWeight = function getSigopsWeight(view, flags) { +TX.prototype.getSigopsCost = function getSigopsCost(view, flags) { var weight = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR; var i, input, coin; @@ -1220,7 +1220,7 @@ TX.prototype.getSigops = function getSigops(view, flags) { if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; - return (this.getSigopsWeight(view, flags) + scale - 1) / scale | 0; + return (this.getSigopsCost(view, flags) + scale - 1) / scale | 0; }; /** diff --git a/lib/protocol/constants.js b/lib/protocol/constants.js index c066fbcd..c3610692 100644 --- a/lib/protocol/constants.js +++ b/lib/protocol/constants.js @@ -385,9 +385,10 @@ exports.WITNESS_SCALE_FACTOR = 4; exports.block = { MAX_SIZE: 1000000, + MAX_RAW_SIZE: 4000000, MAX_WEIGHT: 4000000, MAX_SIGOPS: 1000000 / 50, - MAX_SIGOPS_WEIGHT: 80000, + MAX_SIGOPS_COST: 80000, MEDIAN_TIMESPAN: 11, BIP16_TIME: 1333238400, SIGHASH_LIMIT: 1300000000 @@ -421,7 +422,7 @@ exports.tx = { BARE_MULTISIG: true, FREE_THRESHOLD: exports.COIN * 144 / 250, MAX_SIGOPS: exports.block.MAX_SIGOPS / 5, - MAX_SIGOPS_WEIGHT: exports.block.MAX_SIGOPS_WEIGHT / 5, + MAX_SIGOPS_COST: exports.block.MAX_SIGOPS_COST / 5, BYTES_PER_SIGOP: 20, COINBASE_MATURITY: 100 }; diff --git a/test/block-test.js b/test/block-test.js index 0bfe112e..791e32c2 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -180,7 +180,7 @@ describe('Block', function() { assert(tx.checkInputs(view, height)); assert(tx.verify(view, flags)); assert(!tx.hasWitness()); - sigops += tx.getSigopsWeight(view, flags); + sigops += tx.getSigopsCost(view, flags); view.addTX(tx, height); }