Browse Source

Refactored transaction.getSerializationError to be more concise.

- _hasMoreOutputThanInput() and _isInvalidSatoshis() merged with getSerializationError()
- _isFeeDifferent(), _isFeeTooLarge() and _isFeeTooSmall merged with _hasFeeError()
patch-2
Braydon Fuller 10 years ago
parent
commit
589d017a14
  1. 75
      lib/transaction/transaction.js

75
lib/transaction/transaction.js

@ -191,58 +191,54 @@ 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() ||
this._hasMoreOutputThanInput(opts, unspent) ||
this._hasFeeError(opts, unspent) ||
this._hasDustOutputs(opts) ||
this._isMissingSignatures(opts);
};
Transaction.prototype._isInvalidSatoshis = function() {
if (this.invalidSatoshis()) { if (this.invalidSatoshis()) {
return new errors.Transaction.InvalidSatoshis(); return new errors.Transaction.InvalidSatoshis();
} }
};
Transaction.prototype._hasFeeError = function(opts, unspent) { var unspent = this._getUnspentValue();
var unspentError;
if (unspent < 0) { if (unspent < 0) {
// The concept of a fee is meaningless when the unspent output value is negative. if (!opts.disableMoreOutputThanInput) {
return; unspentError = new errors.Transaction.InvalidOutputAmountSum();
}
} else {
unspentError = this._hasFeeError(opts, unspent);
} }
return this._isFeeDifferent(unspent) ||
this._isFeeTooLarge(opts, unspent) || return unspentError ||
this._isFeeTooSmall(opts, unspent); this._hasDustOutputs(opts) ||
this._isMissingSignatures(opts);
}; };
Transaction.prototype._isFeeDifferent = function(unspent) { Transaction.prototype._hasFeeError = function(opts, unspent) {
if (!_.isUndefined(this._fee) && this._fee !== unspent) { if (!_.isUndefined(this._fee) && this._fee !== unspent) {
return new errors.Transaction.FeeError.Different( return new errors.Transaction.FeeError.Different(
'Unspent value is ' + unspent + ' but specified fee is ' + this._fee 'Unspent value is ' + unspent + ' but specified fee is ' + this._fee
); );
} }
};
Transaction.prototype._isFeeTooLarge = function(opts, fee) { if (!opts.disableLargeFees) {
if (opts.disableLargeFees) { var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee());
return; if (unspent > maximumFee) {
} if (this._missingChange()) {
var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee()); return new errors.Transaction.ChangeAddressMissing(
if (fee > maximumFee) { 'Fee is too large and no change address was provided'
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) {
if (opts.disableSmallFees) { var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN);
return; if (unspent < minimumFee) {
} return new errors.Transaction.FeeError.TooSmall(
var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN); 'expected more than ' + minimumFee + ' but got ' + unspent
if (fee < minimumFee) { );
return new errors.Transaction.FeeError.TooSmall('expected more than ' + minimumFee + ' but got ' + fee); }
} }
}; };
@ -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() { Transaction.prototype.inspect = function() {
return '<Transaction: ' + this.uncheckedSerialize() + '>'; return '<Transaction: ' + this.uncheckedSerialize() + '>';
}; };

Loading…
Cancel
Save