From ea70d09e8c3be3b52fa394dce402727bdbf9e9b7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:29:45 -0500 Subject: [PATCH] move ethereum and whisper checks; refactor web3 connection --- lib/contracts/deploy_manager.js | 32 ++++++++++++++------------ lib/core/engine.js | 35 +++++++++++++++++++++++++++-- lib/core/services_monitor.js | 40 --------------------------------- lib/dashboard/monitor.js | 2 -- lib/index.js | 2 +- 5 files changed, 52 insertions(+), 59 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0293b97c2..04ce315f2 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -33,21 +33,25 @@ DeployManager.prototype.deployContracts = function(done) { }); contractsManager.build(callback); }, - function connectWithWeb3(contractsManager, callback) { - var web3; - if (self.web3) { - web3 = self.web3; - } else { - web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - 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")); - } + function checkWeb3IsConnected(contractsManager, callback) { + if (!self.web3) { + return callback(Error("no web3 instance found")); + } + if (self.web3.currentProvider.isConnected !== undefined && !self.web3.isConnected()) { + self.logger.error(("Couldn't connect to an Ethereum node 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")); + } + 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) { web3.eth.getAccounts(function(err, accounts) { diff --git a/lib/core/engine.js b/lib/core/engine.js index eee754a42..3a821ea97 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,4 +1,5 @@ var http = require('http'); +var Web3 = require('web3'); var utils = require('./utils.js'); var Events = require('./events.js'); @@ -48,7 +49,8 @@ Engine.prototype.startService = function(serviceName, _options) { "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, "webServer": this.webServerService, - "ipfs": this.ipfsService + "ipfs": this.ipfsService, + "web3": this.web3Service }; var service = services[serviceName]; @@ -111,7 +113,7 @@ Engine.prototype.abiService = function(options) { Engine.prototype.deploymentService = function(options) { var self = this; this.deployManager = new DeployManager({ - web3: options.web3, + web3: options.web3 || self.web3, trackContracts: options.trackContracts, config: this.config, 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; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 25e24c62f..f0f61b769 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -55,46 +55,6 @@ ServicesMonitor.prototype.startMonitor = function() { }); }, 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; diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index c1ce4c8de..b1a243c48 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -47,8 +47,6 @@ Dashboard.prototype.availableServices = function(_services) { services.push(_services[checkName]); } - console.log(services); - this.progress.setContent(services.join('\n')); this.screen.render(); }; diff --git a/lib/index.js b/lib/index.js index 926dc6e1d..91b671af3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -92,7 +92,6 @@ var Embark = { engine.logger.info('dashboard start'); engine.events.on('servicesState', function(servicesState) { - engine.logger.info('servicesState event'); dashboard.monitor.availableServices(servicesState); }); @@ -105,6 +104,7 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } + engine.startService("web3"); engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment");