Added proxy to blockchain
This commit is contained in:
parent
f84970f824
commit
b4934c7b16
|
@ -45,9 +45,11 @@ var Blockchain = function(options) {
|
||||||
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
|
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
|
||||||
light: this.blockchainConfig.light || false,
|
light: this.blockchainConfig.light || false,
|
||||||
fast: this.blockchainConfig.fast || 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}') {
|
if (this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') {
|
||||||
this.config.account = {};
|
this.config.account = {};
|
||||||
this.config.account.password = fs.embarkPath("templates/boilerplate/config/development/password");
|
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});
|
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) {
|
Blockchain.prototype.runCommand = function(cmd, options, callback) {
|
||||||
console.log(__("running: %s", cmd.underline).green);
|
console.log(__("running: %s", cmd.underline).green);
|
||||||
if (this.blockchainConfig.silent) {
|
if (this.blockchainConfig.silent) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const httpProxy = require('http-proxy');
|
const httpProxy = require('http-proxy');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
|
||||||
exports.serve = function(host, port){
|
exports.serve = function(host, port, ws){
|
||||||
let commList = {};
|
let commList = {};
|
||||||
|
|
||||||
let proxy = httpProxy.createProxyServer({});
|
let proxy = httpProxy.createProxyServer({});
|
||||||
|
@ -10,19 +10,24 @@ exports.serve = function(host, port){
|
||||||
let reqBody = [];
|
let reqBody = [];
|
||||||
req.on('data', (b) => { reqBody.push(b); })
|
req.on('data', (b) => { reqBody.push(b); })
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
reqBody = Buffer.concat(reqBody).toString();
|
reqBody = Buffer.concat(reqBody).toString();
|
||||||
if(reqBody){
|
if(reqBody){
|
||||||
let jsonO = JSON.parse(reqBody);
|
let jsonO = JSON.parse(reqBody);
|
||||||
if(jsonO.method == "eth_sendTransaction"){
|
if(jsonO.method == "eth_sendTransaction"){
|
||||||
commList[jsonO.id] = {
|
commList[jsonO.id] = {
|
||||||
requestData: jsonO.params.data
|
address: jsonO.params[0].to,
|
||||||
};
|
requestData: jsonO.params[0].data
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
proxy.proxyRequest(req, res, {
|
proxy.proxyRequest(req, res, {
|
||||||
target: `http://${host}:${port + 1}`
|
target: {
|
||||||
|
host: host,
|
||||||
|
port: port + 10,
|
||||||
|
ws: ws
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
proxy.on('proxyRes', (proxyRes, req, res) => {
|
proxy.on('proxyRes', (proxyRes, req, res) => {
|
||||||
|
@ -31,15 +36,14 @@ exports.serve = function(host, port){
|
||||||
proxyRes.on('end', function () {
|
proxyRes.on('end', function () {
|
||||||
resBody = Buffer.concat(resBody).toString();
|
resBody = Buffer.concat(resBody).toString();
|
||||||
try {
|
try {
|
||||||
let jsonO = JSON.parse(resBody);
|
let jsonO = JSON.parse(resBody);
|
||||||
if(commList[jsonO.id]){
|
if(commList[jsonO.id]){
|
||||||
commList[jsonO.id].transactionHash = resBody;
|
commList[jsonO.id].transactionHash = jsonO.result;
|
||||||
|
// TODO: decode commlist
|
||||||
// TODO: decode commlist
|
// TODO: send messages to console
|
||||||
// ” SimpleStorage> set(5) | tx: 0xef234f16etc ”
|
// ” SimpleStorage> set(5) | tx: 0xef234f16etc ”
|
||||||
|
delete commList[jsonO.id];
|
||||||
delete commList[jsonO.id];
|
}
|
||||||
}
|
|
||||||
} catch(e){
|
} catch(e){
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
@ -49,4 +53,4 @@ exports.serve = function(host, port){
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(port);
|
server.listen(port);
|
||||||
}
|
};
|
||||||
|
|
|
@ -4,12 +4,9 @@ class Simulator {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.blockchainConfig = options.blockchainConfig;
|
this.blockchainConfig = options.blockchainConfig;
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
this.contractsConfig = options.contractsConfig;
|
|
||||||
this.events = options.events;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
run(options) {
|
run(options) { let cmds = [];
|
||||||
let cmds = [];
|
|
||||||
|
|
||||||
const testrpc = shelljs.which('testrpc');
|
const testrpc = shelljs.which('testrpc');
|
||||||
const ganache = shelljs.which('ganache-cli');
|
const ganache = shelljs.which('ganache-cli');
|
||||||
|
@ -19,10 +16,11 @@ class Simulator {
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let useProxy = this.blockchainConfig.proxy || false;
|
||||||
let host = (options.host || this.blockchainConfig.rpcHost || 'localhost');
|
let host = (options.host || this.blockchainConfig.rpcHost || 'localhost');
|
||||||
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
|
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("-h " + host);
|
||||||
cmds.push("-a " + (options.numAccounts || 10));
|
cmds.push("-a " + (options.numAccounts || 10));
|
||||||
cmds.push("-e " + (options.defaultBalance || 100));
|
cmds.push("-e " + (options.defaultBalance || 100));
|
||||||
|
@ -42,9 +40,12 @@ class Simulator {
|
||||||
|
|
||||||
const program = ganache ? 'ganache-cli' : 'testrpc';
|
const program = ganache ? 'ganache-cli' : 'testrpc';
|
||||||
|
|
||||||
shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true});
|
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
|
||||||
|
|
||||||
proxy.serve(host, port);
|
if(useProxy){
|
||||||
|
proxy.serve(host, port);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,10 +52,8 @@ class Embark {
|
||||||
this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain];
|
this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain];
|
||||||
let Simulator = require('./cmds/simulator.js');
|
let Simulator = require('./cmds/simulator.js');
|
||||||
let simulator = new Simulator({
|
let simulator = new Simulator({
|
||||||
contractsConfig: this.config.contractsConfig,
|
|
||||||
blockchainConfig: this.config.blockchainConfig,
|
blockchainConfig: this.config.blockchainConfig,
|
||||||
logger: this.logger,
|
logger: this.logger
|
||||||
events: this.events
|
|
||||||
});
|
});
|
||||||
simulator.run(options);
|
simulator.run(options);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue