Merge pull request #464 from isocolsky/fix/2-step-balance

Fix/2 step balance
This commit is contained in:
Matias Alejo Garcia 2016-02-18 16:14:57 -03:00
commit 77141d19ab
5 changed files with 11 additions and 12 deletions

View File

@ -34,7 +34,8 @@ Insight.prototype.getConnectionInfo = function() {
/**
* Retrieve a list of unspent outputs associated with an address or set of addresses
*/
Insight.prototype.getUnspentUtxos = function(addresses, cb) {
Insight.prototype.getUtxos = function(addresses, cb) {
var url = this.url + this.apiPrefix + '/addrs/utxo';
var args = {
method: 'POST',
hosts: this.hosts,
@ -73,7 +74,7 @@ Insight.prototype.getTransaction = function(txid, cb) {
var args = {
method: 'GET',
hosts: this.hosts,
path: this.apiPrefix + '/tx/' + txid,
path: this.apiPrefix + '/tx/' + txid,
json: true,
};

View File

@ -876,7 +876,7 @@ WalletService.prototype._getUtxos = function(addresses, cb) {
var networkName = Bitcore.Address(addresses[0]).toObject().network;
var bc = self._getBlockchainExplorer(networkName);
bc.getUnspentUtxos(addresses, function(err, utxos) {
bc.getUtxos(addresses, function(err, utxos) {
if (err) return cb(err);
var utxos = _.map(utxos, function(utxo) {
@ -1018,7 +1018,7 @@ WalletService.prototype._getBalanceFromAddresses = function(addresses, cb) {
// Compute balance by address
var byAddress = {};
_.each(_.indexBy(utxos, 'address'), function(value, key) {
_.each(_.indexBy(_.sortBy(utxos, 'address'), 'address'), function(value, key) {
byAddress[key] = {
address: key,
path: value.path,
@ -1121,7 +1121,6 @@ WalletService.prototype.getBalance = function(opts, cb) {
if (nbAddresses < Defaults.TWO_STEP_BALANCE_THRESHOLD) {
return self._getBalanceOneStep(opts, cb);
}
self._getActiveAddresses(function(err, activeAddresses) {
if (err) return cb(err);
if (!_.isArray(activeAddresses)) {
@ -1135,7 +1134,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
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');
log.info('Balance in active addresses differs from final balance');
self._notify('BalanceUpdated', fullBalance, {
isGlobal: true
});

View File

@ -15,10 +15,9 @@ describe('Blockchain explorer', function() {
});
should.exist(exp);
exp.should.respondTo('broadcast');
exp.should.respondTo('getUnspentUtxos');
exp.should.respondTo('getUtxos');
exp.should.respondTo('getTransactions');
exp.should.respondTo('getAddressActivity');
exp.should.respondTo('getUnspentUtxos');
exp.should.respondTo('estimateFee');
exp.should.respondTo('initSocket');
var exp = new BlockchainExplorer({

View File

@ -271,7 +271,7 @@ helpers.stubUtxos = function(server, wallet, amounts, opts, cb) {
helpers._utxos = utxos;
}
blockchainExplorer.getUnspentUtxos = function(addresses, cb) {
blockchainExplorer.getUtxos = function(addresses, cb) {
var selected = _.filter(helpers._utxos, function(utxo) {
return _.contains(addresses, utxo.address);
});

View File

@ -1452,7 +1452,7 @@ describe('Wallet service', function() {
});
});
it('should get balance when there are no funds', function(done) {
blockchainExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, null, []);
blockchainExplorer.getUtxos = sinon.stub().callsArgWith(1, null, []);
server.createAddress({}, function(err, address) {
should.not.exist(err);
server.getBalance({}, function(err, balance) {
@ -1495,7 +1495,7 @@ describe('Wallet service', function() {
});
});
it('should fail gracefully when blockchain is unreachable', function(done) {
blockchainExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, 'dummy error');
blockchainExplorer.getUtxos = sinon.stub().callsArgWith(1, 'dummy error');
server.createAddress({}, function(err, address) {
should.not.exist(err);
server.getBalance({}, function(err, balance) {
@ -2135,7 +2135,7 @@ describe('Wallet service', function() {
});
it('should fail gracefully if unable to reach the blockchain', function(done) {
blockchainExplorer.getUnspentUtxos = sinon.stub().callsArgWith(1, 'dummy error');
blockchainExplorer.getUtxos = sinon.stub().callsArgWith(1, 'dummy error');
server.createAddress({}, function(err, address) {
should.not.exist(err);
var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 80, TestData.copayers[0].privKey_1H_0, {