handle initial conditions (empty cache)

This commit is contained in:
Ivan Socolsky 2015-12-14 17:04:37 -03:00
parent 87b96d4a8f
commit 4198d5c47c
3 changed files with 56 additions and 14 deletions

View File

@ -866,8 +866,12 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) {
async.waterfall([
function(next) {
if (_.isArray(addresses) && addresses.length > 0) {
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
async.series([
function(next) {
self.storage.cleanActiveAddresses(self.walletId, next);
},
function(next) {
var active = _.pluck(balance.byAddress, 'address')
self.storage.storeActiveAddresses(self.walletId, active, function(err) {
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) {

View File

@ -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')));
});
};

View File

@ -1562,12 +1562,16 @@ describe('Wallet service', function() {
clock.tick(7 * 24 * 3600 * 1000);
helpers.createAddresses(server, wallet, 2, 0, function(addrs) {
newAddrs = addrs;
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();
});
});
});
},
function(next) {
server.getBalance({
@ -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();
});
},