fix Transaction.sortOutputs

This commit is contained in:
Kirill Fomichev 2015-06-05 20:53:36 +03:00
parent 49e621f4f2
commit b9d52b79c8
2 changed files with 17 additions and 10 deletions

View File

@ -899,16 +899,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

@ -812,15 +812,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() {