Merge pull request #282 from matiu/feat/handle-invalid

Feat/handle invalid
This commit is contained in:
Ivan Socolsky 2015-07-15 10:22:35 -03:00
commit 43be63859a
2 changed files with 41 additions and 19 deletions

View File

@ -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;
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,

View File

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