From a9065b1eaeb620b41d522e0bfb48c41b5b12b9d5 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 12 Jun 2018 08:50:49 -0400 Subject: [PATCH] Websocket handling --- lib/core/proxy.js | 121 ++++++++++++++++---------- lib/modules/console_listener/index.js | 8 +- package.json | 1 + 3 files changed, 82 insertions(+), 48 deletions(-) diff --git a/lib/core/proxy.js b/lib/core/proxy.js index 71e2a95a..66784549 100644 --- a/lib/core/proxy.js +++ b/lib/core/proxy.js @@ -2,11 +2,63 @@ const httpProxy = require('http-proxy'); const http = require('http'); const constants = require('../constants.json'); -exports.serve = function(ipc, host, port, ws){ - let commList = {}; - let transactions = {}; - let receipts = {}; +let commList = {}; +let transactions = {}; +let receipts = {}; +const parseRequest = function(reqBody){ + let jsonO; + try { + jsonO = JSON.parse(reqBody); + } catch(e){ + return; // Request is not a json. Do nothing + } + if(jsonO.method === "eth_sendTransaction"){ + commList[jsonO.id] = { + type: 'contract-log', + address: jsonO.params[0].to, + data: jsonO.params[0].data + }; + } else if(jsonO.method === "eth_getTransactionReceipt"){ + if(transactions[jsonO.params[0]]){ + transactions[jsonO.params[0]].receiptId = jsonO.id; + receipts[jsonO.id] = transactions[jsonO.params[0]].commListId; + } + } +}; + +const parseResponse = function(ipc, resBody){ + let jsonO; + try { + jsonO = JSON.parse(resBody); + } catch(e) { + return; // Response is not a json. Do nothing + } + + if(commList[jsonO.id]){ + commList[jsonO.id].transactionHash = jsonO.result; + transactions[jsonO.result] = {commListId: jsonO.id}; + } else if(receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber){ + commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber; + commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed; + commList[receipts[jsonO.id]].status = jsonO.result.status; + + if(ipc.connected && !ipc.connecting){ + ipc.request('log', commList[receipts[jsonO.id]]); + } else { + ipc.connecting = true; + ipc.connect(() => { + ipc.connecting = false; + }); + } + + delete transactions[commList[receipts[jsonO.id]].transactionHash]; + delete receipts[jsonO.id]; + delete commList[jsonO.id]; + } +} + +exports.serve = function(ipc, host, port, ws){ let proxy = httpProxy.createProxyServer({ target: { host, @@ -25,34 +77,8 @@ exports.serve = function(ipc, host, port, ws){ proxyRes.on('data', (b) => resBody.push(b)); proxyRes.on('end', function () { resBody = Buffer.concat(resBody).toString(); - - let jsonO; - try { - jsonO = JSON.parse(resBody); - } catch(e) { - return; - } - - if(commList[jsonO.id]){ - commList[jsonO.id].transactionHash = jsonO.result; - transactions[jsonO.result] = {commListId: jsonO.id}; - } else if(receipts[jsonO.id]){ - commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber; - commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed; - commList[receipts[jsonO.id]].status = jsonO.result.status; - - if(ipc.connected && !ipc.connecting){ - ipc.request('log', commList[receipts[jsonO.id]]); - } else { - ipc.connecting = true; - ipc.connect(() => { - ipc.connecting = false; - }); - } - - delete transactions[commList[receipts[jsonO.id]].transactionHash]; - delete receipts[jsonO.id]; - delete commList[jsonO.id]; + if(resBody){ + parseResponse(ipc, resBody); } }); }); @@ -63,19 +89,7 @@ exports.serve = function(ipc, host, port, ws){ .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 - }; - } else if(jsonO.method === "eth_getTransactionReceipt"){ - if(transactions[jsonO.params[0]]){ - transactions[jsonO.params[0]].receiptId = jsonO.id; - receipts[jsonO.id] = transactions[jsonO.params[0]].commListId; - } - } + parseRequest(reqBody); } }); @@ -85,9 +99,26 @@ exports.serve = function(ipc, host, port, ws){ }); if(ws){ + const WsParser = require('simples/lib/parsers/ws'); // npm install simples + server.on('upgrade', function (req, socket, head) { proxy.ws(req, socket, head); }); + + proxy.on('open', (proxySocket) => { + proxySocket.on('data', (data) => { + parseResponse(ipc, data.toString().substr(data.indexOf("{"))); + }); + }); + + proxy.on('proxyReqWs', (proxyReq, req, socket) => { + var parser = new WsParser(0, false); + socket.pipe(parser); + parser.on('frame', function (frame) { + parseRequest(frame.data); + }); + + }); } server.listen(port); diff --git a/lib/modules/console_listener/index.js b/lib/modules/console_listener/index.js index e5c8874d..210b6ae2 100644 --- a/lib/modules/console_listener/index.js +++ b/lib/modules/console_listener/index.js @@ -8,11 +8,11 @@ class ConsoleListener { this.addressToContract = []; this.contractsConfig = embark.config.contractsConfig; this.contractsDeployed = false; - this._listenForLogRequests(); this.events.on("contractsDeployed", () => { this.contractsDeployed = true; + this._updateContractList(); }); } @@ -23,6 +23,8 @@ class ConsoleListener { return; } contractsList.forEach(contract => { + if(!contract.deployedAddress) return; + let address = contract.deployedAddress.toLowerCase(); if(!this.addressToContract[address]){ let funcSignatures = {}; @@ -49,7 +51,7 @@ class ConsoleListener { }); } - _listenForLogRequests(){ + _listenForLogRequests(){ if(this.ipc.ipcRole !== 'server') return; this.ipc.on('log', (request) => { if(request.type == 'contract-log'){ @@ -59,9 +61,9 @@ class ConsoleListener { if(!this.addressToContract[address]){ this._updateContractList(); } - if(!this.addressToContract[address]) return; + const name = this.addressToContract[address].name; const func = this.addressToContract[address].functions[data.substring(0, 10)]; const functionName = func.functionName; diff --git a/package.json b/package.json index 57abdf87..363d1f06 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "request": "^2.85.0", "serve-static": "^1.11.1", "shelljs": "^0.5.0", + "simples": "^0.8.8", "solc": "0.4.24", "string-replace-async": "^1.2.1", "style-loader": "^0.19.0",