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

View File

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