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 = [];
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);
var item = {
key: data.key,
ts: END_OF_WORLD_TS - parseInt(k[2]),
@ -417,17 +417,20 @@ TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
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
// console.log('[TransactionDb.js.356] CACHE HIT CONF:', item.key);
// Sent, confirmed
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.spentIsConfirmedCached = 1;
item.spentTxId = v[3];
@ -436,9 +439,9 @@ TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
}
// Scriptpubkey cached
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);
}
}
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});
});
@ -144,6 +158,40 @@ describe('Address cache ', function() {
}, {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});
});
});
});