Rebasing changes from develop

This commit is contained in:
Richard Ramos 2018-06-11 16:40:14 -04:00
parent 05f5a145f1
commit bf25381fa6
7 changed files with 167 additions and 70 deletions

View File

@ -76,13 +76,17 @@ var Blockchain = function(options) {
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);
const proxy = require('../../core/proxy');
const Ipc = require('../../core/ipc');
let ipcObject = new Ipc({ipcRole: 'client'});
proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false);
// proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true);
this.config.rpcPort += 10;
this.config.wsPort += 10;
}
//this.config.wsPort += 10;
}
};
Blockchain.prototype.runCommand = function(cmd, options, callback) {
console.log(__("running: %s", cmd.underline).green);

View File

@ -1,56 +0,0 @@
const httpProxy = require('http-proxy');
const http = require('http');
exports.serve = function(host, port, ws){
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] = {
address: jsonO.params[0].to,
requestData: jsonO.params[0].data
};
}
}
});
proxy.proxyRequest(req, res, {
target: {
host: host,
port: port + 10,
ws: ws
}
});
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 = jsonO.result;
// TODO: decode commlist
// TODO: send messages to console
// ” SimpleStorage> set(5) | tx: 0xef234f16etc ”
delete commList[jsonO.id];
}
} catch(e){
//
}
});
});
});
server.listen(port);
};

View File

@ -1,5 +1,7 @@
let shelljs = require('shelljs');
let proxy = require('./proxy');
let proxy = require('../core/proxy');
const Ipc = require('../core/ipc');
class Simulator {
constructor(options) {
this.blockchainConfig = options.blockchainConfig;
@ -43,7 +45,8 @@ class Simulator {
shelljs.exec(`${program} ${cmds.join(' ')}`, {async : true});
if(useProxy){
proxy.serve(host, port);
let ipcObject = new Ipc({ipcRole: 'client'});
proxy.serve(ipcObject, host, port, false);
}
}

View File

@ -184,6 +184,11 @@ class Engine {
});
this.ipc = new IPC({logger: this.logger, ipcRole: options.ipcRole});
if (this.ipc.isServer()) {
this.ipc.serve();
}
this.registerModule('solidity', {ipc: this.ipc});
this.registerModule('vyper');
this.registerModule('profiler');
@ -219,6 +224,40 @@ class Engine {
plugins: this.plugins
});
// Console logger
// TODO: extract to its own file
let addressToContract = {};
this.ipc.on('log', (jsonObj) => {
if(jsonObj.type == 'contract-log'){
if(!addressToContract[jsonObj.address]){
let contractList = Object.keys(this.config.contractsConfig.contracts);
for(let i = 0; i < contractList.length; i++){
let cont = this.config.contractsConfig.contracts[contractList[i]];
if(!addressToContract[cont.deployedAddress.toLowerCase()]){
let funcSignatures = {};
cont.abiDefinition
.filter(func => func.type == "function")
.map(func => func.name + '(' +
(func.inputs ? func.inputs.map(input => input.type).join(',') : '') +
')')
.forEach(func => {
funcSignatures[utils.sha3(func).substring(0, 10)] = func;
});
addressToContract[cont.deployedAddress.toLowerCase()] = {
name: contractList[i],
functions: funcSignatures
};
}
}
}
let funcHash = addressToContract[jsonObj.address].functions[jsonObj.data.substring(0, 10)];
this.logger.debug(addressToContract[jsonObj.address].name + "." + funcHash + " : " + jsonObj.transactionHash);
} else {
this.logger.info(jsonObj);
}
});
this.events.on('file-event', function (fileType) {
clearTimeout(self.fileTimeout);
self.fileTimeout = setTimeout(() => {

66
lib/core/proxy.js Normal file
View File

@ -0,0 +1,66 @@
const httpProxy = require('http-proxy');
const http = require('http');
exports.serve = function(ipc, host, port, ws){
let commList = {};
let proxy = httpProxy.createProxyServer({
target: {
host: host,
port: port + 10,
ws: ws
}
});
proxy.on('proxyRes', (proxyRes) => {
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 = jsonO.result;
if(ipc.connected && !ipc.connecting){
ipc.request('log', commList[jsonO.id]);
} else {
ipc.connecting = true;
ipc.connect((err) => {
ipc.connecting = false;
});
}
delete commList[jsonO.id];
}
} catch(e){
//
}
});
});
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] = {
type: 'contract-log',
address: jsonO.params[0].to,
data: jsonO.params[0].data
};
}
}
});
if(ws){
proxy.ws(req, res);
} else {
proxy.web(req, res);
}
});
server.listen(port);
};

View File

@ -47,7 +47,6 @@ class SolcW {
});
if (this.ipc.isServer()) {
this.ipc.serve();
this.ipc.on('compile', self.compile.bind(this));
}

54
package-lock.json generated
View File

