Merge pull request #276 from maraoz/bug/bitcoind-error-message

bitcoind connection status derived from PeerSync
This commit is contained in:
Gustavo Maximiliano Cortez 2014-02-12 18:40:35 -02:00
commit 78aed173fa
7 changed files with 42 additions and 13 deletions

View File

@ -50,3 +50,8 @@ exports.sync = function(req, res) {
if (req.historicSync) if (req.historicSync)
res.jsonp(req.historicSync.info()); res.jsonp(req.historicSync.info());
}; };
exports.peer = function(req, res) {
if (req.peerSync)
res.jsonp(req.peerSync.info());
};

View File

@ -7,13 +7,17 @@ var express = require('express'),
helpers = require('view-helpers'), helpers = require('view-helpers'),
config = require('./config'); config = require('./config');
module.exports = function(app, historicSync) { module.exports = function(app, historicSync, peerSync) {
//custom middleware //custom middleware
function setHistoric(req, res, next) { function setHistoric(req, res, next) {
req.historicSync = historicSync; req.historicSync = historicSync;
next(); next();
} }
function setPeer(req, res, next) {
req.peerSync = peerSync;
next();
}
app.set('showStackError', true); app.set('showStackError', true);
@ -28,6 +32,7 @@ module.exports = function(app, historicSync) {
app.enable('jsonp callback'); app.enable('jsonp callback');
app.use('/api/sync', setHistoric); app.use('/api/sync', setHistoric);
app.use('/api/peer', setPeer);
app.use(express.logger('dev')); app.use(express.logger('dev'));
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded()); app.use(express.urlencoded());

View File

@ -29,6 +29,7 @@ module.exports = function(app) {
app.get('/api/status', st.show); app.get('/api/status', st.show);
app.get('/api/sync', st.sync); app.get('/api/sync', st.sync);
app.get('/api/peer', st.peer);
// Currency // Currency
var currency = require('../app/controllers/currency'); var currency = require('../app/controllers/currency');

View File

@ -43,8 +43,11 @@ walk(models_path);
/** /**
* p2pSync process * p2pSync process
*/ */
var peerSync = new PeerSync();
if (!config.disableP2pSync) { if (!config.disableP2pSync) {
var ps = new PeerSync(); var ps = peerSync;
ps.init({ ps.init({
shouldBroadcast: true, shouldBroadcast: true,
}, function() { }, function() {
@ -83,7 +86,7 @@ if (!config.disableHistoricSync) {
//express settings //express settings
require('./config/express')(expressApp, historicSync); require('./config/express')(expressApp, historicSync, peerSync);
//Bootstrap routes //Bootstrap routes
require('./config/routes')(expressApp); require('./config/routes')(expressApp);

View File

@ -11,12 +11,12 @@ function spec() {
var networks = require('bitcore/networks'); var networks = require('bitcore/networks');
var peerdb_fn = 'peerdb.json'; var peerdb_fn = 'peerdb.json';
function PeerSync() {} function PeerSync() {}
PeerSync.prototype.init = function(opts, cb) { PeerSync.prototype.init = function(opts, cb) {
if (!opts) opts = {}; if (!opts) opts = {};
this.connected = false;
this.peerdb = undefined; this.peerdb = undefined;
this.allowReorgs = false; this.allowReorgs = false;
@ -41,6 +41,12 @@ function spec() {
fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb)); fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb));
}; };
PeerSync.prototype.info = function() {
return {
connected: this.connected
};
};
PeerSync.prototype.handleInv = function(info) { PeerSync.prototype.handleInv = function(info) {
var invs = info.message.invs; var invs = info.message.invs;
invs.forEach(function(inv) { invs.forEach(function(inv) {
@ -98,12 +104,17 @@ function spec() {
}); });
this.peerman.on('connection', function(conn) { this.peerman.on('connection', function(conn) {
self.connected = true;
conn.on('inv', self.handleInv.bind(self)); conn.on('inv', self.handleInv.bind(self));
conn.on('block', self.handleBlock.bind(self)); conn.on('block', self.handleBlock.bind(self));
conn.on('tx', self.handleTx.bind(self)); conn.on('tx', self.handleTx.bind(self));
}); });
this.peerman.on('connect', self.handle_connected.bind(self)); this.peerman.on('connect', self.handle_connected.bind(self));
this.peerman.on('netDisconnected', function() {
self.connected = false;
});
this.peerman.start(); this.peerman.start();
}; };

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('insight.connection').controller('ConnectionController', angular.module('insight.connection').controller('ConnectionController',
function($scope, $window, Status, Sync, getSocket) { function($scope, $window, Status, getSocket, PeerSync) {
// Set initial values // Set initial values
$scope.apiOnline = true; $scope.apiOnline = true;
@ -21,9 +21,9 @@ function($scope, $window, Status, Sync, getSocket) {
// Check for the api connection // Check for the api connection
$scope.getConnStatus = function() { $scope.getConnStatus = function() {
Sync.get({}, PeerSync.get({},
function(sync) { function(peer) {
$scope.apiOnline = (sync.status !== 'aborted' && sync.status !== 'error'); $scope.apiOnline = peer.connected;
}, },
function() { function() {
$scope.apiOnline = false; $scope.apiOnline = false;

View File

@ -3,11 +3,15 @@
angular.module('insight.status') angular.module('insight.status')
.factory('Status', .factory('Status',
function($resource) { function($resource) {
return $resource('/api/status', { return $resource('/api/status', {
q: '@q' q: '@q'
}); });
}) })
.factory('Sync', .factory('Sync',
function($resource) { function($resource) {
return $resource('/api/sync'); return $resource('/api/sync');
}); })
.factory('PeerSync',
function($resource) {
return $resource('api/peer');
});