diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index a68b27e7..d3d5a4c5 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -4,6 +4,8 @@ class Simulator { constructor(options) { this.blockchainConfig = options.blockchainConfig; this.logger = options.logger; + this.contractsConfig = options.contractsConfig; + this.events = options.events; } run(options) { @@ -17,7 +19,9 @@ class Simulator { process.exit(); } - cmds.push("-p " + (options.port || this.blockchainConfig.rpcPort || 8545)); + cmds.push("-p " + ((options.port || this.blockchainConfig.rpcPort || 8545) + 1)); + + cmds.push("-h " + (options.host || this.blockchainConfig.rpcHost || 'localhost')); cmds.push("-a " + (options.numAccounts || 10)); cmds.push("-e " + (options.defaultBalance || 100)); @@ -36,7 +40,62 @@ 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); + } } diff --git a/lib/index.js b/lib/index.js index bc5410df..bb71aed1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,7 +51,12 @@ class Embark { simulator(options) { this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain]; let Simulator = require('./cmds/simulator.js'); - let simulator = new Simulator({blockchainConfig: this.config.blockchainConfig, logger: this.logger}); + let simulator = new Simulator({ + contractsConfig: this.config.contractsConfig, + blockchainConfig: this.config.blockchainConfig, + logger: this.logger, + events: this.events + }); simulator.run(options); } diff --git a/package.json b/package.json index cd660c0a..57abdf87 100644 --- a/package.json +++ b/package.json @@ -49,11 +49,13 @@ "ganache-cli": "^6.1.0", "globule": "^1.1.0", "hard-source-webpack-plugin": "^0.6.6", + "http-proxy": "^1.17.0", "http-shutdown": "^1.2.0", "i18n": "^0.8.3", "ipfs-api": "17.2.4", "live-plugin-manager": "https://github.com/iurimatias/live-plugin-manager.git", "merge": "^1.2.0", + "node-http-proxy": "^0.2.3", "node-ipc": "^9.1.1", "os-locale": "^2.1.0", "p-iteration": "^1.1.7",