From fba71ee1aadaad3364aeba4d27fa6ca6a4e4cc14 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Mon, 9 Oct 2017 13:37:10 -0400 Subject: [PATCH] Fixed get raw tx calls that failed when input values were also in mempool. --- lib/services/transaction/index.js | 48 ++++++++++++++++++------- test/services/transaction/index.unit.js | 4 ++- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 71581a1f..bae7753f 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -195,9 +195,6 @@ TransactionService.prototype._getMempoolTransaction = function(txid, tx, options var self = this; var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; - if (!tx) { - console.log(txid); - } if (tx || !queryMempool) { return callback(null, tx, options); } @@ -212,6 +209,7 @@ TransactionService.prototype._getMempoolTransaction = function(txid, tx, options return callback(null, tx, options); } + // the tx's that contain these input values could, themselves be unconfirmed self._getInputValues(tx, options, function(err, inputValues) { if (err) { @@ -268,6 +266,8 @@ TransactionService.prototype._getInputValues = function(tx, options, callback) { var self = this; + var _tx = tx; + async.mapLimit(tx.inputs, 4, function(input, next) { if (input.isCoinbase()) { @@ -276,18 +276,42 @@ TransactionService.prototype._getInputValues = function(tx, options, callback) { var outputIndex = input.prevout.index; - self._getTransaction(input.prevout.txid(), options, function(err, txid, _tx) { + async.waterfall([ + // check tx index first, most likely place + function(next) { + self._getTransaction(input.prevout.txid(), options, next); + }, + // if not there, then check mempool + function(txid, tx, options, next) { + if (tx) { + return next(null, txid, tx); + } + self._mempool.getMempoolTransaction(input.prevout.txid(), function(err, memTx) { + if (err) { + return next(err); + } + next(null, txid, memTx); + }); + }, + // if not in mempool or tx index, we just don't have it, yet? + function(txid, tx, next) { + if (!tx) { + return next(new Error('Transaction Service: prev transacion: (' + input.prevout.txid() + ') for tx: ' + + _tx.txid() + ' at input index: ' + outputIndex + ' is missing from the index or not in the memory pool. It could be' + + ' that the parent tx has not yet been relayed to us, but will be relayed in the near future.')); + } + var output = tx.outputs[outputIndex]; - if (err || !_tx) { - return next(err || new Error('Transaction Service: tx not found for tx id: ' + input.prevout.txid())); + assert(output, 'Expected an output, but did not get one for tx: ' + tx.txid() + ' outputIndex: ' + outputIndex); + + next(null, output.value); } - - var output = _tx.outputs[outputIndex]; - assert(output, 'Expected an output, but did not get one for tx: ' + _tx.txid() + ' outputIndex: ' + outputIndex); - next(null, output.value); - + ], function(err, val) { + if (err) { + return next(err); + } + next(null, val); }); - }, callback); }; diff --git a/test/services/transaction/index.unit.js b/test/services/transaction/index.unit.js index e7dbd9ab..98bd1d5e 100644 --- a/test/services/transaction/index.unit.js +++ b/test/services/transaction/index.unit.js @@ -94,7 +94,7 @@ describe('Transaction Service', function() { var put = sandbox.stub().callsArgWith(2, null); txService._db = { put: put }; - sandbox.stub(txService, '_getTransaction').callsArgWith(2, null, tx.txid(), tx); + sandbox.stub(txService, '_getTransaction').callsArgWith(2, null, tx.txid(), tx, {}); tx.__inputValues = []; @@ -103,8 +103,10 @@ describe('Transaction Service', function() { if (err) { return done(err); } + values.should.deep.equal([1139033, 1139033, 500000, 1139033]); done(); + }); });