From b4934c7b1645d42d9a1b1907f3d6833da48e6d1a Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 7 Jun 2018 15:13:35 -0400 Subject: [PATCH] Added proxy to blockchain --- lib/cmds/blockchain/blockchain.js | 14 +++++++++- lib/cmds/proxy.js | 44 +++++++++++++++++-------------- lib/cmds/simulator.js | 17 ++++++------ lib/index.js | 4 +-- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 213ac4fed..b1e75a5fc 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -45,9 +45,11 @@ var Blockchain = function(options) { targetGasLimit: this.blockchainConfig.targetGasLimit || false, light: this.blockchainConfig.light || false, fast: this.blockchainConfig.fast || false, - verbosity: this.blockchainConfig.verbosity + verbosity: this.blockchainConfig.verbosity, }; + this.setupProxy(); + if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') { this.config.account = {}; this.config.account.password = fs.embarkPath("templates/boilerplate/config/development/password"); @@ -72,6 +74,16 @@ var Blockchain = function(options) { this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev}); }; +Blockchain.prototype.setupProxy = function(){ + if(this.blockchainConfig.proxy){ + const proxy = require('../proxy'); + proxy.serve(this.config.rpcHost, this.config.rpcPort, false); + proxy.serve(this.config.wsHost, this.config.wsPort, true); + this.config.rpcPort += 10; + this.config.wsPort += 10; + } +} + Blockchain.prototype.runCommand = function(cmd, options, callback) { console.log(__("running: %s", cmd.underline).green); if (this.blockchainConfig.silent) { diff --git a/lib/cmds/proxy.js b/lib/cmds/proxy.js index 2e52645c7..0f809fb1e 100644 --- a/lib/cmds/proxy.js +++ b/lib/cmds/proxy.js @@ -1,7 +1,7 @@ const httpProxy = require('http-proxy'); const http = require('http'); -exports.serve = function(host, port){ +exports.serve = function(host, port, ws){ let commList = {}; let proxy = httpProxy.createProxyServer({}); @@ -10,19 +10,24 @@ exports.serve = function(host, port){ let reqBody = []; req.on('data', (b) => { reqBody.push(b); }) .on('end', () => { - reqBody = Buffer.concat(reqBody).toString(); - if(reqBody){ - let jsonO = JSON.parse(reqBody); - if(jsonO.method == "eth_sendTransaction"){ - commList[jsonO.id] = { - requestData: jsonO.params.data - }; + reqBody = Buffer.concat(reqBody).toString(); + if(reqBody){ + let jsonO = JSON.parse(reqBody); + if(jsonO.method == "eth_sendTransaction"){ + commList[jsonO.id] = { + address: jsonO.params[0].to, + requestData: jsonO.params[0].data + }; + } } - } }); proxy.proxyRequest(req, res, { - target: `http://${host}:${port + 1}` + target: { + host: host, + port: port + 10, + ws: ws + } }); proxy.on('proxyRes', (proxyRes, req, res) => { @@ -31,15 +36,14 @@ exports.serve = function(host, port){ proxyRes.on('end', function () { resBody = Buffer.concat(resBody).toString(); try { - let jsonO = JSON.parse(resBody); - if(commList[jsonO.id]){ - commList[jsonO.id].transactionHash = resBody; - - // TODO: decode commlist - // ” SimpleStorage> set(5) | tx: 0xef234f16etc ” - - delete commList[jsonO.id]; - } + let jsonO = JSON.parse(resBody); + if(commList[jsonO.id]){ + commList[jsonO.id].transactionHash = jsonO.result; + // TODO: decode commlist + // TODO: send messages to console + // ” SimpleStorage> set(5) | tx: 0xef234f16etc ” + delete commList[jsonO.id]; + } } catch(e){ // } @@ -49,4 +53,4 @@ exports.serve = function(host, port){ }); server.listen(port); -} +}; diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index ddfb18cbf..c319e8427 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -4,12 +4,9 @@ class Simulator { constructor(options) { this.blockchainConfig = options.blockchainConfig; this.logger = options.logger; - this.contractsConfig = options.contractsConfig; - this.events = options.events; } - run(options) { - let cmds = []; + run(options) { let cmds = []; const testrpc = shelljs.which('testrpc'); const ganache = shelljs.which('ganache-cli'); @@ -19,10 +16,11 @@ class Simulator { process.exit(); } + let useProxy = this.blockchainConfig.proxy || false; let host = (options.host || this.blockchainConfig.rpcHost || 'localhost'); let port = (options.port || this.blockchainConfig.rpcPort || 8545); - cmds.push("-p " + (port + 1)); + cmds.push("-p " + (port + (useProxy ? 10 : 0))); cmds.push("-h " + host); cmds.push("-a " + (options.numAccounts || 10)); cmds.push("-e " + (options.defaultBalance || 100)); @@ -42,9 +40,12 @@ class Simulator { const program = ganache ? 'ganache-cli' : 'testrpc'; - shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true}); - - proxy.serve(host, port); + shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true}); + + if(useProxy){ + proxy.serve(host, port); + } + } } diff --git a/lib/index.js b/lib/index.js index bb71aed19..ae2a5b2fc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,10 +52,8 @@ class Embark { this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain]; let Simulator = require('./cmds/simulator.js'); let simulator = new Simulator({ - contractsConfig: this.config.contractsConfig, blockchainConfig: this.config.blockchainConfig, - logger: this.logger, - events: this.events + logger: this.logger }); simulator.run(options); }