Extracted console logging to its own file

This commit is contained in:
Richard Ramos 2018-06-08 14:36:35 -04:00
parent 9b37f807df
commit 66353bb8d5
2 changed files with 58 additions and 35 deletions

View File

@ -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(() => {

View File

@ -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;