From 695305cf68b422168d992dae47602bf1028b2023 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 8 Mar 2017 07:40:36 -0800 Subject: [PATCH] mtx: add by-value sorting. --- lib/primitives/mtx.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index 6a0a9a26..7928d779 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -1448,7 +1448,7 @@ function CoinSelector(tx, options) { this.change = 0; this.fee = CoinSelector.MIN_FEE; - this.selection = 'age'; + this.selection = 'value'; this.shouldSubtract = false; this.subtractFee = null; this.height = -1; @@ -1600,6 +1600,9 @@ CoinSelector.prototype.init = function init(coins) { case 'age': this.coins.sort(sortAge); break; + case 'value': + this.coins.sort(sortValue); + break; default: throw new FundingError('Bad selection type: ' + this.selection); } @@ -1839,6 +1842,14 @@ function sortRandom(a, b) { return Math.random() > 0.5 ? 1 : -1; } +function sortValue(a, b) { + if (a.height === -1 && b.height !== -1) + return 1; + if (a.height !== -1 && b.height === -1) + return -1; + return b.value - a.value; +} + function sortInputs(a, b) { var ahash = a.prevout.txid(); var bhash = b.prevout.txid();