fix(@embark/cockpit): fix gas estimation

This commit is contained in:
Andre Medeiros 2019-01-22 09:57:08 -05:00 committed by Iuri Matias
parent 1d459e45b1
commit 43fed4f3e5

View File

@ -1,4 +1,3 @@
/*global web3*/
const async = require('async'); const async = require('async');
const ContractFuzzer = require('./fuzzer.js'); const ContractFuzzer = require('./fuzzer.js');
@ -13,63 +12,65 @@ class GasEstimator {
estimateGas(contractName, cb) { estimateGas(contractName, cb) {
const self = this; const self = this;
let gasMap = {}; let gasMap = {};
self.events.request('contracts:contract', contractName, (contract) => { self.events.request('blockchain:object', ({ web3 }) => {
let fuzzMap = self.fuzzer.generateFuzz(3, contract); self.events.request('contracts:contract', contractName, (contract) => {
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); let fuzzMap = self.fuzzer.generateFuzz(3, contract);
async.each(contract.abiDefinition.filter((x) => x.type !== "event"), let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
(abiMethod, gasCb) => { async.each(contract.abiDefinition.filter((x) => x.type !== "event"),
let name = abiMethod.name; (abiMethod, gasCb) => {
if (abiMethod.type === "constructor") { let name = abiMethod.name;
// already provided for us if (abiMethod.type === "constructor") {
gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString(); // already provided for us
return gasCb(null, name, abiMethod.type); gasMap['constructor'] = contract.gasEstimates.creation.totalCost.toString();
} else if (abiMethod.type === "fallback") { return gasCb(null, name, abiMethod.type);
gasMap['fallback'] = contract.gasEstimates.external[""].toString(); } else if (abiMethod.type === "fallback") {
return gasCb(null, name, abiMethod.type); gasMap['fallback'] = contract.gasEstimates.external[""].toString();
} else if ( return gasCb(null, name, abiMethod.type);
(abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0) } else if (
) { (abiMethod.inputs === null || abiMethod.inputs === undefined || abiMethod.inputs.length === 0)
// just run it and register it ) {
contractObj.methods[name] // just run it and register it
.apply(contractObj.methods[name], []) contractObj.methods[name]
.estimateGas((err, gasAmount) => { .apply(contractObj.methods[name], [])
if (err) { .estimateGas((err, gasAmount) => {
self.logger.error(`Error getting gas estimate for "${name}"`, err.message || err); if (err) {
return gasCb(null, name, abiMethod.type); 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;
} }
gasMap[name] = gasAmount;
return gasCb(null, name, abiMethod.type); 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);
}
);
}); });
} }
} }