Add error if shuffle function doesnt return an expected result

This commit is contained in:
eordano 2015-03-11 15:49:42 -03:00
parent a37e7b140c
commit 294ff097a1
3 changed files with 18 additions and 1 deletions

View File

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

View File

@ -831,9 +831,13 @@ Transaction.prototype.sortOutputs = function(sortingFunction) {
Transaction.prototype._newOutputOrder = function(newOutputs) {
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++;
}
if (changeIndex === length) {
throw new errors.Transaction.InvalidSorting();
}
this.outputs = newOutputs;
this._changeIndex = changeIndex;
return this;

View File

@ -665,6 +665,16 @@ describe('Transaction', function() {
_.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);
});
});
});