diff --git a/lib/mining/miner.js b/lib/mining/miner.js index 3ca66453..2543efc7 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -300,7 +300,9 @@ Miner.prototype._createBlock = co(function* createBlock(tip, address) { address: address, coinbaseFlags: this.options.coinbaseFlags, witness: this.chain.state.hasWitness(), - network: this.network + network: this.network, + weight: this.options.reservedWeight, + sigops: this.options.reservedSigops }); this.build(attempt); @@ -465,7 +467,8 @@ Miner.prototype.build = function build(attempt) { attempt.refresh(); - assert(block.getWeight() <= attempt.weight); + assert(block.getWeight() <= attempt.weight, + 'Block exceeds reserved weight!'); }; /** @@ -493,6 +496,8 @@ function MinerOptions(options) { this.priorityWeight = policy.PRIORITY_BLOCK_WEIGHT; this.minPriority = policy.MIN_BLOCK_PRIORITY; this.maxSigops = consensus.MAX_BLOCK_SIGOPS_COST; + this.reservedWeight = 4000; + this.reservedSigops = 400; this.fromOptions(options); } @@ -580,6 +585,16 @@ MinerOptions.prototype.fromOptions = function fromOptions(options) { this.minPriority = options.minPriority; } + if (options.reservedWeight != null) { + assert(util.isNumber(options.reservedWeight)); + this.reservedWeight = options.reservedWeight; + } + + if (options.reservedSigops != null) { + assert(util.isNumber(options.reservedSigops)); + this.reservedSigops = options.reservedSigops; + } + return this; }; diff --git a/lib/mining/minerblock.js b/lib/mining/minerblock.js index c65431ff..dcc80272 100644 --- a/lib/mining/minerblock.js +++ b/lib/mining/minerblock.js @@ -67,8 +67,8 @@ function MinerBlock(options) { this.destroyed = false; this.committed = false; - this.sigops = 0; - this.weight = 0; + this.sigops = options.sigops; + this.weight = options.weight; this.fees = 0; this.items = []; @@ -119,6 +119,8 @@ MinerBlock.prototype._init = function _init() { var hash = encoding.ZERO_HASH; var block = this.block; var cb = this.coinbase; + var weight = 0; + var sigops = 0; var input, output, commit, padding; assert(this.coinbaseFlags.length <= 20); @@ -176,16 +178,16 @@ MinerBlock.prototype._init = function _init() { block.txs.push(cb); // Initialize weight. - this.weight = block.getWeight(); + weight = block.getWeight(); // 4 extra bytes for varint tx count. - this.weight += 4 * scale; + weight += 4 * scale; // Padding for the CB height (constant size). padding = 5 - input.script.code[0].getSize(); assert(padding >= 0); - this.weight += padding * scale; + weight += padding * scale; // Reserved size. // Without segwit: @@ -205,14 +207,14 @@ MinerBlock.prototype._init = function _init() { // CB size = 208 // Sigops cost = 4 if (!this.witness) { - assert.equal(this.weight, 840); + assert.equal(weight, 840); assert.equal(block.getBaseSize() + 4 + padding, 210); assert.equal(block.getSize() + 4 + padding, 210); assert.equal(cb.getWeight() + padding * scale, 500); assert.equal(cb.getBaseSize() + padding, 125); assert.equal(cb.getSize() + padding, 125); } else { - assert.equal(this.weight, 1064); + assert.equal(weight, 1064); assert.equal(block.getBaseSize() + 4 + padding, 257); assert.equal(block.getSize() + 4 + padding, 293); assert.equal(cb.getWeight() + padding * scale, 724); @@ -221,7 +223,7 @@ MinerBlock.prototype._init = function _init() { } // Initialize sigops weight. - this.sigops = 4; + sigops = 4; // Setup coinbase flags (variable size). input.script.set(1, this.coinbaseFlags); @@ -236,11 +238,17 @@ MinerBlock.prototype._init = function _init() { // Ensure the variable size // stuff didn't break anything. - assert(block.getWeight() <= this.weight, + assert(block.getWeight() <= weight, 'Coinbase exceeds reserved size!'); - assert(cb.getSigopsCost(null, this.flags) <= this.sigops, - 'Coinbase exceeds reserved sigops'); + assert(cb.getSigopsCost(null, this.flags) <= sigops, + 'Coinbase exceeds reserved sigops!'); + + assert(this.weight >= weight, + 'Coinbase exceeds reserved size!'); + + assert(this.sigops >= sigops, + 'Coinbase exceeds reserved sigops!'); }; /**