Browse Source

Call getUnspentValue() only once in getSerializationError().

patch-2
David de Kloet 10 years ago
committed by Braydon Fuller
parent
commit
0b6eaf0f1e
  1. 37
      lib/transaction/transaction.js

37
lib/transaction/transaction.js

@ -191,9 +191,10 @@ Transaction.prototype.invalidSatoshis = function() {
Transaction.prototype.getSerializationError = function(opts) { Transaction.prototype.getSerializationError = function(opts) {
opts = opts || {}; opts = opts || {};
var unspent = this._getUnspentValue();
return this._isInvalidSatoshis() || return this._isInvalidSatoshis() ||
this._hasMoreOutputThanInput(opts) || this._hasMoreOutputThanInput(opts, unspent) ||
this._hasFeeError(opts) || this._hasFeeError(opts, unspent) ||
this._hasDustOutputs(opts) || this._hasDustOutputs(opts) ||
this._isMissingSignatures(opts); this._isMissingSignatures(opts);
}; };
@ -204,31 +205,28 @@ Transaction.prototype._isInvalidSatoshis = function() {
} }
}; };
Transaction.prototype._hasFeeError = function(opts) { Transaction.prototype._hasFeeError = function(opts, unspent) {
if (this._getUnspentValue() < 0) { if (unspent < 0) {
// The concept of a fee is meaningless when the unspent output value is negative. // The concept of a fee is meaningless when the unspent output value is negative.
return; return;
} }
return this._isFeeDifferent() || return this._isFeeDifferent(unspent) ||
this._isFeeTooLarge(opts) || this._isFeeTooLarge(opts, unspent) ||
this._isFeeTooSmall(opts); this._isFeeTooSmall(opts, unspent);
}; };
Transaction.prototype._isFeeDifferent = function() { Transaction.prototype._isFeeDifferent = function(unspent) {
if (!_.isUndefined(this._fee)) { if (!_.isUndefined(this._fee) && this._fee !== unspent) {
var fee = this._fee; return new errors.Transaction.FeeError.Different(
var unspent = this._getUnspentValue(); 'Unspent value is ' + unspent + ' but specified fee is ' + this._fee
if (fee !== unspent) { );
return new errors.Transaction.FeeError.Different('Unspent value is ' + unspent + ' but specified fee is ' + fee);
}
} }
}; };
Transaction.prototype._isFeeTooLarge = function(opts) { Transaction.prototype._isFeeTooLarge = function(opts, fee) {
if (opts.disableLargeFees) { if (opts.disableLargeFees) {
return; return;
} }
var fee = this._getUnspentValue();
var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee()); var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee());
if (fee > maximumFee) { if (fee > maximumFee) {
if (this._missingChange()) { if (this._missingChange()) {
@ -238,11 +236,10 @@ Transaction.prototype._isFeeTooLarge = function(opts) {
} }
}; };
Transaction.prototype._isFeeTooSmall = function(opts) { Transaction.prototype._isFeeTooSmall = function(opts, fee) {
if (opts.disableSmallFees) { if (opts.disableSmallFees) {
return; return;
} }
var fee = this._getUnspentValue();
var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN); var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN);
if (fee < minimumFee) { if (fee < minimumFee) {
return new errors.Transaction.FeeError.TooSmall('expected more than ' + minimumFee + ' but got ' + fee); return new errors.Transaction.FeeError.TooSmall('expected more than ' + minimumFee + ' but got ' + fee);
@ -275,11 +272,11 @@ Transaction.prototype._isMissingSignatures = function(opts) {
} }
}; };
Transaction.prototype._hasMoreOutputThanInput = function(opts) { Transaction.prototype._hasMoreOutputThanInput = function(opts, unspent) {
if (opts.disableMoreOutputThanInput) { if (opts.disableMoreOutputThanInput) {
return; return;
} }
if (this._getUnspentValue() < 0) { if (unspent < 0) {
return new errors.Transaction.InvalidOutputAmountSum(); return new errors.Transaction.InvalidOutputAmountSum();
} }
}; };

Loading…
Cancel
Save