From 3ef7008a322a29a09c4c319000ba42d019a76219 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sun, 11 May 2014 02:41:00 +0400 Subject: [PATCH] wallet: make `.fill()` throw --- lib/bcoin/wallet.js | 10 +++++----- test/wallet-test.js | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 82c90f68..5d37011d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -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 diff --git a/test/wallet-test.js b/test/wallet-test.js index bf33002b..b34871db 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -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); + }); }); });