From c220a093543eb0b6b30ba4a3dbf4b1b0ccfabb73 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 29 Jul 2016 11:38:03 -0300 Subject: [PATCH] fix bug when asking beyong cache limit --- lib/server.js | 1 - lib/storage.js | 12 ++++++++---- test/integration/server.js | 18 +++++++++--------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/server.js b/lib/server.js index 97fce89..80e6c72 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2846,7 +2846,6 @@ WalletService.prototype.getTxHistory = function(opts, cb) { var proposals = res[0]; var notes = res[2]; - var finalTxs = decorate(normalizedTxs, addresses, proposals, notes); if (fromCache) diff --git a/lib/storage.js b/lib/storage.js index 83788d8..ca57e70 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -616,6 +616,8 @@ Storage.prototype.storeActiveAddresses = function(walletId, addresses, cb) { Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { var self = this; + $.checkArgument(from >= 0); + $.checkArgument(from<=to); self.db.collection(collections.CACHE).findOne({ walletId: walletId, @@ -628,7 +630,9 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { // Reverse indexes var fwdIndex = result.totalItems - to; - if (fwdIndex < 0) fwdIndex = 0; + + if (fwdIndex < 0) + return cb(); var end = fwdIndex + to - from; @@ -640,13 +644,13 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { }, function(err, result) { if (err) return cb(err); - if (result.history.length < fwdIndex) + if (result.history.length < end) return cb(); var ret = result.history.slice(fwdIndex, end); - if (!_.any(ret, function(i) { - return !!i; + if (_.any(ret, function(i) { + return !i; })) { // some items are not yet defined. return cb(); diff --git a/test/integration/server.js b/test/integration/server.js index 3ac5766..0b7f3c9 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -6243,7 +6243,7 @@ describe('Wallet service', function() { helpers.stubHistory(h); var storeTxHistoryCacheSpy = sinon.spy(server.storage, 'storeTxHistoryCache'); - var skip = 95; + var skip = 31; var limit = 10; server.getTxHistory({ @@ -6265,8 +6265,8 @@ describe('Wallet service', function() { calls[0].args[3].length.should.equal(5); // 5 txs have confirmations>= 100 // should be reversed! - calls[0].args[3][0].confirmations.should.equal(104); - calls[0].args[3][0].txid.should.equal(h[104].txid); + calls[0].args[3][0].confirmations.should.equal(skip + limit - 1); + calls[0].args[3][0].txid.should.equal(h[skip + limit - 1].txid); server.storage.storeTxHistoryCache.restore(); done(); }); @@ -6343,17 +6343,17 @@ describe('Wallet service', function() { next(); }); }, function() { - async.eachSeries(_.range(0, 200, 5), function(i, next) { + // Ask more that cached. + async.eachSeries(_.range(0, 210, 5), function(i, next) { server.getTxHistory({ skip: i, limit: 5, }, function(err, txs, fromCache) { should.not.exist(err); should.exist(txs); - txs.length.should.equal(5); var s = h.slice(i, i + 5); _.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); - fromCache.should.equal(i >= 100); + fromCache.should.equal(i >= Defaults.CONFIRMATIONS_TO_START_CACHING && i < 200); next(); }); }, done); @@ -6386,7 +6386,7 @@ describe('Wallet service', function() { txs.length.should.equal(5); var s = h.slice(i, i + 5); _.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); - fromCache.should.equal(i >= 100); + fromCache.should.equal(i >= Defaults.CONFIRMATIONS_TO_START_CACHING); next(); }); }, done); @@ -6413,7 +6413,7 @@ describe('Wallet service', function() { async.eachSeries(_.range(0, 200, 5), function(i, next) { function resetCache(cb) { - if (!(i%25)) { + if (!(i % 25)) { storage.softResetTxHistoryCache(server.walletId, function() { return cb(true); }); @@ -6432,7 +6432,7 @@ describe('Wallet service', function() { txs.length.should.equal(5); var s = h.slice(i, i + 5); _.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); - fromCache.should.equal(i >= 100 && !reset); + fromCache.should.equal(i >= Defaults.CONFIRMATIONS_TO_START_CACHING && !reset); next(); }); });