define api for profiler

This commit is contained in:
Iuri Matias 2018-08-01 13:27:50 -04:00
parent 86412414ba
commit e7263b1a01
2 changed files with 36 additions and 6 deletions

View File

@ -168,7 +168,7 @@ class Engine {
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard}); this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
this.registerModule('vyper'); this.registerModule('vyper');
this.registerModule('profiler'); this.registerModule('profiler', {plugins: this.plugins});
this.registerModule('deploytracker'); this.registerModule('deploytracker');
this.registerModule('specialconfigs'); this.registerModule('specialconfigs');
this.registerModule('console_listener', {ipc: this.ipc}); this.registerModule('console_listener', {ipc: this.ipc});

View File

@ -2,16 +2,18 @@ const asciiTable = require('ascii-table');
const GasEstimator = require('./gasEstimator.js'); const GasEstimator = require('./gasEstimator.js');
class Profiler { class Profiler {
constructor(embark) { constructor(embark, options) {
this.embark = embark; this.embark = embark;
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.plugins = options.plugins;
this.gasEstimator = new GasEstimator(embark); this.gasEstimator = new GasEstimator(embark);
this.registerConsoleCommand(); this.registerConsoleCommand();
this.registerApi();
} }
profile(contractName, contract) { profile(contractName, contract, returnCb) {
const self = this; const self = this;
let table = new asciiTable(contractName); let table = new asciiTable(contractName);
table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates'); table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates');
@ -19,7 +21,7 @@ class Profiler {
if (err) { if (err) {
self.logger.error('error found in method: ', name); self.logger.error('error found in method: ', name);
self.logger.error(JSON.stringify(err)); self.logger.error(JSON.stringify(err));
return; return returnCb(err);
} }
contract.abiDefinition.forEach((abiMethod) => { contract.abiDefinition.forEach((abiMethod) => {
switch(abiMethod.type) { switch(abiMethod.type) {
@ -33,7 +35,7 @@ class Profiler {
table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates[abiMethod.name]); table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates[abiMethod.name]);
} }
}); });
self.logger.info(table.toString()); return returnCb(null, table.toString());
}); });
} }
@ -60,13 +62,41 @@ class Profiler {
return ""; return "";
} }
self.logger.info("-- profile for " + contractName); self.logger.info("-- profile for " + contractName);
this.profile(contractName, contract); self.profile(contractName, contract, (err, table) => {
self.logger.info(table);
});
}); });
return ""; return "";
} }
return false; return false;
}); });
} }
registerApi() {
const self = this;
let plugin = this.plugins.createPlugin('profiler', {});
plugin.registerAPICall(
'get',
'/embark-api/profiler/:contractName',
(req, res) => {
let contractName = req.params.contractName;
//self.events.request('contracts:contract', req.params.contractName, res.send.bind(res));
self.events.request('contracts:contract', contractName, (contract) => {
if (!contract || !contract.deployedAddress) {
return res.send("-- couldn't profile " + contractName + " - it's not deployed or could be an interface");
}
self.profile(contractName, contract, (err, table) => {
if (err) {
return res.send(err);
}
res.send(table);
});
});
}
);
}
} }
module.exports = Profiler; module.exports = Profiler;