From 9fd3bbb827290c608a509f44af06d550fe631ac5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 16 Apr 2016 20:04:38 -0700 Subject: [PATCH] improve coin selection. --- lib/bcoin/mtx.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 6c158ecb..f3a793ec 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -1075,12 +1075,6 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { // Null the inputs if there are any. tx.inputs.length = 0; - if (options.confirmed) { - coins = coins.filter(function(coin) { - return coin.height !== -1; - }); - } - if (!options.selection || options.selection === 'age') { // Oldest unspents first coins = coins.slice().sort(function(a, b) { @@ -1106,12 +1100,24 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { } function addCoins() { + var coin; + while (index < coins.length) { + coin = coins[index]; + + if (options.confirmed && coin.height === -1) + continue; + + if (network.height !== -1 && coin.coinbase) { + if (network.height + 1 < coin.height + constants.tx.coinbaseMaturity) + continue; + } + // Add new inputs until TX will have enough // funds to cover both minimum post cost // and fee. - tx.addInput(coins[index]); - chosen.push(coins[index]); + tx.addInput(coin); + chosen.push(coin); index++; if (options.selection === 'all')