mirror of https://github.com/embarklabs/embark.git
redo logic so that it uses the async library
Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
parent
5c7e9f0054
commit
90f342b65a
|
@ -17,7 +17,23 @@ class ContractFuzzer {
|
||||||
generateFuzz(iterations, contract) {
|
generateFuzz(iterations, contract) {
|
||||||
const self = this;
|
const self = this;
|
||||||
let fuzzMap = {};
|
let fuzzMap = {};
|
||||||
for (let i = 0; i < iterations; i++) {
|
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++) {
|
||||||
|
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;
|
||||||
|
|
|
@ -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] = {};
|
let name = abiMethod.name;
|
||||||
for (let name in fuzzMap[i]) {
|
if (abiMethod.type === "constructor") {
|
||||||
tempGasMap[i][name] = contractObj.methods[name]
|
// already provided for us
|
||||||
.apply(contractObj.methods[name], fuzzMap[i][name])
|
gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost);
|
||||||
.estimateGas()
|
} else if (abiMethod.inputs.length === 0) {
|
||||||
.then(estimatedGas => estimatedGas)
|
// just run it and register it
|
||||||
.catch(err => err);
|
contractObj.methods[name]
|
||||||
}
|
.apply(contractObj.methods[name], [])
|
||||||
}
|
.estimateGas(function(err, gasAmount) {
|
||||||
contract.abiDefinition.forEach((abiMethod) => {
|
gasCb(err, gasAmount, name);
|
||||||
let name = abiMethod.name;
|
});
|
||||||
console.log("estimaticating the gas for method name: ", name);
|
|
||||||
if (abiMethod.type === "constructor") {
|
|
||||||
gasMap[name] = contract.gasEstimates.creation.totalCost;
|
|
||||||
} else if (abiMethod.inputs.length === 0) {
|
|
||||||
gasMap[name] = contractObj.methods[name].apply(contractObj.methods[name], []).estimateGas()
|
|
||||||
.then(estimatedGas => estimatedGas)
|
|
||||||
.catch(err => err);
|
|
||||||
} else if (tempGasMap[0][name] !== tempGasMap[1][name] && tempGasMap[1][name] !== tempGasMap[2][name]) {
|
|
||||||
gasMap[name] = 'variable';
|
|
||||||
} else {
|
} else {
|
||||||
gasMap[name] = tempGasMap[name][0];
|
// 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;
|
||||||
}
|
}
|
||||||
console.log("Estimate Gas ForEAch: ", gasMap[name]);
|
|
||||||
});
|
|
||||||
return gasMap;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue