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