From c477445896084fce5405315fe8a9609ce63899a3 Mon Sep 17 00:00:00 2001 From: emizzle Date: Thu, 20 Sep 2018 15:08:02 +1000 Subject: [PATCH] Start HTTP and WS proxies individually As geth is starting up, the output is monitored for endpoint info. Once the HTTP or WS endpoints are opened, the corresponding proxies are started. This is more maintainable in the long run in case the geth process being started does not allow for rpc or websockets, or geth modifies the order in which endpoint are opened. --- lib/modules/blockchain_process/blockchain.js | 27 ++++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) 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) => {