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 19:26:32 +00:00
|
|
|
async.each(contract.abiDefinition,
|
|
|
|
(abiMethod, gasCb) => {
|
2018-06-08 17:46:57 +00:00
|
|
|
let name = abiMethod.name;
|
2018-06-12 15:31:26 +00:00
|
|
|
console.log("NAME: ", name);
|
|
|
|
console.log("ABI INPUTS: ", abiMethod.inputs);
|
2018-06-08 17:46:57 +00:00
|
|
|
if (abiMethod.type === "constructor") {
|
|
|
|
// already provided for us
|
2018-06-12 15:31:26 +00:00
|
|
|
gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost.toString());
|
|
|
|
} else if (abiMethod.type === "fallback") {
|
|
|
|
gasCb(null, 'fallback', contract.gasEstimates.external[""].toString());
|
|
|
|
} else if (abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0) {
|
|
|
|
console.log("Are we hitting here");
|
2018-06-08 17:46:57 +00:00
|
|
|
// just run it and register it
|
|
|
|
contractObj.methods[name]
|
|
|
|
.apply(contractObj.methods[name], [])
|
2018-06-08 19:26:32 +00:00
|
|
|
.estimateGas((err, gasAmount) => {
|
|
|
|
if (err) gasCb(err);
|
|
|
|
gasMap[name] = gasAmount;
|
2018-06-08 17:46:57 +00:00
|
|
|
gasCb(err, gasAmount, name);
|
|
|
|
});
|
2018-06-08 19:26:32 +00:00
|
|
|
} 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)
|
2018-06-12 15:31:26 +00:00
|
|
|
} else if (_.isEqual(variance[0], variance[1]) && _.isEqual(variance[1], variance[2])) {
|
2018-06-08 19:26:32 +00:00
|
|
|
gasMap[name] = variance[0];
|
|
|
|
} else {
|
|
|
|
gasMap[name] = 'variable';
|
|
|
|
}
|
|
|
|
gasCb();
|
|
|
|
});
|
|
|
|
};
|
2018-06-08 17:46:57 +00:00
|
|
|
},
|
2018-06-08 19:26:32 +00:00
|
|
|
(err) => {
|
2018-06-08 17:46:57 +00:00
|
|
|
if (err) return cb(err);
|
2018-06-08 19:26:32 +00:00
|
|
|
cb(null, gasMap);
|
2018-06-06 19:37:08 +00:00
|
|
|
}
|
2018-06-08 19:26:32 +00:00
|
|
|
);
|
2018-06-06 19:37:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GasEstimator;
|