From 589d017a14e70b29e6f701a8d17c0205ae9a739e Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Sat, 23 May 2015 19:58:22 -0400 Subject: [PATCH] Refactored transaction.getSerializationError to be more concise. - _hasMoreOutputThanInput() and _isInvalidSatoshis() merged with getSerializationError() - _isFeeDifferent(), _isFeeTooLarge() and _isFeeTooSmall merged with _hasFeeError() --- lib/transaction/transaction.js | 75 ++++++++++++++-------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 693e97f..232dafe 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -191,58 +191,54 @@ Transaction.prototype.invalidSatoshis = function() { Transaction.prototype.getSerializationError = function(opts) { opts = opts || {}; - var unspent = this._getUnspentValue(); - return this._isInvalidSatoshis() || - this._hasMoreOutputThanInput(opts, unspent) || - this._hasFeeError(opts, unspent) || - this._hasDustOutputs(opts) || - this._isMissingSignatures(opts); -}; - -Transaction.prototype._isInvalidSatoshis = function() { if (this.invalidSatoshis()) { return new errors.Transaction.InvalidSatoshis(); } -}; -Transaction.prototype._hasFeeError = function(opts, unspent) { + var unspent = this._getUnspentValue(); + var unspentError; if (unspent < 0) { - // The concept of a fee is meaningless when the unspent output value is negative. - return; + if (!opts.disableMoreOutputThanInput) { + unspentError = new errors.Transaction.InvalidOutputAmountSum(); + } + } else { + unspentError = this._hasFeeError(opts, unspent); } - return this._isFeeDifferent(unspent) || - this._isFeeTooLarge(opts, unspent) || - this._isFeeTooSmall(opts, unspent); + + return unspentError || + this._hasDustOutputs(opts) || + this._isMissingSignatures(opts); }; -Transaction.prototype._isFeeDifferent = function(unspent) { +Transaction.prototype._hasFeeError = function(opts, unspent) { + if (!_.isUndefined(this._fee) && this._fee !== unspent) { return new errors.Transaction.FeeError.Different( 'Unspent value is ' + unspent + ' but specified fee is ' + this._fee ); } -}; -Transaction.prototype._isFeeTooLarge = function(opts, fee) { - if (opts.disableLargeFees) { - return; - } - var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee()); - if (fee > maximumFee) { - if (this._missingChange()) { - return new errors.Transaction.ChangeAddressMissing('Fee is too large and no change address was provided'); + if (!opts.disableLargeFees) { + var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee()); + if (unspent > maximumFee) { + if (this._missingChange()) { + return new errors.Transaction.ChangeAddressMissing( + 'Fee is too large and no change address was provided' + ); + } + return new errors.Transaction.FeeError.TooLarge( + 'expected less than ' + maximumFee + ' but got ' + unspent + ); } - return new errors.Transaction.FeeError.TooLarge('expected less than ' + maximumFee + ' but got ' + fee); } -}; -Transaction.prototype._isFeeTooSmall = function(opts, fee) { - if (opts.disableSmallFees) { - return; - } - var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN); - if (fee < minimumFee) { - return new errors.Transaction.FeeError.TooSmall('expected more than ' + minimumFee + ' but got ' + fee); + if (!opts.disableSmallFees) { + var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN); + if (unspent < minimumFee) { + return new errors.Transaction.FeeError.TooSmall( + 'expected more than ' + minimumFee + ' but got ' + unspent + ); + } } }; @@ -272,15 +268,6 @@ Transaction.prototype._isMissingSignatures = function(opts) { } }; -Transaction.prototype._hasMoreOutputThanInput = function(opts, unspent) { - if (opts.disableMoreOutputThanInput) { - return; - } - if (unspent < 0) { - return new errors.Transaction.InvalidOutputAmountSum(); - } -}; - Transaction.prototype.inspect = function() { return ''; };