2018-06-08 17:46:57 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('underscore');
|
2018-06-06 19:37:08 +00:00
|
|
|
const ContractFuzzer = require('../fuzzer');
|
|
|
|
|
2018-06-07 20:23:54 +00:00
|
|
|
/*global web3*/
|
|
|
|
|
2018-06-06 19:37:08 +00:00
|
|
|
class GasEstimator {
|
|
|
|
constructor(embark) {
|
|
|
|
this.embark = embark;
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.fuzzer = new ContractFuzzer(embark);
|
|
|
|
}
|
|
|
|
|
2018-06-08 17:46:57 +00:00
|
|
|
estimateGas(contractName, cb) {
|
2018-06-06 20:30:29 +00:00
|
|
|
const self = this;
|
2018-06-06 19:37:08 +00:00
|
|
|
let gasMap = {};
|
|
|
|
self.events.request('contracts:contract', contractName, (contract) => {
|
2018-06-06 20:30:29 +00:00
|
|
|
self.logger.info("Generating three rounds of fuzz for contract -- ", contractName);
|
|
|
|
let fuzzMap = self.fuzzer.generateFuzz(3, contract);
|
2018-06-06 19:37:08 +00:00
|
|
|
self.logger.info("-- Beginning gastimation for contract -- " + contractName);
|
2018-06-07 20:23:54 +00:00
|
|
|
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
|
2018-06-08 17:46:57 +00:00
|
|
|
async.each(contract.abiDefinition, function(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) {
|
|
|
|
// just run it and register it
|
|
|
|
contractObj.methods[name]
|
|
|
|
.apply(contractObj.methods[name], [])
|
|
|
|
.estimateGas(function(err, gasAmount) {
|
|
|
|
gasCb(err, gasAmount, name);
|
|
|
|
});
|
2018-06-06 19:37:08 +00:00
|
|
|
} else {
|
2018-06-08 17:46:57 +00:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(err, gasAmount, name) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
gasMap[name] = gasAmount;
|
2018-06-06 19:37:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GasEstimator;
|