From 99e88f5dddf460b7c69ff4992609ac11b11f86c5 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Tue, 21 Oct 2014 14:20:16 +0200 Subject: [PATCH] Random test optimizations --- libevmface/Instruction.cpp | 5 +++ libevmface/Instruction.h | 3 ++ test/createRandomTest.cpp | 67 ++++++++++++++++++++++++++++++-------- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/libevmface/Instruction.cpp b/libevmface/Instruction.cpp index 7bf845b06..5538805a2 100644 --- a/libevmface/Instruction.cpp +++ b/libevmface/Instruction.cpp @@ -318,3 +318,8 @@ InstructionInfo dev::eth::instructionInfo(Instruction _inst) return InstructionInfo({"", 0, 0, 0}); } } + +bool dev::eth::isValidInstruction(Instruction _inst) +{ + return c_instructionInfo.count(_inst); +} diff --git a/libevmface/Instruction.h b/libevmface/Instruction.h index 753bd0ad6..b6aa477b1 100644 --- a/libevmface/Instruction.h +++ b/libevmface/Instruction.h @@ -180,6 +180,9 @@ struct InstructionInfo /// Information on all the instructions. InstructionInfo instructionInfo(Instruction _inst); +/// check whether instructions exists +bool isValidInstruction(Instruction _inst); + /// Convert from string mnemonic to Instruction type. extern const std::map c_instructions; diff --git a/test/createRandomTest.cpp b/test/createRandomTest.cpp index 874869a5c..2508d89f2 100644 --- a/test/createRandomTest.cpp +++ b/test/createRandomTest.cpp @@ -42,11 +42,11 @@ void doMyTests(json_spirit::mValue& v); int main(int argc, char *argv[]) { - if (argc != 2) - { - cout << "usage: createRandomTest \n"; - return 0; - } +// if (argc != 2) +// { +// cout << "usage: createRandomTest \n"; +// return 0; +// } // create random code @@ -64,14 +64,48 @@ int main(int argc, char *argv[]) string randomCode; for (int i = 0; i < lengthOfCode; ++i) - randomCode += toHex(toCompactBigEndian(randGen())); + { + uint8_t opcode = randGen(); - // read template test file + // disregard all invalid commands, except of one (0x10) + if (dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || opcode == 0x10) + randomCode += toHex(toCompactBigEndian(opcode)); + else + i--; + } + + const string s =\ +"{\n\ + \"randomVMtest\": {\n\ + \"env\" : {\n\ + \"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\ + \"currentNumber\" : \"0\",\n\ + \"currentGasLimit\" : \"1000000\",\n\ + \"currentDifficulty\" : \"256\",\n\ + \"currentTimestamp\" : 1,\n\ + \"currentCoinbase\" : \"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba\"\n\ + },\n\ + \"pre\" : {\n\ + \"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6\" : {\n\ + \"balance\" : \"1000000000000000000\",\n\ + \"nonce\" : 0,\n\ + \"code\" : \"random\",\n\ + \"storage\": {}\n\ + }\n\ + },\n\ + \"exec\" : {\n\ + \"address\" : \"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6\",\n\ + \"origin\" : \"cd1722f3947def4cf144679da39c4c32bdc35681\",\n\ + \"caller\" : \"cd1722f3947def4cf144679da39c4c32bdc35681\",\n\ + \"value\" : \"1000000000000000000\",\n\ + \"data\" : \"\",\n\ + \"gasPrice\" : \"100000000000000\",\n\ + \"gas\" : \"10000\"\n\ + }\n\ + }\n\ +}"; mValue v; - boost::filesystem::path p(__FILE__); - boost::filesystem::path dir = p.parent_path(); - string s = asString(contents(dir.string() + "/randomTestFiller.json")); read_string(s, v); // insert new random code @@ -80,9 +114,16 @@ int main(int argc, char *argv[]) // execute code in vm doMyTests(v); - // write new test - string filename = argv[1]; - writeFile(filename, asBytes(json_spirit::write_string(v, true))); +// // write new test +// string filename = argv[1]; +// writeFile(filename, asBytes(json_spirit::write_string(v, true))); + + // write resultsing test to envirnoment variable + string str = "ETHEREUM_RANDOM_TEST=" + json_spirit::write_string(v, true); + char *cstr = new char[str.length() + 1]; + strcpy(cstr, str.c_str()); + putenv(cstr); + delete [] cstr; return 0; }