fix error in scriptpubkey cache

This commit is contained in:
Matias Alejo Garcia 2014-06-08 20:10:33 -03:00
parent 7542138847
commit 9df1674577
2 changed files with 33 additions and 16 deletions

View File

@ -348,22 +348,23 @@ TransactionDb.prototype.cacheConfirmations = function(txouts,cb) {
}
var infoToCache = [];
if (txout.confirmations > self.safeConfirmations) {
//console.log('[TransactionDb.js.351:infoToCache:]',infoToCache); //TODO
if (txout.confirmations >= self.safeConfirmations) {
if (txout.spentConfirmations > self.safeConfirmations) {
if (txout.spentConfirmations >= self.safeConfirmations) {
// if spent, we overwrite scriptPubKey cache (not needed anymore)
// First 1 = txout.isConfirmedCached (must be equal to 1 at this point)
infoToCache = infoToCache.concat([1, 1, txout.spentTxId, txout.spentIndex, txout.spentTs]);
infoToCache = [1, 1, txout.spentTxId, txout.spentIndex, txout.spentTs];
}
else {
if (!txout.isConfirmedCached) infoToCache.push(1);
if (!txout.isConfirmedCached) {
infoToCache.push(1);
txout.confirmedWillBeCached=1;
}
}
//console.log('[TransactionDb.js.352:infoToCache:]',infoToCache); //TODO
if (infoToCache.length){
infoToCache.unshift(txout.value_sat);
//console.log('[BlockDb.js.373:txs:]' ,txout.key, infoToCache.join(':')); //TODO
dbScript.push({
type: 'put',
key: txout.key,
@ -379,20 +380,19 @@ TransactionDb.prototype.cacheConfirmations = function(txouts,cb) {
TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) {
// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO
// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO
var self = this;
var dbScript=[];
for(var ii in txouts){
var txout=txouts[ii];
//everything already cached?
if (txout.scriptPubKeyCached || txout.spentTxId) {
continue;
}
if (txout.scriptPubKey) {
var infoToCache = [txout.value_sat,txout.ts, txout.isConfirmedCached?1:0, txout.scriptPubKey];
var infoToCache = [txout.value_sat,
(txout.isConfirmedCached || txout.confirmedWillBeCached)?1:0, txout.scriptPubKey];
dbScript.push({
type: 'put',
key: txout.key,
@ -408,7 +408,7 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) {
TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
var v = data.value.split(':');
// console.log('[TransactionDb.js.375]',data.key,data.value); //TODO
//console.log('[TransactionDb.js.375]',data.key,data.value); //TODO
var item = {
key: data.key,
ts: END_OF_WORLD_TS - parseInt(k[2]),
@ -416,12 +416,14 @@ TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
index: parseInt(k[4]),
value_sat: parseInt(v[0]),
};
if (ignoreCache)
return item;
// Cache:
// v[1]== isConfirmedCached
// v[2]=== '1' -> is SpendCached -> [4]=spendTxId [5]=spentIndex [6]=spendTs
// v[3]!== '1' -> is ScriptPubkey -> [[3] = scriptPubkey
if (v[1] && !ignoreCache){
// v[2]!== '1' -> is ScriptPubkey -> [[2] = scriptPubkey
if (v[1]==='1'){
item.isConfirmed = 1;
item.isConfirmedCached = 1;
// console.log('[TransactionDb.js.356] CACHE HIT CONF:', item.key); //TODO
@ -435,10 +437,11 @@ TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
item.spentTs = parseInt(v[5]);
}
// Scriptpubkey cached
// // TODO
else if (v[2]) {
// console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key); //TODO
item.scriptPubKey = v[2];
item.scriptPubKeyCached = 1;
// console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key, v, item.scriptPubKey); //TODO
}
}
return item;

View File

@ -82,21 +82,35 @@ describe('Address cache ', function() {
a.update(function(err) {
if (err) done(err);
a.unspent.length.should.equal(1);
a.unspent[0].scriptPubKey.should.equal('a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87');
a.unspent[0].confirmations.should.be.above(15000);
a.unspent[0].confirmationsFromCache.should.equal(false);
return done();
a.update(function(err) {
a.balance.should.equal(0.23, 'balance');
a.totalReceived.should.equal(0.23, 'totalReceived');
a.txApperances.should.equal(1, 'txApperances');
return done();
});
}, {onlyUnspent:1});
});
});
it('cache case 2 unspent w cache', function(done) {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) {
if (err) done(err);
a.unspent.length.should.equal(1);
a.unspent[0].confirmationsFromCache.should.equal(true);
a.unspent[0].confirmations.should.equal(6);
return done();
a.unspent[0].scriptPubKey.should.equal('a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87');
a.update(function(err) {
a.balance.should.equal(0.23, 'balance');
a.totalReceived.should.equal(0.23, 'totalReceived');
a.txApperances.should.equal(1, 'txApperances');
return done();
});
}, {onlyUnspent:1});
});