From 819dec1bc3bfc5427cf7b4806b8f9c08a1ba003b Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Tue, 8 May 2018 10:30:46 -0500 Subject: [PATCH] profile me officer, here's a new module Signed-off-by: VoR0220 --- lib/contracts/deploy.js | 4 +++ lib/core/engine.js | 5 ++++ lib/modules/profiler/index.js | 56 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 lib/modules/profiler/index.js diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 1551331c5..62e54f6b0 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -16,6 +16,10 @@ class Deploy { this.chainConfig = options.chainConfig; this.plugins = options.plugins; this.gasLimit = options.gasLimit; + + this.events.setCommandHandler("contracts:contract", (contractName, cb) => { + cb(this.contractsManager.getContract(contractName)); + }); } initTracker(cb) { diff --git a/lib/core/engine.js b/lib/core/engine.js index e7aa45bad..043141ee9 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -187,6 +187,11 @@ class Engine { this.registerModule('vyper', { contractDirectories: self.config.contractDirectories }); + this.registerModule('profiler', { + events: this.events, + logger: this.logger, + plugins: this.plugins, + }); this.contractsManager = new ContractsManager({ contractFiles: this.config.contractsFiles, diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js new file mode 100644 index 000000000..5036e836c --- /dev/null +++ b/lib/modules/profiler/index.js @@ -0,0 +1,56 @@ +const asciiTable = require('ascii-table'); + +class Profiler { + constructor(embark ,options) { + this.embark = embark; + this.logger = embark.logger; + this.events = embark.events; + this.plugins = embark.plugins; + + this.registerConsoleCommand(); + } + + profile(contractName, contract) { + const self = this; + let table = new asciiTable(contractName); + table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs'); + contract.abiDefinition.forEach((abiMethod) => { + switch(abiMethod.type) { + case "constructor": + table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs)); + break; + default: + table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs)); + } + }); + self.logger.info(table.toString()); + } + + formatParams(params) { + if (undefined !== params && params.length) { + let paramString = "("; + let mappedParams = params.map(param => param.type); + paramString += mappedParams.join(',') + paramString += ")"; + return paramString; + } + return "()"; + } + + registerConsoleCommand() { + const self = this; + self.embark.registerConsoleCommand((cmd, _options) => { + let cmdName = cmd.split(' ')[0]; + let contractName = cmd.split(' ')[1]; + if (cmdName === 'profile') { + self.events.request('contracts:contract', contractName, (contract) => { + self.logger.info("-- profile for " + contractName); + this.profile(contractName, contract); + }); + return "profiled..." + } + }); + } +} + +module.exports = Profiler; \ No newline at end of file