Browse Source

fix big input threshold computation

activeAddress
Ivan Socolsky 9 years ago
parent
commit
485b98de86
  1. 12
      lib/server.js
  2. 26
      test/integration/server.js

12
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);
});
};

26
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() {
});
});
});
});
});

Loading…
Cancel
Save