Call getUnspentValue() only once in getSerializationError().

This commit is contained in:
David de Kloet 2015-05-21 19:15:06 +02:00 committed by Braydon Fuller
parent 3ace170ac5
commit 0b6eaf0f1e
1 changed files with 17 additions and 20 deletions

View File

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