From 5c7e9f0054251339059456688640703cd1dd8ed1 Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Thu, 7 Jun 2018 15:23:54 -0500 Subject: [PATCH] changes to the logic around gas estimator and profiler Signed-off-by: VoR0220 --- lib/modules/gasEstimator/index.js | 26 ++++++++++++++++++++------ lib/modules/profiler/index.js | 1 + 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/modules/gasEstimator/index.js b/lib/modules/gasEstimator/index.js index e96dcfe8..329f869b 100644 --- a/lib/modules/gasEstimator/index.js +++ b/lib/modules/gasEstimator/index.js @@ -1,5 +1,7 @@ const ContractFuzzer = require('../fuzzer'); +/*global web3*/ + class GasEstimator { constructor(embark) { this.embark = embark; @@ -16,20 +18,32 @@ class GasEstimator { let fuzzMap = self.fuzzer.generateFuzz(3, contract); self.logger.info("-- Beginning gastimation for contract -- " + contractName); let tempGasMap = {}; + let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); for (let i = 0; i < 3; i++) { - tempGasMap[i] = {} + tempGasMap[i] = {}; for (let name in fuzzMap[i]) { - tempGasMap[i][name] = contract.methods[name].apply(contract.methods[name], fuzzMap[i][name]).estimateGas(); + tempGasMap[i][name] = contractObj.methods[name] + .apply(contractObj.methods[name], fuzzMap[i][name]) + .estimateGas() + .then(estimatedGas => estimatedGas) + .catch(err => err); } - }; - tempGasMap.forEach((name) => { - if (name === "constructor") { + } + contract.abiDefinition.forEach((abiMethod) => { + let name = abiMethod.name; + console.log("estimaticating the gas for method name: ", name); + if (abiMethod.type === "constructor") { gasMap[name] = contract.gasEstimates.creation.totalCost; - } else if (tempGasMap[name][0] !== tempGasMap[name][1] && tempGasMap[name][1] !== tempGasMap[name][2]) { + } else if (abiMethod.inputs.length === 0) { + gasMap[name] = contractObj.methods[name].apply(contractObj.methods[name], []).estimateGas() + .then(estimatedGas => estimatedGas) + .catch(err => err); + } else if (tempGasMap[0][name] !== tempGasMap[1][name] && tempGasMap[1][name] !== tempGasMap[2][name]) { gasMap[name] = 'variable'; } else { gasMap[name] = tempGasMap[name][0]; } + console.log("Estimate Gas ForEAch: ", gasMap[name]); }); return gasMap; }); diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index a86f239d..1477ef69 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -17,6 +17,7 @@ class Profiler { 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']);