From 43fed4f3e509ec59bcf1a01ed7313a4a631c3ed3 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Tue, 22 Jan 2019 09:57:08 -0500 Subject: [PATCH] fix(@embark/cockpit): fix gas estimation --- src/lib/modules/profiler/gasEstimator.js | 111 ++++++++++++----------- 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/src/lib/modules/profiler/gasEstimator.js b/src/lib/modules/profiler/gasEstimator.js index 73a589a72..58ff0b8c0 100644 --- a/src/lib/modules/profiler/gasEstimator.js +++ b/src/lib/modules/profiler/gasEstimator.js @@ -1,4 +1,3 @@ -/*global web3*/ const async = require('async'); const ContractFuzzer = require('./fuzzer.js'); @@ -13,63 +12,65 @@ class GasEstimator { estimateGas(contractName, cb) { const self = this; let gasMap = {}; - self.events.request('contracts:contract', contractName, (contract) => { - let fuzzMap = self.fuzzer.generateFuzz(3, contract); - let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); - async.each(contract.abiDefinition.filter((x) => x.type !== "event"), - (abiMethod, gasCb) => { - let name = abiMethod.name; - if (abiMethod.type === "constructor") { - // already provided for us - gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString(); - return gasCb(null, name, abiMethod.type); - } else if (abiMethod.type === "fallback") { - gasMap['fallback'] = contract.gasEstimates.external[""].toString(); - return gasCb(null, name, abiMethod.type); - } else if ( - (abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0) - ) { - // just run it and register it - contractObj.methods[name] - .apply(contractObj.methods[name], []) - .estimateGas((err, gasAmount) => { - if (err) { - self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err); - return gasCb(null, name, abiMethod.type); - } - gasMap[name] = gasAmount; - return gasCb(null, name, abiMethod.type); - }); - } 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) => { - if (err) { - self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err); - } - getVarianceCb(null, gasAmount); - }); - }, (err, variance) => { - if (variance.every(v => v === variance[0])) { - gasMap[name] = variance[0]; - } else { - // get average - let sum = variance.reduce(function(memo, num) { return memo + num; }); - gasMap[name] = sum / variance.length; + self.events.request('blockchain:object', ({ web3 }) => { + self.events.request('contracts:contract', contractName, (contract) => { + let fuzzMap = self.fuzzer.generateFuzz(3, contract); + let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); + async.each(contract.abiDefinition.filter((x) => x.type !== "event"), + (abiMethod, gasCb) => { + let name = abiMethod.name; + if (abiMethod.type === "constructor") { + // already provided for us + gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString(); + return gasCb(null, name, abiMethod.type); + } else if (abiMethod.type === "fallback") { + gasMap['fallback'] = contract.gasEstimates.external[""].toString(); + return gasCb(null, name, abiMethod.type); + } else if ( + (abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0) + ) { + // just run it and register it + contractObj.methods[name] + .apply(contractObj.methods[name], []) + .estimateGas((err, gasAmount) => { + if (err) { + self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err); + return gasCb(null, name, abiMethod.type); } + gasMap[name] = gasAmount; return gasCb(null, name, abiMethod.type); - }); + }); + } 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) => { + if (err) { + self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err); + } + getVarianceCb(null, gasAmount); + }); + }, (err, variance) => { + if (variance.every(v => v === variance[0])) { + gasMap[name] = variance[0]; + } else { + // get average + let sum = variance.reduce(function(memo, num) { return memo + num; }); + gasMap[name] = sum / variance.length; + } + return gasCb(null, name, abiMethod.type); + }); + } + }, + (err, name, type) => { + if (err) { + if (type === "constructor" || type === "fallback") name = type; + return cb(err, null, name); + } + cb(null, gasMap, null); } - }, - (err, name, type) => { - if (err) { - if (type === "constructor" || type === "fallback") name = type; - return cb(err, null, name); - } - cb(null, gasMap, null); - } - ); + ); + }); }); } }