From aa02aeb74cb5f01913f8dcaf45024513cf6da76d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 19 Jun 2018 09:02:19 -0400 Subject: [PATCH 1/2] move pingEndpoint to utils --- lib/contracts/blockchain.js | 41 ++--------------------------------- lib/utils/utils.js | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 6f6e4e15e7..6ae0c9c76c 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -256,45 +256,8 @@ class Blockchain { if (!self.contractsConfig || !self.contractsConfig.deployment || !self.contractsConfig.deployment.host) { return next(); } - const origin = self.blockchainConfig.wsOrigins.split(',')[0]; - const options = { - protocolVersion: 13, - perMessageDeflate: true, - origin: origin, - host: self.contractsConfig.deployment.host, - port: self.contractsConfig.deployment.port - }; - if (self.contractsConfig.deployment.type === 'ws') { - options.headers = { - 'Sec-WebSocket-Version': 13, - Connection: 'Upgrade', - Upgrade: 'websocket', - 'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits', - Origin: origin - }; - } - let req; - // remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl - if(options.host.indexOf('/') > -1){ - options.host = options.host.split('/')[0]; - } - if((self.contractsConfig.deployment.protocol || 'http') === 'https'){ - req = require('https').get(options); - }else{ - req = require('http').get(options); - } - - req.on('error', (err) => { - next(err); - }); - - req.on('response', (_response) => { - next(); - }); - - req.on('upgrade', (_res, _socket, _head) => { - next(); - }); + const {host, port, type, protocol} = self.contractsConfig.deployment; + utils.pingEndpoint(host, port, type, protocol, self.blockchainConfig.wsOrigins.split(',')[0], next); } ], function (err) { if (!noLogs && err === NO_NODE_ERROR) { diff --git a/lib/utils/utils.js b/lib/utils/utils.js index c09dfa7673..67de521267 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -73,6 +73,48 @@ function httpsGetJson(url, callback) { }); } +function pingEndpoint(host, port, type, protocol, wsOrigins, callback) { + const origin = wsOrigins.split(',')[0]; + const options = { + protocolVersion: 13, + perMessageDeflate: true, + origin: origin, + host: host, + port: port + }; + if (type === 'ws') { + options.headers = { + 'Sec-WebSocket-Version': 13, + Connection: 'Upgrade', + Upgrade: 'websocket', + 'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits', + Origin: origin + }; + } + let req; + // remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl + if(options.host.indexOf('/') > -1){ + options.host = options.host.split('/')[0]; + } + if(protocol === 'https') { + req = require('https').get(options); + } else { + req = require('http').get(options); + } + + req.on('error', (err) => { + callback(err); + }); + + req.on('response', (_response) => { + callback(); + }); + + req.on('upgrade', (_res, _socket, _head) => { + callback(); + }); +} + function runCmd(cmd, options) { const shelljs = require('shelljs'); let result = shelljs.exec(cmd, options || {silent: true}); @@ -267,6 +309,7 @@ module.exports = { httpGetJson: httpGetJson, httpsGetJson: httpsGetJson, hexToNumber: hexToNumber, + pingEndpoint, decodeParams: decodeParams, runCmd: runCmd, cd: cd, From 1a91f3c68889b4b4ceef6b23ce220d9ce6afb88d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 19 Jun 2018 09:24:36 -0400 Subject: [PATCH 2/2] ping endpoint before connecting to see if available --- lib/tests/test.js | 31 ++++++++++++++++++++++++------- lib/utils/utils.js | 7 +++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/tests/test.js b/lib/tests/test.js index 2b48b166a6..1a5f8a6781 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -7,6 +7,7 @@ const Events = require('../core/events'); const cloneDeep = require('clone-deep'); const AccountParser = require('../contracts/accountParser'); const Provider = require('../contracts/provider'); +const utils = require('../utils/utils'); const EmbarkJS = require('../../js/embark_node'); @@ -44,18 +45,29 @@ class Test { initWeb3Provider(callback) { if (this.simOptions.host) { - const protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws'; + let {host, port, type, protocol, accounts} = this.simOptions; + if (!protocol) { + protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws'; + } + const endpoint = `${protocol}://${host}:${port}`; const providerOptions = { web3: this.web3, - type: this.simOptions.type, - accountsConfig: this.simOptions.accounts, + type, + accountsConfig: accounts, blockchainConfig: this.engine.config.blockchainConfig, logger: this.engine.logger, isDev: false, - web3Endpoint: `${protocol}://${this.simOptions.host}:${this.simOptions.port}` + web3Endpoint: endpoint }; - this.provider = new Provider(providerOptions); - return this.provider.startWeb3Provider(callback); + console.info(`Connecting to node at ${endpoint}`.cyan); + return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => { + if (err) { + console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red); + return callback(err); + } + this.provider = new Provider(providerOptions); + return this.provider.startWeb3Provider(callback); + }); } if (this.simOptions.accounts) { @@ -214,7 +226,12 @@ class Test { next(null, accounts); }); } - ], callback); + ], (err, accounts) => { + if (err) { + process.exit(1); + } + callback(null, accounts); + }); } _deploy(config, callback) { diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 67de521267..172423d3f6 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -73,8 +73,7 @@ function httpsGetJson(url, callback) { }); } -function pingEndpoint(host, port, type, protocol, wsOrigins, callback) { - const origin = wsOrigins.split(',')[0]; +function pingEndpoint(host, port, type, protocol, origin, callback) { const options = { protocolVersion: 13, perMessageDeflate: true, @@ -93,10 +92,10 @@ function pingEndpoint(host, port, type, protocol, wsOrigins, callback) { } let req; // remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl - if(options.host.indexOf('/') > -1){ + if (options.host.indexOf('/') > -1){ options.host = options.host.split('/')[0]; } - if(protocol === 'https') { + if (protocol === 'https') { req = require('https').get(options); } else { req = require('http').get(options);