From 03490768394f5c849646b4106a1369dcacc5cd1c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 15 Jul 2015 09:57:45 -0300 Subject: [PATCH 1/3] handle invalid txs in history --- config.js | 2 +- lib/server.js | 47 +++++++++++++++++++++++--------------- test/integration/server.js | 13 +++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/config.js b/config.js index a4e82a4..e099ed0 100644 --- a/config.js +++ b/config.js @@ -42,7 +42,7 @@ var config = { }, testnet: { provider: 'insight', - url: 'https://test-insight.bitpay.com:443', + url: 'http://localhost:3001', }, }, // To use email notifications uncomment this: diff --git a/lib/server.js b/lib/server.js index 38ba428..6721e87 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1420,28 +1420,37 @@ WalletService.prototype.getTxHistory = function(opts, cb) { var now = Math.floor(Date.now() / 1000); return _.map(txs, function(tx) { - var inputs = classify(tx.inputs); - var outputs = classify(tx.outputs); - var amountIn = sum(inputs, true); - var amountOut = sum(outputs, true, false); - var amountOutChange = sum(outputs, true, true); + var amountIn, amountOut, amountOutChange, amount var amount, action, addressTo; - if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? tx.fees : 0))) { - amount = amountOut; - action = 'moved'; - } else { - amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0); - action = amount > 0 ? 'sent' : 'received'; - } - amount = Math.abs(amount); - if (action == 'sent' || action == 'moved') { - var firstExternalOutput = _.find(outputs, { - isMine: false - }); - addressTo = firstExternalOutput ? firstExternalOutput.address : 'N/A'; - }; + if (tx.outputs.length || tx.inputs.length) { + + var inputs = classify(tx.inputs); + var outputs = classify(tx.outputs); + + amountIn = sum(inputs, true); + amountOut = sum(outputs, true, false); + amountOutChange = sum(outputs, true, true); + if (amountIn == (amountOut + amountOutChange + (amountIn > 0 ? tx.fees : 0))) { + amount = amountOut; + action = 'moved'; + } else { + amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0); + action = amount > 0 ? 'sent' : 'received'; + } + + amount = Math.abs(amount); + if (action == 'sent' || action == 'moved') { + var firstExternalOutput = _.find(outputs, { + isMine: false + }); + addressTo = firstExternalOutput ? firstExternalOutput.address : 'N/A'; + }; + } else { + action = 'invalid'; + amount = 0; + } var newTx = { txid: tx.txid, diff --git a/test/integration/server.js b/test/integration/server.js index 2d7abce..8d14f2c 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -3592,6 +3592,19 @@ describe('Wallet service', function() { done(); }); }); + it('should handle invalid tx in history ', function(done) { + var h = _.clone(TestData.history); + h.push({txid:'xx'}) + helpers.stubHistory(h); + + server.getTxHistory({}, function(err, txs) { + should.not.exist(err); + should.exist(txs); + txs.length.should.equal(3); + txs[2].action.should.equal('invalid'); + done(); + }); + }); }); describe('#scan', function() { From c293635f56603629cd4099720a9fc54d0e3150e7 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 15 Jul 2015 10:07:25 -0300 Subject: [PATCH 2/3] fix config --- config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.js b/config.js index e099ed0..a4e82a4 100644 --- a/config.js +++ b/config.js @@ -42,7 +42,7 @@ var config = { }, testnet: { provider: 'insight', - url: 'http://localhost:3001', + url: 'https://test-insight.bitpay.com:443', }, }, // To use email notifications uncomment this: From bbe6167bb2567b9016974524c8d9d037520ba16f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 15 Jul 2015 10:18:33 -0300 Subject: [PATCH 3/3] fix syntax Signed-off-by: Matias Alejo Garcia --- lib/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index 6721e87..7501019 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1421,7 +1421,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) { return _.map(txs, function(tx) { - var amountIn, amountOut, amountOutChange, amount + var amountIn, amountOut, amountOutChange; var amount, action, addressTo; if (tx.outputs.length || tx.inputs.length) {