fix: fix node connection test to use the endpoints correctly

This caused issues to connect to Infura for example, because we did
not support HTTPS at all, nor WSS
This commit is contained in:
Jonathan Rainville 2019-12-12 14:57:52 -05:00 committed by Iuri Matias
parent 1a7fc663b7
commit 0503bb24a3
8 changed files with 72 additions and 51 deletions

View File

@ -10,6 +10,7 @@ const constants = require('embark-core/constants');
import { __ } from 'embark-i18n';
import {
buildUrlFromConfig,
deconstructUrl,
canonicalHost,
dappPath,
defaultHost,
@ -389,16 +390,27 @@ export class Config {
});
}
if (!this.blockchainConfig.endpoint) {
if (this.blockchainConfig.endpoint) {
const {type, host, port} = deconstructUrl(this.blockchainConfig.endpoint);
if (type === 'ws') {
this.blockchainConfig.wsHost = host;
this.blockchainConfig.wsPort = port;
this.blockchainConfig.wsRPC = true;
} else {
this.blockchainConfig.rpcHost = host;
this.blockchainConfig.rpcPort = port;
this.blockchainConfig.wsRPC = false;
}
} else {
const urlConfig = (this.blockchainConfig.wsHost) ? {
host: this.blockchainConfig.wsHost,
port: this.blockchainConfig.wsPort,
type: 'ws'
} : {
host: this.blockchainConfig.rpcHost,
port: this.blockchainConfig.rpcPort,
type: 'rpc'
};
host: this.blockchainConfig.rpcHost,
port: this.blockchainConfig.rpcPort,
type: 'rpc'
};
this.blockchainConfig.endpoint = buildUrlFromConfig(urlConfig);
this.blockchainConfig.isAutoEndpoint = true;
}

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws");
const http = require("http");
const https = require("https");
const LIVENESS_CHECK = `{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`;
@ -20,10 +21,8 @@ const parseAndRespond = (data, cb) => {
cb(null, version);
};
const rpc = (host, port, cb) => {
const rpcWithEndpoint = (endpoint, cb) => {
const options = {
hostname: host, // TODO(andremedeiros): get from config
port: port, // TODO(andremedeiros): get from config
method: "POST",
timeout: 1000,
headers: {
@ -32,7 +31,12 @@ const rpc = (host, port, cb) => {
}
};
const req = http.request(options, (res) => {
let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = "";
res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb));
@ -42,8 +46,8 @@ const rpc = (host, port, cb) => {
req.end();
};
const ws = (host, port, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port);
const ws = (endpoint, cb) => {
const conn = new WebSocket(endpoint);
conn.on("message", (data) => {
parseAndRespond(data, cb);
conn.close();
@ -54,5 +58,5 @@ const ws = (host, port, cb) => {
module.exports = {
ws,
rpc
rpcWithEndpoint
};

View File

@ -1,8 +1,8 @@
import { __ } from 'embark-i18n';
const { normalizeInput } = require('embark-utils');
const {normalizeInput} = require('embark-utils');
import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import { BlockchainClient } from './blockchain';
import { ws, rpc } from './check.js';
import { ws, rpcWithEndpoint } from './check.js';
import DevTxs from "./devtxs";
const constants = require('embark-core/constants');
@ -53,7 +53,7 @@ class Geth {
}
});
}
async setupDevTxs() {
const devTxs = new DevTxs(this.embark);
await devTxs.init();
@ -82,21 +82,16 @@ class Geth {
}
_doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
if (this.blockchainConfig.endpoint.startsWith('ws')) {
return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}
// TODO: need to get correct port taking into account the proxy
registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
this._doCheck(cb);
}, 5000, 'off');
}

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws");
const http = require("http");
const https = require("https");
const LIVENESS_CHECK=`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`;
@ -20,10 +21,8 @@ const parseAndRespond = (data, cb) => {
cb(null, version);
};
const rpc = (host, port, cb) => {
const rpcWithEndpoint = (endpoint, cb) => {
const options = {
hostname: host, // TODO(andremedeiros): get from config
port: port, // TODO(andremedeiros): get from config
method: "POST",
timeout: 1000,
headers: {
@ -32,7 +31,12 @@ const rpc = (host, port, cb) => {
}
};
const req = http.request(options, (res) => {
let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = "";
res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb));
@ -42,8 +46,8 @@ const rpc = (host, port, cb) => {
req.end();
};
const ws = (host, port, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port);
const ws = (endpoint, cb) => {
const conn = new WebSocket(endpoint);
conn.on("message", (data) => {
parseAndRespond(data, cb);
conn.close();
@ -54,5 +58,5 @@ const ws = (host, port, cb) => {
module.exports = {
ws,
rpc
rpcWithEndpoint
};

View File

@ -2,7 +2,7 @@ import { __ } from 'embark-i18n';
import {BlockchainClient} from "./blockchain";
const {normalizeInput} = require('embark-utils');
import {BlockchainProcessLauncher} from './blockchainProcessLauncher';
import {ws, rpc} from './check.js';
import {ws, rpcWithEndpoint} from './check.js';
const constants = require('embark-core/constants');
class Parity {
@ -70,11 +70,10 @@ class Parity {
}
_doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig;
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
if (this.blockchainConfig.endpoint.startsWith('ws')) {
return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}
// TODO: need to get correct port taking into account the proxy

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws");
const http = require("http");
const https = require("https");
const LIVENESS_CHECK = JSON.stringify({
jsonrpc:'2.0',
@ -25,10 +26,8 @@ const parseAndRespond = (data, cb) => {
cb(null, resp.result);
};
const rpc = (host, port, cb) => {
const rpcWithEndpoint = (endpoint, cb) => {
const options = {
hostname: host, // TODO(andremedeiros): get from config
port, // TODO(andremedeiros): get from config
method: "POST",
timeout: 1000,
headers: {
@ -37,9 +36,14 @@ const rpc = (host, port, cb) => {
}
};
const req = http.request(options, (res) => {
let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = "";
res.on("data", (chunk) => { data += chunk; });
res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb));
});
req.on("error", (e) => cb(e));
@ -47,8 +51,8 @@ const rpc = (host, port, cb) => {
req.end();
};
const ws = (host, port, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port);
const ws = (endpoint, cb) => {
const conn = new WebSocket(endpoint);
conn.on("message", (data) => {
parseAndRespond(data, cb);
conn.close();
@ -59,5 +63,5 @@ const ws = (host, port, cb) => {
module.exports = {
ws,
rpc
rpcWithEndpoint
};

View File

@ -3,7 +3,7 @@ import { dappPath, canonicalHost, defaultHost } from "embark-utils";
const constants = require("embark-core/constants");
const API = require("./api.js");
import { BlockchainProcessLauncher } from "./blockchainProcessLauncher";
import { ws, rpc } from "./check.js";
import { ws, rpcWithEndpoint } from "./check.js";
const { normalizeInput } = require("embark-utils");
class Whisper {
@ -64,12 +64,10 @@ class Whisper {
registerServiceCheck() {
this.events.request("services:register", "Whisper", (cb) => {
const { host, port, type } = this.communicationConfig.connection;
if (type === "ws") {
return ws(host, port, (err, version) => this._getNodeState(err, version, cb));
if (this.blockchainConfig.endpoint.startsWith('ws')) {
return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}
rpc(host, port, (err, version) => this._getNodeState(err, version, cb));
rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}, 5000, "off");
}

View File

@ -92,8 +92,13 @@ export default class ProxyManager {
}
this.inited = true;
let port = this.embark.config.blockchainConfig.rpcPort;
if (!port && port !== 0) {
port = 8545;
}
// setup ports
const rpcPort = await findNextPort(this.embark.config.blockchainConfig.rpcPort + constants.blockchain.servicePortOnProxy);
const rpcPort = await findNextPort(port + constants.blockchain.servicePortOnProxy);
const wsPort = await findNextPort(rpcPort + 1);
this.rpcPort = rpcPort;
this.wsPort = wsPort;