Merge pull request #457 from matiu/opt/scriptPubKeyCache
Opt/script pub key cache
This commit is contained in:
commit
0fccc1c915
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -115,9 +116,9 @@ TransactionDb.prototype.fromTxId = function(txid, cb) {
|
|||||||
|
|
||||||
// outs.
|
// outs.
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
var v = data.value.split(':');
|
var v = data.value.split(':');
|
||||||
@ -135,9 +136,9 @@ TransactionDb.prototype.fromTxId = function(txid, cb) {
|
|||||||
|
|
||||||
var k = SPENT_PREFIX + txid + '-';
|
var k = SPENT_PREFIX + txid + '-';
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
var j = idx[parseInt(k[2])];
|
var j = idx[parseInt(k[2])];
|
||||||
@ -164,9 +165,9 @@ TransactionDb.prototype._fillSpent = function(info, cb) {
|
|||||||
|
|
||||||
var k = SPENT_PREFIX + info.txid + '-';
|
var k = SPENT_PREFIX + info.txid + '-';
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
self._addSpentInfo(info.vout[k[2]], k[3], k[4], data.value);
|
self._addSpentInfo(info.vout[k[2]], k[3], k[4], data.value);
|
||||||
@ -247,8 +248,8 @@ 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);
|
||||||
@ -301,15 +302,15 @@ TransactionDb.prototype.fromTxIdN = function(txid, n, cb, opts) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.noExtraInfo)
|
if (opts.noExtraInfo)
|
||||||
return cb(null, ret);
|
return cb(null, ret);
|
||||||
|
|
||||||
// spent?
|
// spent?
|
||||||
var k = SPENT_PREFIX + txid + '-' + n + '-';
|
var k = SPENT_PREFIX + txid + '-' + n + '-';
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
self._addSpentInfo(ret, k[3], k[4], data.value);
|
self._addSpentInfo(ret, k[3], k[4], data.value);
|
||||||
@ -328,9 +329,9 @@ TransactionDb.prototype.deleteCacheForAddress = function(addr, cb) {
|
|||||||
var k = ADDR_PREFIX + addr + '-';
|
var k = ADDR_PREFIX + addr + '-';
|
||||||
var dbScript = [];
|
var dbScript = [];
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var v = data.value.split(':');
|
var v = data.value.split(':');
|
||||||
dbScript.push({
|
dbScript.push({
|
||||||
@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -466,10 +470,10 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
|
|||||||
var unique = {};
|
var unique = {};
|
||||||
|
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~',
|
end: k + '~',
|
||||||
limit: opts.txLimit > 0 ? opts.txLimit : -1, // -1 means not limit
|
limit: opts.txLimit > 0 ? opts.txLimit : -1, // -1 means not limit
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
var index = k[3] + k[4];
|
var index = k[3] + k[4];
|
||||||
@ -495,9 +499,9 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
|
|||||||
}), CONCURRENCY, function(o, e_c) {
|
}), CONCURRENCY, function(o, e_c) {
|
||||||
var k = SPENT_PREFIX + o.txid + '-' + o.index + '-';
|
var k = SPENT_PREFIX + o.txid + '-' + o.index + '-';
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
self._addSpentInfo(o, k[3], k[4], data.value);
|
self._addSpentInfo(o, k[3], k[4], data.value);
|
||||||
@ -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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -588,14 +598,14 @@ TransactionDb.prototype.removeFromTxId = function(txid, cb) {
|
|||||||
},
|
},
|
||||||
function(c) {
|
function(c) {
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: SPENT_PREFIX + txid + '-',
|
start: SPENT_PREFIX + txid + '-',
|
||||||
end: SPENT_PREFIX + txid + '~'
|
end: SPENT_PREFIX + txid + '~'
|
||||||
})
|
})
|
||||||
.pipe(
|
.pipe(
|
||||||
db.createWriteStream({
|
db.createWriteStream({
|
||||||
type: 'del'
|
type: 'del'
|
||||||
})
|
})
|
||||||
).on('close', c);
|
).on('close', c);
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
function(err) {
|
function(err) {
|
||||||
@ -717,10 +727,10 @@ TransactionDb.prototype.checkVersion02 = function(cb) {
|
|||||||
var k = 'txa-';
|
var k = 'txa-';
|
||||||
var isV2 = 1;
|
var isV2 = 1;
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~',
|
end: k + '~',
|
||||||
limit: 1,
|
limit: 1,
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
isV2 = 0;
|
isV2 = 0;
|
||||||
})
|
})
|
||||||
@ -736,9 +746,9 @@ TransactionDb.prototype.migrateV02 = function(cb) {
|
|||||||
var c2 = 0;
|
var c2 = 0;
|
||||||
var N = 50000;
|
var N = 50000;
|
||||||
db.createReadStream({
|
db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
var v = data.value.split(':');
|
var v = data.value.split(':');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user