Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix tests, add lock to long utxo queries
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Nov 1, 2017
1 parent 3a75b38 commit 002f9c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/errors/errordefinitions.js
Expand Up @@ -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',
};
Expand Down
55 changes: 28 additions & 27 deletions lib/server.js
Expand Up @@ -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);

var balance = self._totalizeUtxos(utxos);
// 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);

// Compute balance by address
var byAddress = {};
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) {
byAddress[key] = {
address: key,
path: value.path,
amount: 0,
};
});
var balance = self._totalizeUtxos(utxos);

_.each(utxos, function(utxo) {
byAddress[utxo.address].amount += utxo.satoshis;
});
// Compute balance by address
var byAddress = {};
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) {
byAddress[key] = {
address: key,
path: value.path,
amount: 0,
};
});

balance.byAddress = _.values(byAddress);
_.each(utxos, function(utxo) {
byAddress[utxo.address].amount += utxo.satoshis;
});

return cb(null, balance);
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({
Expand All @@ -1297,7 +1297,6 @@ WalletService.prototype._getBalanceOneStep = function(opts, cb) {
});
});
});
});
};


Expand Down Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1423,7 +1424,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
});
}, 1);
return;
});
}, i);
}
});
});
Expand Down
8 changes: 5 additions & 3 deletions test/integration/server.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 002f9c7

Please sign in to comment.