From 646b3c3028e625c0f2dd2b24c0a6b1ed9a6262b8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 9 Apr 2016 00:55:22 -0700 Subject: [PATCH] improve block.getVirtualSize. --- lib/bcoin/block.js | 10 ++++++++-- lib/bcoin/miner.js | 2 +- lib/bcoin/script.js | 8 +++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 05c50a29..e8f6d85e 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -71,6 +71,12 @@ Block.prototype.getRaw = function getRaw() { return raw; }; +Block.prototype._getSize = function _getSize() { + var sizes = bcoin.protocol.framer.block._sizes(this); + this._size = sizes.size; + this._witnessSize = sizes.witnessSize; +}; + Block.prototype.getVirtualSize = function getVirtualSize(force) { var size, witnessSize, base; @@ -83,13 +89,13 @@ Block.prototype.getVirtualSize = function getVirtualSize(force) { Block.prototype.getSize = function getSize(force) { if (force || this._size === 0) - this.getRaw(); + this._getSize(); return this._size; }; Block.prototype.getWitnessSize = function getWitnessSize(force) { if (force || this._size === 0) - this.getRaw(); + this._getSize(); return this._witnessSize; }; diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index ae2450f3..d17868f4 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -366,7 +366,7 @@ MinerBlock.prototype.updateMerkle = function updateMerkle() { }; MinerBlock.prototype.addTX = function addTX(tx) { - var size = this.block.getVirtualSize() + tx.getVirtualSize(); + var size = this.block.getVirtualSize(true) + tx.getVirtualSize(); // Deliver me from the block size debate, please if (size > constants.blocks.maxSize) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 90ab5621..659960a6 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -970,7 +970,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version n = Script.num(stack.pop(), flags).toNumber(); - if (!(n >= 1 && n <= 15)) + if (!(n >= 1 && n <= constants.script.maxPubkeysPerMultisig)) throw new ScriptError('`n` is out of bounds.', op, ip); if (stack.length < n + 1) @@ -1731,9 +1731,7 @@ Script.prototype.getInputType = function getInputType() { }; Script.getInputType = function getInputType(code, isWitness) { - var type; - - type = (Script.isPubkeyInput(code) && 'pubkey') + var type = (Script.isPubkeyInput(code) && 'pubkey') || (Script.isPubkeyhashInput(code) && 'pubkeyhash') || (Script.isMultisigInput(code, isWitness) && 'multisig') || (Script.isScripthashInput(code, isWitness) && 'scripthash') @@ -1758,7 +1756,7 @@ Script.isUnknownInput = function isUnknownInput(code, isWitness) { return Script.getInputType(code, isWitness) === 'unknown'; }; -Script.createOutputScript = function(options) { +Script.createOutputScript = function createOutputScript(options) { var script, m, n, hash, flags, address, redeem; if (!options)