move ethereum and whisper checks; refactor web3 connection

This commit is contained in:
Iuri Matias 2017-03-11 10:29:45 -05:00
parent 4bf31328e2
commit ea70d09e8c
5 changed files with 52 additions and 59 deletions

View File

@ -33,21 +33,25 @@ DeployManager.prototype.deployContracts = function(done) {
}); });
contractsManager.build(callback); contractsManager.build(callback);
}, },
function connectWithWeb3(contractsManager, callback) { function checkWeb3IsConnected(contractsManager, callback) {
var web3; if (!self.web3) {
if (self.web3) { return callback(Error("no web3 instance found"));
web3 = self.web3; }
} else { if (self.web3.currentProvider.isConnected !== undefined && !self.web3.isConnected()) {
web3 = new Web3(); self.logger.error(("Couldn't connect to an Ethereum node are you sure it's on?").red);
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; self.logger.info("make sure you have an Ethereum node or simulator running. e.g 'embark blockchain'".magenta);
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
if (!web3.isConnected()) {
self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red);
self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta);
return callback(Error("error connecting to blockchain node")); return callback(Error("error connecting to blockchain node"));
} }
if (self.web3.currentProvider.isConnected === undefined) {
self.web3.version.getNode(function(err, version) {
if (err) {
return callback(Error("error connecting to blockchain node"));
}
return callback(null, contractsManager, self.web3);
});
} else {
return callback(null, contractsManager, self.web3);
} }
callback(null, contractsManager, web3);
}, },
function setDefaultAccount(contractsManager, web3, callback) { function setDefaultAccount(contractsManager, web3, callback) {
web3.eth.getAccounts(function(err, accounts) { web3.eth.getAccounts(function(err, accounts) {

View File

@ -1,4 +1,5 @@
var http = require('http'); var http = require('http');
var Web3 = require('web3');
var utils = require('./utils.js'); var utils = require('./utils.js');
var Events = require('./events.js'); var Events = require('./events.js');
@ -48,7 +49,8 @@ Engine.prototype.startService = function(serviceName, _options) {
"deployment": this.deploymentService, "deployment": this.deploymentService,
"fileWatcher": this.fileWatchService, "fileWatcher": this.fileWatchService,
"webServer": this.webServerService, "webServer": this.webServerService,
"ipfs": this.ipfsService "ipfs": this.ipfsService,
"web3": this.web3Service
}; };
var service = services[serviceName]; var service = services[serviceName];
@ -111,7 +113,7 @@ Engine.prototype.abiService = function(options) {
Engine.prototype.deploymentService = function(options) { Engine.prototype.deploymentService = function(options) {
var self = this; var self = this;
this.deployManager = new DeployManager({ this.deployManager = new DeployManager({
web3: options.web3, web3: options.web3 || self.web3,
trackContracts: options.trackContracts, trackContracts: options.trackContracts,
config: this.config, config: this.config,
logger: this.logger, logger: this.logger,
@ -197,4 +199,33 @@ Engine.prototype.ipfsService = function(options) {
}); });
}; };
Engine.prototype.web3Service = function(options) {
var self = this;
this.web3 = options.web3;
if (this.web3 === undefined) {
this.web3 = new Web3();
var web3Endpoint = 'http://' + this.config.blockchainConfig.rpcHost + ':' + this.config.blockchainConfig.rpcPort;
this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint));
}
self.servicesMonitor.addCheck('Ethereum', function(cb) {
if (self.web3.isConnected()) {
return cb({name: (self.web3.version.node.split("/")[0] + " " + self.web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)"), status: 'green'});
} else {
return cb({name: "No Blockchain node found", status: 'red'});
}
});
self.servicesMonitor.addCheck('Whisper', function(cb) {
self.web3.version.getWhisper(function(err, res) {
if (err) {
return cb({name: 'Whisper', status: 'red'});
} else {
return cb({name: 'Whisper', status: 'green'});
}
});
});
};
module.exports = Engine; module.exports = Engine;

View File

@ -55,46 +55,6 @@ ServicesMonitor.prototype.startMonitor = function() {
}); });
}, function(err) { }, function(err) {
}); });
this.logger.info(JSON.stringify(this.checkState));
};
// TODO: old checks to be moved
ServicesMonitor.prototype.check = function() {
var self = this;
async.waterfall([
function connectWeb3(callback) {
self.logger.trace('connectWeb3');
var web3 = new Web3();
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
callback(null, web3, []);
},
function checkEthereum(web3, result, callback) {
self.logger.trace('checkEthereum');
var service;
if (web3.isConnected()) {
service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green;
} else {
service = "No Blockchain node found".red;
}
result.push(service);
callback(null, web3, result);
},
function checkWhisper(web3, result, callback) {
self.logger.trace('checkWhisper');
web3.version.getWhisper(function(err, res) {
var service = 'Whisper';
result.push(err ? service.red : service.green);
callback(null, result);
});
}
], function(err, result) {
if (err) {
self.logger.error(err.message);
} else {
self.logger.availableServices(result);
}
});
}; };
module.exports = ServicesMonitor; module.exports = ServicesMonitor;

View File

@ -47,8 +47,6 @@ Dashboard.prototype.availableServices = function(_services) {
services.push(_services[checkName]); services.push(_services[checkName]);
} }
console.log(services);
this.progress.setContent(services.join('\n')); this.progress.setContent(services.join('\n'));
this.screen.render(); this.screen.render();
}; };

View File

@ -92,7 +92,6 @@ var Embark = {
engine.logger.info('dashboard start'); engine.logger.info('dashboard start');
engine.events.on('servicesState', function(servicesState) { engine.events.on('servicesState', function(servicesState) {
engine.logger.info('servicesState event');
dashboard.monitor.availableServices(servicesState); dashboard.monitor.availableServices(servicesState);
}); });
@ -105,6 +104,7 @@ var Embark = {
engine.logger.info("loaded plugins: " + pluginList.join(", ")); engine.logger.info("loaded plugins: " + pluginList.join(", "));
} }
engine.startService("web3");
engine.startService("pipeline"); engine.startService("pipeline");
engine.startService("abi"); engine.startService("abi");
engine.startService("deployment"); engine.startService("deployment");