fix the callback schema for async and pretty it up a bit

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
VoR0220 2018-06-08 14:26:32 -05:00
parent 90f342b65a
commit 0ff39f48b3
2 changed files with 41 additions and 30 deletions

View File

@ -20,37 +20,46 @@ class GasEstimator {
let fuzzMap = self.fuzzer.generateFuzz(3, contract);
self.logger.info("-- Beginning gastimation for contract -- " + contractName);
let contractObj = new web3.eth.Contract(contract.abiDefinition, contract.deployedAddress);
async.each(contract.abiDefinition, function(abiMethod, gasCb) => {
async.each(contract.abiDefinition,
(abiMethod, gasCb) => {
let name = abiMethod.name;
if (abiMethod.type === "constructor") {
// already provided for us
gasCb(null, 'constructor', contract.gasEstimates.creation.totalCost);
} else if (abiMethod.inputs.length === 0) {
} else if (abiMethod.inputs === []) {
// just run it and register it
contractObj.methods[name]
.apply(contractObj.methods[name], [])
.estimateGas(function(err, gasAmount) {
.estimateGas((err, gasAmount) => {
if (err) gasCb(err);
gasMap[name] = gasAmount;
gasCb(err, gasAmount, name);
});
} 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);
});
});
} 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) => {
getVarianceCb(err, [gasAmount]);
});
}, (err, variance) => {
if (err) {
gasCb(err)
} else if (variance.reduce(_.isEqual, variance[2])) {
gasMap[name] = variance[0];
} else {
gasMap[name] = 'variable';
}
gasCb();
});
};
},
function(err, gasAmount, name) => {
(err) => {
if (err) return cb(err);
gasMap[name] = gasAmount;
cb(null, gasMap);
}
);
});
}
}

View File

@ -15,18 +15,20 @@ class Profiler {
const self = this;
let table = new asciiTable(contractName);
table.setHeading('Function', 'Payable', 'Mutability', 'Inputs', 'Outputs', 'Gas Estimates');
let gastimates = self.gasEstimator.estimateGas(contractName);
contract.abiDefinition.forEach((abiMethod) => {
console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]);
switch(abiMethod.type) {
case "constructor":
table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']);
break;
default:
table.addRow(abiMethod.name, abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates[abiMethod.name]);
}
self.gasEstimator.estimateGas(contractName, function(err, gastimates) {
if (err) throw new Error(err);
contract.abiDefinition.forEach((abiMethod) => {
console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]);
switch(abiMethod.type) {
case "constructor":
table.addRow("constructor", abiMethod.payable, abiMethod.stateMutability, this.formatParams(abiMethod.inputs), this.formatParams(abiMethod.outputs), gastimates['constructor']);
break;
default:
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());
}
formatParams(params) {