From 98130d3647ad84ea8f92278c76c4f51eb74eddff Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 28 May 2014 08:17:47 -0300 Subject: [PATCH] fix cache usage, and test --- dev-util/stats | 2 +- lib/BlockDb.js | 1 + lib/TransactionDb.js | 33 +++++++----- test/integration/addr.js | 53 ------------------- test/integration/addr.json | 4 +- test/integration/addrCache.js | 98 +++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 68 deletions(-) create mode 100644 test/integration/addrCache.js diff --git a/dev-util/stats b/dev-util/stats index 671622d3..73399ecf 100644 --- a/dev-util/stats +++ b/dev-util/stats @@ -65,6 +65,6 @@ Thu May 22 13:42:50 ART 2014 (base58check + toString opts + custom getStandardiz 100% testnet - => 17m6s (user time) + => 17m10s diff --git a/lib/BlockDb.js b/lib/BlockDb.js index 633cf753..80562705 100644 --- a/lib/BlockDb.js +++ b/lib/BlockDb.js @@ -369,6 +369,7 @@ BlockDb.prototype.fillConfirmations = function(txouts, cb) { return !x.spentIsConfirmedCached // not 100%cached && !(x.isConfirmedCached && !x.spentTxId); // and not 50%cached but not spent }); + //console.log('[BlockDb.js.373:txs:]',txs.length, txs.slice(0,5)); //TODO async.eachLimit(txs, CONCURRENCY, function(txout, e_c) { if(txout.isConfirmedCached) { diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index 117f78a8..66762a25 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -349,17 +349,21 @@ TransactionDb.prototype.cacheConfirmations = function(txouts,cb) { var infoToCache = []; if (txout.confirmations > self.safeConfirmations) { - if (!txout.isConfirmedCached) infoToCache.push(1); +//console.log('[TransactionDb.js.351:infoToCache:]',infoToCache); //TODO if (txout.spentConfirmations > self.safeConfirmations) { -// console.log('[TransactionDb.js.309]',txout); //TODO - infoToCache = infoToCache.concat([1, txout.spentTxId, txout.spentIndex, txout.spentTs]); + // 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]); } + else { + if (!txout.isConfirmedCached) infoToCache.push(1); + } +//console.log('[TransactionDb.js.352:infoToCache:]',infoToCache); //TODO if (infoToCache.length){ - // if spent, we overwrite scriptPubKey cache (not needed anymore) - // Last 1 = txout.isConfirmedCached (must be equal to 1 at this point) - infoToCache.unshift(txout.value_sat,txout.ts, 1); + infoToCache.unshift(txout.value_sat,txout.ts); +//console.log('[BlockDb.js.373:txs:]' ,txout.key, infoToCache.join(':')); //TODO dbScript.push({ type: 'put', key: txout.key, @@ -375,6 +379,7 @@ TransactionDb.prototype.cacheConfirmations = function(txouts,cb) { TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) { +// console.log('[TransactionDb.js.381:cacheScriptPubKey:]'); //TODO var self = this; var dbScript=[]; @@ -404,6 +409,7 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) { TransactionDb.prototype._parseAddrData = function(data) { var k = data.key.split('-'); var v = data.value.split(':'); +// console.log('[TransactionDb.js.410]',v); //TODO var item = { key: data.key, txid: k[2], @@ -412,15 +418,17 @@ TransactionDb.prototype._parseAddrData = function(data) { ts: parseInt(v[1]), }; - // Cache + // Cache: + // v[2]== isConfirmedCached + // v[3]=== '1' -> is SpendCached -> [4]=spendTxId [5]=spentIndex [6]=spendTs + // v[4]!== '1' -> is ScriptPubkey -> [[3] = scriptPubkey if (v[2]){ 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); //TODO // Sent, confirmed - if (v[3] === 1){ - - //console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO + if (v[3] === '1'){ + // console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO item.spentIsConfirmed = 1; item.spentIsConfirmedCached = 1; item.spentTxId = v[4]; @@ -429,6 +437,7 @@ TransactionDb.prototype._parseAddrData = function(data) { } // Scriptpubkey cached else if (v[3]) { + // console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key); //TODO item.scriptPubKey = v[3]; item.scriptPubKeyCached = 1; } @@ -558,7 +567,7 @@ TransactionDb.prototype.removeFromTxId = function(txid, cb) { }; -// relatedAddress is an optional hash, to collect related addresses in the transaction +// relatedAddrs is an optional hash, to collect related addresses in the transaction TransactionDb.prototype._addScript = function(tx, relatedAddrs) { var dbScript = []; var ts = tx.time; diff --git a/test/integration/addr.js b/test/integration/addr.js index b5fc5399..8b527357 100644 --- a/test/integration/addr.js +++ b/test/integration/addr.js @@ -75,59 +75,6 @@ describe('Address balances', function() { }); -describe('Address cache ', function() { - this.timeout(5000); - - before(function(c) { - txDb = TransactionDb; - txDb.deleteCacheForAddress('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG',function(){ - txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',c); - }); - }); - - it('cache case 1 w/o cache', function(done) { - var a = new Address('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG', txDb); - a.update(function(err) { - if (err) done(err); - a.balance.should.equal(0, 'balance'); - a.totalReceived.should.equal(19175, 'totalReceived'); - a.txApperances.should.equal(2, 'txApperances'); - return done(); - }); - }); - it('cache case 1 w cache', function(done) { - var a = new Address('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG', txDb); - a.update(function(err) { - if (err) done(err); - a.balance.should.equal(0, 'balance'); - a.totalReceived.should.equal(19175, 'totalReceived'); - a.txApperances.should.equal(2, 'txApperances'); - return done(); - }); - }); - - it('cache case 2 w/o cache', function(done) { - var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); - a.update(function(err) { - if (err) done(err); - a.balance.should.equal(0, 'balance'); - a.totalReceived.should.equal(1376000, 'totalReceived'); - a.txApperances.should.equal(8003, 'txApperances'); - return done(); - }); - },1); - it('cache case 2 w cache', function(done) { - var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); - a.update(function(err) { - if (err) done(err); - a.balance.should.equal(0, 'balance'); - a.totalReceived.should.equal(1376000, 'totalReceived'); - a.txApperances.should.equal(8003, 'txApperances'); - return done(); - },1); - }); -}); - //tested against https://api.biteasy.com/testnet/v1/addresses/2N1pLkosf6o8Ciqs573iwwgVpuFS6NbNKx5/unspent-outputs?per_page=40 describe('Address utxo', function() { diff --git a/test/integration/addr.json b/test/integration/addr.json index b646a2db..c973ce6b 100644 --- a/test/integration/addr.json +++ b/test/integration/addr.json @@ -1,8 +1,8 @@ [ { "addr": "mm8CDNJnk8PtQPEtTns2ah5nmxN63ENHtY", - "balance": 1.401, - "txApperances": 2 + "balance": 1.4, + "txApperances": 3 }, { diff --git a/test/integration/addrCache.js b/test/integration/addrCache.js new file mode 100644 index 00000000..556775e2 --- /dev/null +++ b/test/integration/addrCache.js @@ -0,0 +1,98 @@ +#!/usr/bin/env node + +'use strict'; + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + +var assert = require('assert'), + fs = require('fs'), + Address = require('../../app/models/Address'), + TransactionDb = require('../../lib/TransactionDb').default(), + addrValid = JSON.parse(fs.readFileSync('test/integration/addr.json')), + utxoValid = JSON.parse(fs.readFileSync('test/integration/utxo.json')); + +var should = require('chai'); + +var txDb; + + +describe('Address cache ', function() { + this.timeout(5000); + + before(function(c) { + txDb = TransactionDb; + txDb.deleteCacheForAddress('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG',function(){ + txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',c); + }); + }); + + + + it('cache case 1 w/o cache', function(done) { + var a = new Address('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG', txDb); + a.update(function(err) { + if (err) done(err); + a.balance.should.equal(0, 'balance'); + a.totalReceived.should.equal(19175, 'totalReceived'); + a.txApperances.should.equal(2, 'txApperances'); + return done(); + }); + }); + it('cache case 1 w cache', function(done) { + var a = new Address('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG', txDb); + a.update(function(err) { + if (err) done(err); + a.balance.should.equal(0, 'balance'); + a.totalReceived.should.equal(19175, 'totalReceived'); + a.txApperances.should.equal(2, 'txApperances'); + return done(); + }); + }); + + + + it('cache case unspent w/o cache', function(done) { + 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'); + return done(); + }); + }); + + + it('cache case unspent w cache', function(done) { + 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'); + return done(); + }); + }); + it('cache case 2 w/o cache', function(done) { + var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); + a.update(function(err) { + if (err) done(err); + a.balance.should.equal(0, 'balance'); + a.totalReceived.should.equal(1376000, 'totalReceived'); + a.txApperances.should.equal(8003, 'txApperances'); + return done(); + }); + },1); + it('cache case 2 w cache', function(done) { + var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); + a.update(function(err) { + if (err) done(err); + a.balance.should.equal(0, 'balance'); + a.totalReceived.should.equal(1376000, 'totalReceived'); + a.txApperances.should.equal(8003, 'txApperances'); + return done(); + },1); + }); +}); + +