Added proxy to blockchain
This commit is contained in:
parent
84c01c3197
commit
05f5a145f1
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue