Revert "mtx: allow multiple indicies for subtractIndex."

This reverts commit d489238711.
This commit is contained in:
Christopher Jeffrey 2018-03-29 21:55:36 -07:00
parent d489238711
commit 31651ce76e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 49 additions and 50 deletions

View File

@ -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) {

View File

@ -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: []
};