diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index 67a7b05b..1a9e8f86 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -3,6 +3,7 @@ const child_process = require('child_process'); const _ = require('underscore'); const fs = require('../../core/fs.js'); +const constants = require('../../constants.json'); const GethCommands = require('./geth_commands.js'); @@ -83,10 +84,18 @@ Blockchain.prototype.setupProxy = function() { let ipcObject = new Ipc({ipcRole: 'client'}); +<<<<<<< HEAD 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; +======= + proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false); + proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true); + this.config.rpcPort += constants.blockchain.servicePortOnProxy; + this.config.wsPort += constants.blockchain.servicePortOnProxy; + } +>>>>>>> Changes based on code review }; Blockchain.prototype.runCommand = function(cmd, options, callback) { diff --git a/lib/cmds/simulator.js b/lib/cmds/simulator.js index 4c0a89f0..638020e3 100644 --- a/lib/cmds/simulator.js +++ b/lib/cmds/simulator.js @@ -1,6 +1,7 @@ let shelljs = require('shelljs'); let proxy = require('../core/proxy'); const Ipc = require('../core/ipc'); +const constants = require('../constants.json'); class Simulator { constructor(options) { @@ -8,7 +9,7 @@ class Simulator { this.logger = options.logger; } - run(options) { + run(options) { let cmds = []; const testrpc = shelljs.which('testrpc'); @@ -23,7 +24,7 @@ class Simulator { let host = (options.host || this.blockchainConfig.rpcHost || 'localhost'); let port = (options.port || this.blockchainConfig.rpcPort || 8545); - cmds.push("-p " + (port + (useProxy ? 10 : 0))); + cmds.push("-p " + (port + (useProxy ? constants.blockchain.servicePortOnProxy : 0))); cmds.push("-h " + host); cmds.push("-a " + (options.numAccounts || 10)); cmds.push("-e " + (options.defaultBalance || 100)); diff --git a/lib/constants.json b/lib/constants.json index 0315ffbe..d8a0759b 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -35,7 +35,8 @@ "blockchain": { "blockchainReady": "blockchainReady", "init": "init", - "initiated": "initiated" + "initiated": "initiated", + "servicePortOnProxy": 10 }, "storage": { "init": "init", diff --git a/lib/core/proxy.js b/lib/core/proxy.js index 14b33025..71e2a95a 100644 --- a/lib/core/proxy.js +++ b/lib/core/proxy.js @@ -1,5 +1,6 @@ const httpProxy = require('http-proxy'); const http = require('http'); +const constants = require('../constants.json'); exports.serve = function(ipc, host, port, ws){ let commList = {}; @@ -8,14 +9,15 @@ exports.serve = function(ipc, host, port, ws){ let proxy = httpProxy.createProxyServer({ target: { - host: host, - port: port + 10 + host, + port: port + constants.blockchain.servicePortOnProxy }, ws: ws }); proxy.on('error', function () { - proxy.close(); + console.log(__("Error forwarding requests to blockchain/simulator")); + process.exit(); }); proxy.on('proxyRes', (proxyRes) => { @@ -23,31 +25,34 @@ exports.serve = function(ipc, host, port, ws){ 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; - 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]; + 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; + }); } - } catch(e){ - // + + delete transactions[commList[receipts[jsonO.id]].transactionHash]; + delete receipts[jsonO.id]; + delete commList[jsonO.id]; } }); }); @@ -59,13 +64,13 @@ exports.serve = function(ipc, host, port, ws){ reqBody = Buffer.concat(reqBody).toString(); if(reqBody){ let jsonO = JSON.parse(reqBody); - if(jsonO.method == "eth_sendTransaction"){ + 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"){ + } 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; diff --git a/lib/modules/console_listener/index.js b/lib/modules/console_listener/index.js index ca058035..64f02dc8 100644 --- a/lib/modules/console_listener/index.js +++ b/lib/modules/console_listener/index.js @@ -4,75 +4,83 @@ class ConsoleListener { constructor(embark, options) { this.logger = embark.logger; this.ipc = options.ipc; + this.events = embark.events; this.addressToContract = []; this.contractsConfig = embark.config.contractsConfig; - this.listenForLogRequests(); - } + this.contractsDeployed = false; - _updateContractList(){ - Object.keys(this.contractsConfig.contracts).forEach(contractName => { - let contract = this.contractsConfig.contracts[contractName]; - let address = contract.deployedAddress.toLowerCase(); + this._listenForLogRequests(); - if(!this.addressToContract[address]){ - let funcSignatures = {}; - contract.abiDefinition - .filter(func => func.type == "function") - .map(func => { - return { - name: func.name + - '(' + - (func.inputs ? func.inputs.map(input => input.type).join(',') : '') + - ')', - abi: func, - functionName: func.name - }; - }) - .forEach(func => { - funcSignatures[utils.sha3(func.name).substring(0, 10)] = func; - }); - - this.addressToContract[address] = { - name: contractName, - functions: funcSignatures - }; - } + this.events.on("contractsDeployed", () => { + this.contractsDeployed = true; }); } - listenForLogRequests(){ - if(this.ipc.ipcRole === 'server'){ - this.ipc.on('log', (request) => { - if(request.type == 'contract-log'){ - - let {address, data, transactionHash, blockNumber, gasUsed, status} = request; - if(!this.addressToContract[address]){ - this._updateContractList(); - } - - let name = this.addressToContract[address].name; - let func = this.addressToContract[address].functions[data.substring(0, 10)]; - let functionName = func.functionName; - - let decodedParameters = utils.decodeParams(func.abi.inputs, data.substring(10)); - let paramString = ""; - if(func.abi.inputs){ - func.abi.inputs.forEach((input) => { - let quote = input.type.indexOf("int") == -1 ? '"' : ''; - paramString += quote + decodedParameters[input.name] + quote + ", "; + _updateContractList(){ + this.events.request("contracts:list", (_err, contractsList) => { + if(_err) return; + contractsList.forEach(contract => { + let address = contract.deployedAddress.toLowerCase(); + if(!this.addressToContract[address]){ + let funcSignatures = {}; + contract.abiDefinition + .filter(func => func.type == "function") + .map(func => { + const name = func.name + + '(' + + (func.inputs ? func.inputs.map(input => input.type).join(',') : '') + + ')'; + funcSignatures[utils.sha3(name).substring(0, 10)] = { + name, + abi: func, + functionName: func.name + }; }); - paramString = paramString.substring(0, paramString.length - 2); - } - gasUsed = utils.hexToNumber(gasUsed); - blockNumber = utils.hexToNumber(blockNumber); - - this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`); - } else { - this.logger.info(JSON.stringify(request)); + this.addressToContract[address] = { + name: contract.className, + functions: funcSignatures + }; } }); - } + }); + } + + _listenForLogRequests(){ + if(this.ipc.ipcRole !== 'server') return; + this.ipc.on('log', (request) => { + if(request.type == 'contract-log'){ + if(!this.contractsDeployed) return; + + let {address, data, transactionHash, blockNumber, gasUsed, status} = request; + 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; + + const decodedParameters = utils.decodeParams(func.abi.inputs, data.substring(10)); + let paramString = ""; + if(func.abi.inputs){ + func.abi.inputs.forEach((input) => { + let quote = input.type.indexOf("int") == -1 ? '"' : ''; + paramString += quote + decodedParameters[input.name] + quote + ", "; + }); + paramString = paramString.substring(0, paramString.length - 2); + } + + gasUsed = utils.hexToNumber(gasUsed); + blockNumber = utils.hexToNumber(blockNumber); + + this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`); + } else { + this.logger.info(JSON.stringify(request)); + } + }); } }