Adding proxy for simulator

This commit is contained in:
Richard Ramos 2018-06-07 12:50:22 -04:00
parent 29599112de
commit 339ffa5869
3 changed files with 69 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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",