some changes to the way randomness is calculated and getting rid of unnecessary garbage and added logging

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
This commit is contained in:
VoR0220 2018-06-05 13:39:10 -05:00
parent f8b79904cb
commit ffe26d052a
3 changed files with 34 additions and 41 deletions

View File

@ -15,23 +15,27 @@ class ContractFuzzer {
}
generateFuzz(iterations, contract) {
let fuzz = [];
let fuzzMap = {};
for (let i = 0; i < iterations; i++)
contract.abiDefinition.forEach((abiMethod) => {
contract.abiDefinition.filter((x) => x.inputs && x.inputs.length != 0)
.forEach((abiMethod) => {
let name = abiMethod.type === "constructor" ? "constructor" : abiMethod.name;
console.log("name");
let inputTypes = abiMethod.inputs.map(input => input.type);
console.log("INPUT TYPES:", inputTypes);
let fuzzedInputs = _.map(inputTypes, this.getTypeFuzz.bind(this));
console.log("FUZZED INPUTS:", fuzzedInputs);
//fuzz.push(ethAbi.encodeFunctionCall(abiMethod, fuzzedInputs));
console.log("FUZZ SO FAR:", fuzz);
});
console.log("FUZZ:", fuzz);
return fuzz;
fuzzMap[name] = fuzzedInputs;
console.log("FUZZ KEYS SO FAR:", Object.keys(fuzzMap));
console.log("FUZZ VALS SO FAR:", Object.values(fuzzMap));
});
console.log("FUZZ:", Object.keys(fuzzMap), Object.values(fuzzMap));
return fuzzMap;
}
getTypeFuzz(typeString) {
const self = this;
console.log("TYPE:", typeString);
console.log("TYPESTRING:", typeString);
// Group 0: uint256[3]
// Group 1: uint256
// Group 2: uint
@ -39,26 +43,36 @@ class ContractFuzzer {
// Group 4: [3]
// Group 5: 3
let regexObj = typeString.match(/((bool|int|uint|bytes|string|address)([0-9]*)?)(\[([0-9]*)\])*$/);
console.log("ARRAY OBJ:", regexObj);
console.log("REGEX OBJ:", regexObj);
let type = regexObj[1];
console.log("type:", type);
let kind = regexObj[2];
console.log("kind:", kind);
let size = regexObj[3];
/*switch(true) {
case (regexObj[2] !== undefined):
let length = regexObj[5] === undefined ? self.generateRandomInt(256) : regexObj[5];
console.log("size:", size);
let array = regexObj[4];
console.log("array:", array);
let arraySize = regexObj[5];
console.log("array size:", arraySize);
switch(true) {
case (array !== undefined):
// if it's a dynamic array pick a number between 1 and 256 for length of array
let length = arraySize === undefined || arraySize === null || arraySize === '' ? Math.floor((Math.random() * 256) + 1) : arraySize;
console.log("LENGTH: ", length);
return self.generateArrayOfType(length, type)
case (/bool/).test(type):
case kind == "bool":
return self.generateRandomBool();
case (/(int|uint)([0-9]*)?/).test(type):
case kind == "uint" || kind == "int":
return self.generateRandomInt(size);
case (/^bytes([0-9]{1,})/).test(type):
case kind === "bytes" && size !== undefined:
return self.generateRandomStaticBytes(size);
case (/(string|bytes)/).test(type):
case kind === "string" || kind === "bytes":
return self.generateRandomDynamicType()
case (/address/).test(type):
case kind === "address":
return self.generateRandomAddress();
default:
throw new Error("Couldn't find proper ethereum abi type");
}*/
}
}
generateRandomBool() {
@ -68,8 +82,9 @@ class ContractFuzzer {
generateArrayOfType(length, type) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(this.getTypeFuzz(type));
arr.push(this.getTypeFuzz(type));
}
console.log("Final Array:", arr);
return arr;
}

View File

@ -1,11 +0,0 @@
const ethAbi = require('web3-eth-abi');
const contract = require('web3-eth-contract');
class GasEstimator {
constructor(provider, abi, iterations) {
this.abi = abi;
this.iters = iterations;
}
}

View File

@ -37,17 +37,6 @@ class Profiler {
return paramString;
}
/*generateGasEstimation(method) {
}
generateRandomType(type) {
switch(type) {
case "uint256":
case "uint"
}
}*/
registerConsoleCommand() {
const self = this;
self.embark.registerConsoleCommand((cmd, _options) => {