Fix transaction logger

This commit is contained in:
Anthony Laibe 2018-08-14 14:20:31 +01:00 committed by Iuri Matias
parent eb874f32ac
commit 926ed7c258
1 changed files with 102 additions and 100 deletions

View File

@ -8,43 +8,43 @@ let receipts = {};
const {canonicalHost, defaultHost} = require('../../utils/host'); const {canonicalHost, defaultHost} = require('../../utils/host');
const parseRequest = function(reqBody){ const parseRequest = function (reqBody) {
let jsonO; let jsonO;
try { try {
jsonO = JSON.parse(reqBody); jsonO = JSON.parse(reqBody);
} catch(e){ } catch (e) {
return; // Request is not a json. Do nothing return; // Request is not a json. Do nothing
} }
if(jsonO.method === "eth_sendTransaction"){ if (jsonO.method === "eth_sendTransaction") {
commList[jsonO.id] = { commList[jsonO.id] = {
type: 'contract-log', type: 'contract-log',
address: jsonO.params[0].to, address: jsonO.params[0].to,
data: jsonO.params[0].data data: jsonO.params[0].data
}; };
} else if(jsonO.method === "eth_getTransactionReceipt"){ } else if (jsonO.method === "eth_getTransactionReceipt") {
if(transactions[jsonO.params[0]]){ if (transactions[jsonO.params[0]]) {
transactions[jsonO.params[0]].receiptId = jsonO.id; transactions[jsonO.params[0]].receiptId = jsonO.id;
receipts[jsonO.id] = transactions[jsonO.params[0]].commListId; receipts[jsonO.id] = transactions[jsonO.params[0]].commListId;
} }
} }
}; };
const parseResponse = function(ipc, resBody){ const parseResponse = function (ipc, resBody) {
let jsonO; let jsonO;
try { try {
jsonO = JSON.parse(resBody); jsonO = JSON.parse(resBody);
} catch(e) { } catch (e) {
return; // Response is not a json. Do nothing return; // Response is not a json. Do nothing
} }
if(commList[jsonO.id]){ if (commList[jsonO.id]) {
commList[jsonO.id].transactionHash = jsonO.result; commList[jsonO.id].transactionHash = jsonO.result;
transactions[jsonO.result] = {commListId: jsonO.id}; transactions[jsonO.result] = {commListId: jsonO.id};
} else if(receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber){ } else if (receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber) {
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber; commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed; commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
commList[receipts[jsonO.id]].status = jsonO.result.status; commList[receipts[jsonO.id]].status = jsonO.result.status;
if(ipc.connected && !ipc.connecting){ if (ipc.connected && !ipc.connecting) {
ipc.request('log', commList[receipts[jsonO.id]]); ipc.request('log', commList[receipts[jsonO.id]]);
} else { } else {
ipc.connecting = true; ipc.connecting = true;
@ -54,12 +54,12 @@ const parseResponse = function(ipc, resBody){
} }
delete transactions[commList[receipts[jsonO.id]].transactionHash]; delete transactions[commList[receipts[jsonO.id]].transactionHash];
delete commList[receipts[jsonO.id]];
delete receipts[jsonO.id]; delete receipts[jsonO.id];
delete commList[jsonO.id];
} }
}; };
exports.serve = function(ipc, host, port, ws){ exports.serve = function (ipc, host, port, ws) {
let proxy = httpProxy.createProxyServer({ let proxy = httpProxy.createProxyServer({
target: { target: {
host: canonicalHost(host), host: canonicalHost(host),
@ -77,7 +77,7 @@ exports.serve = function(ipc, host, port, ws){
proxyRes.on('data', (b) => resBody.push(b)); proxyRes.on('data', (b) => resBody.push(b));
proxyRes.on('end', function () { proxyRes.on('end', function () {
resBody = Buffer.concat(resBody).toString(); resBody = Buffer.concat(resBody).toString();
if(resBody){ if (resBody) {
parseResponse(ipc, resBody); parseResponse(ipc, resBody);
} }
}); });
@ -85,20 +85,22 @@ exports.serve = function(ipc, host, port, ws){
let server = http.createServer((req, res) => { let server = http.createServer((req, res) => {
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) {
parseRequest(reqBody); parseRequest(reqBody);
} }
}); });
if(!ws){ if (!ws) {
proxy.web(req, res); proxy.web(req, res);
} }
}); });
if(ws){ if (ws) {
const WsParser = require('simples/lib/parsers/ws'); // npm install simples const WsParser = require('simples/lib/parsers/ws'); // npm install simples
server.on('upgrade', function (req, socket, head) { server.on('upgrade', function (req, socket, head) {