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) { Transaction.prototype._newOutputOrder = function(newOutputs) {
var changeIndex = 0; var isInvalidSorting = (this.outputs.length !== newOutputs.length ||
var length = this.outputs.length; _.difference(this.outputs, newOutputs).length !== 0);
while (changeIndex < length && this.outputs[this._changeIndex] !== newOutputs[changeIndex]) { if (isInvalidSorting) {
changeIndex++;
}
if (changeIndex === length) {
throw new errors.Transaction.InvalidSorting(); throw new errors.Transaction.InvalidSorting();
} }
if (!_.isUndefined(this._changeIndex)) {
var changeOutput = this.outputs[this._changeIndex];
this._changeIndex = _.findIndex(newOutputs, changeOutput);
}
this.outputs = newOutputs; this.outputs = newOutputs;
this._changeIndex = changeIndex;
return this; return this;
}; };

View File

@ -812,15 +812,20 @@ describe('Transaction', function() {
it('fails if the provided function does not work as expected', function() { it('fails if the provided function does not work as expected', function() {
var sorting = function(array) { var sorting = function(array) {
return []; return [array[0], array[1], array[2]];
}; };
expect(function() { expect(function() {
transaction.sortOutputs(sorting); transaction.sortOutputs(sorting);
}).to.throw(errors.Transaction.InvalidSorting); }).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() { describe('clearOutputs', function() {