bitcoind: added getspentinfo method

This commit is contained in:
Braydon Fuller 2016-04-12 15:02:37 -04:00
parent b757bd3148
commit 37f31fdb19
3 changed files with 47 additions and 9 deletions

View File

@ -60,12 +60,11 @@ Bitcoin.prototype._initCaches = function() {
this.txidsCache = LRU(50000); this.txidsCache = LRU(50000);
this.balanceCache = LRU(50000); this.balanceCache = LRU(50000);
this.summaryCache = LRU(50000); this.summaryCache = LRU(50000);
this.transactionInfoCache = LRU(100000);
// caches valid indefinitely // caches valid indefinitely
this.transactionCache = LRU(100000); this.transactionCache = LRU(100000);
this.rawTransactionCache = LRU(50000); this.rawTransactionCache = LRU(50000);
this.transactionInfoCache = LRU(100000);
this.transactionInfoCacheConfirmations = 6;
this.blockCache = LRU(144); this.blockCache = LRU(144);
this.rawBlockCache = LRU(72); this.rawBlockCache = LRU(72);
this.blockHeaderCache = LRU(288); this.blockHeaderCache = LRU(288);
@ -100,6 +99,7 @@ Bitcoin.prototype.getAPIMethods = function() {
['getBlockHeader', this, this.getBlockHeader, 1], ['getBlockHeader', this, this.getBlockHeader, 1],
['getBlockHashesByTimestamp', this, this.getBlockHashesByTimestamp, 2], ['getBlockHashesByTimestamp', this, this.getBlockHashesByTimestamp, 2],
['getBestBlockHash', this, this.getBestBlockHash, 0], ['getBestBlockHash', this, this.getBestBlockHash, 0],
['getSpentInfo', this, this.getSpentInfo, 1],
['getInfo', this, this.getInfo, 0], ['getInfo', this, this.getInfo, 0],
['syncPercentage', this, this.syncPercentage, 0], ['syncPercentage', this, this.syncPercentage, 0],
['isSynced', this, this.isSynced, 0], ['isSynced', this, this.isSynced, 0],
@ -230,6 +230,7 @@ Bitcoin.prototype._loadSpawnConfiguration = function(node) {
}; };
Bitcoin.prototype._resetCaches = function() { Bitcoin.prototype._resetCaches = function() {
this.transactionInfoCache.reset();
this.utxosCache.reset(); this.utxosCache.reset();
this.txidsCache.reset(); this.txidsCache.reset();
this.balanceCache.reset(); this.balanceCache.reset();
@ -1281,10 +1282,12 @@ Bitcoin.prototype.getTransactionWithBlockInfo = function(txid, callback) {
tx.__blockHash = response.result.blockhash; tx.__blockHash = response.result.blockhash;
tx.__height = response.result.height ? response.result.height : -1; tx.__height = response.result.height ? response.result.height : -1;
tx.__timestamp = response.result.time; tx.__timestamp = response.result.time;
var confirmations = self._getConfirmationsDetail(tx);
if (confirmations >= self.transactionInfoCacheConfirmations) { for (var i = 0; i < response.result.vout.length; i++) {
self.transactionInfoCache.set(txid, tx); 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); done(null, tx);
}); });
}, callback); }, callback);
@ -1305,9 +1308,20 @@ Bitcoin.prototype.getBestBlockHash = function(callback) {
}); });
}; };
Bitcoin.prototype.getInputForOutput = function(txid, index, options, callback) { /**
// TODO * Will give the txid and inputIndex that spent an output
setImmediate(callback); * @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);
});
}; };
/** /**

View File

@ -9,6 +9,30 @@ var errors = index.errors;
var MAX_TRANSACTION_LIMIT = 5; 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) { Transaction.prototype.populateInputs = function(db, poolTransactions, callback) {
var self = this; var self = this;

View File

@ -40,7 +40,7 @@
], ],
"dependencies": { "dependencies": {
"async": "^1.3.0", "async": "^1.3.0",
"bitcoind-rpc": "braydonf/bitcoind-rpc#8d27a545f4e7de5a8faca5de6bdbb1a6c1e41f5c", "bitcoind-rpc": "braydonf/bitcoind-rpc#4850733b9806bc5e8e1508fa90f3c45782e6ee80",
"bitcore-lib": "^0.13.13", "bitcore-lib": "^0.13.13",
"body-parser": "^1.13.3", "body-parser": "^1.13.3",
"colors": "^1.1.2", "colors": "^1.1.2",