From ffe26d052a2e3b270fe02c160830501ea7e78cdc Mon Sep 17 00:00:00 2001 From: VoR0220 Date: Tue, 5 Jun 2018 13:39:10 -0500 Subject: [PATCH] some changes to the way randomness is calculated and getting rid of unnecessary garbage and added logging Signed-off-by: VoR0220 --- lib/modules/fuzzer/index.js | 53 ++++++++++++++++++---------- lib/modules/profiler/gasEstimator.js | 11 ------ lib/modules/profiler/index.js | 11 ------ 3 files changed, 34 insertions(+), 41 deletions(-) delete mode 100644 lib/modules/profiler/gasEstimator.js diff --git a/lib/modules/fuzzer/index.js b/lib/modules/fuzzer/index.js index 69ef8fe4d..a0e3a88ec 100644 --- a/lib/modules/fuzzer/index.js +++ b/lib/modules/fuzzer/index.js @@ -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; } diff --git a/lib/modules/profiler/gasEstimator.js b/lib/modules/profiler/gasEstimator.js deleted file mode 100644 index 287227b84..000000000 --- a/lib/modules/profiler/gasEstimator.js +++ /dev/null @@ -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; - } - - -} \ No newline at end of file diff --git a/lib/modules/profiler/index.js b/lib/modules/profiler/index.js index d5f40fa7e..6f5ce2aa2 100644 --- a/lib/modules/profiler/index.js +++ b/lib/modules/profiler/index.js @@ -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) => {