From 31651ce76e53e88653bccfd9800b181e1aae91f3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 29 Mar 2018 21:55:36 -0700 Subject: [PATCH] Revert "mtx: allow multiple indicies for subtractIndex." This reverts commit d489238711ab69c802412ee15177b76733f62913. --- lib/primitives/mtx.js | 95 +++++++++++++++++++++---------------------- lib/wallet/http.js | 4 +- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index fccc29ae..6dfe8a46 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -1223,27 +1223,41 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { }; /** - * Attempt to subtract a fee from all outputs evenly. + * Attempt to subtract a fee from a single output. + * @param {Number} index * @param {Amount} fee - * @param {Set|null} set */ -MTX.prototype.subtractFee = function subtractFee(fee, set) { +MTX.prototype.subtractIndex = function subtractIndex(index, fee) { + assert(typeof index === 'number'); + assert(typeof fee === 'number'); + + const output = this.outputs[index]; + + if (!output) + throw new Error('Subtraction index does not exist.'); + + if (output.value < fee + output.getDustThreshold()) + throw new Error('Could not subtract fee.'); + + output.value -= fee; +}; + +/** + * Attempt to subtract a fee from all outputs evenly. + * @param {Amount} fee + */ + +MTX.prototype.subtractFee = function subtractFee(fee) { assert(typeof fee === 'number'); let outputs = 0; - for (let i = 0; i < this.outputs.length; i++) { - const output = this.outputs[i]; - - if (set && !set.has(i)) - continue; - + for (const output of this.outputs) { // Ignore nulldatas and // other OP_RETURN scripts. if (output.script.isUnspendable()) continue; - outputs += 1; } @@ -1254,12 +1268,7 @@ MTX.prototype.subtractFee = function subtractFee(fee, set) { const share = (fee - left) / outputs; // First pass, remove even shares. - for (let i = 0; i < this.outputs.length; i++) { - const output = this.outputs[i]; - - if (set && !set.has(i)) - continue; - + for (const output of this.outputs) { if (output.script.isUnspendable()) continue; @@ -1271,12 +1280,7 @@ MTX.prototype.subtractFee = function subtractFee(fee, set) { // Second pass, remove the remainder // for the one unlucky output. - for (let i = 0; i < this.outputs.length; i++) { - const output = this.outputs[i]; - - if (set && !set.has(i)) - continue; - + for (const output of this.outputs) { if (output.script.isUnspendable()) continue; @@ -1309,8 +1313,13 @@ MTX.prototype.fund = async function fund(coins, options) { this.addCoin(coin); // Attempt to subtract fee. - if (select.subtractFee) - this.subtractFee(select.fee, select.subtractIndex); + if (select.subtractFee) { + const index = select.subtractIndex; + if (index !== -1) + this.subtractIndex(index, select.fee); + else + this.subtractFee(select.fee); + } // Add a change output. const output = new Output(); @@ -1545,7 +1554,7 @@ function CoinSelector(tx, options) { this.selection = 'value'; this.subtractFee = false; - this.subtractIndex = null; + this.subtractIndex = -1; this.height = -1; this.depth = -1; this.hardFee = -1; @@ -1601,32 +1610,22 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) { } if (options.subtractFee != null) { - assert(typeof options.subtractFee === 'boolean'); - this.subtractFee = options.subtractFee; + if (typeof options.subtractFee === 'number') { + assert(util.isInt(options.subtractFee)); + assert(options.subtractFee >= -1); + this.subtractIndex = options.subtractFee; + this.subtractFee = this.subtractIndex !== -1; + } else { + assert(typeof options.subtractFee === 'boolean'); + this.subtractFee = options.subtractFee; + } } if (options.subtractIndex != null) { - let indicies = null; - - if (typeof options.subtractIndex === 'number') { - indicies = [options.subtractIndex]; - } else { - assert(Array.isArray(options.subtractIndex)); - indicies = options.subtractIndex; - } - - if (indicies.length > 0) { - const set = new Set(); - - for (const index of indicies) { - assert(util.isU32(index)); - assert(index < this.tx.outputs.length); - set.add(index); - } - - this.subtractIndex = set; - this.subtractFee = true; - } + assert(util.isInt(options.subtractIndex)); + assert(options.subtractIndex >= -1); + this.subtractIndex = options.subtractIndex; + this.subtractFee = this.subtractIndex !== -1; } if (options.height != null) { diff --git a/lib/wallet/http.js b/lib/wallet/http.js index 5aedb857..b70d2ee7 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -391,7 +391,7 @@ HTTPServer.prototype.initRouter = function initRouter() { selection: valid.str('selection'), smart: valid.bool('smart'), subtractFee: valid.bool('subtractFee'), - subtractIndex: valid.get('subtractIndex'), + subtractIndex: valid.i32('subtractIndex'), depth: valid.u32(['confirmations', 'depth']), outputs: [] }; @@ -431,7 +431,7 @@ HTTPServer.prototype.initRouter = function initRouter() { selection: valid.str('selection'), smart: valid.bool('smart'), subtractFee: valid.bool('subtractFee'), - subtractIndex: valid.get('subtractIndex'), + subtractIndex: valid.i32('subtractIndex'), depth: valid.u32(['confirmations', 'depth']), outputs: [] };