From d7169ff6395c39eb060bed2807ddc8be2b1e96e4 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 15 Aug 2017 19:38:19 -0400 Subject: [PATCH] Get transaction fixes. --- lib/services/mempool/index.js | 18 ++- lib/services/transaction/index.js | 175 ++++++++++++++---------------- 2 files changed, 98 insertions(+), 95 deletions(-) diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index 2a5800fd..75b31ddb 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -130,7 +130,23 @@ MempoolService.prototype._onTransaction = function(tx) { }; MempoolService.prototype.getMempoolTransaction = function(txid, callback) { - this._db.get(this._encoding.encodeMempoolTransactionKey(txid), callback); + + var self = this; + + self._db.get(self._encoding.encodeMempoolTransactionKey(txid), function(err, tx) { + + if (err) { + return callback(err); + } + + if (!tx) { + return callback(); + } + + callback(null, self._encoding.decodeMempoolTransactionValue(tx)); + + }); + }; MempoolService.prototype.stop = function(callback) { diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 67ff999b..8492a483 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -36,7 +36,7 @@ TransactionService.prototype.getAPIMethods = function() { ['getRawTransaction', this, this.getRawTransaction, 1], ['getTransaction', this, this.getTransaction, 1], ['getDetailedTransaction', this, this.getDetailedTransaction, 1], - ['getInputValues', this, this._addMissingInputValues, 1] + ['getInputValues', this, this.getInputValues, 1] ]; }; @@ -52,7 +52,65 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) callback = options; } - self._getTransaction(txid, options, function(err, tx) { + async.waterfall([ + function(next) { + self._getTransaction(txid, options, next); + }, + self._getMempoolTransaction.bind(self), + self.getInputValues.bind(self), + self._setMetaInfo.bind(self) + ], callback); + +}; + +TransactionService.prototype._setMetaInfo = function(tx, options, callback) { + + if (!tx) { + return callback(); + } + + // output values + var outputSatoshis = 0; + + tx.outputs.forEach(function(output) { + outputSatoshis += output.value; + }); + + tx.outputSatoshis = outputSatoshis; + + + //input values + if (!tx.inputs[0].isCoinbase()) { + + var inputSatoshis = 0; + + tx.__inputValues.forEach(function(val) { + + if (val >+ 0) { + inputSatoshis += val; + } + }); + + var feeSatoshis = inputSatoshis - outputSatoshis; + tx.inputSatosbis = inputSatoshis; + tx.feeSatosbis = feeSatoshis; + + } + + callback(null, tx); + +}; + +TransactionService.prototype._getMempoolTransaction = function(txid, tx, options, callback) { + + var self = this; + var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; + + if (tx || !queryMempool) { + return callback(null, tx, options); + } + + self._mempool.getMempoolTransaction(txid, function(err, tx) { if (err) { return callback(err); @@ -62,7 +120,8 @@ TransactionService.prototype.getTransaction = function(txid, options, callback) return callback(); } - self._addMissingInputValues(tx, options, callback); + tx.confirmations = 0; + callback(null, tx, options); }); @@ -72,113 +131,41 @@ TransactionService.prototype._getTransaction = function(txid, options, callback) var self = this; - var queryMempool = _.isUndefined(options.queryMempool) ? true : options.queryMempool; - var key = self._encoding.encodeTransactionKey(txid); - async.waterfall([ - // main index? - function(next) { - self._db.get(key, function(err, tx) { - - if (err) { - return next(err); - } - - if (!tx) { - return next(null, tx); - } - - tx = self._encoding.decodeTransactionValue(tx); - tx.confirmations = self._header.getBestHeight() - tx.__height; - - self._header.getBlockHeader(tx.__height, function(err, header) { - - if (err) { - return next(err); - } - - if (header) { - tx.blockHash = header.hash; - } - - next(null, tx); - - }); - }); - }, - // mempool? - function(tx, next) { - - if (queryMempool && !tx) { - - return self._mempool.getMempoolTransaction(txid, function(err, memTx) { - - if (err) { - return next(err); - } - - if (memTx) { - memTx = self._encoding.decodeTransactionValue(memTx); - memTx.confirmations = 0; - } - - next(null, memTx); - }); - - } - - next(null, tx); - - } - ], function(err, tx) { + self._db.get(key, function(err, tx) { if (err) { return callback(err); } if (!tx) { - return callback(); + return callback(null, tx, options); } - // output values - var outputSatoshis = 0; + tx = self._encoding.decodeTransactionValue(tx); + tx.confirmations = self._header.getBestHeight() - tx.__height; + + self._header.getBlockHeader(tx.__height, function(err, header) { + + if (err) { + return callback(err); + } + + if (header) { + tx.blockHash = header.hash; + } + + callback(null, txid, tx, options); - tx.outputs.forEach(function(output) { - outputSatoshis += output.value; }); - tx.outputSatoshis = outputSatoshis; - - - //input values - if (!tx.inputs[0].isCoinbase()) { - - var inputSatoshis = 0; - - tx.__inputValues.forEach(function(val) { - - if (val >- 0) { - inputSatoshis += val; - } - }); - - var feeSatoshis = inputSatoshis - outputSatoshis; - tx.inputSatosbis = inputSatoshis; - tx.feeSatosbis = feeSatoshis; - - } - - callback(null, tx); - }); }; -TransactionService.prototype._addMissingInputValues = function(tx, options, callback) { +TransactionService.prototype.getInputValues = function(tx, options, callback) { - // if we have cache misses from when we populated input values, - // then we must go and find them after the fact (lazy-load). var self = this; async.eachOfLimit(tx.inputs, 4, function(input, index, next) { @@ -190,7 +177,7 @@ TransactionService.prototype._addMissingInputValues = function(tx, options, call var outputIndex = input.prevout.index; - self._getTransaction(input.prevout.txid(), options, function(err, _tx) { + self._getTransaction(input.prevout.txid(), options, function(err, txid, _tx) { if (err || !_tx) { return next(err || new Error('tx not found for tx id: ' + input.prevout.txid())); @@ -218,7 +205,7 @@ TransactionService.prototype._addMissingInputValues = function(tx, options, call return callback(err); } - callback(null, tx); + callback(null, tx, options); });