Merge pull request #457 from matiu/opt/scriptPubKeyCache

Opt/script pub key cache
This commit is contained in:
Matias Alejo Garcia 2016-02-23 09:31:12 -03:00
commit 0fccc1c915

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var imports = require('soop').imports(); var imports = require('soop').imports();
var _ = require('lodash');
@ -248,7 +249,7 @@ TransactionDb.prototype._getInfo = function(txid, next, opts) {
self._fillOutpoints(txInfo, function() { self._fillOutpoints(txInfo, function() {
if (opts.noExtraInfo) if (opts.noExtraInfo)
return next(null,txInfo); return next(null, txInfo);
self._fillSpent(txInfo, function() { self._fillSpent(txInfo, function() {
return next(null, txInfo); return next(null, txInfo);
@ -394,22 +395,25 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts, cb) {
// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO // console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO
var self = this; var self = this;
var dbScript = []; var dbScript = [];
for (var ii in txouts) { _.each(txouts, function(txout) {
var txout = txouts[ii];
//everything already cached? //everything already cached?
if (txout.scriptPubKeyCached || txout.spentTxId) { if (txout.scriptPubKeyCached || txout.spentTxId)
continue; return;
}
// not hard confirmed ?
if (!txout.isConfirmedCached && !txout.confirmedWillBeCached)
return;
if (txout.scriptPubKey) { if (txout.scriptPubKey) {
var infoToCache = [txout.value_sat, (txout.isConfirmedCached || txout.confirmedWillBeCached) ? 1 : 0, txout.scriptPubKey]; var infoToCache = [txout.value_sat, 1, txout.scriptPubKey];
dbScript.push({ dbScript.push({
type: 'put', type: 'put',
key: txout.key, key: txout.key,
value: infoToCache.join(':'), value: infoToCache.join(':'),
}); });
} }
} });
db.batch(dbScript, cb); db.batch(dbScript, cb);
}; };
@ -560,8 +564,12 @@ TransactionDb.prototype.getStandardizedTx = function(tx, time, isCoinBase) {
TransactionDb.prototype.fillScriptPubKey = function(txouts, cb) { TransactionDb.prototype.fillScriptPubKey = function(txouts, cb) {
var self = this; var self = this;
var allCached = true;
// Complete utxo info // Complete utxo info
async.eachLimit(txouts, CONCURRENCY, function(txout, a_c) { async.eachLimit(txouts, CONCURRENCY, function(txout, a_c) {
if (txout.scriptPubKeyCached) return a_c();
allCached = false;
self.fromIdInfoSimple(txout.txid, function(err, info) { self.fromIdInfoSimple(txout.txid, function(err, info) {
if (!info || !info.vout) return a_c(err); if (!info || !info.vout) return a_c(err);
@ -569,6 +577,8 @@ TransactionDb.prototype.fillScriptPubKey = function(txouts, cb) {
return a_c(); return a_c();
}); });
}, function() { }, function() {
if (allCached)
return cb();
self.cacheScriptPubKey(txouts, cb); self.cacheScriptPubKey(txouts, cb);
}); });
}; };