Merge pull request #1264 from fanatid/fix/sortOutputs

fix Transaction.sortOutputs
This commit is contained in:
Braydon Fuller 2015-06-18 11:06:20 -04:00
commit 37b5d3d588
2 changed files with 17 additions and 10 deletions

View File

@ -914,16 +914,18 @@ Transaction.prototype.sortOutputs = function(sortingFunction) {
};
Transaction.prototype._newOutputOrder = function(newOutputs) {
var changeIndex = 0;
var length = this.outputs.length;
while (changeIndex < length && this.outputs[this._changeIndex] !== newOutputs[changeIndex]) {
changeIndex++;
}
if (changeIndex === length) {
var isInvalidSorting = (this.outputs.length !== newOutputs.length ||
_.difference(this.outputs, newOutputs).length !== 0);
if (isInvalidSorting) {
throw new errors.Transaction.InvalidSorting();
}
if (!_.isUndefined(this._changeIndex)) {
var changeOutput = this.outputs[this._changeIndex];
this._changeIndex = _.findIndex(newOutputs, changeOutput);
}
this.outputs = newOutputs;
this._changeIndex = changeIndex;
return this;
};

View File

@ -830,15 +830,20 @@ describe('Transaction', function() {
it('fails if the provided function does not work as expected', function() {
var sorting = function(array) {
return [];
return [array[0], array[1], array[2]];
};
expect(function() {
transaction.sortOutputs(sorting);
}).to.throw(errors.Transaction.InvalidSorting);
});
it('shuffle without change', function() {
var tx = new Transaction(transaction.toObject()).to(toAddress, half);
expect(tx.getChangeOutput()).to.be.null;
expect(function() {
tx.shuffleOutputs();
}).to.not.throw(errors.Transaction.InvalidSorting);
})
});
describe('clearOutputs', function() {