|
|
@ -1348,10 +1348,6 @@ WalletService.prototype._selectTxInputs = function(txp, utxosToExclude, cb) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR = 2; |
|
|
|
var UTXO_SELECTION_MAX_FEE_FACTOR_FOR_SMALL_UTXOS = 2; |
|
|
|
var UTXO_SELECTION_MAX_TX_AMOUNT_VS_FEE_FACTOR_FOR_SMALL_UTXOS = 0.002; |
|
|
|
|
|
|
|
WalletService.prototype._selectTxInputs2 = function(txp, utxosToExclude, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
@ -1388,13 +1384,15 @@ WalletService.prototype._selectTxInputs2 = function(txp, utxosToExclude, cb) { |
|
|
|
console.log('*** [server.js ln1362] ----------------------- select for amount of:', txpAmount); // TODO
|
|
|
|
|
|
|
|
// TODO: fix for when fee is specified instead of feePerKb
|
|
|
|
var baseTxpFee = txp.getEstimatedSize() * txp.feePerKb / 1000.; |
|
|
|
var feePerInput = txp.getEstimatedSizeForSingleInput() * txp.feePerKb / 1000.; |
|
|
|
var baseTxpSize = txp.getEstimatedSize(); |
|
|
|
var baseTxpFee = baseTxpSize * txp.feePerKb / 1000.; |
|
|
|
var sizePerInput = txp.getEstimatedSizeForSingleInput(); |
|
|
|
var feePerInput = sizePerInput * txp.feePerKb / 1000.; |
|
|
|
|
|
|
|
console.log('*** [server.js ln1375] feePerInput:', feePerInput); // TODO
|
|
|
|
|
|
|
|
var partitions = _.partition(utxos, function(utxo) { |
|
|
|
return utxo.satoshis > txpAmount * UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR; |
|
|
|
return utxo.satoshis > txpAmount * Defaults.UTXO_SELECTION_MAX_SINGLE_UTXO_FACTOR; |
|
|
|
}); |
|
|
|
|
|
|
|
var bigInputs = _.sortBy(partitions[0], 'satoshis'); |
|
|
@ -1406,40 +1404,69 @@ WalletService.prototype._selectTxInputs2 = function(txp, utxosToExclude, cb) { |
|
|
|
console.log('*** [server.js ln1386] smallInputs:', _.pluck(smallInputs, 'satoshis')); // TODO
|
|
|
|
|
|
|
|
|
|
|
|
_.each(smallInputs, function(input) { |
|
|
|
_.each(smallInputs, function(input, i) { |
|
|
|
console.log('****************************** [server.js ln1395] INPUT #', i); // TODO
|
|
|
|
|
|
|
|
if (input.satoshis < feePerInput) return false; |
|
|
|
|
|
|
|
var inputAmount = input.satoshis - feePerInput; |
|
|
|
console.log('*** [server.js ln1398] inputAmount:', inputAmount); // TODO
|
|
|
|
|
|
|
|
selected.push(input); |
|
|
|
|
|
|
|
var txpFee = baseTxpFee + (feePerInput * selected.length); |
|
|
|
var txpSize = baseTxpSize + selected.length * sizePerInput; |
|
|
|
var txpFee = baseTxpFee + selected.length * feePerInput; |
|
|
|
|
|
|
|
var amountVsFeeRatio = txpFee / txpAmount; |
|
|
|
var singleInputFeeVsFeeRatio = txpFee / (baseTxpFee + feePerInput); |
|
|
|
var amountVsUtxoRatio = inputAmount / txpAmount; |
|
|
|
|
|
|
|
console.log('*** [server.js ln1402] txpFee, amountVsFeeRatio, singleInputFeeVsFeeRatio:', txpFee, amountVsFeeRatio, singleInputFeeVsFeeRatio); // TODO
|
|
|
|
console.log('*** [server.js ln1402] txpSize, txpFee', txpSize, txpFee); // TODO
|
|
|
|
console.log('*** [server.js ln1403] amountVsFeeRatio, singleInputFeeVsFeeRatio, amountVsUtxoRatio:', amountVsFeeRatio, singleInputFeeVsFeeRatio, amountVsUtxoRatio); // TODO
|
|
|
|
|
|
|
|
if (!_.isEmpty(bigInputs)) { |
|
|
|
if ((amountVsFeeRatio > Defaults.UTXO_SELECTION_MAX_TX_AMOUNT_VS_FEE_FACTOR && |
|
|
|
singleInputFeeVsFeeRatio > Defaults.UTXO_SELECTION_MAX_FEE_VS_SINGLE_UTXO_FEE_FACTOR)) { |
|
|
|
console.log('*** [server.js ln1413] breaking because fee exceeded fee/amount ratio and fee is too expensive compared to single input tx'); // TODO
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (amountVsFeeRatio > UTXO_SELECTION_MAX_TX_AMOUNT_VS_FEE_FACTOR_FOR_SMALL_UTXOS && |
|
|
|
(_.isEmpty(bigInputs) || (singleInputFeeVsFeeRatio > UTXO_SELECTION_MAX_FEE_FACTOR_FOR_SMALL_UTXOS))) |
|
|
|
return false; |
|
|
|
if (amountVsUtxoRatio < Defaults.UTXO_SELECTION_MIN_TX_AMOUNT_VS_UTXO_FACTOR) { |
|
|
|
console.log('*** [server.js ln1418] breaking because utxo is too small compared to tx amount'); // TODO
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (txpSize / 1000. > Defaults.MAX_TX_SIZE_IN_KB) { |
|
|
|
console.log('*** [server.js ln1423] breaking because tx size is too big'); // TODO
|
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
console.log('*** [server.js ln1380] input:', input.satoshis, ' aporta:', input.satoshis - feePerInput); // TODO
|
|
|
|
|
|
|
|
total += input.satoshis - feePerInput; |
|
|
|
total += inputAmount; |
|
|
|
console.log('*** [server.js ln1421] total:', total); // TODO
|
|
|
|
|
|
|
|
if (total >= txpAmount) return false; |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('*** [server.js ln1400] total, txpAmount:', total, txpAmount); // TODO
|
|
|
|
|
|
|
|
if (total < txpAmount) { |
|
|
|
console.log('*** [server.js ln1401] no alcanzó:'); // TODO
|
|
|
|
console.log('*** [server.js ln1401] could not reach txp total, still missing:', txpAmount - total); // TODO
|
|
|
|
|
|
|
|
selected = []; |
|
|
|
if (!_.isEmpty(bigInputs)) { |
|
|
|
console.log('*** [server.js ln1405] pero hay bigInputs!:', _.first(bigInputs).satoshis); // TODO
|
|
|
|
|
|
|
|
selected = [_.first(bigInputs)]; |
|
|
|
console.log('*** [server.js ln1405] using big inputs!:', _.first(bigInputs).satoshis); // TODO
|
|
|
|
var input = _.first(bigInputs); |
|
|
|
total = input.satoshis; |
|
|
|
selected = [input]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!_.isEmpty(selected)) { |
|
|
|
console.log('*** [server.js ln1400] SUCCESS! locked overhead:', total - txpAmount, ' ratio to txp:', (total - txpAmount) / txpAmount); // TODO
|
|
|
|
} |
|
|
|
|
|
|
|
return selected; |
|
|
|
}; |
|
|
|
|
|
|
|