2014-10-14 10:41:01 +00:00
|
|
|
/*
|
2014-12-05 13:39:24 +00:00
|
|
|
This file is part of cpp-ethereum.
|
2014-10-14 10:41:01 +00:00
|
|
|
|
2014-12-05 13:39:24 +00:00
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2014-10-14 10:41:01 +00:00
|
|
|
|
2014-12-05 13:39:24 +00:00
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-10-14 10:41:01 +00:00
|
|
|
|
2014-12-05 13:39:24 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-14 10:41:01 +00:00
|
|
|
*/
|
|
|
|
/** @file createRandomTest.cpp
|
|
|
|
* @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com>
|
|
|
|
* @date 2014
|
|
|
|
* Creating a random virtual machine test.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2014-10-18 18:17:19 +00:00
|
|
|
#include <chrono>
|
2014-10-14 10:41:01 +00:00
|
|
|
#include <boost/random.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#include <json_spirit/json_spirit.h>
|
|
|
|
#include <json_spirit/json_spirit_reader_template.h>
|
|
|
|
#include <json_spirit/json_spirit_writer_template.h>
|
|
|
|
#include <libdevcore/CommonIO.h>
|
|
|
|
#include <libdevcore/CommonData.h>
|
2014-11-06 13:50:18 +00:00
|
|
|
#include <libevmcore/Instruction.h>
|
2014-12-10 16:41:53 +00:00
|
|
|
#include <libevm/VMFactory.h>
|
2014-10-14 10:41:01 +00:00
|
|
|
#include "vm.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace json_spirit;
|
|
|
|
using namespace dev;
|
|
|
|
|
|
|
|
void doMyTests(json_spirit::mValue& v);
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-10-21 15:02:49 +00:00
|
|
|
g_logVerbosity = 0;
|
2014-10-14 10:41:01 +00:00
|
|
|
|
|
|
|
// create random code
|
|
|
|
|
|
|
|
boost::random::mt19937 gen;
|
|
|
|
|
2014-10-18 18:17:19 +00:00
|
|
|
auto now = chrono::steady_clock::now().time_since_epoch();
|
|
|
|
auto timeSinceEpoch = chrono::duration_cast<chrono::nanoseconds>(now).count();
|
2014-10-14 10:41:01 +00:00
|
|
|
gen.seed(static_cast<unsigned int>(timeSinceEpoch));
|
|
|
|
boost::random::uniform_int_distribution<> lengthOfCodeDist(2, 16);
|
|
|
|
boost::random::uniform_int_distribution<> opcodeDist(0, 255);
|
2015-01-13 08:53:46 +00:00
|
|
|
boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x40, 0x45);
|
2014-10-14 10:41:01 +00:00
|
|
|
boost::random::variate_generator<boost::mt19937&,
|
|
|
|
boost::random::uniform_int_distribution<> > randGen(gen, opcodeDist);
|
2015-01-12 11:46:40 +00:00
|
|
|
boost::random::variate_generator<boost::mt19937&,
|
|
|
|
boost::random::uniform_int_distribution<> > randGenBlockInfoOpcode(gen, BlockInfoOpcodeDist);
|
2014-10-14 10:41:01 +00:00
|
|
|
|
|
|
|
int lengthOfCode = lengthOfCodeDist(gen);
|
|
|
|
string randomCode;
|
|
|
|
|
|
|
|
for (int i = 0; i < lengthOfCode; ++i)
|
2014-10-21 12:20:16 +00:00
|
|
|
{
|
2015-01-12 11:46:40 +00:00
|
|
|
if (i < 8 && (randGen() < 192))
|
|
|
|
{
|
|
|
|
randomCode += toHex(toCompactBigEndian((uint8_t)randGenBlockInfoOpcode()));
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-14 10:41:01 +00:00
|
|
|
|
2015-01-12 11:46:40 +00:00
|
|
|
uint8_t opcode = randGen();
|
|
|
|
// disregard all invalid commands, except of one (0x0c)
|
2015-01-14 08:39:21 +00:00
|
|
|
if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 250)))
|
2014-10-21 12:20:16 +00:00
|
|
|
randomCode += toHex(toCompactBigEndian(opcode));
|
|
|
|
else
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string s =\
|
|
|
|
"{\n\
|
|
|
|
\"randomVMtest\": {\n\
|
|
|
|
\"env\" : {\n\
|
|
|
|
\"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\
|
2015-01-13 16:58:14 +00:00
|
|
|
\"currentNumber\" : \"300\",\n\
|
2014-10-21 12:20:16 +00:00
|
|
|
\"currentGasLimit\" : \"1000000\",\n\
|
2015-01-12 14:42:28 +00:00
|
|
|
\"currentDifficulty\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\
|
2015-01-13 13:38:36 +00:00
|
|
|
\"currentTimestamp\" : 2,\n\
|
2014-10-21 12:20:16 +00:00
|
|
|
\"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\
|
|
|
|
}";
|
2014-10-14 10:41:01 +00:00
|
|
|
|
|
|
|
mValue v;
|
|
|
|
read_string(s, v);
|
|
|
|
|
|
|
|
// insert new random code
|
2015-01-13 16:58:14 +00:00
|
|
|
v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + (randGen() > 128 ? "55" : "");
|
2014-10-14 10:41:01 +00:00
|
|
|
|
|
|
|
// execute code in vm
|
|
|
|
doMyTests(v);
|
|
|
|
|
2014-10-21 15:02:49 +00:00
|
|
|
// stream to output for further handling by the bash script
|
|
|
|
cout << json_spirit::write_string(v, true);
|
2014-10-14 10:41:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void doMyTests(json_spirit::mValue& v)
|
|
|
|
{
|
2015-01-13 13:22:12 +00:00
|
|
|
eth::VMFactory::setKind(eth::VMKind::Interpreter);
|
|
|
|
|
2014-10-14 10:41:01 +00:00
|
|
|
for (auto& i: v.get_obj())
|
|
|
|
{
|
2014-12-01 21:26:13 +00:00
|
|
|
cnote << i.first;
|
2014-10-14 10:41:01 +00:00
|
|
|
mObject& o = i.second.get_obj();
|
|
|
|
|
|
|
|
assert(o.count("env") > 0);
|
|
|
|
assert(o.count("pre") > 0);
|
|
|
|
assert(o.count("exec") > 0);
|
|
|
|
|
2014-12-01 21:26:13 +00:00
|
|
|
dev::test::FakeExtVM fev;
|
2014-10-14 10:41:01 +00:00
|
|
|
fev.importEnv(o["env"].get_obj());
|
|
|
|
fev.importState(o["pre"].get_obj());
|
|
|
|
|
|
|
|
o["pre"] = mValue(fev.exportState());
|
|
|
|
|
|
|
|
fev.importExec(o["exec"].get_obj());
|
2014-11-07 14:16:42 +00:00
|
|
|
if (fev.code.empty())
|
2014-10-14 10:41:01 +00:00
|
|
|
{
|
|
|
|
fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress));
|
2014-11-03 22:49:05 +00:00
|
|
|
fev.code = fev.thisTxCode;
|
2014-10-14 10:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes output;
|
2014-12-10 16:41:53 +00:00
|
|
|
auto vm = eth::VMFactory::create(fev.gas);
|
2014-12-01 21:26:13 +00:00
|
|
|
|
2014-11-10 21:26:07 +00:00
|
|
|
u256 gas;
|
2014-12-01 21:26:13 +00:00
|
|
|
bool vmExceptionOccured = false;
|
2014-10-14 10:41:01 +00:00
|
|
|
try
|
|
|
|
{
|
2014-12-10 16:41:53 +00:00
|
|
|
output = vm->go(fev, fev.simpleTrace()).toBytes();
|
|
|
|
gas = vm->gas();
|
2014-10-14 10:41:01 +00:00
|
|
|
}
|
2014-11-10 21:26:07 +00:00
|
|
|
catch (eth::VMException const& _e)
|
|
|
|
{
|
|
|
|
cnote << "VM did throw an exception: " << diagnostic_information(_e);
|
2014-12-01 21:26:13 +00:00
|
|
|
vmExceptionOccured = true;
|
2014-11-10 21:26:07 +00:00
|
|
|
}
|
2014-10-14 10:41:01 +00:00
|
|
|
catch (Exception const& _e)
|
|
|
|
{
|
|
|
|
cnote << "VM did throw an exception: " << diagnostic_information(_e);
|
|
|
|
}
|
|
|
|
catch (std::exception const& _e)
|
|
|
|
{
|
|
|
|
cnote << "VM did throw an exception: " << _e.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete null entries in storage for the sake of comparison
|
|
|
|
|
|
|
|
for (auto &a: fev.addresses)
|
|
|
|
{
|
|
|
|
vector<u256> keystoDelete;
|
|
|
|
for (auto &s: get<2>(a.second))
|
|
|
|
{
|
|
|
|
if (s.second == 0)
|
|
|
|
keystoDelete.push_back(s.first);
|
|
|
|
}
|
|
|
|
for (auto const key: keystoDelete )
|
|
|
|
{
|
|
|
|
get<2>(a.second).erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
o["env"] = mValue(fev.exportEnv());
|
|
|
|
o["exec"] = mValue(fev.exportExec());
|
2014-12-01 21:26:13 +00:00
|
|
|
if (!vmExceptionOccured)
|
|
|
|
{
|
|
|
|
o["post"] = mValue(fev.exportState());
|
|
|
|
o["callcreates"] = fev.exportCallCreates();
|
|
|
|
o["out"] = "0x" + toHex(output);
|
|
|
|
fev.push(o, "gas", gas);
|
2014-12-05 18:26:32 +00:00
|
|
|
o["logs"] = test::exportLog(fev.sub.logs);
|
2014-12-01 21:26:13 +00:00
|
|
|
}
|
2014-10-14 10:41:01 +00:00
|
|
|
}
|
|
|
|
}
|