<commit_before>const httpProxy = require('http-proxy');
const http = require('http');
const constants = require('../constants.json');

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,
            port: port + constants.blockchain.servicePortOnProxy
          },
        ws: ws
    });

    proxy.on('error', function (e) {
        console.error(__("Error forwarding requests to blockchain/simulator"), e);
    });

    proxy.on('proxyRes', (proxyRes) => {
        let resBody = [];
        proxyRes.on('data', (b) => resBody.push(b));
        proxyRes.on('end', function () {
            resBody = Buffer.concat(resBody).toString();
            if(resBody){
                parseResponse(ipc, resBody);
            }
        });
    });

    let server = http.createServer((req, res) => {
        let reqBody = [];
        req.on('data', (b) => { reqBody.push(b); })
            .on('end', () => {
                reqBody = Buffer.concat(reqBody).toString();
                if(reqBody){
                    parseRequest(reqBody);
                }
            });

        if(!ws){
            proxy.web(req, res);
        }
    });

    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);
};
<commit_msg>Fix failing test on blockchain<commit_after>const httpProxy = require('http-proxy');
const http = require('http');
const constants = require('../constants.json');

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,
            port: port + constants.blockchain.servicePortOnProxy
          },
        ws: ws
    });

    proxy.on('error', function (e) {
        console.error(__("Error forwarding requests to blockchain/simulator"), e);
    });

    proxy.on('proxyRes', (proxyRes) => {
        let resBody = [];
        proxyRes.on('data', (b) => resBody.push(b));
        proxyRes.on('end', function () {
            resBody = Buffer.concat(resBody).toString();
            if(resBody){
                parseResponse(ipc, resBody);
            }
        });
    });

    let server = http.createServer((req, res) => {
        let reqBody = [];
        req.on('data', (b) => { reqBody.push(b); })
            .on('end', () => {
                reqBody = Buffer.concat(reqBody).toString();
                if(reqBody){
                    parseRequest(reqBody);
                }
            });

        if(!ws){
            proxy.web(req, res);
        }
    });

    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);
    return server;
};

{'fraction_non_alphanumeric': 0.15422885572139303, 'fraction_numerical': 0.0016135538523598225, 'mean_word_length': 1.9492466296590008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1827771', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2238, 'n_words': 386}