From 4198d5c47c45d79b0895081a7efb9914298f536a Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 14 Dec 2015 17:04:37 -0300 Subject: [PATCH] handle initial conditions (empty cache) --- lib/server.js | 26 ++++++++++++++++++++------ lib/storage.js | 30 +++++++++++++++++++++++++++--- test/integration/server.js | 14 +++++++++----- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/lib/server.js b/lib/server.js index f8cf55e..2c8d479 100644 --- a/lib/server.js +++ b/lib/server.js @@ -866,8 +866,12 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) { async.waterfall([ function(next) { - if (_.isArray(addresses) && addresses.length > 0) { - next(null, addresses); + if (_.isArray(addresses)) { + if (!_.isEmpty(addresses)) { + next(null, addresses); + } else { + next(null, []); + } } else { self.storage.fetchAddresses(self.walletId, next); } @@ -1002,8 +1006,16 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) { if (err) return cb(err); // Update cache - var active = _.pluck(balance.byAddress, 'address') - self.storage.storeActiveAddresses(self.walletId, active, function(err) { + async.series([ + + function(next) { + self.storage.cleanActiveAddresses(self.walletId, next); + }, + function(next) { + var active = _.pluck(balance.byAddress, 'address') + self.storage.storeActiveAddresses(self.walletId, active, next); + }, + ], function(err) { if (err) { log.warn('Could not update wallet cache', err); } @@ -1020,9 +1032,11 @@ WalletService.prototype._getActiveAddresses = function(cb) { self.storage.fetchActiveAddresses(self.walletId, function(err, active) { if (err) { log.warn('Could not fetch active addresses from cache', err); - return cb(null, []); + return cb(); } + if (!_.isArray(active)) return cb(); + self.storage.fetchAddresses(self.walletId, function(err, allAddresses) { if (err) return cb(err); @@ -1064,7 +1078,7 @@ WalletService.prototype.getBalance = function(opts, cb) { self._getActiveAddresses(function(err, activeAddresses) { if (err) return cb(err); - if (_.isEmpty(activeAddresses)) { + if (!_.isArray(activeAddresses)) { return self._getBalanceOneStep(opts, cb); } else { self._getBalanceFromAddresses(activeAddresses, function(err, partialBalance) { diff --git a/lib/storage.js b/lib/storage.js index 5122c5c..d159ece 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -513,6 +513,31 @@ Storage.prototype.fetchEmailByNotification = function(notificationId, cb) { }); }; +Storage.prototype.cleanActiveAddresses = function(walletId, cb) { + var self = this; + + async.series([ + + function(next) { + self.db.collection(collections.CACHE).remove({ + walletId: walletId, + type: 'activeAddresses', + }, { + w: 1 + }, next); + }, + function(next) { + self.db.collection(collections.CACHE).insert({ + walletId: walletId, + type: 'activeAddresses', + key: null + }, { + w: 1 + }, next); + }, + ], cb); +}; + Storage.prototype.storeActiveAddresses = function(walletId, addresses, cb) { var self = this; @@ -521,7 +546,6 @@ Storage.prototype.storeActiveAddresses = function(walletId, addresses, cb) { walletId: walletId, type: 'activeAddresses', key: address, - value: null, }; self.db.collection(collections.CACHE).update({ walletId: record.walletId, @@ -542,9 +566,9 @@ Storage.prototype.fetchActiveAddresses = function(walletId, cb) { type: 'activeAddresses', }).toArray(function(err, result) { if (err) return cb(err); - if (!result || _.isEmpty(result)) return cb(null, []); + if (_.isEmpty(result)) return cb(); - return cb(null, _.pluck(result, 'key')); + return cb(null, _.compact(_.pluck(result, 'key'))); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index be7ee25..c0ea169 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1562,10 +1562,14 @@ describe('Wallet service', function() { clock.tick(7 * 24 * 3600 * 1000); helpers.createAddresses(server, wallet, 2, 0, function(addrs) { newAddrs = addrs; - helpers.stubUtxos(server, wallet, [1, 2], { - addresses: [oldAddrs[0], newAddrs[0]], - }, function() { - next(); + server._getActiveAddresses(function(err, active) { + should.not.exist(err); + should.not.exist(active); + helpers.stubUtxos(server, wallet, [1, 2], { + addresses: [oldAddrs[0], newAddrs[0]], + }, function() { + next(); + }); }); }); }, @@ -1575,7 +1579,7 @@ describe('Wallet service', function() { }, function(err, balance) { should.not.exist(err); should.exist(balance); - balance.totalAmount.should.equal(helpers.toSatoshi(2)); + balance.totalAmount.should.equal(helpers.toSatoshi(3)); next(); }); },