refactor coin selection.
This commit is contained in:
parent
c312905585
commit
27f6e6e678
@ -960,7 +960,7 @@ TX.prototype.maxSize = function maxSize(maxM, maxN) {
|
||||
return total;
|
||||
};
|
||||
|
||||
TX.prototype.getInputs = function getInputs(unspent, options) {
|
||||
TX.prototype.selectCoins = function selectCoins(unspent, options) {
|
||||
var self = this;
|
||||
var tx = this.clone();
|
||||
var outputValue = tx.getOutputValue();
|
||||
@ -1088,17 +1088,16 @@ TX.prototype.getInputs = function getInputs(unspent, options) {
|
||||
|
||||
// Return necessary inputs and change.
|
||||
return {
|
||||
inputs: chosen,
|
||||
coins: chosen,
|
||||
change: change,
|
||||
fee: fee,
|
||||
total: total(),
|
||||
kb: totalkb,
|
||||
unspent: unspent.slice(0, lastAdded)
|
||||
kb: totalkb
|
||||
};
|
||||
};
|
||||
|
||||
TX.prototype.fill = function fill(unspent, options) {
|
||||
var result;
|
||||
var result, err;
|
||||
|
||||
if (!options || typeof options !== 'object') {
|
||||
options = {
|
||||
@ -1110,15 +1109,16 @@ TX.prototype.fill = function fill(unspent, options) {
|
||||
assert(unspent);
|
||||
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)
|
||||
return result;
|
||||
|
||||
result.inputs.forEach(function(input) {
|
||||
this.addInput(input);
|
||||
result.coins.forEach(function(coin) {
|
||||
this.addInput(coin);
|
||||
}, this);
|
||||
|
||||
if (result.change.cmpn(constants.tx.dustThreshold) < 0) {
|
||||
@ -1735,7 +1735,6 @@ TX.prototype.inspect = function inspect() {
|
||||
copy.block = this.block;
|
||||
delete copy._raw;
|
||||
delete copy._chain;
|
||||
delete copy.requiredFunds;
|
||||
copy.hash = this.hash('hex');
|
||||
copy.rhash = this.rhash;
|
||||
copy.rblock = this.rblock;
|
||||
|
||||
@ -508,14 +508,12 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
|
||||
};
|
||||
|
||||
Wallet.prototype.fill = function fill(tx, options) {
|
||||
var result;
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
assert(this._initialized);
|
||||
|
||||
result = tx.fill(this.getUnspent(), {
|
||||
tx.fill(this.getUnspent(), {
|
||||
// wallet: this,
|
||||
fee: options.fee,
|
||||
subtractFee: options.subtractFee,
|
||||
@ -524,9 +522,6 @@ Wallet.prototype.fill = function fill(tx, options) {
|
||||
n: this.n
|
||||
});
|
||||
|
||||
if (!result.inputs)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@ -554,8 +549,7 @@ Wallet.prototype.createTX = function createTX(outputs, options) {
|
||||
});
|
||||
|
||||
// Fill the inputs with unspents
|
||||
if (!this.fill(tx, options))
|
||||
return;
|
||||
this.fill(tx, options);
|
||||
|
||||
// Sort members a la BIP69
|
||||
tx.sortMembers();
|
||||
|
||||
@ -173,8 +173,13 @@ describe('Wallet', function() {
|
||||
|
||||
// Create new transaction
|
||||
var t3 = bcoin.tx().out(w2, 15000);
|
||||
assert(!w1.fill(t3));
|
||||
assert.equal(t3.requiredFunds.toString(10), 25000);
|
||||
try {
|
||||
w1.fill(t3);
|
||||
} catch (e) {
|
||||
var err = e;
|
||||
}
|
||||
assert(err);
|
||||
assert.equal(err.requiredFunds.toString(10), 25000);
|
||||
|
||||
cb();
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user