2018-06-12 17:16:19 +00:00
|
|
|
/*global web3*/
|
2018-06-08 17:46:57 +00:00
|
|
|
const async = require('async');
|
2018-07-07 13:30:47 +00:00
|
|
|
const ContractFuzzer = require('./fuzzer.js');
|
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
|
|
|
let fuzzMap = self.fuzzer.generateFuzz(3, contract);
|
2018-06-07 20:23:54 +00:00
|
|
|
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
|
2018-06-12 17:10:35 +00:00
|
|
|
async.each(contract.abiDefinition.filter((x) => x.type !== "event"),
|
2018-06-08 19:26:32 +00:00
|
|
|
(abiMethod, gasCb) => {
|
2018-06-08 17:46:57 +00:00
|
|
|
let name = abiMethod.name;
|
2018-06-15 21:24:19 +00:00
|
|
|
if (abiMethod.type === "constructor") {
|
2018-06-08 17:46:57 +00:00
|
|
|
// already provided for us
|
2018-06-12 17:10:35 +00:00
|
|
|
gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString();
|
2018-06-15 20:22:27 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
2018-08-02 19:17:40 +00:00
|
|
|
} else if (abiMethod.type === "fallback") {
|
2018-06-12 17:10:35 +00:00
|
|
|
gasMap['fallback'] = contract.gasEstimates.external[""].toString();
|
2018-06-15 20:22:27 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
|
|
|
} else if (
|
|
|
|
(abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0)
|
|
|
|
) {
|
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) => {
|
2018-06-15 20:22:27 +00:00
|
|
|
if (err) return gasCb(err, name, abiMethod.type);
|
2018-06-12 17:54:41 +00:00
|
|
|
gasMap[name] = gasAmount;
|
2018-06-15 20:22:27 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
2018-06-08 17:46:57 +00:00
|
|
|
});
|
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) => {
|
2018-06-12 17:10:35 +00:00
|
|
|
contractObj.methods[name].apply(contractObj.methods[name], values)
|
2018-06-08 19:26:32 +00:00
|
|
|
.estimateGas((err, gasAmount) => {
|
2018-06-12 17:52:23 +00:00
|
|
|
getVarianceCb(err, gasAmount);
|
2018-06-08 19:26:32 +00:00
|
|
|
});
|
|
|
|
}, (err, variance) => {
|
2018-08-31 12:41:41 +00:00
|
|
|
if (variance.every(v => v === variance[0])) {
|
2018-06-08 19:26:32 +00:00
|
|
|
gasMap[name] = variance[0];
|
|
|
|
} else {
|
2018-06-20 19:17:05 +00:00
|
|
|
// get average
|
2018-07-24 12:29:06 +00:00
|
|
|
let sum = variance.reduce(function(memo, num) { return memo + num; });
|
2018-06-20 19:17:05 +00:00
|
|
|
gasMap[name] = sum / variance.length;
|
2018-06-08 19:26:32 +00:00
|
|
|
}
|
2018-06-15 20:22:27 +00:00
|
|
|
return gasCb(null, name, abiMethod.type);
|
2018-06-08 19:26:32 +00:00
|
|
|
});
|
2018-06-12 15:39:45 +00:00
|
|
|
}
|
2018-06-08 17:46:57 +00:00
|
|
|
},
|
2018-06-15 20:22:27 +00:00
|
|
|
(err, name, type) => {
|
|
|
|
if (err) {
|
2018-06-15 21:24:19 +00:00
|
|
|
if (type === "constructor" || type === "fallback") name = type;
|
2018-06-15 20:22:27 +00:00
|
|
|
return cb(err, null, name);
|
|
|
|
}
|
|
|
|
cb(null, gasMap, null);
|
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;
|