From b5ff10e74f95ceaa1d7ac50faa1547388602e4ce Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 29 Dec 2016 23:49:55 -0800 Subject: [PATCH] tx: minor. --- lib/primitives/tx.js | 28 +++++++++++++++------------- lib/utils/util.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 6be7463f..8c5da763 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -93,12 +93,12 @@ TX.prototype.fromOptions = function fromOptions(options) { assert(options, 'TX data is required.'); if (options.version != null) { - assert(util.isNumber(options.version)); + assert(util.isUInt32(options.version)); this.version = options.version; } if (options.flag != null) { - assert(util.isNumber(options.flag)); + assert(util.isUInt8(options.flag)); this.flag = options.flag; } @@ -115,7 +115,7 @@ TX.prototype.fromOptions = function fromOptions(options) { } if (options.locktime != null) { - assert(util.isNumber(options.locktime)); + assert(util.isUInt32(options.locktime)); this.locktime = options.locktime; } @@ -1195,24 +1195,25 @@ TX.prototype.getScripthashSigops = function getScripthashSigops(view) { }; /** - * Calculate sigops weight, taking into account witness programs. + * Calculate sigops cost, taking into account witness programs. * @param {CoinView} view * @param {VerifyFlags?} flags * @returns {Number} sigop weight */ TX.prototype.getSigopsCost = function getSigopsCost(view, flags) { - var weight = this.getLegacySigops() * constants.WITNESS_SCALE_FACTOR; + var scale = constants.WITNESS_SCALE_FACTOR; + var cost = this.getLegacySigops() * scale; var i, input, coin; if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; if (this.isCoinbase()) - return weight; + return cost; if (flags & constants.flags.VERIFY_P2SH) - weight += this.getScripthashSigops(view) * constants.WITNESS_SCALE_FACTOR; + cost += this.getScripthashSigops(view) * scale; for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; @@ -1221,14 +1222,14 @@ TX.prototype.getSigopsCost = function getSigopsCost(view, flags) { if (!coin) continue; - weight += Script.getWitnessSigops( + cost += Script.getWitnessSigops( input.script, coin.script, input.witness, flags); } - return weight; + return cost; }; /** @@ -1746,8 +1747,7 @@ TX.prototype.getModifiedSize = function getModifiedSize(size) { /** * Calculate the transaction priority. * @param {CoinView} view - * @param {Number?} height - If not present, tx height - * or network height will be used. + * @param {Number} height * @param {Number?} size - Size to calculate priority * based on. If not present, virtual size will be used. * @returns {Number} @@ -1789,7 +1789,7 @@ TX.prototype.getPriority = function getPriority(view, height, size) { /** * Calculate the transaction's on-chain value. * @param {CoinView} view - * @param {Number?} height + * @param {Number} height * @returns {Number} */ @@ -1797,6 +1797,8 @@ TX.prototype.getChainValue = function getChainValue(view, height) { var value = 0; var i, input, coin, coinHeight; + assert(typeof height === 'number', 'Must pass in height.'); + if (this.isCoinbase()) return value; @@ -2380,7 +2382,7 @@ TX.prototype.frameWitness = function frameWitness() { /** * Serialize transaction without witness. * @private - * @param {BufferWriter} writer + * @param {BufferWriter} bw * @returns {RawTX} */ diff --git a/lib/utils/util.js b/lib/utils/util.js index 0b7b79cb..39daf1c7 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -315,6 +315,26 @@ util.isInt = function isInt(value) { return util.isNumber(value) && value % 1 === 0; }; +/** + * Test whether an object is an int8. + * @param {Number?} value + * @returns {Boolean} + */ + +util.isInt8 = function isInt8(value) { + return util.isInt(value) && Math.abs(value) <= 0x7f; +}; + +/** + * Test whether an object is a uint8. + * @param {Number?} value + * @returns {Boolean} + */ + +util.isUInt8 = function isUInt8(value) { + return util.isInt(value) && value >= 0 && value <= 0xff; +}; + /** * Test whether an object is an int32. * @param {Number?} value