fix reconnect to insight
This commit is contained in:
parent
dd8fcaa1b1
commit
45fd46e3bc
5 changed files with 77 additions and 53 deletions
|
|
@ -4,7 +4,7 @@ var defaultConfig = {
|
|||
// DEFAULT network (livenet or testnet)
|
||||
networkName: 'testnet',
|
||||
forceNetwork: false,
|
||||
logLevel: 'info',
|
||||
logLevel: 'debug',
|
||||
|
||||
// DEFAULT unit: Bit
|
||||
unitName: 'bits',
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
var config = require('../config');
|
||||
var _ = require('underscore');
|
||||
|
||||
console.log('[log.js.3]'); //TODO
|
||||
/**
|
||||
* @desc
|
||||
* A simple logger that wraps the <tt>console.log</tt> methods when available.
|
||||
|
|
@ -100,6 +101,9 @@ Logger.prototype.setLevel = function(level) {
|
|||
*/
|
||||
|
||||
var logger = new Logger('copay');
|
||||
|
||||
console.log('Log level:' + config.logLevel);
|
||||
|
||||
logger.setLevel(config.logLevel);
|
||||
|
||||
module.exports = logger;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ var async = require('async');
|
|||
var request = require('request');
|
||||
var bitcore = require('bitcore');
|
||||
var io = require('socket.io-client');
|
||||
var log = require('../../log');
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
|
@ -57,7 +58,7 @@ Insight.prototype.STATUS = {
|
|||
/** @private */
|
||||
Insight.prototype.subscribeToBlocks = function() {
|
||||
var socket = this.getSocket();
|
||||
if (this.listeningBlocks || ! socket.connected) return;
|
||||
if (this.listeningBlocks || !socket.connected) return;
|
||||
|
||||
var self = this;
|
||||
socket.emit('subscribe', 'inv');
|
||||
|
|
@ -69,40 +70,46 @@ Insight.prototype.subscribeToBlocks = function() {
|
|||
|
||||
/** @private */
|
||||
Insight.prototype._getSocketIO = function(url, opts) {
|
||||
return io(this.url, this.opts);
|
||||
return io(this.url, this.opts);
|
||||
};
|
||||
|
||||
|
||||
Insight.prototype._setMainHandlers = function(url, opts) {
|
||||
// Emmit connection events
|
||||
var self = this;
|
||||
this.socket.on('connect', function() {
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
self.subscribeToBlocks();
|
||||
self.emit('connect', 0);
|
||||
});
|
||||
|
||||
this.socket.on('connect_error', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('connect_timeout', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('reconnect', function(attempt) {
|
||||
if (self.status != self.STATUS.DISCONNECTED) return;
|
||||
self.emit('reconnect', attempt);
|
||||
self.reSubscribe();
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/** @private */
|
||||
Insight.prototype.getSocket = function(url, opts) {
|
||||
|
||||
if (!this.socket) {
|
||||
this.socket = this._getSocketIO(this.url, this.opts);
|
||||
|
||||
// Emmit connection events
|
||||
var self = this;
|
||||
this.socket.on('connect', function() {
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
self.subscribeToBlocks();
|
||||
self.emit('connect', 0);
|
||||
});
|
||||
|
||||
this.socket.on('connect_error', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('connect_timeout', function() {
|
||||
if (self.status != self.STATUS.CONNECTED) return;
|
||||
self.status = self.STATUS.DISCONNECTED;
|
||||
self.emit('disconnect');
|
||||
});
|
||||
|
||||
this.socket.on('reconnect', function(attempt) {
|
||||
if (self.status != self.STATUS.DISCONNECTED) return;
|
||||
self.status = self.STATUS.CONNECTED;
|
||||
self.emit('connect', attempt);
|
||||
});
|
||||
this._setMainHandlers();
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
|
|
@ -124,8 +131,6 @@ Insight.prototype.requestPost = function(path, data, cb) {
|
|||
}
|
||||
|
||||
Insight.prototype.destroy = function() {
|
||||
|
||||
console.log('[Insight.js.127] INSIGHT destroy' ); //TODO
|
||||
var socket = this.getSocket();
|
||||
this.socket.disconnect();
|
||||
this.socket.removeAllListeners();
|
||||
|
|
@ -143,6 +148,9 @@ Insight.prototype.subscribe = function(addresses) {
|
|||
return function(txid) {
|
||||
// verify the address is still subscribed
|
||||
if (!self.subscribed[address]) return;
|
||||
|
||||
log.debug('insight tx event');
|
||||
|
||||
self.emit('tx', {
|
||||
address: address,
|
||||
txid: txid
|
||||
|
|
@ -150,14 +158,18 @@ Insight.prototype.subscribe = function(addresses) {
|
|||
}
|
||||
}
|
||||
|
||||
var s = self.getSocket();
|
||||
addresses.forEach(function(address) {
|
||||
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
||||
|
||||
// skip already subscibed
|
||||
if (!self.subscribed[address]) {
|
||||
self.subscribed[address] = true;
|
||||
self.getSocket().emit('subscribe', address);
|
||||
self.getSocket().on(address, handlerFor(self, address));
|
||||
var handler = handlerFor(self, address);
|
||||
self.subscribed[address] = handler;
|
||||
log.debug('Subcribe to: ', address);
|
||||
|
||||
s.emit('subscribe', address);
|
||||
s.on(address, handler);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -166,20 +178,20 @@ Insight.prototype.getSubscriptions = function(addresses) {
|
|||
return this.subscribed;
|
||||
}
|
||||
|
||||
Insight.prototype.unsubscribe = function(addresses) {
|
||||
addresses = Array.isArray(addresses) ? addresses : [addresses];
|
||||
var self = this;
|
||||
|
||||
addresses.forEach(function(address) {
|
||||
preconditions.checkArgument(new bitcore.Address(address).isValid());
|
||||
self.getSocket().removeEventListener(address);
|
||||
delete self.subscribed[address];
|
||||
});
|
||||
Insight.prototype.reSubscribe = function() {
|
||||
log.debug('insight reSubscribe');
|
||||
var allAddresses = Object.keys(this.subscribed);
|
||||
this.subscribed = {};
|
||||
var s = this.socket;
|
||||
if (s) {
|
||||
s.removeAllListeners();
|
||||
this._setMainHandlers();
|
||||
this.subscribe(allAddresses);
|
||||
this.subscribeToBlocks();
|
||||
}
|
||||
};
|
||||
|
||||
Insight.prototype.unsubscribeAll = function() {
|
||||
this.unsubscribe(Object.keys(this.subscribed));
|
||||
};
|
||||
|
||||
Insight.prototype.broadcast = function(rawtx, cb) {
|
||||
preconditions.checkArgument(rawtx);
|
||||
|
|
|
|||
|
|
@ -649,20 +649,27 @@ Wallet.prototype._lockIncomming = function() {
|
|||
|
||||
Wallet.prototype._setBlockchainListeners = function() {
|
||||
var self = this;
|
||||
this.blockchain.on('connect', self.emit.bind(self,'networkReconnected'));
|
||||
this.blockchain.on('disconnect', self.emit.bind(self,'networkError'));
|
||||
this.blockchain.removeAllListeners();
|
||||
|
||||
this.blockchain.on('reconnect', function(attempts) {
|
||||
log.debug('blockchain reconnect event');
|
||||
self.emit('insightReconnected');
|
||||
});
|
||||
|
||||
this.blockchain.on('disconnect', function() {
|
||||
log.debug('blockchain disconnect event');
|
||||
self.emit('insightError');
|
||||
});
|
||||
this.blockchain.on('tx', function(tx) {
|
||||
log.debug('blockchain tx event');
|
||||
self.emit('tx', tx.address);
|
||||
});
|
||||
|
||||
if (!self.spendUnconfirmed) {
|
||||
self.blockchain.on('block', self.emit.bind(self,'balanceUpdated'));
|
||||
self.blockchain.on('block', self.emit.bind(self, 'balanceUpdated'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @desc Sets up the networking with other peers.
|
||||
*
|
||||
|
|
@ -1887,7 +1894,7 @@ Wallet.prototype.getAddressesStr = function(opts) {
|
|||
* @desc Alias for {@link PublicKeyRing#getAddressesInfo}
|
||||
*/
|
||||
Wallet.prototype.getAddressesInfo = function(opts) {
|
||||
var addrInfo = this.publicKeyRing.getAddressesInfo(opts, this.publicKey);
|
||||
var addrInfo = this.publicKeyRing.getAddressesInfo(opts, this.publicKey);
|
||||
var currentAddrs = this.blockchain.getSubscriptions();
|
||||
|
||||
var newAddrs = [];
|
||||
|
|
|
|||
|
|
@ -83,14 +83,15 @@ angular.module('copayApp.services')
|
|||
});
|
||||
});
|
||||
|
||||
w.on('networkReconnected', function() {
|
||||
w.on('insightReconnected', function() {
|
||||
$rootScope.reconnecting = false;
|
||||
root.updateAddressList();
|
||||
root.updateBalance(function() {
|
||||
$rootScope.$digest();
|
||||
});
|
||||
});
|
||||
|
||||
w.on('networkError', function() {
|
||||
w.on('insightError', function() {
|
||||
$rootScope.reconnecting = true;
|
||||
$rootScope.$digest();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue