2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
|
|
|
//let web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8549'));
|
2019-07-23 01:04:32 -03:00
|
|
|
//import { compile as _compile } from "solc";
|
2019-08-25 16:20:34 -04:00
|
|
|
import { solc } from "@ethersproject/cli";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { randomHexString, randomNumber } from "..";
|
|
|
|
import { BN, keccak256, toChecksumAddress } from "ethereumjs-util";
|
2019-05-14 18:48:48 -04:00
|
|
|
function hasPrefix(str, prefix) {
|
|
|
|
return (str.substring(0, prefix.length) === prefix);
|
|
|
|
}
|
|
|
|
function repeat(str, count) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = "";
|
|
|
|
for (let i = 0; i < count; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
result += str;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function indent(tabs) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = '';
|
2019-05-14 18:48:48 -04:00
|
|
|
while (result.length < 4 * tabs) {
|
|
|
|
result += " ";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function getStructName(base) {
|
2019-08-25 02:39:20 -04:00
|
|
|
return "Struct" + keccak256(base).slice(0, 4).toString("hex");
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
class Code {
|
|
|
|
constructor() {
|
2019-05-14 18:48:48 -04:00
|
|
|
this.depth = 0;
|
|
|
|
this.lines = [];
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
get code() {
|
|
|
|
return this.lines.join("\n"); //.replace(/ +\n/g, "\n").replace(/(\}\n\n+)/g, "}\n");
|
|
|
|
}
|
|
|
|
comment(line) {
|
2019-05-14 18:48:48 -04:00
|
|
|
this.add("");
|
|
|
|
this.add("/" + "/ " + line);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
add(line) {
|
|
|
|
let open = (line.trim().substring(line.trim().length - 1) === "{");
|
|
|
|
let close = line.trim()[0] === "}";
|
2019-05-14 18:48:48 -04:00
|
|
|
if (close) {
|
|
|
|
this.depth--;
|
|
|
|
}
|
|
|
|
this.lines.push(indent(this.depth) + line);
|
|
|
|
//if (close) { this.lines.push(""); }
|
|
|
|
if (open) {
|
|
|
|
this.depth++;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let chars = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
function addChars(start, length) {
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = start; i < start + length; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
chars.push(String.fromCharCode(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addChars(48, 10);
|
|
|
|
addChars(65, 26);
|
|
|
|
// Returns the functions required to generate code for a specific parameter type and value
|
|
|
|
function getGenCode(testData) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let type = testData.type;
|
|
|
|
let value = (testData.value != null) ? testData.value : "__crash__";
|
|
|
|
let isArray = type.match(/^(.*)(\[[0-9]*\])$/);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (isArray) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let base = isArray[1];
|
|
|
|
let suffix = isArray[2];
|
|
|
|
let isDynamic = (isArray[2] === "[]");
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
assign: (name, code) => {
|
|
|
|
if (isDynamic) {
|
2019-05-14 18:48:48 -04:00
|
|
|
//let child = getGenCode({ type: base });
|
|
|
|
//let decl = child.decl(name).split(" ");
|
2019-08-25 02:39:20 -04:00
|
|
|
let struct = base;
|
2019-05-14 18:48:48 -04:00
|
|
|
if (type.substring(0, 5) === "tuple") {
|
2019-08-25 02:39:20 -04:00
|
|
|
struct = getStructName(base);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
code.add(name + " = new " + struct + "[](" + String(value.length) + ");");
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
value.forEach((value, index) => {
|
|
|
|
console.log("SSS", base, value);
|
|
|
|
let child = getGenCode({ type: base, value: value });
|
2019-05-14 18:48:48 -04:00
|
|
|
child.assign(name + "[" + String(index) + "]", code);
|
|
|
|
});
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
decl: (name) => {
|
|
|
|
let child = getGenCode({ type: isArray[1] });
|
2019-05-14 18:48:48 -04:00
|
|
|
// Inject the array suffix to the type and add memory location
|
|
|
|
// - uint256 p0 => uint256[] memory p0
|
|
|
|
// - bytes memory p0 => bytes[] memory p0
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = child.decl(name).split(" ");
|
|
|
|
result[0] = result[0] + suffix;
|
2019-05-14 18:48:48 -04:00
|
|
|
if (result[1] !== "memory") {
|
|
|
|
result.splice(1, 0, "memory");
|
|
|
|
}
|
|
|
|
return result.join(" ");
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
structs: (code) => {
|
|
|
|
let child = getGenCode({ type: isArray[1] });
|
2019-05-14 18:48:48 -04:00
|
|
|
child.structs(code);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let isTuple = type.match(/^tuple\((.*)\)$/);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (isTuple) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let children = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
// Split up the child types
|
2019-08-25 02:39:20 -04:00
|
|
|
let accum = "";
|
|
|
|
let balance = 0;
|
|
|
|
let types = isTuple[1];
|
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
let c = types[i];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (c === "(") {
|
|
|
|
balance++;
|
|
|
|
accum += c;
|
|
|
|
}
|
|
|
|
else if (c === ")") {
|
|
|
|
balance--;
|
|
|
|
accum += c;
|
|
|
|
}
|
|
|
|
else if (c === ",") {
|
|
|
|
if (balance === 0) {
|
2019-08-25 02:39:20 -04:00
|
|
|
children.push(accum);
|
2019-05-14 18:48:48 -04:00
|
|
|
accum = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
accum += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
accum += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accum) {
|
2019-08-25 02:39:20 -04:00
|
|
|
children.push(accum);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
assign: (name, code) => {
|
|
|
|
children.forEach((child, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
console.log("TT", child, value[index]);
|
|
|
|
getGenCode({
|
|
|
|
type: child,
|
|
|
|
value: value[index]
|
|
|
|
}).assign(name + ".m" + String(index), code);
|
|
|
|
});
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
decl: (name) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return (getStructName(type) + " memory " + name);
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
structs: (code) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
// Include any dependency Structs first
|
2019-08-25 02:39:20 -04:00
|
|
|
children.forEach((child) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
getGenCode({ type: child }).structs(code);
|
|
|
|
});
|
|
|
|
// Add this struct
|
|
|
|
code.add("struct " + getStructName(type) + " {");
|
2019-08-25 02:39:20 -04:00
|
|
|
children.forEach((child, index) => {
|
|
|
|
let decl = getGenCode({
|
2019-05-14 18:48:48 -04:00
|
|
|
type: child
|
|
|
|
}).decl("m" + String(index)).replace(" memory ", " ");
|
|
|
|
code.add(decl + ";");
|
|
|
|
});
|
|
|
|
code.add("}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let isFixedBytes = type.match(/^bytes([0-9]+)$/);
|
|
|
|
let isNumber = type.match(/^(u?)int([0-9]*)$/);
|
|
|
|
let isFixedNumber = type.match(/^(u?)fixed(([0-9]+)x([0-9]+))?$/);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (type === "address" || type === "bool" || isNumber || isFixedNumber || isFixedBytes) {
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
assign: (name, code) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (type === "boolean") {
|
|
|
|
code.add(name + " = " + (value ? "true" : "false") + ";");
|
|
|
|
}
|
|
|
|
else if (isFixedBytes) {
|
|
|
|
code.add(name + " = hex\"" + value.substring(2) + "\";");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
code.add(name + " = " + value + ";");
|
|
|
|
}
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
decl: (name) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return (type + " " + name);
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
structs: (code) => { }
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (type === "string") {
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
assign: (name, code) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add(name + " = " + JSON.stringify(value) + ";");
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
decl: (name) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return ("string memory " + name);
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
structs: (code) => { }
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (type === "bytes") {
|
2019-08-25 02:39:20 -04:00
|
|
|
let valueBytes = Buffer.from(value.substring(2), "hex");
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
assign: (name, code) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add("{");
|
2019-08-25 02:39:20 -04:00
|
|
|
code.add("bytes memory temp = new bytes(" + valueBytes.length + ");");
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add(name + " = temp;");
|
|
|
|
code.add("assembly {");
|
|
|
|
// Store the length
|
2019-08-25 02:39:20 -04:00
|
|
|
code.add("mstore(temp, " + valueBytes.length + ")");
|
2019-05-14 18:48:48 -04:00
|
|
|
// Store each byte
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < valueBytes.length; i++) {
|
|
|
|
code.add("mstore8(add(temp, " + (32 + i) + "), " + valueBytes[i] + ")");
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
code.add("}");
|
|
|
|
code.add("}");
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
decl: (name) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
return ("bytes memory " + name);
|
|
|
|
},
|
2019-08-25 02:39:20 -04:00
|
|
|
structs: (code) => { }
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
throw new Error("Could not produce GenCode: " + type);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Generates a random type and value for the type
|
|
|
|
function generateTest(seed) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let basetype = randomNumber(seed + "-type", 0, 10);
|
2019-05-14 18:48:48 -04:00
|
|
|
switch (basetype) {
|
|
|
|
// Address
|
|
|
|
case 0:
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let value = toChecksumAddress(randomHexString(seed + "-value", 20));
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
|
|
|
type: "address",
|
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Boolean
|
|
|
|
case 1:
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let value = (randomNumber(seed + "-value", 0, 2) ? true : false);
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
|
|
|
type: "bool",
|
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Number
|
|
|
|
case 2: {
|
2019-08-25 02:39:20 -04:00
|
|
|
let signed = randomNumber(seed + "-signed", 0, 2);
|
|
|
|
let width = randomNumber(seed + "-width", 0, 33) * 8;
|
|
|
|
let type = (signed ? "" : "u") + "int";
|
2019-05-14 18:48:48 -04:00
|
|
|
// Allow base int and uint
|
2019-08-25 02:39:20 -04:00
|
|
|
if (width) {
|
|
|
|
type += String(width);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-08-25 02:39:20 -04:00
|
|
|
width = 256;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let hex = randomHexString(seed + "-value", width / 8).substring(2);
|
|
|
|
if (signed) {
|
2019-05-14 18:48:48 -04:00
|
|
|
// Sign bit set (we don't bother with 2's compliment
|
2019-08-25 02:39:20 -04:00
|
|
|
let msb = parseInt(hex[0], 16);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (msb >= 8) {
|
|
|
|
hex = "-" + String(msb & 0x7) + hex.substring(1);
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let value = (new BN(hex, 16)).toString();
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
type: type,
|
2019-05-14 18:48:48 -04:00
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Fixed
|
|
|
|
case 3: {
|
|
|
|
// Fixed Point values are not supported yet
|
|
|
|
return generateTest(seed + "-next");
|
2019-08-25 02:39:20 -04:00
|
|
|
let signed = randomNumber(seed + "-signed", 0, 2);
|
|
|
|
let width = randomNumber(seed + "-width", 0, 33) * 8;
|
|
|
|
let decimals = 0;
|
|
|
|
let maxDecimals = (new BN(repeat("7f", ((width === 0) ? 32 : (width / 8))), 16)).toString().length - 1;
|
|
|
|
let attempt = 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
while (true) {
|
2019-08-25 02:39:20 -04:00
|
|
|
decimals = randomNumber(seed + "-decimals" + String(attempt), 0, 80);
|
|
|
|
if (decimals < maxDecimals) {
|
2019-05-14 18:48:48 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
attempt++;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let type = (signed ? "" : "u") + "fixed";
|
2019-05-14 18:48:48 -04:00
|
|
|
// Allow base int and uint
|
2019-08-25 02:39:20 -04:00
|
|
|
if (width) {
|
|
|
|
type += String(width) + "x" + String(decimals);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-08-25 02:39:20 -04:00
|
|
|
width = 128;
|
|
|
|
decimals = 18;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let hex = randomHexString(seed + "-value", width / 8).substring(2);
|
2019-05-14 18:48:48 -04:00
|
|
|
// Use the top bit to indicate negative values
|
2019-08-25 02:39:20 -04:00
|
|
|
let negative = false;
|
|
|
|
if (signed) {
|
2019-05-14 18:48:48 -04:00
|
|
|
// Sign bit set (we don't bother with 2's compliment
|
2019-08-25 02:39:20 -04:00
|
|
|
let msb = parseInt(hex[0], 16);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (msb >= 8) {
|
|
|
|
hex = String(msb & 0x7) + hex.substring(1);
|
|
|
|
negative = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Zero-pad the value so we get at least 1 whole digit
|
2019-08-25 02:39:20 -04:00
|
|
|
let dec = (new BN(hex, 16)).toString();
|
|
|
|
while (dec.length < decimals + 1) {
|
2019-05-14 18:48:48 -04:00
|
|
|
dec = "0" + dec;
|
|
|
|
}
|
|
|
|
// Split the decimals with the decimal point
|
2019-08-25 02:39:20 -04:00
|
|
|
let split = dec.length - decimals;
|
|
|
|
let value = dec.substring(0, split) + "." + dec.substring(split);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (negative) {
|
|
|
|
value = "-" + value;
|
|
|
|
}
|
|
|
|
// Prevent ending in a decimal (e.g. "45."
|
|
|
|
if (value.substring(value.length - 1) === ".") {
|
|
|
|
value = value.substring(0, value.length - 1);
|
|
|
|
}
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
type: type,
|
2019-05-14 18:48:48 -04:00
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// BytesXX
|
|
|
|
case 4: {
|
2019-08-25 02:39:20 -04:00
|
|
|
let length = randomNumber(seed + "-length", 1, 33);
|
|
|
|
let type = "bytes" + String(length);
|
|
|
|
return (seed) => {
|
|
|
|
let value = randomHexString(seed + "-value", length);
|
2019-05-14 18:48:48 -04:00
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
type: type,
|
2019-05-14 18:48:48 -04:00
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// String
|
|
|
|
case 5:
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let length = randomNumber(seed + "-length", 0, 36);
|
|
|
|
let value = "";
|
2019-05-14 18:48:48 -04:00
|
|
|
while (value.length < length) {
|
2019-08-25 02:39:20 -04:00
|
|
|
value += chars[randomNumber(seed + "-value" + String(value.length), 0, chars.length)];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: "string",
|
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Bytes
|
|
|
|
case 6:
|
2019-08-25 02:39:20 -04:00
|
|
|
return (seed) => {
|
|
|
|
let length = randomNumber(seed + "-length", 0, 12); // @TODO: increase this
|
|
|
|
let value = randomHexString(seed + "-value", length);
|
2019-05-14 18:48:48 -04:00
|
|
|
//let valueBytes = Buffer.from(value.substring(2), "hex");
|
|
|
|
return {
|
|
|
|
type: "bytes",
|
|
|
|
value: value
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// Fixed-Length Array (e.g. address[4])
|
|
|
|
case 7:
|
|
|
|
// Falls-through
|
|
|
|
// Dynamic-Length Array (e.g. address[])
|
|
|
|
case 8: {
|
2019-08-25 02:39:20 -04:00
|
|
|
let dynamic = (basetype === 8);
|
|
|
|
let subType = generateTest(seed + "-subtype");
|
|
|
|
let length = randomNumber(seed + "-length", 1, 3);
|
|
|
|
let suffix = "[" + ((!dynamic) ? length : "") + "]";
|
|
|
|
let type = subType("-index0").type + suffix;
|
|
|
|
return (seed) => {
|
|
|
|
if (dynamic) {
|
|
|
|
length = randomNumber(seed + "-length", 0, 3);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let children = [];
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
children.push(subType(seed + "-index" + String(i)));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
type: type,
|
|
|
|
value: children.map((data) => data.value)
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Tuple
|
|
|
|
case 9: {
|
2019-08-25 02:39:20 -04:00
|
|
|
let count = randomNumber(seed + "-count", 1, 8);
|
|
|
|
let subTypes = [];
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
let cSeed = seed + "-subtype" + String(i);
|
|
|
|
subTypes.push(generateTest(cSeed));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let type = "tuple(" + subTypes.map(s => s("-index0").type).join(",") + ")";
|
|
|
|
let struct = "Struct" + randomHexString(seed + "-name", 4).substring(2);
|
|
|
|
return (seed) => {
|
|
|
|
let children = [];
|
|
|
|
subTypes.forEach((subType) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
children.push(subType(seed + "-value"));
|
|
|
|
});
|
|
|
|
return {
|
2019-08-25 02:39:20 -04:00
|
|
|
type: type,
|
|
|
|
struct: struct,
|
|
|
|
value: children.map(c => c.value),
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("bad things");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Returns true iff the types are able to be non-standard pack encoded
|
|
|
|
function checkPack(types) {
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < types.length; i++) {
|
|
|
|
let type = types[i];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (hasPrefix(type, "tuple")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hasPrefix(type, "bytes[")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hasPrefix(type, "string[")) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let firstDynamic = type.indexOf("[]");
|
2019-05-14 18:48:48 -04:00
|
|
|
if (firstDynamic >= 0 && firstDynamic != type.length - 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Generates a Solidity source files with the parameter types and values
|
|
|
|
function generateSolidity(params) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let plist = [];
|
|
|
|
for (let i = 0; i < params.length; i++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
plist.push("p" + String(i));
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let genCodes = params.map(p => getGenCode(p));
|
|
|
|
let code = new Code();
|
2019-05-14 18:48:48 -04:00
|
|
|
///////////////////
|
|
|
|
// Pragma
|
|
|
|
code.add("pragma experimental ABIEncoderV2;");
|
|
|
|
code.add("pragma solidity ^0.5.5;");
|
|
|
|
code.add("");
|
|
|
|
///////////////////
|
|
|
|
// Header
|
|
|
|
code.add("contract Test {");
|
|
|
|
///////////////////
|
|
|
|
// Structs
|
2019-08-25 02:39:20 -04:00
|
|
|
genCodes.forEach((genCode) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
genCode.structs(code);
|
|
|
|
});
|
|
|
|
///////////////////
|
|
|
|
// test function
|
2019-08-25 02:39:20 -04:00
|
|
|
code.add("function test() public pure returns (" + genCodes.map((g, i) => (g.decl("p" + String(i)))).join(", ") + ") {");
|
|
|
|
genCodes.forEach((genCode, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
genCode.assign("p" + index, code);
|
|
|
|
});
|
|
|
|
code.add("}");
|
|
|
|
///////////////////
|
|
|
|
// encode
|
|
|
|
code.add("function encode() public pure returns (bytes memory data){");
|
|
|
|
code.comment("Declare all parameters");
|
2019-08-25 02:39:20 -04:00
|
|
|
genCodes.forEach((genCode, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add(genCode.decl("p" + index) + ";");
|
|
|
|
});
|
|
|
|
code.comment("Assign all parameters");
|
2019-08-25 02:39:20 -04:00
|
|
|
genCodes.forEach((genCode, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
genCode.assign("p" + index, code);
|
|
|
|
});
|
|
|
|
code.add("");
|
2019-08-25 02:39:20 -04:00
|
|
|
code.add("return abi.encode(" + params.map((p, i) => ("p" + i)).join(", ") + ");");
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add("}");
|
|
|
|
///////////////////
|
|
|
|
// encodePacked
|
2019-08-25 02:39:20 -04:00
|
|
|
if (checkPack(params.map(p => p.type))) {
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add("function encodePacked() public pure returns (bytes memory data){");
|
|
|
|
code.comment("Declare all parameters");
|
2019-08-25 02:39:20 -04:00
|
|
|
genCodes.forEach((genCode, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add(genCode.decl("p" + index) + ";");
|
|
|
|
});
|
|
|
|
code.comment("Assign all parameters");
|
2019-08-25 02:39:20 -04:00
|
|
|
genCodes.forEach((genCode, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
genCode.assign("p" + index, code);
|
|
|
|
});
|
|
|
|
code.add("");
|
2019-08-25 02:39:20 -04:00
|
|
|
code.add("return abi.encodePacked(" + params.map((p, i) => ("p" + i)).join(", ") + ");");
|
2019-05-14 18:48:48 -04:00
|
|
|
code.add("}");
|
|
|
|
}
|
|
|
|
///////////////////
|
|
|
|
// Footer
|
|
|
|
code.add("}");
|
|
|
|
return code.code;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
let params = [];
|
|
|
|
console.log(i, randomNumber(String(i) + "-length", 1, 6));
|
|
|
|
let length = randomNumber(String(i) + "-length", 1, 6);
|
|
|
|
for (let j = 0; j < length; j++) {
|
2019-05-14 18:48:48 -04:00
|
|
|
params.push(generateTest(String(i) + String(j) + "-type")(String(i) + String(j) + "-test"));
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let solidity = generateSolidity(params);
|
2019-05-14 18:48:48 -04:00
|
|
|
console.log(solidity);
|
|
|
|
console.log(i);
|
2019-08-25 16:20:34 -04:00
|
|
|
let bytecode = solc.compile(solidity)[0].bytecode;
|
2019-05-14 18:48:48 -04:00
|
|
|
//console.log(params.map(p => p.type).join(", "));
|
|
|
|
//console.log(bytecode);
|
2019-08-25 02:39:20 -04:00
|
|
|
let testcase = {
|
2019-05-14 18:48:48 -04:00
|
|
|
//solidity: solidity,
|
|
|
|
bytecode: bytecode,
|
2019-08-25 02:39:20 -04:00
|
|
|
types: params.map(p => p.type),
|
|
|
|
value: params.map(p => p.value),
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
console.log(testcase);
|
|
|
|
}
|