first attempt at gas estimation using a 3 round fuzzer and linking it into the profiler

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
VoR0220 2018-06-06 14:37:08 -05:00
parent 787162575e
commit fedbe0d2bc
3 changed files with 46 additions and 5 deletions

View File

@ -21,10 +21,11 @@ class ContractFuzzer {
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;
let inputTypes = abiMethod.inputs.map(input => input.type); let inputTypes = abiMethod.inputs.map(input => input.type);
fuzzMap[name] = inputTypes.map(input => this.getTypeFuzz(input)); fuzzMap[name][i] = inputTypes.map(input => this.getTypeFuzz(input));
}); });
self.logger.info('\n');
for (let key in fuzzMap) self.logger.info(key + ":" + fuzzMap[key][i]);
} }
for (let key in fuzzMap) self.logger.info(key + ":" + fuzzMap[key]);
return fuzzMap; return fuzzMap;
} }

View File

@ -0,0 +1,37 @@
const ContractFuzzer = require('../fuzzer');
class GasEstimator {
constructor(embark) {
this.embark = embark;
this.logger = embark.logger;
this.events = embark.events;
this.fuzzer = new ContractFuzzer(embark);
}
estimateGas(contractName) {
let gasMap = {};
this.logger.info("Generating three rounds of fuzz for contract -- ", contractName);
let fuzzMap = this.fuzzer.generateFuzz(3, contractName);
self.events.request('contracts:contract', contractName, (contract) => {
self.logger.info("-- Beginning gastimation for contract -- " + contractName);
tempGasMap = {};
fuzzMap.forEach((name) => {
for (let i in fuzzMap[name]) {
tempGasMap[name][i] = contract.methods[name].apply(contract.methods[name], fuzzMap[name][i]).estimateGas();
}
});
tempGasMap.forEach((name) => {
if (name === "constructor") {
gasMap[name] = contract.gasEstimates.creation.totalCost;
} else if (tempGasMap[name][0] !== tempGasMap[name][1] && tempGasMap[name][1] !== tempGasMap[name][2]) {
gasMap[name] = 'variable';
} else {
gasMap[name] = tempGasMap[name][0];
}
});
return gasMap;
});
}
}
module.exports = GasEstimator;

View File

@ -1,10 +1,12 @@
const asciiTable = require('ascii-table'); const asciiTable = require('ascii-table');
const GasEstimator = require('../gasEstimator');
class Profiler { class Profiler {
constructor(embark) { constructor(embark) {
this.embark = embark; this.embark = embark;
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.gasEstimator = new GasEstimator(embark);
this.registerConsoleCommand(); this.registerConsoleCommand();
} }
@ -12,14 +14,15 @@ class Profiler {
profile(contractName, contract) { profile(contractName, contract) {
const self = this; const self = this;
let table = new asciiTable(contractName); let table = new asciiTable(contractName);
table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs'); table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates');
let gastimates = self.gasEstimator.estimateGas(contractName);
contract.abiDefinition.forEach((abiMethod) => { contract.abiDefinition.forEach((abiMethod) => {
switch(abiMethod.type) { switch(abiMethod.type) {
case "constructor": case "constructor":
table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs)); table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']);
break; break;
default: default:
table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs)); table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates[abiMethod.name]);
} }
}); });
self.logger.info(table.toString()); self.logger.info(table.toString());