From c3ee9e9b93a8164b5eacacb63173ccb264f32ef3 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 8 Mar 2016 15:47:31 -0300 Subject: [PATCH] shuffle inputs --- lib/server.js | 2 +- test/integration/server.js | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/server.js b/lib/server.js index 67b0824..76bad80 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1496,7 +1496,7 @@ WalletService.prototype._selectTxInputs = function(txp, utxosToExclude, cb) { if (err) return cb(err); if (selectionError || _.isEmpty(inputs)) return cb(selectionError || new Error('Could not select tx inputs')); - txp.setInputs(inputs); + txp.setInputs(_.shuffle(inputs)); txp.fee = fee; var err = self._checkTx(txp); diff --git a/test/integration/server.js b/test/integration/server.js index 5eac473..6736d92 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -3168,7 +3168,7 @@ describe('Wallet service', function() { }); }); - describe.only('UTXO Selection', function() { + describe('UTXO Selection', function() { var server, wallet; beforeEach(function(done) { // log.level = 'debug'; @@ -3201,6 +3201,29 @@ describe('Wallet service', function() { }); }); }); + it('should return inputs in random order', function(done) { + // NOTE: this test has a chance of failing of 1 in 1'073'741'824 :P + helpers.stubUtxos(server, wallet, _.range(1, 31), function(utxos) { + var txOpts = { + outputs: [{ + toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', + amount: _.sum(utxos, 'satoshis') - 0.5e8, + }], + feePerKb: 100e2, + }; + server.createTx(txOpts, function(err, txp) { + should.not.exist(err); + should.exist(txp); + var amounts = _.pluck(txp.inputs, 'satoshis'); + amounts.length.should.equal(30); + _.all(amounts, function(amount, i) { + if (i == 0) return true; + return amount < amounts[i - 1]; + }).should.be.false; + done(); + }); + }); + }); it('should select a confirmed utxos if within thresholds relative to tx amount', function(done) { helpers.stubUtxos(server, wallet, [1, 'u 350bit', '100bit', '100bit', '100bit'], function() { var txOpts = { @@ -3492,7 +3515,6 @@ describe('Wallet service', function() { }; server.createTx(txOpts, function(err, txp) { should.not.exist(err); - txp.inputs[0].satoshis.should.equal(200e2); (_.sum(txp.inputs, 'satoshis') - txp.outputs[0].amount - txp.fee).should.equal(0); done(); });