From 26c829f6de84a101bc2737f6d4ac33be18f2ddad Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Thu, 19 Jun 2014 10:30:53 -0300 Subject: [PATCH] Add validation and tests for spendUnconfirmed = false --- config.js | 2 +- js/models/core/Wallet.js | 3 ++- test/mocks/FakeBlockchain.js | 14 ++++++++++++++ test/test.Wallet.js | 35 ++++++++++++++++++++++++++++++----- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/config.js b/config.js index a15369429..5f33dfdfe 100644 --- a/config.js +++ b/config.js @@ -89,7 +89,7 @@ var defaultConfig = { wallet: { requiredCopayers: 2, totalCopayers: 3, - spendUnconfirmed: 1, + spendUnconfirmed: true, verbose: 1, reconnectDelay: 5000, }, diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index f0e94993d..6e437b1c4 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -647,7 +647,8 @@ Wallet.prototype.getUnspent = function(cb) { for (var i in unspentList) { var u = unspentList[i]; - if (!uu[u.txid + ',' + u.vout]) + var name = u.txid + ',' + u.vout; + if (!uu[name] && (self.spendUnconfirmed || u.confirmations >= 1)) safeUnspendList.push(u); } diff --git a/test/mocks/FakeBlockchain.js b/test/mocks/FakeBlockchain.js index 6a1c27a78..bfacb1924 100644 --- a/test/mocks/FakeBlockchain.js +++ b/test/mocks/FakeBlockchain.js @@ -30,6 +30,20 @@ FakeBlockchain.prototype.getUnspent = function(addresses, cb) { }]); }; +FakeBlockchain.prototype.getUnspent2 = function(addresses, cb) { + if (!addresses || !addresses.length) return cb(null, []); + return cb(null, this.u || [{ + 'address': 'mji7zocy8QzYywQakwWf99w9bCT6orY1C1', + 'txid': '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa', + 'vout': 0, + 'ts': 1402323949, + 'scriptPubKey': '21032ca453c1d9a93b7de8cf3d44d7bb8d52a45dbdf8fff63f69de4e51b740bb1da3ac', + 'amount': 25.0001, + 'confirmations': 1, + 'confirmationsFromCache': false + }]); +}; + FakeBlockchain.prototype.sendRawTransaction = function(rawtx, cb) { var txid = '0be0fb4579911be829e3077202e1ab47fcc12cf3ab8f8487ccceae768e1f95fa'; return cb(txid); diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 4c89a54b4..68aca9d9c 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -26,7 +26,7 @@ describe('Wallet model', function() { var config = { requiredCopayers: 3, totalCopayers: 5, - spendUnconfirmed: 1, + spendUnconfirmed: true, reconnectDelay: 100, networkName: 'testnet', }; @@ -42,9 +42,9 @@ describe('Wallet model', function() { w.getNetworkName().should.equal('testnet'); }); - var createW = function(netKey, N) { + var createW = function(netKey, N, conf) { - var c = JSON.parse(JSON.stringify(config)); + var c = JSON.parse(JSON.stringify(conf || config)); if (!N) N = c.totalCopayers; if (netKey) c.netKey = netKey; @@ -122,10 +122,10 @@ describe('Wallet model', function() { "confirmations": 7 }]; - var createW2 = function(privateKeys, N) { + var createW2 = function(privateKeys, N, conf) { if (!N) N = 3; var netKey = 'T0FbU2JLby0='; - var w = createW(netKey, N); + var w = createW(netKey, N, conf); should.exist(w); var pkr = w.publicKeyRing; @@ -467,6 +467,31 @@ describe('Wallet model', function() { }); }); + it('#getUnspent should honor spendUnconfirmed = false', function(done) { + var conf = JSON.parse(JSON.stringify(config)); + conf.spendUnconfirmed = false; + var w = createW2(null, null, conf); + w.getBalance(function(err, balance, balanceByAddr, safeBalance) { + balance.should.equal(2500010000); + safeBalance.should.equal(0); + balanceByAddr.mji7zocy8QzYywQakwWf99w9bCT6orY1C1.should.equal(2500010000); + done(); + }); + }); + + it('#getUnspent and spendUnconfirmed should count transactions with 1 confirmations', function(done) { + var conf = JSON.parse(JSON.stringify(config)); + conf.spendUnconfirmed = false; + var w = createW2(null, null, conf); + w.blockchain.getUnspent = w.blockchain.getUnspent2; + w.getBalance(function(err, balance, balanceByAddr, safeBalance) { + balance.should.equal(2500010000); + safeBalance.should.equal(2500010000); + balanceByAddr.mji7zocy8QzYywQakwWf99w9bCT6orY1C1.should.equal(2500010000); + done(); + }); + }); + var roundErrorChecks = [{ unspent: [1.0001], balance: 100010000