coin selection.

This commit is contained in:
Christopher Jeffrey 2016-03-31 04:56:16 -07:00
parent 399052d95a
commit e907f35267

View File

@ -786,15 +786,12 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) {
};
MTX.prototype.selectCoins = function selectCoins(coins, options) {
var dustThreshold = constants.tx.dustThreshold;
var tx = this.clone();
var outputValue = tx.getOutputValue();
var totalkb = 1;
var chosen = [];
var lastAdded = 0;
var minFee = constants.tx.minFee;
var dustThreshold = constants.tx.dustThreshold;
var i, size, newkb, change;
var fee;
var i, size, change, fee;
assert(tx.inputs.length === 0);
@ -856,7 +853,7 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
// Transfer `total` funds maximum.
addCoins();
} else {
fee = new bn(minFee);
fee = new bn(constants.tx.minFee);
// Transfer `total` funds maximum.
addCoins();
@ -875,20 +872,17 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
size = tx.maxSize(options.m, options.n);
if (options.free) {
if (tx.isFree(null, size)) {
if (tx.isFree(network.height + 1, size)) {
fee = new bn(0);
break;
}
options.free = false;
}
if (options.accurate) {
if (options.accurate)
fee = tx.getMinFee(size);
} else {
newkb = Math.ceil(size / 1024) - totalkb;
fee.iaddn(newkb * minFee);
totalkb += newkb;
}
else
fee = tx.getMaxFee(size);
// Failed to get enough funds, add more inputs.
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 {
coins: chosen,
change: change,
fee: fee,
total: total(),
kb: totalkb
total: total()
};
};
@ -952,12 +951,6 @@ MTX.prototype.fill = function fill(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) {
self.addInput(coin);
});