refactor coin selection.

This commit is contained in:
Christopher Jeffrey 2016-02-24 04:05:16 -08:00
parent c312905585
commit 27f6e6e678
3 changed files with 21 additions and 23 deletions

View File

@ -960,7 +960,7 @@ TX.prototype.maxSize = function maxSize(maxM, maxN) {
return total; return total;
}; };
TX.prototype.getInputs = function getInputs(unspent, options) { TX.prototype.selectCoins = function selectCoins(unspent, options) {
var self = this; var self = this;
var tx = this.clone(); var tx = this.clone();
var outputValue = tx.getOutputValue(); var outputValue = tx.getOutputValue();
@ -1088,17 +1088,16 @@ TX.prototype.getInputs = function getInputs(unspent, options) {
// Return necessary inputs and change. // Return necessary inputs and change.
return { return {
inputs: chosen, coins: chosen,
change: change, change: change,
fee: fee, fee: fee,
total: total(), total: total(),
kb: totalkb, kb: totalkb
unspent: unspent.slice(0, lastAdded)
}; };
}; };
TX.prototype.fill = function fill(unspent, options) { TX.prototype.fill = function fill(unspent, options) {
var result; var result, err;
if (!options || typeof options !== 'object') { if (!options || typeof options !== 'object') {
options = { options = {
@ -1110,15 +1109,16 @@ TX.prototype.fill = function fill(unspent, options) {
assert(unspent); assert(unspent);
assert(options.changeAddress); assert(options.changeAddress);
result = this.getInputs(unspent, options); result = this.selectCoins(unspent, options);
this.requiredFunds = result.total; if (!result.coins) {
err = new Error('Could not fill transaction');
err.requiredFunds = result.total;
throw err;
}
if (!result.inputs) result.coins.forEach(function(coin) {
return result; this.addInput(coin);
result.inputs.forEach(function(input) {
this.addInput(input);
}, this); }, this);
if (result.change.cmpn(constants.tx.dustThreshold) < 0) { if (result.change.cmpn(constants.tx.dustThreshold) < 0) {
@ -1735,7 +1735,6 @@ TX.prototype.inspect = function inspect() {
copy.block = this.block; copy.block = this.block;
delete copy._raw; delete copy._raw;
delete copy._chain; delete copy._chain;
delete copy.requiredFunds;
copy.hash = this.hash('hex'); copy.hash = this.hash('hex');
copy.rhash = this.rhash; copy.rhash = this.rhash;
copy.rblock = this.rblock; copy.rblock = this.rblock;

View File

@ -508,14 +508,12 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
}; };
Wallet.prototype.fill = function fill(tx, options) { Wallet.prototype.fill = function fill(tx, options) {
var result;
if (!options) if (!options)
options = {}; options = {};
assert(this._initialized); assert(this._initialized);
result = tx.fill(this.getUnspent(), { tx.fill(this.getUnspent(), {
// wallet: this, // wallet: this,
fee: options.fee, fee: options.fee,
subtractFee: options.subtractFee, subtractFee: options.subtractFee,
@ -524,9 +522,6 @@ Wallet.prototype.fill = function fill(tx, options) {
n: this.n n: this.n
}); });
if (!result.inputs)
return false;
return true; return true;
}; };
@ -554,8 +549,7 @@ Wallet.prototype.createTX = function createTX(outputs, options) {
}); });
// Fill the inputs with unspents // Fill the inputs with unspents
if (!this.fill(tx, options)) this.fill(tx, options);
return;
// Sort members a la BIP69 // Sort members a la BIP69
tx.sortMembers(); tx.sortMembers();

View File

@ -173,8 +173,13 @@ describe('Wallet', function() {
// Create new transaction // Create new transaction
var t3 = bcoin.tx().out(w2, 15000); var t3 = bcoin.tx().out(w2, 15000);
assert(!w1.fill(t3)); try {
assert.equal(t3.requiredFunds.toString(10), 25000); w1.fill(t3);
} catch (e) {
var err = e;
}
assert(err);
assert.equal(err.requiredFunds.toString(10), 25000);
cb(); cb();
}); });