diff --git a/lib/modules/blockchain_process/blockchain.js b/lib/modules/blockchain_process/blockchain.js index 6bca2825..fa013e21 100644 --- a/lib/modules/blockchain_process/blockchain.js +++ b/lib/modules/blockchain_process/blockchain.js @@ -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,17 +94,18 @@ Blockchain.prototype.initProxy = function() { this.config.wsPort += constants.blockchain.servicePortOnProxy; }; -Blockchain.prototype.setupProxy = async function() { +Blockchain.prototype.setupProxy = async function(type) { const proxy = require('./proxy'); const Ipc = require('../../core/ipc'); - let ipcObject = new Ipc({ipcRole: 'client'}); - const [rpcProxy, wsProxy] = await Promise.all([ - proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false), - proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins) - ]); - this.rpcProxy = rpcProxy; - this.wsProxy = wsProxy; + if(!this.proxyIpc) this.proxyIpc = new Ipc({ipcRole: 'client'}); + + if (type === 'rpc') { + this.rpcProxy = await proxy.serve(this.proxyIpc, this.config.rpcHost, this.config.rpcPort, false); + } + else if (type === 'ws'){ + this.wsProxy = await proxy.serve(this.proxyIpc, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins); + } }; Blockchain.prototype.shutdownProxy = function() { @@ -111,8 +113,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) { @@ -181,9 +183,12 @@ Blockchain.prototype.run = function() { // Geth logs appear in stderr somehow self.child.stderr.on('data', async (data) => { data = data.toString(); + if (!self.readyCalled && data.indexOf('HTTP endpoint opened') > -1 && self.config.proxy) { + await self.setupProxy('rpc'); + } if (!self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) { if (self.config.proxy) { - await self.setupProxy(); + await self.setupProxy('ws'); } if (self.isDev) { self.createFundAndUnlockAccounts((err) => {