Merge pull request #877 from embark-framework/bug_fix/proxy-setup-awaits-blockchain

proxy should try to wait on target endpoint
This commit is contained in:
Iuri Matias 2018-09-21 09:31:48 -04:00 committed by GitHub
commit 9a914d0b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 17 deletions

View File

@ -18,6 +18,7 @@ var Blockchain = function(options) {
this.isDev = options.isDev;
this.onReadyCallback = options.onReadyCallback || (() => {});
this.onExitCallback = options.onExitCallback;
this.proxyIpc = null;
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
console.log("===> " + __("warning: running default config on a non-development environment"));
@ -93,13 +94,21 @@ Blockchain.prototype.initProxy = function() {
this.config.wsPort += constants.blockchain.servicePortOnProxy;
};
Blockchain.prototype.setupProxy = function() {
Blockchain.prototype.setupProxy = async function() {
const proxy = require('./proxy');
const Ipc = require('../../core/ipc');
let ipcObject = new Ipc({ipcRole: 'client'});
this.rpcProxy = proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false);
this.wsProxy = proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true);
if(!this.proxyIpc) this.proxyIpc = new Ipc({ipcRole: 'client'});
let wsProxy;
if(this.config.wsRPC) {
wsProxy = proxy.serve(this.proxyIpc, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins);
}
[this.rpcProxy, this.wsProxy] = await Promise.all([
proxy.serve(this.proxyIpc, this.config.rpcHost, this.config.rpcPort, false),
wsProxy
]);
};
Blockchain.prototype.shutdownProxy = function() {
@ -107,8 +116,8 @@ Blockchain.prototype.shutdownProxy = function() {
return;
}
this.rpcProxy.close();
this.wsProxy.close();
if(this.rpcProxy) this.rpcProxy.close();
if(this.wsProxy) this.wsProxy.close();
};
Blockchain.prototype.runCommand = function(cmd, options, callback) {
@ -149,9 +158,6 @@ Blockchain.prototype.run = function() {
},
function getMainCommand(next) {
self.client.mainCommand(address, function(cmd, args) {
if (self.config.proxy) {
self.setupProxy();
}
next(null, cmd, args);
}, true);
}
@ -177,19 +183,33 @@ Blockchain.prototype.run = function() {
self.child.stdout.on('data', (data) => {
console.error(`Geth error: ${data}`);
});
let httpReady = false;
let wsReady = !self.config.wsRPC;
// Geth logs appear in stderr somehow
self.child.stderr.on('data', (data) => {
self.child.stderr.on('data', async (data) => {
data = data.toString();
if (!self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
if (data.indexOf('HTTP endpoint opened') > -1) {
httpReady = true;
}
if (data.indexOf('WebSocket endpoint opened') > -1) {
wsReady = true;
}
if (!self.readyCalled && wsReady && httpReady) {
self.readyCalled = true;
if (self.isDev) {
self.createFundAndUnlockAccounts((err) => {
// TODO: this is never called!
if(err) console.error('Error creating, unlocking, and funding accounts', err);
});
}
self.readyCalled = true;
if (self.config.proxy) {
await self.setupProxy();
}
self.readyCallback();
}
console.log('Geth: ' + data);
});
self.child.on('exit', (code) => {

View File

@ -1,6 +1,7 @@
const httpProxy = require('http-proxy');
const http = require('http');
const constants = require('../../constants.json');
const utils = require('../../utils/utils');
let commList = {};
let transactions = {};
@ -63,7 +64,25 @@ const parseResponse = function (ipc, resBody) {
}
};
exports.serve = function (ipc, host, port, ws) {
exports.serve = async function (ipc, host, port, ws, origin) {
const _origin = origin ? origin.split(',')[0] : undefined;
const start = Date.now();
function awaitTarget() {
return new Promise(resolve => {
utils.pingEndpoint(
canonicalHost(host), port, ws ? 'ws': false, 'http', _origin, async (err) => {
if (!err || (Date.now() - start > 10000)) {
return resolve();
}
await utils.timer(250).then(awaitTarget).then(resolve);
}
);
});
}
await awaitTarget();
let proxy = httpProxy.createProxyServer({
target: {
host: canonicalHost(host),
@ -127,6 +146,9 @@ exports.serve = function (ipc, host, port, ws) {
});
}
const listenPort = port - constants.blockchain.servicePortOnProxy;
server.listen(listenPort, defaultHost);
return server;
return new Promise(resolve => {
server.listen(listenPort, defaultHost, () => {
resolve(server);
});
});
};

View File

@ -30,8 +30,9 @@ class Simulator {
let useProxy = this.blockchainConfig.proxy || false;
let host = (dockerHostSwap(options.host || this.blockchainConfig.rpcHost) || defaultHost);
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
port = parseInt(port, 10) + (useProxy ? constants.blockchain.servicePortOnProxy : 0);
cmds.push("-p " + (port + (useProxy ? constants.blockchain.servicePortOnProxy : 0)));
cmds.push("-p " + port);
cmds.push("-h " + host);
cmds.push("-a " + (options.numAccounts || 10));
cmds.push("-e " + (options.defaultBalance || 100));

View File

@ -490,6 +490,10 @@ function errorMessage(e) {
return e;
}
function timer(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = {
joinPath,
dirname,
@ -532,5 +536,6 @@ module.exports = {
sample,
last,
interceptLogs,
errorMessage
errorMessage,
timer
};