From 66353bb8d5229d191db9ccb7f1428c19470081b9 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 8 Jun 2018 14:36:35 -0400 Subject: [PATCH] Extracted console logging to its own file --- lib/core/engine.js | 36 +---------------- lib/modules/console_listener/index.js | 57 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 lib/modules/console_listener/index.js diff --git a/lib/core/engine.js b/lib/core/engine.js index 7ebb843e..17b89b18 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -188,13 +188,13 @@ class Engine { this.ipc.serve(); } - this.registerModule('solidity', {ipc: this.ipc}); this.registerModule('vyper'); this.registerModule('profiler'); this.registerModule('fuzzer'); this.registerModule('deploytracker'); this.registerModule('specialconfigs'); + this.registerModule('console_listener', {ipc: this.ipc}); const ContractsManager = require('../contracts/contracts.js'); this.contractsManager = new ContractsManager({ @@ -224,40 +224,6 @@ 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(() => { diff --git a/lib/modules/console_listener/index.js b/lib/modules/console_listener/index.js new file mode 100644 index 00000000..8194c03e --- /dev/null +++ b/lib/modules/console_listener/index.js @@ -0,0 +1,57 @@ +const utils = require('../../utils/utils.js'); + +class ConsoleListener { + constructor(embark, options) { + this.logger = embark.logger; + this.ipc = options.ipc; + this.addressToContract = []; + this.contractsConfig = embark.config.contractsConfig; + this.listenForLogRequests(); + } + + _updateContractList(){ + Object.keys(this.contractsConfig.contracts).forEach(contractName => { + let contract = this.contractsConfig.contracts[contractName]; + let address = contract.deployedAddress.toLowerCase(); + + if(!this.addressToContract[address]){ + let funcSignatures = {}; + contract.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; + }); + + this.addressToContract[address] = { + name: contractName, + functions: funcSignatures + }; + } + }); + } + + listenForLogRequests(){ + this.ipc.on('log', (request) => { + if(request.type == 'contract-log'){ + + let {address, data, transactionHash} = request; + if(!this.addressToContract[address]){ + this._updateContractList(); + } + + let name = this.addressToContract[address].name; + let funcHash = this.addressToContract[address].functions[data.substring(0, 10)]; + + this.logger.debug(`${name}.${funcHash} : ${transactionHash}`); + } else { + this.logger.debug(request); + } + }); + } +} + +module.exports = ConsoleListener;