diff --git a/lib/server.js b/lib/server.js index dc6e2e4..81acde4 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1410,7 +1410,7 @@ WalletService.prototype._selectTxInputs2 = function(txp, utxosToExclude, cb) { return false; } - var bigInputThreshold = txpAmount * Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR; + var bigInputThreshold = txpAmount * Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR + (baseTxpFee + feePerInput); log.debug('Big input threshold ' + formatAmount(bigInputThreshold)); var partitions = _.partition(utxos, function(utxo) { @@ -1562,8 +1562,14 @@ WalletService.prototype._selectTxInputs2 = function(txp, utxosToExclude, cb) { if (txp.getEstimatedSize() / 1000 > Defaults.MAX_TX_SIZE_IN_KB) return cb(Errors.TX_MAX_SIZE_EXCEEDED); - var bitcoreError = self._checkTxAndEstimateFee(txp); - return cb(bitcoreError); + var err = self._checkTxAndEstimateFee(txp); + if (!err) { + log.debug('Successfully built transaction. Total fees: ', formatAmount(txp.fee)); + } else { + log.warn('Error building transaction', err); + } + + return cb(err); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index bc1c2b2..2dded8c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -5760,7 +5760,6 @@ describe('Wallet service', function() { }); }); }); - it('should select smaller utxos if within fee constraints', function(done) { helpers.stubUtxos(server, wallet, [1, '800bit', '800bit', '800bit'], function() { var txOpts = { @@ -5799,6 +5798,30 @@ describe('Wallet service', function() { }); }); }); + it('should account for fee when selecting smallest big utxo', function(done) { + // log.level = 'debug'; + var _old = Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR; + Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR = 2; + // The 605 bits input cannot be selected even if it is > 2 * tx amount + // because it cannot cover for fee on its own. + helpers.stubUtxos(server, wallet, [1, '605bit', '100bit', '100bit', '100bit'], function() { + var txOpts = { + outputs: [{ + toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', + amount: 300e2, + }], + feePerKb: 1200e2, + }; + server.createTx(txOpts, function(err, txp) { + should.not.exist(err); + should.exist(txp); + txp.inputs.length.should.equal(1); + txp.inputs[0].satoshis.should.equal(1e8); + Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR = _old; + done(); + }); + }); + }); it('should select smallest big utxo if small utxos exceed maximum fee', function(done) { helpers.stubUtxos(server, wallet, [3, 1, 2].concat(_.times(20, function() { return '1000bit'; @@ -5958,7 +5981,6 @@ describe('Wallet service', function() { }); }); }); - }); });