Merge pull request #127 from matiu/bug/cache01

Bug/cache01
This commit is contained in:
Matias Alejo Garcia 2014-06-08 20:35:07 -03:00
commit 0d3e9ea7dd
2 changed files with 70 additions and 19 deletions

View File

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

View File

@ -82,21 +82,35 @@ describe('Address cache ', function() {
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
a.unspent.length.should.equal(1); a.unspent.length.should.equal(1);
a.unspent[0].scriptPubKey.should.equal('a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87');
a.unspent[0].confirmations.should.be.above(15000); a.unspent[0].confirmations.should.be.above(15000);
a.unspent[0].confirmationsFromCache.should.equal(false); a.unspent[0].confirmationsFromCache.should.equal(false);
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(); return done();
});
}, {onlyUnspent:1}); }, {onlyUnspent:1});
}); });
}); });
it('cache case 2 unspent w cache', function(done) { it('cache case 2 unspent w cache', function(done) {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
a.unspent.length.should.equal(1); a.unspent.length.should.equal(1);
a.unspent[0].confirmationsFromCache.should.equal(true); a.unspent[0].confirmationsFromCache.should.equal(true);
a.unspent[0].confirmations.should.equal(6); a.unspent[0].confirmations.should.equal(6);
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(); return done();
});
}, {onlyUnspent:1}); }, {onlyUnspent:1});
}); });
@ -144,6 +158,40 @@ describe('Address cache ', function() {
}, {onlyUnspent:1}); }, {onlyUnspent:1});
}); });
it('cache fix broken cases', function(done) {
txDb._db.put('txa2-2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk-9998599199253-16c0287dbea7e323431caff7f7e490da6de66530717f86f8dae9549b3355301a-0', '23000000:1399232338:0:a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87', function(){
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) {
if (err) done(err);
a.balance.should.equal(0.23, 'balance');
a.totalReceived.should.equal(0.23, 'totalReceived');
a.txApperances.should.equal(1, 'txApperances');
a.transactions.length.should.equal(1);
a.transactions[0].should.equal('16c0287dbea7e323431caff7f7e490da6de66530717f86f8dae9549b3355301a');
return done();
});
});
});
it('cache fix broken cases 2)', function(done) {
txDb._db.put('txa2-2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk-9998599199253-16c0287dbea7e323431caff7f7e490da6de66530717f86f8dae9549b3355301a-0', '23000000:1399232338:0:a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87', function(){
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(false);
a.unspent[0].confirmations.should.above(6);
a.unspent[0].scriptPubKey.should.equal('a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87');
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);
a.unspent[0].scriptPubKey.should.equal('a914a1d5be9f72224b5e83d00d7f5b9b674d456c573f87');
return done();
}, {onlyUnspent:1});
}, {onlyUnspent:1});
});
});
}); });