redo logic so that it uses the async library

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
VoR0220 2018-06-08 12:46:57 -05:00
parent 5c7e9f0054
commit 90f342b65a
2 changed files with 51 additions and 30 deletions

View File

@ -17,7 +17,23 @@ class ContractFuzzer {
generateFuzz(iterations, contract) { generateFuzz(iterations, contract) {
const self = this; const self = this;
let fuzzMap = {}; let fuzzMap = {};
contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0).forEach((abiMethod) => {
let name = abiMethod.type === "constructor" ? "constructor" : abiMethod.name;
let inputTypes = abiMethod.inputs.map(input => input.type);
self.logger.debug("input method:", name);
fuzzMap[name] = {};
for (let i = 0; i < iterations; i++) { for (let i = 0; i < iterations; i++) {
fuzzMap[name][i] = {};
self.logger.debug("iteration:", i);
fuzzMap[name][i] = inputTypes.map(input => this.getTypeFuzz(input));
}
for (let key in fuzzMap[name]) self.logger.info(name + ":" + fuzzMap[name][key]);
});
self.logger.info('\n');
return fuzzMap;
}
/*for (let i = 0; i < iterations; i++) {
fuzzMap[i] = {}; fuzzMap[i] = {};
contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0).forEach((abiMethod) => { contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0).forEach((abiMethod) => {
let name = abiMethod.type === "constructor" ? "constructor" : abiMethod.name; let name = abiMethod.type === "constructor" ? "constructor" : abiMethod.name;
@ -28,9 +44,9 @@ class ContractFuzzer {
}); });
self.logger.info('\n'); self.logger.info('\n');
for (let key in fuzzMap[i]) self.logger.info(key + ":" + fuzzMap[i][key]); for (let key in fuzzMap[i]) self.logger.info(key + ":" + fuzzMap[i][key]);
} });
return fuzzMap; return fuzzMap;
} }*/
getTypeFuzz(typeString) { getTypeFuzz(typeString) {
const self = this; const self = this;

View File

@ -1,3 +1,5 @@
const async = require('async');
const _ = require('underscore');
const ContractFuzzer = require('../fuzzer'); const ContractFuzzer = require('../fuzzer');
/*global web3*/ /*global web3*/
@ -10,42 +12,45 @@ class GasEstimator {
this.fuzzer = new ContractFuzzer(embark); this.fuzzer = new ContractFuzzer(embark);
} }
estimateGas(contractName) { estimateGas(contractName, cb) {
const self = this; const self = this;
let gasMap = {}; let gasMap = {};
self.events.request('contracts:contract', contractName, (contract) => { self.events.request('contracts:contract', contractName, (contract) => {
self.logger.info("Generating three rounds of fuzz for contract -- ", contractName); self.logger.info("Generating three rounds of fuzz for contract -- ", contractName);
let fuzzMap = self.fuzzer.generateFuzz(3, contract); let fuzzMap = self.fuzzer.generateFuzz(3, contract);
self.logger.info("-- Beginning gastimation for contract -- " + contractName); self.logger.info("-- Beginning gastimation for contract -- " + contractName);
let tempGasMap = {};
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress); let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
for (let i = 0; i < 3; i++) { async.each(contract.abiDefinition, function(abiMethod, gasCb) => {
tempGasMap[i] = {};
for (let name in fuzzMap[i]) {
tempGasMap[i][name] = contractObj.methods[name]
.apply(contractObj.methods[name], fuzzMap[i][name])
.estimateGas()
.then(estimatedGas => estimatedGas)
.catch(err => err);
}
}
contract.abiDefinition.forEach((abiMethod) => {
let name = abiMethod.name; let name = abiMethod.name;
console.log("estimaticating the gas for method name: ", name);
if (abiMethod.type === "constructor") { if (abiMethod.type === "constructor") {
gasMap[name] = contract.gasEstimates.creation.totalCost; // already provided for us
gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost);
} else if (abiMethod.inputs.length === 0) { } else if (abiMethod.inputs.length === 0) {
gasMap[name] = contractObj.methods[name].apply(contractObj.methods[name], []).estimateGas() // just run it and register it
.then(estimatedGas => estimatedGas) contractObj.methods[name]
.catch(err => err); .apply(contractObj.methods[name], [])
} else if (tempGasMap[0][name] !== tempGasMap[1][name] && tempGasMap[1][name] !== tempGasMap[2][name]) { .estimateGas(function(err, gasAmount) {
gasMap[name] = 'variable'; gasCb(err, gasAmount, name);
} else {
gasMap[name] = tempGasMap[name][0];
}
console.log("Estimate Gas ForEAch: ", gasMap[name]);
}); });
return gasMap; } else {
// 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;
}
}); });
} }
} }