@ -50,7 +50,7 @@
"resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-5.0.2.tgz",
"integrity": "sha512-Q3FWsbdmkQd1ib11A4XNWQvRD//5KpPoGawA8aB2DR7pWKoW9XQv3+dGxD/Z1eVFze23Okdo27ZQytVFlweKvQ==",
"requires": {
"@types/node": "10.1.4"
"@types/node": "10.3.1"
}
},
"@types/lockfile": {
@ -59,16 +59,16 @@
"integrity": "sha512-pD6JuijPmrfi84qF3/TzGQ7zi0QIX+d7ZdetD6jUA6cp+IsCzAquXZfi5viesew+pfpOTIdAVKuh1SHA7KeKzg=="
},
"@types/node": {
"version": "10.1.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.1.4.tgz",
"integrity": "sha512-GpQxofkdlHYxjHad98UUdNoMO7JrmzQZoAaghtNg14Gwg7YkohcrCoJEcEMSgllx4VIZ+mYw7ZHjfaeIagP/rg=="
"version": "10.3.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.3.1.tgz",
"integrity": "sha512-IsX9aDHDzJohkm3VCDB8tkzl5RQ34E/PFA29TQk6uDGb7Oc869ZBtmdKVDBzY3+h9GnXB8ssrRXEPVZrlIOPOw=="
},
"@types/node-fetch": {
"version": "1.6.9",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-1.6.9.tgz",
"integrity": "sha512-n2r6WLoY7+uuPT7pnEtKJCmPUGyJ+cbyBR8Avnu4+m1nzz7DwBVuyIvvlBzCZ/nrpC7rIgb3D6pNavL7rFEa9g==",
"requires": {
"@types/node": "10.1.4"
"@types/node": "10.3.1"
}
},
"@types/semver": {
@ -81,7 +81,7 @@
"resolved": "https://registry.npmjs.org/@types/tar/-/tar-4.0.0.tgz",
"integrity": "sha512-YybbEHNngcHlIWVCYsoj7Oo1JU9JqONuAlt1LlTH/lmL8BMhbzdFUgReY87a05rY1j8mfK47Del+TCkaLAXwLw==",
"requires": {
"@types/node": "10.1.4"
"@types/node": "10.3.1"
}
},
"@types/url-join": {
@ -1913,6 +1913,11 @@
"resolved": "https://registry.npmjs.org/buffer-xor/-/buffer-xor-1.0.3.tgz",
"integrity": "sha1-JuYe0UIvtw3ULm42cp7VHYVf6Nk="
},
"bufferhelper": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/bufferhelper/-/bufferhelper-0.2.0.tgz",
"integrity": "sha1-7C842SU4dpzqKQFIeTXp4h2Bc7M="
},
"builtin-modules": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
@ -2842,6 +2847,11 @@
"source-map": "0.5.7"
}
},
"ctype": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/ctype/-/ctype-0.5.3.tgz",
"integrity": "sha1-gsGMJGH3QRTvFsE1IkrQuRRMoS8="
},
"currently-unhandled": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz",
@ -5745,6 +5755,23 @@
"resolved": "https://registry.npmjs.org/http-https/-/http-https-1.0.0.tgz",
"integrity": "sha1-L5CN1fHbQGjAWM1ubUzjkskTOJs="
},
"http-proxy": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.17.0.tgz",
"integrity": "sha512-Taqn+3nNvYRfJ3bGvKfBSRwy1v6eePlm3oc/aWVxZp57DQr5Eq3xhKJi7Z4hZpS8PC3H4qI+Yly5EmFacGuA/g==",
"requires": {
"eventemitter3": "3.1.0",
"follow-redirects": "1.4.1",
"requires-port": "1.0.0"
},
"dependencies": {
"eventemitter3": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.0.tgz",
"integrity": "sha512-ivIvhpq/Y0uSjcHDcOIccjmYjGLcP09MFGE7ysAwkAvkXfpZlC985pH2/ui64DKazbTW/4kN3yqozUxlXzI6cA=="
}
}
},
"http-shutdown": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-shutdown/-/http-shutdown-1.2.0.tgz",
@ -8465,6 +8492,11 @@
}
}
},
"node-uuid": {
"version": "1.4.8",
"resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.8.tgz",
"integrity": "sha1-sEDrCSOWivq/jTL7HxfxFn/auQc="
},
"nodeify": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/nodeify/-/nodeify-1.0.1.tgz",
@ -9144,6 +9176,11 @@
"integrity": "sha512-ARhBOdzS3e41FbkW/XWrTEtukqqLoK5+Z/4UeDaLuSW+39JPeFgs4gCGqsrJHVZX0fUrx//4OF0K1CUGwlIFow==",
"dev": true
},
"pm": {
"version": "2.2.5",
"resolved": "https://registry.npmjs.org/pm/-/pm-2.2.5.tgz",
"integrity": "sha1-IgJZn1m/trOID6naI0rdL0NLYK8="
},
"posix-character-classes": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz",
@ -10231,6 +10268,11 @@
"resolve-from": "1.0.1"
}
},
"requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
},
"resolve": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.5.0.tgz",