Browse Source

fix bug when asking beyong cache limit

activeAddress
Matias Alejo Garcia 9 years ago
parent
commit
c220a09354
No known key found for this signature in database GPG Key ID: 2470DB551277AB3
  1. 1
      lib/server.js
  2. 12
      lib/storage.js
  3. 18
      test/integration/server.js

1
lib/server.js

@ -2846,7 +2846,6 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
var proposals = res[0]; var proposals = res[0];
var notes = res[2]; var notes = res[2];
var finalTxs = decorate(normalizedTxs, addresses, proposals, notes); var finalTxs = decorate(normalizedTxs, addresses, proposals, notes);
if (fromCache) if (fromCache)

12
lib/storage.js

@ -616,6 +616,8 @@ Storage.prototype.storeActiveAddresses = function(walletId, addresses, cb) {
Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) { Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) {
var self = this; var self = this;
$.checkArgument(from >= 0);
$.checkArgument(from<=to);
self.db.collection(collections.CACHE).findOne({ self.db.collection(collections.CACHE).findOne({
walletId: walletId, walletId: walletId,
@ -628,7 +630,9 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) {
// Reverse indexes // Reverse indexes
var fwdIndex = result.totalItems - to; var fwdIndex = result.totalItems - to;
if (fwdIndex < 0) fwdIndex = 0;
if (fwdIndex < 0)
return cb();
var end = fwdIndex + to - from; var end = fwdIndex + to - from;
@ -640,13 +644,13 @@ Storage.prototype.getTxHistoryCache = function(walletId, from, to, cb) {
}, function(err, result) { }, function(err, result) {
if (err) return cb(err); if (err) return cb(err);
if (result.history.length < fwdIndex) if (result.history.length < end)
return cb(); return cb();
var ret = result.history.slice(fwdIndex, end); var ret = result.history.slice(fwdIndex, end);
if (!_.any(ret, function(i) { if (_.any(ret, function(i) {
return !!i; return !i;
})) { })) {
// some items are not yet defined. // some items are not yet defined.
return cb(); return cb();

18
test/integration/server.js

@ -6243,7 +6243,7 @@ describe('Wallet service', function() {
helpers.stubHistory(h); helpers.stubHistory(h);
var storeTxHistoryCacheSpy = sinon.spy(server.storage, 'storeTxHistoryCache'); var storeTxHistoryCacheSpy = sinon.spy(server.storage, 'storeTxHistoryCache');
var skip = 95; var skip = 31;
var limit = 10; var limit = 10;
server.getTxHistory({ server.getTxHistory({
@ -6265,8 +6265,8 @@ describe('Wallet service', function() {
calls[0].args[3].length.should.equal(5); // 5 txs have confirmations>= 100 calls[0].args[3].length.should.equal(5); // 5 txs have confirmations>= 100
// should be reversed! // should be reversed!
calls[0].args[3][0].confirmations.should.equal(104); calls[0].args[3][0].confirmations.should.equal(skip + limit - 1);
calls[0].args[3][0].txid.should.equal(h[104].txid); calls[0].args[3][0].txid.should.equal(h[skip + limit - 1].txid);
server.storage.storeTxHistoryCache.restore(); server.storage.storeTxHistoryCache.restore();
done(); done();
}); });
@ -6343,17 +6343,17 @@ describe('Wallet service', function() {
next(); next();
}); });
}, function() { }, 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({ server.getTxHistory({
skip: i, skip: i,
limit: 5, limit: 5,
}, function(err, txs, fromCache) { }, function(err, txs, fromCache) {
should.not.exist(err); should.not.exist(err);
should.exist(txs); should.exist(txs);
txs.length.should.equal(5);
var s = h.slice(i, i + 5); var s = h.slice(i, i + 5);
_.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); _.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(); next();
}); });
}, done); }, done);
@ -6386,7 +6386,7 @@ describe('Wallet service', function() {
txs.length.should.equal(5); txs.length.should.equal(5);
var s = h.slice(i, i + 5); var s = h.slice(i, i + 5);
_.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); _.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid'));
fromCache.should.equal(i >= 100); fromCache.should.equal(i >= Defaults.CONFIRMATIONS_TO_START_CACHING);
next(); next();
}); });
}, done); }, done);
@ -6413,7 +6413,7 @@ describe('Wallet service', function() {
async.eachSeries(_.range(0, 200, 5), function(i, next) { async.eachSeries(_.range(0, 200, 5), function(i, next) {
function resetCache(cb) { function resetCache(cb) {
if (!(i%25)) { if (!(i % 25)) {
storage.softResetTxHistoryCache(server.walletId, function() { storage.softResetTxHistoryCache(server.walletId, function() {
return cb(true); return cb(true);
}); });
@ -6432,7 +6432,7 @@ describe('Wallet service', function() {
txs.length.should.equal(5); txs.length.should.equal(5);
var s = h.slice(i, i + 5); var s = h.slice(i, i + 5);
_.pluck(txs, 'txid').should.deep.equal(_.pluck(s, 'txid')); _.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(); next();
}); });
}); });

Loading…
Cancel
Save