diff --git a/lib/cmds/proxy.js b/lib/cmds/proxy.js new file mode 100644 index 000000000..2e52645c7 --- /dev/null +++ b/lib/cmds/proxy.js @@ -0,0 +1,52 @@ +const httpProxy = require('http-proxy'); +const http = require('http'); + +exports.serve = function(host, port){ + let commList = {}; + + let proxy = httpProxy.createProxyServer({}); + let server = http.createServer((req, res) => { + + 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 + }; + } + } + }); + + proxy.proxyRequest(req, res, { + target: `http://${host}:${port + 1}` + }); + + proxy.on('proxyRes', (proxyRes, req, res) => { + let resBody = []; + proxyRes.on('data', (b) => resBody.push(b)) + 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]; + } + } catch(e){ + // + } + }); + }); + + }); + + server.listen(port); +} diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index d3d5a4c50..ddfb18cbf 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -1,5 +1,5 @@ let shelljs = require('shelljs'); - +let proxy = require('./proxy'); class Simulator { constructor(options) { this.blockchainConfig = options.blockchainConfig; @@ -19,10 +19,11 @@ class Simulator { process.exit(); } - cmds.push("-p " + ((options.port || this.blockchainConfig.rpcPort || 8545) + 1)); - - - cmds.push("-h " + (options.host || this.blockchainConfig.rpcHost || 'localhost')); + let host = (options.host || this.blockchainConfig.rpcHost || 'localhost'); + let port = (options.port || this.blockchainConfig.rpcPort || 8545); + + cmds.push("-p " + (port + 1)); + cmds.push("-h " + host); cmds.push("-a " + (options.numAccounts || 10)); cmds.push("-e " + (options.defaultBalance || 100)); cmds.push("-l " + (options.gasLimit || 8000000)); @@ -41,61 +42,9 @@ class Simulator { const program = ganache ? 'ganache-cli' : 'testrpc'; - shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true}); + shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true}); - let httpProxy = require('http-proxy'); - let http = require('http'); - let _port = options.port || this.blockchainConfig.rpcPort || 8545; - let _host = (options.host || this.blockchainConfig.rpcHost || 'localhost'); - - let commList = {}; - - let proxy = httpProxy.createProxyServer({}); - let server = http.createServer((req, res) => { - - 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 - }; - } - } - }); - - proxy.proxyRequest(req, res, { - target: `http://${_host}:${_port + 1}` - }); - - proxy.on('proxyRes', function (proxyRes, req, res) { - let resBody = []; - proxyRes.on('data', (b) => resBody.push(b)) - proxyRes.on('end', function () { - resBody = Buffer.concat(resBody).toString(); - try { - let jsonO = JSON.parse(resBody); - if(commList[jsonO.id]){ - commList[json0.id].transactionHash = resBody; - - // TODO: decode commlist - // ” SimpleStorage> set(5) | tx: 0xef234f16etc ” - - delete commList[json0.id]; - } - } catch(e){ - // - } - }); - }); - - }); - - server.listen(_port); - + proxy.serve(host, port); } }