wallet: make .fill() throw

This commit is contained in:
Fedor Indutny 2014-05-11 02:41:00 +04:00
parent 89877db9ba
commit 3ef7008a32
2 changed files with 11 additions and 5 deletions

View File

@ -247,7 +247,7 @@ Wallet.prototype.balance = function balance() {
return this.tx.balance();
};
Wallet.prototype.fill = function fill(tx, cb) {
Wallet.prototype.fill = function fill(tx) {
// NOTE: tx should be prefilled with all outputs
var cost = tx.funds('out');
@ -257,12 +257,12 @@ Wallet.prototype.fill = function fill(tx, cb) {
// total = cost + fee
var total = cost.add(new bn(this.fee));
var lastAdded = -1;
var lastAdded = 0;
function addInput(unspent, i) {
// Add new inputs until TX will have enough funds to cover both
// minimum post cost and fee
tx.input(unspent);
lastAdded = i;
lastAdded++;
return tx.funds('in').cmp(total) < 0;
}
@ -285,14 +285,14 @@ Wallet.prototype.fill = function fill(tx, cb) {
// Failed to get enough funds, add more inputs
if (tx.funds('in').cmp(total) < 0)
unspent.slice(lastAdded + 1).every(addInput, this);
unspent.slice(lastAdded).every(addInput, this);
} while (tx.funds('in').cmp(total) < 0 && lastAdded < unspent.length);
// Still failing to get enough funds, notify caller
if (tx.funds('in').cmp(total) < 0) {
var err = new Error('Not enough funds');
err.minBalance = total;
return cb(err);
throw err;
}
// How much money is left after sending outputs

View File

@ -148,5 +148,11 @@ describe('Wallet', function() {
assert.equal(t2.funds('in').toString(10), 16380);
assert.equal(t2.funds('out').toString(10), 6380);
// Create new transaction
var t2 = bcoin.tx().out(w2, 15000);
assert.throws(function() {
w1.fill(t2);
});
});
});