From 0ff39f48b33bcb6bcf38a2f2893c801cab2ead07 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Fri, 8 Jun 2018 14:26:32 -0500 Subject: [PATCH] fix the callback schema for async and pretty it up a bit Signed-off-by: VoR0220 --- lib/modules/gasEstimator/index.js | 47 ++++++++++++++++++------------- lib/modules/profiler/index.js | 24 ++++++++-------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/modules/gasEstimator/index.js b/lib/modules/gasEstimator/index.js index f6761581..4eb27853 100644 --- a/lib/modules/gasEstimator/index.js +++ b/lib/modules/gasEstimator/index.js @@ -20,37 +20,46 @@ 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, function(abiMethod, gasCb) => { + async.each(contract.abiDefinition, + (abiMethod, gasCb) => { let name = abiMethod.name; if (abiMethod.type === "constructor") { // already provided for us gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost); - } else if (abiMethod.inputs.length === 0) { + } else if (abiMethod.inputs === []) { // just run it and register it contractObj.methods[name] .apply(contractObj.methods[name], []) - .estimateGas(function(err, gasAmount) { + .estimateGas((err, gasAmount) => { + if (err) gasCb(err); + gasMap[name] = gasAmount; gasCb(err, gasAmount, name); }); - } else { - // async concatenate all the fuzz values and their gas cost outputs and check for equality - async.concat(fuzzMap[name], function(values, getVarianceCb) { - contractObj.methods[name] - .apply(contractObj.methods[name], values) - .estimateGas(function(err, gasAmount) { - getVarianceCb(err, [gasAmount]); - }); - }, function(err, variance) { - if (err) gasCb(err) - else if (variance.reduce(_.isEqual) gasCb(null, variance[0], name); - gasCb(null, 'variable', 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) + .estimateGas((err, gasAmount) => { + getVarianceCb(err, [gasAmount]); + }); + }, (err, variance) => { + if (err) { + gasCb(err) + } else if (variance.reduce(_.isEqual, variance[2])) { + gasMap[name] = variance[0]; + } else { + gasMap[name] = 'variable'; + } + gasCb(); + }); + }; }, - function(err, gasAmount, name) => { + (err) => { if (err) return cb(err); - gasMap[name] = gasAmount; + cb(null, gasMap); } + ); }); } } diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index 1477ef69..14b7859a 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -15,18 +15,20 @@ class Profiler { const self = this; let table = new asciiTable(contractName); table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates'); - let gastimates = self.gasEstimator.estimateGas(contractName); - contract.abiDefinition.forEach((abiMethod) => { - console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]); - switch(abiMethod.type) { - case "constructor": - table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']); - break; - default: - table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates[abiMethod.name]); - } + self.gasEstimator.estimateGas(contractName, function(err, gastimates) { + if (err) throw new Error(err); + contract.abiDefinition.forEach((abiMethod) => { + console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]); + switch(abiMethod.type) { + case "constructor": + table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']); + break; + default: + table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates[abiMethod.name]); + } + }); + self.logger.info(table.toString()); }); - self.logger.info(table.toString()); } formatParams(params) {