diff --git a/lib/errors/errordefinitions.js b/lib/errors/errordefinitions.js index f5c8c2f..245fb20 100644 --- a/lib/errors/errordefinitions.js +++ b/lib/errors/errordefinitions.js @@ -33,7 +33,7 @@ var errors = { UPGRADE_NEEDED: 'Client app needs to be upgraded', WALLET_ALREADY_EXISTS: 'Wallet already exists', WALLET_FULL: 'Wallet full', - WALLET_LOCKED: 'Wallet is locked', + WALLET_LOCKED: 'Wallet is busy, try later', WALLET_NOT_COMPLETE: 'Wallet is not complete', WALLET_NOT_FOUND: 'Wallet not found', }; diff --git a/lib/server.js b/lib/server.js index d821384..5cb776e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1241,44 +1241,44 @@ WalletService.prototype._totalizeUtxos = function(utxos) { }; -WalletService.prototype._getBalanceFromAddresses = function(opts, cb) { +WalletService.prototype._getBalanceFromAddresses = function(opts, cb, i) { var self = this; var opts = opts || {}; - self._getUtxosForCurrentWallet({ - coin: opts.coin, - addresses: opts.addresses - }, function(err, utxos) { - if (err) return cb(err); + // This lock is to prevent server starvation on big wallets + self._runLocked(cb, function(cb) { + self._getUtxosForCurrentWallet({ + coin: opts.coin, + addresses: opts.addresses + }, function(err, utxos) { + if (err) return cb(err); - var balance = self._totalizeUtxos(utxos); + var balance = self._totalizeUtxos(utxos); - // Compute balance by address - var byAddress = {}; - _.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) { - byAddress[key] = { - address: key, - path: value.path, - amount: 0, - }; + // Compute balance by address + var byAddress = {}; + _.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) { + byAddress[key] = { + address: key, + path: value.path, + amount: 0, + }; + }); + + _.each(utxos, function(utxo) { + byAddress[utxo.address].amount += utxo.satoshis; + }); + + balance.byAddress = _.values(byAddress); + return cb(null, balance); }); - - _.each(utxos, function(utxo) { - byAddress[utxo.address].amount += utxo.satoshis; - }); - - balance.byAddress = _.values(byAddress); - - return cb(null, balance); }); }; WalletService.prototype._getBalanceOneStep = function(opts, cb) { var self = this; - // This lock is to prevent server starvation on big wallets - self._runLocked(cb, function(cb) { self.storage.fetchAddresses(self.walletId, function(err, addresses) { if (err) return cb(err); self._getBalanceFromAddresses({ @@ -1297,7 +1297,6 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) { }); }); }); - }); }; @@ -1356,7 +1355,8 @@ WalletService.prototype._checkAndUpdateAddressCount = function(twoStepCache, cb) * @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance * @returns {Object} balance - Total amount & locked amount. */ -WalletService.prototype.getBalance = function(opts, cb) { + +WalletService.prototype.getBalance = function(opts, cb, i) { var self = this; opts = opts || {}; @@ -1388,6 +1388,7 @@ WalletService.prototype.getBalance = function(opts, cb) { return self._getBalanceOneStep(opts, cb); } else { log.debug('Requesting partial balance for ' + activeAddresses.length + ' addresses'); + self._getBalanceFromAddresses({ coin: opts.coin, addresses: activeAddresses @@ -1423,7 +1424,7 @@ WalletService.prototype.getBalance = function(opts, cb) { }); }, 1); return; - }); + }, i); } }); }); diff --git a/test/integration/server.js b/test/integration/server.js index f03b8fb..42f3d20 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -2046,7 +2046,6 @@ describe('Wallet service', function() { server.getBalance({ twoStep: true }, function(err, balance) { -console.log('[server.js.2048:err:]',err); //TODO should.not.exist(err); should.exist(balance); @@ -2451,7 +2450,10 @@ console.log('[server.js.2048:err:]',err); //TODO should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3)); next(); - }); + }, 1); + }, + function(next) { + setTimeout(next, 100); }, function(next) { server.createAddress({}, function(err, addr) { @@ -2471,7 +2473,7 @@ console.log('[server.js.2048:err:]',err); //TODO should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3.5)); next(); - }); + }, 2); }, function(next) { setTimeout(next, 100);