Browse Source

handle invalid txs in history

activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
0349076839
  1. 2
      config.js
  2. 45
      lib/server.js
  3. 13
      test/integration/server.js

2
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:

45
lib/server.js

@ -1420,29 +1420,38 @@ 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';
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 {
amount = amountIn - amountOut - amountOutChange - (amountIn > 0 ? tx.fees : 0);
action = amount > 0 ? 'sent' : 'received';
action = 'invalid';
amount = 0;
}
amount = Math.abs(amount);
if (action == 'sent' || action == 'moved') {
var firstExternalOutput = _.find(outputs, {
isMine: false
});
addressTo = firstExternalOutput ? firstExternalOutput.address : 'N/A';
};
var newTx = {
txid: tx.txid,
action: action,

13
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() {

Loading…
Cancel
Save