keep getStatus() method, add twoStep param

This commit is contained in:
Ivan Socolsky 2015-12-11 16:32:38 -03:00
parent 82f54f7901
commit 87b96d4a8f
4 changed files with 46 additions and 28 deletions

View File

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

View File

@ -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;

View File

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

View File

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