From 43c8525d48253b99ec81eb75a75d45736cd7e11c Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Tue, 12 Jun 2018 12:10:35 -0500 Subject: [PATCH] fix callbacks in gas estimator, add more fallback support and clean up console logs to better display potential errors Signed-off-by: VoR0220 --- lib/modules/fuzzer/index.js | 2 +- lib/modules/gasEstimator/index.js | 27 ++++++++++++++------------- lib/modules/profiler/index.js | 10 ++++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/modules/fuzzer/index.js b/lib/modules/fuzzer/index.js index 819e73b7..6be502d8 100644 --- a/lib/modules/fuzzer/index.js +++ b/lib/modules/fuzzer/index.js @@ -17,7 +17,7 @@ class ContractFuzzer { generateFuzz(iterations, contract) { const self = this; let fuzzMap = {}; - contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0).forEach((abiMethod) => { + contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0 && x.type != "event").forEach((abiMethod) => { let name = abiMethod.type === "constructor" ? "constructor" : abiMethod.name; let inputTypes = abiMethod.inputs.map(input => input.type); fuzzMap[name] = {}; diff --git a/lib/modules/gasEstimator/index.js b/lib/modules/gasEstimator/index.js index f67d5032..cfa41dca 100644 --- a/lib/modules/gasEstimator/index.js +++ b/lib/modules/gasEstimator/index.js @@ -20,41 +20,42 @@ class GasEstimator { let fuzzMap = self.fuzzer.generateFuzz(3, contract); self.logger.info("-- Beginning gastimation for contract -- " + contractName); let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); - async.each(contract.abiDefinition, + async.each(contract.abiDefinition.filter((x) => x.type !== "event"), (abiMethod, gasCb) => { let name = abiMethod.name; - console.log("NAME: ", name); - console.log("ABI INPUTS: ", abiMethod.inputs); if (abiMethod.type === "constructor") { // already provided for us - gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost.toString()); + gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString(); + gasCb(null); } else if (abiMethod.type === "fallback") { - gasCb(null, 'fallback', contract.gasEstimates.external[""].toString()); + gasMap['fallback'] = contract.gasEstimates.external[""].toString(); + gasCb(null); } else if (abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0) { - console.log("Are we hitting here"); // just run it and register it contractObj.methods[name] .apply(contractObj.methods[name], []) .estimateGas((err, gasAmount) => { - if (err) gasCb(err); - gasMap[name] = gasAmount; - gasCb(err, gasAmount, name); + if (err) { + return gasCb(err); + } else { + gasMap[name] = gasAmount; + gasCb(null, gasAmount, name); + } }); } else { // async concatenate all the fuzz values and their gas cost outputs and check for equality async.concat(fuzzMap[name], (values, getVarianceCb) => { - contractObj.methods[name] - .apply(contractObj.methods[name], values) + contractObj.methods[name].apply(contractObj.methods[name], values) .estimateGas((err, gasAmount) => { getVarianceCb(err, [gasAmount]); }); }, (err, variance) => { if (err) { - gasCb(err); + return gasCb(err); } else if (_.isEqual(variance[0], variance[1]) && _.isEqual(variance[1], variance[2])) { gasMap[name] = variance[0]; } else { - gasMap[name] = 'variable'; + gasMap[name] = 'infinite'; } gasCb(); }); diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index 56a46fe1..2c884f49 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -11,18 +11,24 @@ class Profiler { this.registerConsoleCommand(); } - profile(contractName, contract) { + profile(contractName, contract, cb) { const self = this; let table = new asciiTable(contractName); table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates'); self.gasEstimator.estimateGas(contractName, function(err, gastimates) { - if (err) throw new Error(err); + if (err) { + self.logger.error(err); + return; + } contract.abiDefinition.forEach((abiMethod) => { console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]); switch(abiMethod.type) { case "constructor": table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates['constructor']); break; + case "fallback": + table.addRow("fallback", abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates['fallback']); + break; default: table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, self.formatParams(abiMethod.inputs), self.formatParams(abiMethod.outputs), gastimates[abiMethod.name]); }