diff --git a/lib/expressapp.js b/lib/expressapp.js index 60c3639..c78b1d2 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -214,6 +214,7 @@ ExpressApp.prototype.start = function(opts, cb) { getServerWithAuth(req, res, function(server) { var opts = {}; if (req.query.includeExtendedInfo == '1') opts.includeExtendedInfo = true; + if (req.query.twoStep == '1') opts.twoStep = true; server.getStatus(opts, function(err, status) { if (err) return returnError(err, res, req); @@ -316,8 +317,9 @@ ExpressApp.prototype.start = function(opts, cb) { router.get('/v1/balance/', function(req, res) { getServerWithAuth(req, res, function(server) { - var balanceFn = req.query.cache == '1' ? _.bind(server.getBalance2Steps, server) : _.bind(server.getBalance, server); - balanceFn({}, function(err, balance) { + var opts = {}; + if (req.query.twoStep == '1') opts.twoStep = true; + server.getBalance(opts, function(err, balance) { if (err) return returnError(err, res, req); res.json(balance); }); diff --git a/lib/server.js b/lib/server.js index 7e97009..f8cf55e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -263,6 +263,7 @@ WalletService.prototype.getWallet = function(opts, cb) { /** * Retrieves wallet status. * @param {Object} opts + * @param {Object} opts.twoStep[=false] - Optional: use 2-step balance computation for improved performance * @param {Object} opts.includeExtendedInfo - Include PKR info & address managers for wallet & copayers * @returns {Object} status */ @@ -296,7 +297,7 @@ WalletService.prototype.getStatus = function(opts, cb) { }); }, function(next) { - self.getBalance({}, function(err, balance) { + self.getBalance(opts, function(err, balance) { if (err) return next(err); status.balance = balance; next(); @@ -992,12 +993,7 @@ WalletService.prototype._getBalanceFromAddresses = function(addresses, cb) { }); }; -/** - * Get wallet balance. - * @param {Object} opts - * @returns {Object} balance - Total amount & locked amount. - */ -WalletService.prototype.getBalance = function(opts, cb) { +WalletService.prototype._getBalanceOneStep = function(opts, cb) { var self = this; self.storage.fetchAddresses(self.walletId, function(err, addresses) { @@ -1049,27 +1045,33 @@ WalletService.prototype._getActiveAddresses = function(cb) { /** * Get wallet balance. * @param {Object} opts + * @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance * @returns {Object} balance - Total amount & locked amount. */ -WalletService.prototype.getBalance2Steps = function(opts, cb) { +WalletService.prototype.getBalance = function(opts, cb) { var self = this; + opts = opts || {}; + + if (!opts.twoStep) + return self._getBalanceOneStep(opts, cb); + self.storage.countAddresses(self.walletId, function(err, nbAddresses) { if (err) return cb(err); if (nbAddresses < Defaults.TWO_STEP_BALANCE_THRESHOLD) { - return self.getBalance(opts, cb); + return self._getBalanceOneStep(opts, cb); } self._getActiveAddresses(function(err, activeAddresses) { if (err) return cb(err); if (_.isEmpty(activeAddresses)) { - return self.getBalance(opts, cb); + return self._getBalanceOneStep(opts, cb); } else { self._getBalanceFromAddresses(activeAddresses, function(err, partialBalance) { if (err) return cb(err); cb(null, partialBalance); setTimeout(function() { - self.getBalance(opts, function(err, fullBalance) { + self._getBalanceOneStep(opts, function(err, fullBalance) { if (err) return; if (!_.isEqual(partialBalance, fullBalance)) { log.debug('Cache miss: balance in active addresses differs from final balance'); @@ -1084,7 +1086,6 @@ WalletService.prototype.getBalance2Steps = function(opts, cb) { }); }; - WalletService.prototype._sampleFeeLevels = function(network, points, cb) { var self = this; diff --git a/test/expressapp.js b/test/expressapp.js index a97f113..3f20506 100644 --- a/test/expressapp.js +++ b/test/expressapp.js @@ -117,7 +117,6 @@ describe('ExpressApp', function() { it('should handle cache argument', function(done) { var server = { getBalance: sinon.stub().callsArgWith(1, null, {}), - getBalance2Steps: sinon.stub().callsArgWith(1, null, {}), }; var TestExpressApp = proxyquire('../lib/expressapp', { './server': { @@ -136,15 +135,15 @@ describe('ExpressApp', function() { request(reqOpts, function(err, res, body) { should.not.exist(err); res.statusCode.should.equal(200); - server.getBalance.calledOnce.should.be.true; - server.getBalance2Steps.calledOnce.should.be.false; + var args = server.getBalance.getCalls()[0].args[0]; + should.not.exist(args.twoStep); - reqOpts.url += '?cache=1'; + reqOpts.url += '?twoStep=1'; request(reqOpts, function(err, res, body) { should.not.exist(err); res.statusCode.should.equal(200); - server.getBalance.calledTwice.should.be.false; - server.getBalance2Steps.calledOnce.should.be.true; + var args = server.getBalance.getCalls()[1].args[0]; + args.twoStep.should.equal(true); done(); }); }); diff --git a/test/integration/server.js b/test/integration/server.js index a8ffb67..be7ee25 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -1524,7 +1524,9 @@ describe('Wallet service', function() { it('should get balance', function(done) { helpers.stubUtxos(server, wallet, [1, 'u2', 3], function() { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(6)); @@ -1568,7 +1570,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(2)); @@ -1595,7 +1599,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3)); @@ -1644,7 +1650,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3)); @@ -1663,7 +1671,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3.5)); @@ -1703,7 +1713,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3)); @@ -1721,7 +1733,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3.5)); @@ -1769,7 +1783,9 @@ describe('Wallet service', function() { }); }, function(next) { - server.getBalance2Steps({}, function(err, balance) { + server.getBalance({ + twoStep: true + }, function(err, balance) { should.not.exist(err); should.exist(balance); balance.totalAmount.should.equal(helpers.toSatoshi(3));