coin selection.
This commit is contained in:
parent
399052d95a
commit
e907f35267
@ -786,15 +786,12 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
||||||
|
var dustThreshold = constants.tx.dustThreshold;
|
||||||
var tx = this.clone();
|
var tx = this.clone();
|
||||||
var outputValue = tx.getOutputValue();
|
var outputValue = tx.getOutputValue();
|
||||||
var totalkb = 1;
|
|
||||||
var chosen = [];
|
var chosen = [];
|
||||||
var lastAdded = 0;
|
var lastAdded = 0;
|
||||||
var minFee = constants.tx.minFee;
|
var i, size, change, fee;
|
||||||
var dustThreshold = constants.tx.dustThreshold;
|
|
||||||
var i, size, newkb, change;
|
|
||||||
var fee;
|
|
||||||
|
|
||||||
assert(tx.inputs.length === 0);
|
assert(tx.inputs.length === 0);
|
||||||
|
|
||||||
@ -856,7 +853,7 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
// Transfer `total` funds maximum.
|
// Transfer `total` funds maximum.
|
||||||
addCoins();
|
addCoins();
|
||||||
} else {
|
} else {
|
||||||
fee = new bn(minFee);
|
fee = new bn(constants.tx.minFee);
|
||||||
|
|
||||||
// Transfer `total` funds maximum.
|
// Transfer `total` funds maximum.
|
||||||
addCoins();
|
addCoins();
|
||||||
@ -875,20 +872,17 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
size = tx.maxSize(options.m, options.n);
|
size = tx.maxSize(options.m, options.n);
|
||||||
|
|
||||||
if (options.free) {
|
if (options.free) {
|
||||||
if (tx.isFree(null, size)) {
|
if (tx.isFree(network.height + 1, size)) {
|
||||||
fee = new bn(0);
|
fee = new bn(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
options.free = false;
|
options.free = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.accurate) {
|
if (options.accurate)
|
||||||
fee = tx.getMinFee(size);
|
fee = tx.getMinFee(size);
|
||||||
} else {
|
else
|
||||||
newkb = Math.ceil(size / 1024) - totalkb;
|
fee = tx.getMaxFee(size);
|
||||||
fee.iaddn(newkb * minFee);
|
|
||||||
totalkb += newkb;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Failed to get enough funds, add more inputs.
|
// Failed to get enough funds, add more inputs.
|
||||||
if (!isFull())
|
if (!isFull())
|
||||||
@ -926,13 +920,18 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!chosen) {
|
||||||
|
err = new Error('Could not select coins.');
|
||||||
|
err.requiredFunds = total();
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
// Return necessary inputs and change.
|
// Return necessary inputs and change.
|
||||||
return {
|
return {
|
||||||
coins: chosen,
|
coins: chosen,
|
||||||
change: change,
|
change: change,
|
||||||
fee: fee,
|
fee: fee,
|
||||||
total: total(),
|
total: total()
|
||||||
kb: totalkb
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -952,12 +951,6 @@ MTX.prototype.fill = function fill(coins, options) {
|
|||||||
|
|
||||||
result = this.selectCoins(coins, options);
|
result = this.selectCoins(coins, options);
|
||||||
|
|
||||||
if (!result.coins) {
|
|
||||||
err = new Error('Could not fill transaction.');
|
|
||||||
err.requiredFunds = result.total;
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.coins.forEach(function(coin) {
|
result.coins.forEach(function(coin) {
|
||||||
self.addInput(coin);
|
self.addInput(coin);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user