diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index c9dbf4e8..84d253c6 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -60,12 +60,11 @@ Bitcoin.prototype._initCaches = function() { this.txidsCache = LRU(50000); this.balanceCache = LRU(50000); this.summaryCache = LRU(50000); + this.transactionInfoCache = LRU(100000); // caches valid indefinitely this.transactionCache = LRU(100000); this.rawTransactionCache = LRU(50000); - this.transactionInfoCache = LRU(100000); - this.transactionInfoCacheConfirmations = 6; this.blockCache = LRU(144); this.rawBlockCache = LRU(72); this.blockHeaderCache = LRU(288); @@ -100,6 +99,7 @@ Bitcoin.prototype.getAPIMethods = function() { ['getBlockHeader', this, this.getBlockHeader, 1], ['getBlockHashesByTimestamp', this, this.getBlockHashesByTimestamp, 2], ['getBestBlockHash', this, this.getBestBlockHash, 0], + ['getSpentInfo', this, this.getSpentInfo, 1], ['getInfo', this, this.getInfo, 0], ['syncPercentage', this, this.syncPercentage, 0], ['isSynced', this, this.isSynced, 0], @@ -230,6 +230,7 @@ Bitcoin.prototype._loadSpawnConfiguration = function(node) { }; Bitcoin.prototype._resetCaches = function() { + this.transactionInfoCache.reset(); this.utxosCache.reset(); this.txidsCache.reset(); this.balanceCache.reset(); @@ -1281,10 +1282,12 @@ Bitcoin.prototype.getTransactionWithBlockInfo = function(txid, callback) { tx.__blockHash = response.result.blockhash; tx.__height = response.result.height ? response.result.height : -1; tx.__timestamp = response.result.time; - var confirmations = self._getConfirmationsDetail(tx); - if (confirmations >= self.transactionInfoCacheConfirmations) { - self.transactionInfoCache.set(txid, tx); + + for (var i = 0; i < response.result.vout.length; i++) { + tx.outputs[i].__spentTxId = response.result.vout[i].spentTxId; + tx.outputs[i].__spentIndex = response.result.vout[i].spentIndex; } + self.transactionInfoCache.set(txid, tx); done(null, tx); }); }, callback); @@ -1305,9 +1308,20 @@ Bitcoin.prototype.getBestBlockHash = function(callback) { }); }; -Bitcoin.prototype.getInputForOutput = function(txid, index, options, callback) { - // TODO - setImmediate(callback); +/** + * Will give the txid and inputIndex that spent an output + * @param {Function} callback + */ +Bitcoin.prototype.getSpentInfo = function(options, callback) { + var self = this; + this.client.getSpentInfo(options, function(err, response) { + if (err && err.code === -5) { + return callback(null, {}); + } else if (err) { + return callback(self._wrapRPCError(err)); + } + callback(null, response.result); + }); }; /** diff --git a/lib/transaction.js b/lib/transaction.js index adce3086..e4cccd8b 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -9,6 +9,30 @@ var errors = index.errors; var MAX_TRANSACTION_LIMIT = 5; +Transaction.prototype.populateSpentInfo = function(db, options, callback) { + var self = this; + var txid = self.hash; + + async.eachLimit( + Object.keys(self.outputs), + db.maxTransactionlimit || MAX_TRANSACTION_LIMIT, + function(outputIndex, next) { + db.getSpentInfo({ + txid: txid, + index: parseInt(outputIndex) + }, function(err, info) { + if (err) { + return next(err); + } + self.outputs[outputIndex].__spentTxId = info.txid; + self.outputs[outputIndex].__spentIndex = info.index; + next(); + }); + }, + callback + ); +}; + Transaction.prototype.populateInputs = function(db, poolTransactions, callback) { var self = this; diff --git a/package.json b/package.json index 8afbee16..a7bc9fdd 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ ], "dependencies": { "async": "^1.3.0", - "bitcoind-rpc": "braydonf/bitcoind-rpc#8d27a545f4e7de5a8faca5de6bdbb1a6c1e41f5c", + "bitcoind-rpc": "braydonf/bitcoind-rpc#4850733b9806bc5e8e1508fa90f3c45782e6ee80", "bitcore-lib": "^0.13.13", "body-parser": "^1.13.3", "colors": "^1.1.2",