Browse Source

Add error if shuffle function doesnt return an expected result

patch-2
eordano 10 years ago
parent
commit
294ff097a1
  1. 3
      lib/errors/spec.js
  2. 6
      lib/transaction/transaction.js
  3. 10
      test/transaction/transaction.js

3
lib/errors/spec.js

@ -63,6 +63,9 @@ module.exports = [{
}, { }, {
name: 'NeedMoreInfo', name: 'NeedMoreInfo',
message: '{0}' message: '{0}'
}, {
name: 'InvalidSorting',
message: 'The sorting function provided did not return the change output as one of the array elements'
}, { }, {
name: 'InvalidOutputAmountSum', name: 'InvalidOutputAmountSum',
message: '{0}' message: '{0}'

6
lib/transaction/transaction.js

@ -831,9 +831,13 @@ Transaction.prototype.sortOutputs = function(sortingFunction) {
Transaction.prototype._newOutputOrder = function(newOutputs) { Transaction.prototype._newOutputOrder = function(newOutputs) {
var changeIndex = 0; var changeIndex = 0;
while (this.outputs[this._changeIndex] !== newOutputs[changeIndex]) { var length = this.outputs.length;
while (changeIndex < length && this.outputs[this._changeIndex] !== newOutputs[changeIndex]) {
changeIndex++; changeIndex++;
} }
if (changeIndex === length) {
throw new errors.Transaction.InvalidSorting();
}
this.outputs = newOutputs; this.outputs = newOutputs;
this._changeIndex = changeIndex; this._changeIndex = changeIndex;
return this; return this;

10
test/transaction/transaction.js

@ -665,6 +665,16 @@ describe('Transaction', function() {
_.shuffle.restore(); _.shuffle.restore();
}); });
it('fails if the provided function does not work as expected', function() {
var sorting = function(array) {
return [];
};
expect(function() {
transaction.sortOutputs(sorting);
}).to.throw(errors.Transaction.InvalidSorting);
});
}); });
}); });

Loading…
Cancel
Save