diff --git a/config.js b/config.js
index 0fe548945..19ec917b5 100644
--- a/config.js
+++ b/config.js
@@ -109,12 +109,15 @@ var defaultConfig = {
// blockchain service API config
blockchain: {
host: 'test.insight.is',
- port: 80
+ port: 80,
+ retryDelay: 1000,
},
// socket service API config
socket: {
host: 'test.insight.is',
- port: 80
+ port: 80,
+ // will duplicate itself after each try
+ reconnectDelay: 500,
},
// local encryption/security config
diff --git a/index.html b/index.html
index 1f86ca8ac..c3e365e55 100644
--- a/index.html
+++ b/index.html
@@ -82,12 +82,12 @@
-
+
- Error connecting to Insight server. Check
- you settings and Internet connection.
- Trying to reconnect...
+ Having troubles connecting to Insight server. Check
+ you settings and Internet connection.
+ Reconnect Atempt #{{$root.insightError}}
diff --git a/js/controllers/header.js b/js/controllers/header.js
index 66acd9080..757b67958 100644
--- a/js/controllers/header.js
+++ b/js/controllers/header.js
@@ -35,19 +35,15 @@ angular.module('copayApp.controllers').controller('HeaderController',
// Initialize alert notification (not show when init wallet)
$rootScope.txAlertCount = 0;
+ $rootScope.insightError = -1;
- $rootScope.$watch('blockChainStatus', function(status) {
- var h = config.blockchain.host + ':' + config.blockchain.port;
- if (status === 'error')
- $rootScope.insightError = 1;
- else
- if (status === 'restored') {
- $rootScope.insightError = 0;
- $rootScope.$flashMessage = {
- type: 'success',
- message: 'Networking Restored',
- };
- }
+ $rootScope.$watch('insightError', function(status) {
+ if (status === 0) {
+ $rootScope.$flashMessage = {
+ type: 'success',
+ message: 'Networking Restored :)',
+ };
+ }
});
diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js
index 296a7725e..7d6049ff7 100644
--- a/js/models/blockchain/Insight.js
+++ b/js/models/blockchain/Insight.js
@@ -8,6 +8,7 @@ function Insight(opts) {
this.host = opts.host || 'localhost';
this.port = opts.port || '3001';
this.scheme = opts.scheme || 'http';
+ this.retryDelay = opts.retryDelay || 5000;
}
function _asyncForEach(array, fn, callback) {
@@ -123,11 +124,13 @@ Insight.prototype.getUnspent = function(addresses, cb) {
}
};
+ var self = this;
this._request(options, function(err, res) {
if (err) {
return cb(err);
}
+
if (res && res.length > 0) {
all = all.concat(res);
}
@@ -157,6 +160,9 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
};
Insight.prototype._request = function(options, callback) {
+
+
+ var self = this;
if (typeof process === 'undefined' || !process.version) {
var request = new XMLHttpRequest();
@@ -174,28 +180,38 @@ Insight.prototype._request = function(options, callback) {
}
request.open(options.method, url, true);
- request.timeout = 10000;
+ request.timeout = 5000;
request.ontimeout = function() {
- return callback({
- message: 'Insight request timeout. Please check your Insight settings or the Insight server status.'
- });
+ console.log('Insight timeout...retrying', err, self.retryDelay); //TODO
+ setTimeout(function() {
+ return self._request(options,callback);
+ }, self.retryDelay);
+ return callback(new Error('Insight request timeout'));
};
request.onreadystatechange = function() {
if (request.readyState === 4) {
- if (request.status === 200 || request.status === 304 || request.status === 0) {
+ if (request.status === 200 || request.status === 304) {
try {
var ret = JSON.parse(request.responseText);
return callback(null, ret);
} catch (e) {
- return callback({
- message: 'Wrong response from insight'
- });
+ return callback(new Error('CRITICAL: Wrong response from insight'));
}
- } else {
- return callback({
- message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText
- });
+ }
+ // User error
+ else if (request.status >= 400 && request.status < 499) {
+ return callback(new Error('CRITICAL: Bad request to insight. Probably wrong transaction to broadcast?.'));
+ }
+ else {
+ var err= 'Error code: ' + request.status + ' - Status: ' + request.statusText
+ + ' - Description: ' + request.responseText;
+ console.log('Insight Temporary error (will retry):', err);
+ setTimeout(function() {
+ console.log('### Retrying Insight Request....'); //TODO
+ return self._request(options,callback);
+ }, self.retryDelay);
+ return callback(new Error(err));
}
}
};
@@ -205,7 +221,9 @@ Insight.prototype._request = function(options, callback) {
}
request.send(options.data || null);
- } else {
+ }
+
+ else {
var http = require('http');
var req = http.request(options, function(response) {
var ret;
diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js
index 013ede6b1..898400ae2 100644
--- a/js/services/controllerUtils.js
+++ b/js/services/controllerUtils.js
@@ -101,22 +101,21 @@ angular.module('copayApp.services')
root.updateBalance = function(cb) {
var w = $rootScope.wallet;
+ if (!w) return root.onErrorDigest();
+
$rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true;
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
if (err) {
- $rootScope.$flashMessage = {
- type: 'error',
- message: 'Error: ' + err.message
- };
-
- $rootScope.$digest();
console.error('Error: ' + err.message); //TODO
-
+ root._setCommError();
return null;
}
-
+ else {
+ root._clearCommError();
+ }
+
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
$rootScope.availableBalance = safeBalance;
@@ -175,13 +174,15 @@ angular.module('copayApp.services')
};
root._setCommError = function(e) {
- $rootScope.blockChainStatus='error';
+ // first error ever?
+ if ($rootScope.insightError<0)
+ $rootScope.insightError=0;
+ $rootScope.insightError++;
};
root._clearCommError = function(e) {
- if ($rootScope.blockChainStatus==='error')
- $rootScope.blockChainStatus='restored';
+ $rootScope.insightError=0;
};
root.setSocketHandlers = function() {
diff --git a/js/services/socket.js b/js/services/socket.js
index 54609a3c9..dcbeabd95 100644
--- a/js/services/socket.js
+++ b/js/services/socket.js
@@ -6,7 +6,7 @@ angular.module('copayApp.services').factory('Socket',
var url = 'http://' + config.socket.host + ':' + config.socket.port;
var socket = io(url, {
'reconnection': true,
- 'reconnectionDelay': 500,
+ 'reconnectionDelay': config.socket.reconnectDelay || 500,
});
return {
diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js
index a610c7ced..974c0f3c7 100644
--- a/test/unit/controllers/controllersSpec.js
+++ b/test/unit/controllers/controllersSpec.js
@@ -103,15 +103,12 @@ describe("Unit: Controllers", function() {
it('should check blockChainStatus', function() {
$httpBackend.expectGET(GH);
$httpBackend.flush();
- rootScope.blockChainStatus='error';
+ rootScope.insightError=1;
scope.$apply();
expect(rootScope.insightError).equal(1);
- rootScope.blockChainStatus='ok';
scope.$apply();
expect(rootScope.insightError).equal(1);
- rootScope.blockChainStatus='restored';
scope.$apply();
- expect(rootScope.insightError).equal(0);
});
});