Browse Source

fix tests, add lock to long utxo queries

feat/2-step-opt
Matias Alejo Garcia 7 years ago
parent
commit
002f9c77a6
No known key found for this signature in database GPG Key ID: 2470DB551277AB3
  1. 2
      lib/errors/errordefinitions.js
  2. 55
      lib/server.js
  3. 8
      test/integration/server.js

2
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',
};

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

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

Loading…
Cancel
Save