From 5ee83e9114ab61048bb6cfdb43dd6b70b4f6b169 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 15 Jan 2017 17:52:42 -0800 Subject: [PATCH] tx: some misc cleanup. --- lib/mempool/mempoolentry.js | 2 +- lib/primitives/mtx.js | 18 ++++++++++++------ lib/primitives/tx.js | 29 ++++++++++++++--------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/mempool/mempoolentry.js b/lib/mempool/mempoolentry.js index 5454050d..5ddcc307 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 flags = Script.flags.STANDARD_VERIFY_FLAGS; var priority = tx.getPriority(view, height); - var value = tx.getChainValue(view, height); + var value = tx.getChainValue(view); var sigops = tx.getSigopsCost(view, flags); var dependencies = false; var size = tx.getVirtualSize(); diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index 46f8f335..1a8b88e8 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -800,14 +800,13 @@ MTX.prototype.signature = function signature(index, prev, value, key, type, vers if (type == null) type = Script.hashType.ALL; - if (typeof type === 'string') + if (typeof type === 'string') { type = Script.hashType[type.toUpperCase()]; + assert(type != null, 'Unknown sighash type.'); + } - // Get the hash of the current tx, minus the other - // inputs, plus the sighash type. hash = this.signatureHash(index, prev, value, type, version); - // Sign the transaction with our one input return Script.sign(hash, key, type); }; @@ -1508,8 +1507,15 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) { } if (options.subtractFee != null) { - this.subtractFee = options.subtractFee; - this.shouldSubtract = options.subtractFee !== false; + if (typeof options.subtractFee === 'number') { + assert(util.isNumber(options.subtractFee)); + this.subtractFee = options.subtractFee; + this.shouldSubtract = true; + } else { + assert(typeof options.subtractFee === 'boolean'); + this.subtractFee = options.subtractFee; + this.shouldSubtract = options.subtractFee; + } } if (options.height != null) { diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 07a4e1ca..606f781b 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -40,8 +40,7 @@ var BAD_NONSTD_P2WSH = 3; * A static transaction object. * @exports TX * @constructor - * @param {NakedTX} options - Transaction fields. - * @property {String} type - "tx" (inv type). + * @param {Object} options - Transaction fields. * @property {Number} version - Transaction version. Note that BCoin reads * versions as unsigned even though they are signed at the protocol level. * This value will never be negative. @@ -446,8 +445,10 @@ TX.prototype.hasWitness = function hasWitness() { */ TX.prototype.signatureHash = function signatureHash(index, prev, value, type, version) { - if (typeof type === 'string') + if (typeof type === 'string') { type = Script.hashType[type.toUpperCase()]; + assert(type != null, 'Unknown sighash type.'); + } assert(index >= 0 && index < this.inputs.length); assert(prev instanceof Script); @@ -1698,7 +1699,7 @@ TX.prototype.getWitnessStandard = function getWitnessStandard(view) { ret = BAD_NONSTD_P2WSH; } - redeem = Script.fromRaw(redeem); + redeem = new Script(redeem); if (redeem.isPubkey()) { if (input.witness.length - 1 !== 1) @@ -1769,7 +1770,6 @@ TX.prototype.checkInputs = function checkInputs(view, height, ret) { coins = view.get(input.prevout.hash); if (!coins) { - // Note: don't trigger dos score here. ret.reason = 'bad-txns-inputs-missingorspent'; ret.score = 0; return false; @@ -1783,7 +1783,13 @@ TX.prototype.checkInputs = function checkInputs(view, height, ret) { } } - coin = view.getOutput(input); + coin = coins.getOutput(input.prevout.index); + + if (!coin) { + ret.reason = 'bad-txns-inputs-missingorspent'; + ret.score = 0; + return false; + } if (coin.value < 0 || coin.value > consensus.MAX_MONEY) { ret.reason = 'bad-txns-inputvalues-outofrange'; @@ -1898,22 +1904,16 @@ TX.prototype.getPriority = function getPriority(view, height, size) { /** * Calculate the transaction's on-chain value. * @param {CoinView} view - * @param {Number} height * @returns {Number} */ -TX.prototype.getChainValue = function getChainValue(view, height) { +TX.prototype.getChainValue = function getChainValue(view) { var value = 0; var i, input, coin, coinHeight; - assert(typeof height === 'number', 'Must pass in height.'); - if (this.isCoinbase()) return value; - if (height == null) - height = Infinity; - for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; coin = view.getOutput(input); @@ -1926,8 +1926,7 @@ TX.prototype.getChainValue = function getChainValue(view, height) { if (coinHeight === -1) continue; - if (coinHeight <= height) - value += coin.value; + value += coin.value; } return value;