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]);
}); });
}, function(err, variance) { }, (err, variance) => {
if (err) gasCb(err) if (err) {
else if (variance.reduce(_.isEqual) gasCb(null, variance[0], name); gasCb(err)
gasCb(null, 'variable', name); } else if (variance.reduce(_.isEqual, variance[2])) {
}); gasMap[name] = variance[0];
}); } else {
}, gasMap[name] = 'variable';
function(err, gasAmount, name) => {
if (err) return cb(err);
gasMap[name] = gasAmount;
} }
gasCb();
});
};
},
(err) => {
if (err) return cb(err);
cb(null, gasMap);
}
);
}); });
} }
} }

View File

@ -15,7 +15,8 @@ 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) {
if (err) throw new Error(err);
contract.abiDefinition.forEach((abiMethod) => { contract.abiDefinition.forEach((abiMethod) => {
console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]); console.log("Abi Method Gastimate: ", gastimates[abiMethod.name]);
switch(abiMethod.type) { switch(abiMethod.type) {
@ -27,6 +28,7 @@ class Profiler {
} }
}); });
self.logger.info(table.toString()); self.logger.info(table.toString());
});
} }
formatParams(params) { formatParams(params) {