Add error if shuffle function doesnt return an expected result
This commit is contained in:
parent
a37e7b140c
commit
294ff097a1
@ -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}'
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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…
Reference in New Issue
Block a user