From aea270af0261703ccf31b8b53f16a39fc09e3c8d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 18 May 2018 14:58:05 -0400 Subject: [PATCH] detect no node using request --- lib/contracts/blockchain.js | 33 ++++++++++++++++++++++++++++++--- lib/core/engine.js | 3 +++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index c6bb575f4..36da1e960 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -1,4 +1,5 @@ const Web3 = require('web3'); +const Provider = require('./provider.js'); class Blockchain { constructor(options) { @@ -10,6 +11,8 @@ class Blockchain { this.web3 = options.web3; this.addCheck = options.addCheck; + this.isWeb3Started = false; + if (!this.web3) { this.initWeb3(); } @@ -19,10 +22,25 @@ class Blockchain { } initWeb3() { + const self = this; this.web3 = new Web3(); if (this.contractsConfig.deployment.type === "rpc") { - let web3Endpoint = 'http://' + this.contractsConfig.deployment.host + ':' + this.contractsConfig.deployment.port; this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint)); + + let provider; + let web3Endpoint = 'http://' + this.contractsConfig.deployment.host + ':' + this.contractsConfig.deployment.port; + const providerOptions = { + web3: this.web3, + accountsConfig: this.contractsConfig.deployment.accounts, + logger: this.logger, + isDev: this.isDev, + web3Endpoint + }; + provider = new Provider(providerOptions); + + provider.startWeb3Provider(() => { + self.isWeb3Started = true; + }); } else { throw new Error("contracts config error: unknown deployment type " + this.contractsConfig.deployment.type); } @@ -133,13 +151,22 @@ class Blockchain { return cb(Error("error connecting to blockchain node")); } - self.getAccounts(function(err, _accounts) { + // TODO: refactor this + request.get(web3Endpoint, function (err, _response, _body) { if (err) { 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 cb(Error("error connecting to blockchain node")); } - return cb(); + + self.getAccounts(function(err, _accounts) { + if (err) { + 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 cb(Error("error connecting to blockchain node")); + } + return cb(); + }); }); } diff --git a/lib/core/engine.js b/lib/core/engine.js index 6eb92eecb..a30c2e707 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -13,6 +13,8 @@ const Pipeline = require('../pipeline/pipeline.js'); const Watch = require('../pipeline/watch.js'); const LibraryManager = require('../versions/library_manager.js'); const CodeRunner = require('../coderunner/codeRunner.js'); +const request = require('request'); +//const Provider = require('../contracts/provider'); class Engine { constructor(options) { @@ -304,6 +306,7 @@ class Engine { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), events: this.events, logger: this.logger, + isDev: this.isDev, web3: options.web3 });