From 77547e65aae95efeb08e4dac25baccc303889a4e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 29 Jul 2016 11:51:45 -0300 Subject: [PATCH] fix padding last request --- lib/storage.js | 18 ++++++++++++------ test/integration/server.js | 7 ++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index ca57e70..45b3f2f 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -617,7 +617,7 @@ Storage.prototype.storeActiveAddresses = function(walletId, addresses, cb) { Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { var self = this; $.checkArgument(from >= 0); - $.checkArgument(from<=to); + $.checkArgument(from <= to); self.db.collection(collections.CACHE).findOne({ walletId: walletId, @@ -631,10 +631,16 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { // Reverse indexes var fwdIndex = result.totalItems - to; - if (fwdIndex < 0) - return cb(); + if (fwdIndex < 0) { + fwdIndex = 0; + } + + var end = result.totalItems - from; + + // nothing to return + if (end <=0) + return cb(null, []); - var end = fwdIndex + to - from; // Cache is OK. self.db.collection(collections.CACHE).findOne({ @@ -653,7 +659,7 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { return !i; })) { // some items are not yet defined. - return cb(); + return cb(); } return cb(null, ret.reverse()); }); @@ -740,7 +746,7 @@ Storage.prototype.storeTxHistoryCache = function(walletId, totalItems, firstPosi walletId: walletId, type: 'historyCache', key: null, - history:h + history: h }, { w: 1, upsert: true, diff --git a/test/integration/server.js b/test/integration/server.js index 0b7f3c9..47f30bd 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -6334,6 +6334,7 @@ describe('Wallet service', function() { skip: i, limit: 5, }, function(err, txs, fromCache) { + should.not.exist(err); should.exist(txs); txs.length.should.equal(5); @@ -6344,14 +6345,14 @@ describe('Wallet service', function() { }); }, function() { // Ask more that cached. - async.eachSeries(_.range(0, 210, 5), function(i, next) { + async.eachSeries(_.range(0, 210, 7), function(i, next) { server.getTxHistory({ skip: i, - limit: 5, + limit: 7, }, function(err, txs, fromCache) { should.not.exist(err); should.exist(txs); - var s = h.slice(i, i + 5); + var s = h.slice(i, i + 7); _.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); fromCache.should.equal(i >= Defaults.CONFIRMATIONS_TO_START_CACHING && i < 200); next();