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', UPGRADE_NEEDED: 'Client app needs to be upgraded',
WALLET_ALREADY_EXISTS: 'Wallet already exists', WALLET_ALREADY_EXISTS: 'Wallet already exists',
WALLET_FULL: 'Wallet full', 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_COMPLETE: 'Wallet is not complete',
WALLET_NOT_FOUND: 'Wallet not found', 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 self = this;
var opts = opts || {}; var opts = opts || {};
self._getUtxosForCurrentWallet({ // This lock is to prevent server starvation on big wallets
coin: opts.coin, self._runLocked(cb, function(cb) {
addresses: opts.addresses self._getUtxosForCurrentWallet({
}, function(err, utxos) { coin: opts.coin,
if (err) return cb(err); addresses: opts.addresses
}, function(err, utxos) {
var balance = self._totalizeUtxos(utxos); if (err) return cb(err);
// Compute balance by address var balance = self._totalizeUtxos(utxos);
var byAddress = {};
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) {
byAddress[key] = {
address: key,
path: value.path,
amount: 0,
};
});
_.each(utxos, function(utxo) { // Compute balance by address
byAddress[utxo.address].amount += utxo.satoshis; 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) { WalletService.prototype._getBalanceOneStep = function(opts, cb) {
var self = this; 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) { self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
self._getBalanceFromAddresses({ 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 * @param {Boolean} opts.twoStep[=false] - Optional - Use 2 step balance computation for improved performance
* @returns {Object} balance - Total amount & locked amount. * @returns {Object} balance - Total amount & locked amount.
*/ */
WalletService.prototype.getBalance = function(opts, cb) {
WalletService.prototype.getBalance = function(opts, cb, i) {
var self = this; var self = this;
opts = opts || {}; opts = opts || {};
@ -1388,6 +1388,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
return self._getBalanceOneStep(opts, cb); return self._getBalanceOneStep(opts, cb);
} else { } else {
log.debug('Requesting partial balance for ' + activeAddresses.length + ' addresses'); log.debug('Requesting partial balance for ' + activeAddresses.length + ' addresses');
self._getBalanceFromAddresses({ self._getBalanceFromAddresses({
coin: opts.coin, coin: opts.coin,
addresses: activeAddresses addresses: activeAddresses
@ -1423,7 +1424,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
}); });
}, 1); }, 1);
return; return;
}); }, i);
} }
}); });
}); });

8
test/integration/server.js

@ -2046,7 +2046,6 @@ describe('Wallet service', function() {
server.getBalance({ server.getBalance({
twoStep: true twoStep: true
}, function(err, balance) { }, function(err, balance) {
console.log('[server.js.2048:err:]',err); //TODO
should.not.exist(err); should.not.exist(err);
should.exist(balance); should.exist(balance);
@ -2451,7 +2450,10 @@ console.log('[server.js.2048:err:]',err); //TODO
should.exist(balance); should.exist(balance);
balance.totalAmount.should.equal(helpers.toSatoshi(3)); balance.totalAmount.should.equal(helpers.toSatoshi(3));
next(); next();
}); }, 1);
},
function(next) {
setTimeout(next, 100);
}, },
function(next) { function(next) {
server.createAddress({}, function(err, addr) { server.createAddress({}, function(err, addr) {
@ -2471,7 +2473,7 @@ console.log('[server.js.2048:err:]',err); //TODO
should.exist(balance); should.exist(balance);
balance.totalAmount.should.equal(helpers.toSatoshi(3.5)); balance.totalAmount.should.equal(helpers.toSatoshi(3.5));
next(); next();
}); }, 2);
}, },
function(next) { function(next) {
setTimeout(next, 100); setTimeout(next, 100);

Loading…
Cancel
Save