fix cache usage, and test

This commit is contained in:
Matias Alejo Garcia 2014-05-28 08:17:47 -03:00
parent 6a751e2a8f
commit 98130d3647
6 changed files with 123 additions and 68 deletions

View File

@ -65,6 +65,6 @@ Thu May 22 13:42:50 ART 2014 (base58check + toString opts + custom getStandardiz
100% testnet 100% testnet
=> 17m6s (user time) => 17m10s

View File

@ -369,6 +369,7 @@ BlockDb.prototype.fillConfirmations = function(txouts, cb) {
return !x.spentIsConfirmedCached // not 100%cached return !x.spentIsConfirmedCached // not 100%cached
&& !(x.isConfirmedCached && !x.spentTxId); // and not 50%cached but not spent && !(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) { async.eachLimit(txs, CONCURRENCY, function(txout, e_c) {
if(txout.isConfirmedCached) { if(txout.isConfirmedCached) {

View File

@ -349,17 +349,21 @@ TransactionDb.prototype.cacheConfirmations = function(txouts,cb) {
var infoToCache = []; var infoToCache = [];
if (txout.confirmations > self.safeConfirmations) { if (txout.confirmations > self.safeConfirmations) {
if (!txout.isConfirmedCached) infoToCache.push(1); //console.log('[TransactionDb.js.351:infoToCache:]',infoToCache); //TODO
if (txout.spentConfirmations > self.safeConfirmations) { if (txout.spentConfirmations > self.safeConfirmations) {
// console.log('[TransactionDb.js.309]',txout); //TODO // if spent, we overwrite scriptPubKey cache (not needed anymore)
infoToCache = infoToCache.concat([1, txout.spentTxId, txout.spentIndex, txout.spentTs]); // 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 (infoToCache.length){
// if spent, we overwrite scriptPubKey cache (not needed anymore) infoToCache.unshift(txout.value_sat,txout.ts);
// Last 1 = txout.isConfirmedCached (must be equal to 1 at this point) //console.log('[BlockDb.js.373:txs:]' ,txout.key, infoToCache.join(':')); //TODO
infoToCache.unshift(txout.value_sat,txout.ts, 1);
dbScript.push({ dbScript.push({
type: 'put', type: 'put',
key: txout.key, key: txout.key,
@ -375,6 +379,7 @@ 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
var self = this; var self = this;
var dbScript=[]; var dbScript=[];
@ -404,6 +409,7 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) {
TransactionDb.prototype._parseAddrData = function(data) { TransactionDb.prototype._parseAddrData = function(data) {
var k = data.key.split('-'); var k = data.key.split('-');
var v = data.value.split(':'); var v = data.value.split(':');
// console.log('[TransactionDb.js.410]',v); //TODO
var item = { var item = {
key: data.key, key: data.key,
txid: k[2], txid: k[2],
@ -412,15 +418,17 @@ TransactionDb.prototype._parseAddrData = function(data) {
ts: parseInt(v[1]), 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]){ if (v[2]){
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); //TODO
// Sent, confirmed // Sent, confirmed
if (v[3] === 1){ if (v[3] === '1'){
// console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO
//console.log('[TransactionDb.js.356] CACHE HIT SPENT:', item.key); //TODO
item.spentIsConfirmed = 1; item.spentIsConfirmed = 1;
item.spentIsConfirmedCached = 1; item.spentIsConfirmedCached = 1;
item.spentTxId = v[4]; item.spentTxId = v[4];
@ -429,6 +437,7 @@ TransactionDb.prototype._parseAddrData = function(data) {
} }
// Scriptpubkey cached // Scriptpubkey cached
else if (v[3]) { else if (v[3]) {
// console.log('[TransactionDb.js.356] CACHE HIT SCRIPTPUBKEY:', item.key); //TODO
item.scriptPubKey = v[3]; item.scriptPubKey = v[3];
item.scriptPubKeyCached = 1; 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) { TransactionDb.prototype._addScript = function(tx, relatedAddrs) {
var dbScript = []; var dbScript = [];
var ts = tx.time; var ts = tx.time;

View File

@ -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 //tested against https://api.biteasy.com/testnet/v1/addresses/2N1pLkosf6o8Ciqs573iwwgVpuFS6NbNKx5/unspent-outputs?per_page=40
describe('Address utxo', function() { describe('Address utxo', function() {

View File

@ -1,8 +1,8 @@
[ [
{ {
"addr": "mm8CDNJnk8PtQPEtTns2ah5nmxN63ENHtY", "addr": "mm8CDNJnk8PtQPEtTns2ah5nmxN63ENHtY",
"balance": 1.401, "balance": 1.4,
"txApperances": 2 "txApperances": 3
}, },
{ {

View File

@ -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);
});
});