From 3dbfae11a33af3d84a3171c0c6d31dcfef5bdba8 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 5 Jun 2014 17:07:54 -0300 Subject: [PATCH] modifies unspent interfase to be more clear --- README.md | 7 +++-- app/models/Address.js | 3 +- lib/TransactionDb.js | 11 +++++-- test/integration/addrCache.js | 56 ++++++++++++++++++++++++++++++----- test/integration/blocklist.js | 6 ++-- 5 files changed, 66 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3821ad7f..0790aa70 100644 --- a/README.md +++ b/README.md @@ -193,12 +193,13 @@ Sample return: ts: 1401226410, scriptPubKey: "76a914e50575162795cd77366fb80d728e3216bd52deac88ac", amount: 0.001, - confirmations: "6+" + confirmation: 6 + confirmationsFromCache: true, } ] ``` -Please not that in case confirmations are cached and are more that INSIGHT_SAFE_CONFIRMATIONS setting, the -return can be a string of the form `SAFE_CONFIRMATIONS+`, e.g.: the string `6+` +Please note that in case confirmations are cached (because the number of confirmations if bigger that INSIGHT_SAFE_CONFIRMATIONS) the return will include the pair confirmationsFromCache:true, and confirmations will equal INSIGHT_SAFE_CONFIRMATIONS. See noCache and INSIGHT_IGNORE_CACHE options for details. + ### Unspent Outputs for multiple addresses diff --git a/app/models/Address.js b/app/models/Address.js index 0ad6d25f..2f8e8ec1 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -179,7 +179,8 @@ Address.prototype.update = function(next, opts) { ts: x.ts, scriptPubKey: x.scriptPubKey, amount: x.value_sat / BitcoreUtil.COIN, - confirmations: x.isConfirmedCached ? (config.safeConfirmations+'+') : x.confirmations, + confirmations: x.isConfirmedCached ? (config.safeConfirmations) : x.confirmations, + confirmationsFromCache: !!x.isConfirmedCached, }; }); return next(); diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index 7a31d493..3e5498a6 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -406,8 +406,7 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) { -TransactionDb.prototype._parseAddrData = function(data, ignoreCache) { - var k = data.key.split('-'); +TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) { var v = data.value.split(':'); // console.log('[TransactionDb.js.375]',data.key,data.value); //TODO var item = { @@ -450,6 +449,7 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) { var self = this; var k = ADDR_PREFIX + addr + '-'; var ret = []; + var unique={}; db.createReadStream({ start: k, @@ -457,7 +457,12 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) { limit: opts.txLimit>0 ? opts.txLimit: -1, // -1 means not limit }) .on('data', function(data) { - ret.push(self._parseAddrData(data, opts.ignoreCache)); + var k = data.key.split('-'); + var index = k[3]+k[4]; + if (!unique[index]) { + unique[index]=1; + ret.push(self._parseAddrData(k, data, opts.ignoreCache)); + } }) .on('error', cb) .on('end', function() { diff --git a/test/integration/addrCache.js b/test/integration/addrCache.js index 4b847fc6..2d6e7395 100644 --- a/test/integration/addrCache.js +++ b/test/integration/addrCache.js @@ -22,7 +22,10 @@ describe('Address cache ', function() { before(function(c) { txDb = TransactionDb; txDb.deleteCacheForAddress('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG',function(){ - txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',c); + txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',function(){ + txDb.deleteCacheForAddress('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk',c); + }); + }); }); @@ -51,7 +54,7 @@ describe('Address cache ', function() { - it('cache case unspent w/o cache', function(done) { + it('cache case 2 w/o cache', function(done) { var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); a.update(function(err) { if (err) done(err); @@ -62,8 +65,7 @@ describe('Address cache ', function() { }); }); - - it('cache case unspent w cache', function(done) { + it('cache case 2 w cache', function(done) { var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); a.update(function(err) { if (err) done(err); @@ -73,7 +75,34 @@ describe('Address cache ', function() { return done(); }); }); - it('cache case 2 w/o cache', function(done) { + + it('cache case 2 unspent wo cache', function(done) { + txDb.deleteCacheForAddress('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk',function() { + var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); + a.update(function(err) { + if (err) done(err); + a.unspent.length.should.equal(1); + a.unspent[0].confirmations.should.be.above(15000); + a.unspent[0].confirmationsFromCache.should.equal(false); + 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(); + }, {onlyUnspent:1}); + }); + + + + it('cache case 3 w/o cache', function(done) { var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); a.update(function(err) { if (err) done(err); @@ -83,7 +112,7 @@ describe('Address cache ', function() { return done(); }); },1); - it('cache case 2 w cache', function(done) { + it('cache case 4 w cache', function(done) { var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); a.update(function(err) { if (err) done(err); @@ -93,7 +122,7 @@ describe('Address cache ', function() { return done(); },{txLimit:0}); }); - it('cache case 2 w ignore cache', function(done) { + it('cache case 4 w ignore cache', function(done) { var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); a.update(function(err) { if (err) done(err); @@ -104,6 +133,19 @@ describe('Address cache ', function() { },{txLimit:0, ignoreCache:1}); }); + it('cache case 5 unspent w cache', function(done) { + var a = new Address('2NBuTjjZrURxLaMyPUu2sJwNrtpt7GtPX2p', 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(); + }, {onlyUnspent:1}); + }); + + + }); diff --git a/test/integration/blocklist.js b/test/integration/blocklist.js index 4fe8ac80..19b104af 100644 --- a/test/integration/blocklist.js +++ b/test/integration/blocklist.js @@ -23,12 +23,12 @@ describe('BlockDb getBlocksByDate', function(){ it('Get Hash by Date', function(done) { - bDb.getBlocksByDate(START_TS, END_TS, 1000, function(err, list) { + bDb.getBlocksByDate(START_TS, END_TS, 2, function(err, list) { if (err) done(err); assert(list, 'returns list'); assert.equal(list.length,2, 'list has 2 items'); - assert.equal(list[0].hash, TESTING_BLOCK0); - assert.equal(list[1].hash, TESTING_BLOCK1); + assert.equal(list[1].hash, TESTING_BLOCK0); + assert.equal(list[0].hash, TESTING_BLOCK1); done(); }); });