Gav Wood
9 years ago
15 changed files with 223 additions and 1240 deletions
@ -1,228 +0,0 @@ |
|||||
/*
|
|
||||
This file is part of cpp-ethereum. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
You should have received a copy of the GNU General Public License |
|
||||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
||||
*/ |
|
||||
/** @file checkRandomStateTest.cpp
|
|
||||
* @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com> |
|
||||
* @date 2015 |
|
||||
* Check a random test and return 0/1 for success or failure. To be used for efficiency in the random test simulation. |
|
||||
*/ |
|
||||
|
|
||||
#include <libdevcore/Common.h> |
|
||||
#include <libdevcore/Assertions.h> |
|
||||
#include <libdevcore/Exceptions.h> |
|
||||
#include <libdevcore/Log.h> |
|
||||
#include <libevm/VMFactory.h> |
|
||||
#include <test/libevm/vm.h> |
|
||||
#include <test/TestHelper.h> |
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter" |
|
||||
|
|
||||
using namespace std; |
|
||||
using namespace json_spirit; |
|
||||
using namespace dev::test; |
|
||||
using namespace dev; |
|
||||
|
|
||||
bool doStateTest(mValue& _v); |
|
||||
|
|
||||
int main(int argc, char *argv[]) |
|
||||
{ |
|
||||
g_logVerbosity = 0; |
|
||||
bool ret = false; |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
mValue v; |
|
||||
string s; |
|
||||
for (int i = 1; i < argc; ++i) |
|
||||
s += argv[i]; |
|
||||
if (asserts(s.length() > 0)) |
|
||||
{ |
|
||||
cout << "Content of argument is empty\n"; |
|
||||
return 1; |
|
||||
} |
|
||||
read_string(s, v); |
|
||||
ret = doStateTest(v); |
|
||||
} |
|
||||
catch (Exception const& _e) |
|
||||
{ |
|
||||
cout << "Failed test with Exception: " << diagnostic_information(_e) << endl; |
|
||||
ret = false; |
|
||||
} |
|
||||
catch (std::exception const& _e) |
|
||||
{ |
|
||||
cout << "Failed test with Exception: " << _e.what() << endl; |
|
||||
ret = false; |
|
||||
} |
|
||||
return ret; |
|
||||
} |
|
||||
|
|
||||
bool doStateTest(mValue& _v) |
|
||||
{ |
|
||||
eth::VMFactory::setKind(eth::VMKind::JIT); |
|
||||
|
|
||||
for (auto& i: _v.get_obj()) |
|
||||
{ |
|
||||
mObject& o = i.second.get_obj(); |
|
||||
|
|
||||
assert(o.count("env") > 0); |
|
||||
assert(o.count("pre") > 0); |
|
||||
assert(o.count("transaction") > 0); |
|
||||
|
|
||||
ImportTest importer(o, false); |
|
||||
|
|
||||
eth::State theState = importer.m_statePre; |
|
||||
bytes output; |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
output = theState.execute(lastHashes(importer.m_environment.currentBlock.number()), importer.m_transaction).output; |
|
||||
} |
|
||||
catch (Exception const& _e) |
|
||||
{ |
|
||||
cnote << "state execution did throw an exception: " << diagnostic_information(_e); |
|
||||
theState.commit(); |
|
||||
} |
|
||||
catch (std::exception const& _e) |
|
||||
{ |
|
||||
cnote << "state execution did throw an exception: " << _e.what(); |
|
||||
} |
|
||||
|
|
||||
assert(o.count("post") > 0); |
|
||||
assert(o.count("out") > 0); |
|
||||
|
|
||||
//checkOutput(output, o);
|
|
||||
int j = 0; |
|
||||
if (o["out"].type() == array_type) |
|
||||
for (auto const& d: o["out"].get_array()) |
|
||||
{ |
|
||||
if (asserts(output[j] == toInt(d))) |
|
||||
{ |
|
||||
cout << "Output byte [" << j << "] different!"; |
|
||||
return 1; |
|
||||
} |
|
||||
++j; |
|
||||
} |
|
||||
else if (o["out"].get_str().find("0x") == 0) |
|
||||
{ |
|
||||
if (asserts(output == fromHex(o["out"].get_str().substr(2)))) |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
if (asserts(output == fromHex(o["out"].get_str()))) |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
//checkLog(theState.pending().size() ? theState.log(0) : LogEntries(), importer.m_environment.sub.logs);
|
|
||||
eth::LogEntries logs = theState.pending().size() ? theState.log(0) : eth::LogEntries(); |
|
||||
|
|
||||
if (assertsEqual(logs.size(), importer.m_environment.sub.logs.size())) |
|
||||
return 1; |
|
||||
|
|
||||
for (size_t i = 0; i < logs.size(); ++i) |
|
||||
{ |
|
||||
if (assertsEqual(logs[i].address, importer.m_environment.sub.logs[i].address)) |
|
||||
return 1; |
|
||||
if (assertsEqual(logs[i].topics, importer.m_environment.sub.logs[i].topics)) |
|
||||
return 1; |
|
||||
if (asserts(logs[i].data == importer.m_environment.sub.logs[i].data)) |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
// check addresses
|
|
||||
#if ETH_FATDB |
|
||||
auto expectedAddrs = importer.m_statePost.addresses(); |
|
||||
auto resultAddrs = theState.addresses(); |
|
||||
for (auto& expectedPair : expectedAddrs) |
|
||||
{ |
|
||||
auto& expectedAddr = expectedPair.first; |
|
||||
auto resultAddrIt = resultAddrs.find(expectedAddr); |
|
||||
if (resultAddrIt == resultAddrs.end()) |
|
||||
{ |
|
||||
cout << "Missing expected address " << expectedAddr; |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
if (importer.m_statePost.balance(expectedAddr) != theState.balance(expectedAddr)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect balance " << theState.balance(expectedAddr) << ", expected " << importer.m_statePost.balance(expectedAddr); |
|
||||
return 1; |
|
||||
} |
|
||||
if (importer.m_statePost.transactionsFrom(expectedAddr) != theState.transactionsFrom(expectedAddr)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect txCount " << theState.transactionsFrom(expectedAddr) << ", expected " << importer.m_statePost.transactionsFrom(expectedAddr); |
|
||||
return 1; |
|
||||
} |
|
||||
if (importer.m_statePost.code(expectedAddr) != theState.code(expectedAddr)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect code"; |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
//checkStorage(importer.m_statePost.storage(expectedAddr), theState.storage(expectedAddr), expectedAddr);
|
|
||||
unordered_map<u256, u256> _resultStore = theState.storage(expectedAddr); |
|
||||
|
|
||||
for (auto&& expectedStorePair : importer.m_statePost.storage(expectedAddr)) |
|
||||
{ |
|
||||
auto& expectedStoreKey = expectedStorePair.first; |
|
||||
auto resultStoreIt = _resultStore.find(expectedStoreKey); |
|
||||
if (resultStoreIt == _resultStore.end()) |
|
||||
{ |
|
||||
cout << expectedAddr << ": missing store key " << expectedStoreKey << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
auto& expectedStoreValue = expectedStorePair.second; |
|
||||
auto& resultStoreValue = resultStoreIt->second; |
|
||||
if (asserts(expectedStoreValue == resultStoreValue)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
if (assertsEqual(_resultStore.size(), importer.m_statePost.storage(expectedAddr).size())) |
|
||||
return 1; |
|
||||
for (auto&& resultStorePair: _resultStore) |
|
||||
{ |
|
||||
if (!importer.m_statePost.storage(expectedAddr).count(resultStorePair.first)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": unexpected store key " << resultStorePair.first << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
//checkAddresses<map<Address, u256> >(expectedAddrs, resultAddrs);
|
|
||||
for (auto& resultPair : resultAddrs) |
|
||||
{ |
|
||||
auto& resultAddr = resultPair.first; |
|
||||
auto expectedAddrIt = expectedAddrs.find(resultAddr); |
|
||||
if (expectedAddrIt == expectedAddrs.end()) |
|
||||
return 1; |
|
||||
} |
|
||||
if (expectedAddrs != resultAddrs) |
|
||||
return 1; |
|
||||
#endif |
|
||||
if (theState.rootHash() != h256(o["postStateRoot"].get_str())) |
|
||||
{ |
|
||||
cout << "wrong post state root" << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
return 0; |
|
||||
} |
|
@ -1,298 +0,0 @@ |
|||||
/*
|
|
||||
This file is part of cpp-ethereum. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
You should have received a copy of the GNU General Public License |
|
||||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
||||
*/ |
|
||||
/** @file checkRandomTest.cpp
|
|
||||
* @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com> |
|
||||
* @date 2015 |
|
||||
* Check a random test and return 0/1 for success or failure. To be used for efficiency in the random test simulation. |
|
||||
*/ |
|
||||
|
|
||||
#include <libdevcore/Common.h> |
|
||||
#include <libdevcore/Assertions.h> |
|
||||
#include <libdevcore/Exceptions.h> |
|
||||
#include <libdevcore/Log.h> |
|
||||
#include <libevm/VMFactory.h> |
|
||||
#include <test/libevm/vm.h> |
|
||||
#include <test/TestHelper.h> |
|
||||
|
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter" |
|
||||
|
|
||||
using namespace std; |
|
||||
using namespace json_spirit; |
|
||||
using namespace dev::test; |
|
||||
using namespace dev; |
|
||||
|
|
||||
bool doVMTest(mValue& _v); |
|
||||
|
|
||||
int main(int argc, char *argv[]) |
|
||||
{ |
|
||||
g_logVerbosity = 0; |
|
||||
bool ret = false; |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
mValue v; |
|
||||
string s; |
|
||||
for (int i = 1; i < argc; ++i) |
|
||||
s += argv[i]; |
|
||||
if (asserts(s.length() > 0)) |
|
||||
{ |
|
||||
cout << "Content of argument is empty\n"; |
|
||||
return 1; |
|
||||
} |
|
||||
read_string(s, v); |
|
||||
ret = doVMTest(v); |
|
||||
} |
|
||||
catch (Exception const& _e) |
|
||||
{ |
|
||||
cout << "Failed test with Exception: " << diagnostic_information(_e) << endl; |
|
||||
ret = false; |
|
||||
} |
|
||||
catch (std::exception const& _e) |
|
||||
{ |
|
||||
cout << "Failed test with Exception: " << _e.what() << endl; |
|
||||
ret = false; |
|
||||
} |
|
||||
return ret; |
|
||||
} |
|
||||
|
|
||||
bool doVMTest(mValue& _v) |
|
||||
{ |
|
||||
eth::VMFactory::setKind(eth::VMKind::JIT); |
|
||||
|
|
||||
for (auto& i: _v.get_obj()) |
|
||||
{ |
|
||||
cnote << i.first; |
|
||||
mObject& o = i.second.get_obj(); |
|
||||
|
|
||||
assert(o.count("env") > 0); |
|
||||
assert(o.count("pre") > 0); |
|
||||
assert(o.count("exec") > 0); |
|
||||
|
|
||||
FakeExtVM fev; |
|
||||
fev.importEnv(o["env"].get_obj()); |
|
||||
fev.importState(o["pre"].get_obj()); |
|
||||
|
|
||||
fev.importExec(o["exec"].get_obj()); |
|
||||
if (fev.code.empty()) |
|
||||
{ |
|
||||
fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); |
|
||||
fev.code = fev.thisTxCode; |
|
||||
} |
|
||||
|
|
||||
bytes output; |
|
||||
bool vmExceptionOccured = false; |
|
||||
try |
|
||||
{ |
|
||||
auto vm = eth::VMFactory::create(); |
|
||||
output = vm->exec(fev.gas, fev, fev.simpleTrace()); |
|
||||
} |
|
||||
catch (eth::VMException const&) |
|
||||
{ |
|
||||
cnote << "Safe VM Exception"; |
|
||||
vmExceptionOccured = true; |
|
||||
} |
|
||||
catch (Exception const& _e) |
|
||||
{ |
|
||||
cnote << "VM did throw an exception: " << diagnostic_information(_e); |
|
||||
cnote << "Failed VM Test with Exception: " << _e.what(); |
|
||||
return 1; |
|
||||
} |
|
||||
catch (std::exception const& _e) |
|
||||
{ |
|
||||
cnote << "VM did throw an exception: " << _e.what(); |
|
||||
cnote << "Failed VM Test with Exception: " << _e.what(); |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
// 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); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
if (o.count("post") > 0) // No exceptions expected
|
|
||||
{ |
|
||||
if (asserts(!vmExceptionOccured) || asserts(o.count("post") > 0) || asserts(o.count("callcreates") > 0) || asserts(o.count("out") > 0) || asserts(o.count("gas") > 0) || asserts(o.count("logs") > 0)) |
|
||||
return 1; |
|
||||
|
|
||||
dev::test::FakeExtVM test; |
|
||||
test.importState(o["post"].get_obj()); |
|
||||
test.importCallCreates(o["callcreates"].get_array()); |
|
||||
test.sub.logs = importLog(o["logs"].get_array()); |
|
||||
|
|
||||
//checkOutput(output, o);
|
|
||||
int j = 0; |
|
||||
if (o["out"].type() == array_type) |
|
||||
for (auto const& d: o["out"].get_array()) |
|
||||
{ |
|
||||
if (asserts(output[j] == toInt(d))) |
|
||||
{ |
|
||||
cout << "Output byte [" << j << "] different!"; |
|
||||
return 1; |
|
||||
} |
|
||||
++j; |
|
||||
} |
|
||||
else if (o["out"].get_str().find("0x") == 0) |
|
||||
{ |
|
||||
if (asserts(output == fromHex(o["out"].get_str().substr(2)))) |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
if (asserts(output == fromHex(o["out"].get_str()))) |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
if (asserts(toInt(o["gas"]) == fev.gas)) |
|
||||
return 1; |
|
||||
|
|
||||
auto& expectedAddrs = test.addresses; |
|
||||
auto& resultAddrs = fev.addresses; |
|
||||
for (auto&& expectedPair : expectedAddrs) |
|
||||
{ |
|
||||
auto& expectedAddr = expectedPair.first; |
|
||||
auto resultAddrIt = resultAddrs.find(expectedAddr); |
|
||||
if (resultAddrIt == resultAddrs.end()) |
|
||||
{ |
|
||||
cout << "Missing expected address " << expectedAddr; |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
auto& expectedState = expectedPair.second; |
|
||||
auto& resultState = resultAddrIt->second; |
|
||||
if (asserts(std::get<0>(expectedState) == std::get<0>(resultState))) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect balance " << std::get<0>(resultState) << ", expected " << std::get<0>(expectedState); |
|
||||
return 1; |
|
||||
} |
|
||||
if (asserts(std::get<1>(expectedState) == std::get<1>(resultState))) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect txCount " << std::get<1>(resultState) << ", expected " << std::get<1>(expectedState); |
|
||||
return 1; |
|
||||
} |
|
||||
if (asserts(std::get<3>(expectedState) == std::get<3>(resultState))) |
|
||||
{ |
|
||||
cout << expectedAddr << ": incorrect code"; |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
//checkStorage(std::get<2>(expectedState), std::get<2>(resultState), expectedAddr);
|
|
||||
for (auto&& expectedStorePair : std::get<2>(expectedState)) |
|
||||
{ |
|
||||
auto& expectedStoreKey = expectedStorePair.first; |
|
||||
auto resultStoreIt = std::get<2>(resultState).find(expectedStoreKey); |
|
||||
if (resultStoreIt == std::get<2>(resultState).end()) |
|
||||
{ |
|
||||
cout << expectedAddr << ": missing store key " << expectedStoreKey << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
auto& expectedStoreValue = expectedStorePair.second; |
|
||||
auto& resultStoreValue = resultStoreIt->second; |
|
||||
if (asserts(expectedStoreValue == resultStoreValue)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
if (assertsEqual(std::get<2>(resultState).size(), std::get<2>(expectedState).size())) |
|
||||
return 1; |
|
||||
for (auto&& resultStorePair: std::get<2>(resultState)) |
|
||||
{ |
|
||||
if (!std::get<2>(expectedState).count(resultStorePair.first)) |
|
||||
{ |
|
||||
cout << expectedAddr << ": unexpected store key " << resultStorePair.first << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
//checkAddresses<std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes> > >(test.addresses, fev.addresses);
|
|
||||
for (auto& resultPair : fev.addresses) |
|
||||
{ |
|
||||
auto& resultAddr = resultPair.first; |
|
||||
auto expectedAddrIt = test.addresses.find(resultAddr); |
|
||||
if (expectedAddrIt == test.addresses.end()) |
|
||||
{ |
|
||||
cout << "Missing result address " << resultAddr << endl; |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
if (asserts(test.addresses == fev.addresses)) |
|
||||
return 1; |
|
||||
|
|
||||
if (asserts(test.callcreates == fev.callcreates)) |
|
||||
return 1; |
|
||||
|
|
||||
//checkCallCreates(fev.callcreates, test.callcreates);
|
|
||||
{ |
|
||||
if (assertsEqual(test.callcreates.size(), fev.callcreates.size())) |
|
||||
return 1; |
|
||||
|
|
||||
for (size_t i = 0; i < test.callcreates.size(); ++i) |
|
||||
{ |
|
||||
if (asserts(test.callcreates[i].data() == fev.callcreates[i].data())) |
|
||||
return 1; |
|
||||
if (asserts(test.callcreates[i].receiveAddress() == fev.callcreates[i].receiveAddress())) |
|
||||
return 1; |
|
||||
if (asserts(test.callcreates[i].gas() == fev.callcreates[i].gas())) |
|
||||
return 1; |
|
||||
if (asserts(test.callcreates[i].value() == fev.callcreates[i].value())) |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
//checkLog(fev.sub.logs, test.sub.logs);
|
|
||||
{ |
|
||||
if (assertsEqual(fev.sub.logs.size(), test.sub.logs.size())) |
|
||||
return 1; |
|
||||
|
|
||||
for (size_t i = 0; i < fev.sub.logs.size(); ++i) |
|
||||
{ |
|
||||
if (assertsEqual(fev.sub.logs[i].address, test.sub.logs[i].address)) |
|
||||
return 1; |
|
||||
if (assertsEqual(fev.sub.logs[i].topics, test.sub.logs[i].topics)) |
|
||||
return 1; |
|
||||
if (asserts(fev.sub.logs[i].data == test.sub.logs[i].data)) |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
else // Exception expected
|
|
||||
{ |
|
||||
if (asserts(vmExceptionOccured)) |
|
||||
return 1; |
|
||||
} |
|
||||
} |
|
||||
// test passed
|
|
||||
return 0; |
|
||||
} |
|
||||
|
|
@ -1,258 +0,0 @@ |
|||||
/*
|
|
||||
This file is part of cpp-ethereum. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
You should have received a copy of the GNU General Public License |
|
||||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
||||
*/ |
|
||||
/** @file createRandomStateTest.cpp
|
|
||||
* @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com> |
|
||||
* @date 2015 |
|
||||
* Creating a random state test. |
|
||||
*/ |
|
||||
|
|
||||
#include <string> |
|
||||
#include <iostream> |
|
||||
#include <chrono> |
|
||||
|
|
||||
#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> |
|
||||
#include <libevmcore/Instruction.h> |
|
||||
#include <libevm/VMFactory.h> |
|
||||
#include <test/libevm/vm.h> |
|
||||
#include <test/TestHelper.h> |
|
||||
#include <test/fuzzTesting/fuzzHelper.h> |
|
||||
|
|
||||
using namespace std; |
|
||||
using namespace json_spirit; |
|
||||
using namespace dev; |
|
||||
|
|
||||
void doStateTests(json_spirit::mValue& _v); |
|
||||
void doChristophAlgo(); |
|
||||
void doRandomCodeAlgo(); |
|
||||
|
|
||||
string const c_testExample = R"( |
|
||||
{ |
|
||||
"randomStatetest" : { |
|
||||
"env" : { |
|
||||
"currentCoinbase" : "945304eb96065b2a98b57a48a06ae28d285a71b5", |
|
||||
"currentDifficulty" : "5623894562375", |
|
||||
"currentGasLimit" : "115792089237316195423570985008687907853269984665640564039457584007913129639935", |
|
||||
"currentNumber" : "0", |
|
||||
"currentTimestamp" : "1", |
|
||||
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6" |
|
||||
}, |
|
||||
"pre" : { |
|
||||
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : { |
|
||||
"balance" : "0", |
|
||||
"code" : "0x6001600101600055", |
|
||||
"nonce" : "0", |
|
||||
"storage" : { |
|
||||
} |
|
||||
}, |
|
||||
"945304eb96065b2a98b57a48a06ae28d285a71b5" : { |
|
||||
"balance" : "46", |
|
||||
"code" : "0x6000355415600957005b60203560003555", |
|
||||
"nonce" : "0", |
|
||||
"storage" : { |
|
||||
} |
|
||||
}, |
|
||||
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : { |
|
||||
"balance" : "1000000000000000000", |
|
||||
"code" : "0x", |
|
||||
"nonce" : "0", |
|
||||
"storage" : { |
|
||||
} |
|
||||
} |
|
||||
}, |
|
||||
"transaction" : { |
|
||||
"data" : "0x42", |
|
||||
"gasLimit" : "400000", |
|
||||
"gasPrice" : "1", |
|
||||
"nonce" : "0", |
|
||||
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", |
|
||||
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87", |
|
||||
"value" : "100000" |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
)"; |
|
||||
|
|
||||
int main(int argc, char *argv[]) |
|
||||
{ |
|
||||
for (auto i = 0; i < argc; ++i) |
|
||||
{ |
|
||||
auto arg = std::string{argv[i]}; |
|
||||
dev::test::Options& options = const_cast<dev::test::Options&>(dev::test::Options::get()); |
|
||||
if (arg == "--fulloutput") |
|
||||
options.fulloutput = true; |
|
||||
} |
|
||||
|
|
||||
//doChristophAlgo();
|
|
||||
doRandomCodeAlgo(); |
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
void doChristophAlgo() |
|
||||
{ |
|
||||
g_logVerbosity = 0; |
|
||||
|
|
||||
// create random code
|
|
||||
boost::random::mt19937 gen; |
|
||||
auto now = chrono::steady_clock::now().time_since_epoch(); |
|
||||
auto timeSinceEpoch = chrono::duration_cast<chrono::nanoseconds>(now).count(); |
|
||||
gen.seed(static_cast<unsigned int>(timeSinceEpoch)); |
|
||||
|
|
||||
// set min and max length of the random evm code
|
|
||||
boost::random::uniform_int_distribution<> lengthOfCodeDist(8, 24); |
|
||||
boost::random::uniform_int_distribution<> reasonableInputValuesSize(0, 7); |
|
||||
boost::random::uniform_int_distribution<> opcodeDist(0, 255); |
|
||||
boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x40, 0x45); |
|
||||
boost::random::uniform_int_distribution<> uniformInt(0, 0x7fffffff); |
|
||||
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenInputValue(gen, reasonableInputValuesSize); |
|
||||
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenUniformInt(gen, uniformInt); |
|
||||
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGen(gen, opcodeDist); |
|
||||
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenBlockInfoOpcode(gen, BlockInfoOpcodeDist); |
|
||||
|
|
||||
std::vector<u256> reasonableInputValues; |
|
||||
reasonableInputValues.push_back(0); |
|
||||
reasonableInputValues.push_back(1); |
|
||||
reasonableInputValues.push_back(50000); |
|
||||
reasonableInputValues.push_back(u256("0x10000000000000000000000000000000000000000")); |
|
||||
reasonableInputValues.push_back(u256("0xffffffffffffffffffffffffffffffffffffffff")); |
|
||||
reasonableInputValues.push_back(u256("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe")); |
|
||||
reasonableInputValues.push_back(u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
|
||||
reasonableInputValues.push_back(u256("0x945304eb96065b2a98b57a48a06ae28d285a71b5")); |
|
||||
reasonableInputValues.push_back(randGenUniformInt()); |
|
||||
|
|
||||
int lengthOfCode = lengthOfCodeDist(gen); |
|
||||
string randomCode; |
|
||||
for (int i = 0; i < lengthOfCode; ++i) |
|
||||
{ |
|
||||
// pre-fill stack to avoid that most of the test fail with a stackunderflow
|
|
||||
if (i < 8 && (randGen() < 192)) |
|
||||
{ |
|
||||
randomCode += randGen() < 32 ? toHex(toCompactBigEndian((uint8_t)randGenBlockInfoOpcode())) : "7f" + toHex(reasonableInputValues[randGenInputValue()]); |
|
||||
continue; |
|
||||
} |
|
||||
uint8_t opcode = randGen(); |
|
||||
// disregard all invalid commands, except of one (0x0c)
|
|
||||
if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 250))) |
|
||||
randomCode += toHex(toCompactBigEndian(opcode)); |
|
||||
else |
|
||||
i--; |
|
||||
} |
|
||||
|
|
||||
mValue v; |
|
||||
read_string(c_testExample, v); |
|
||||
// insert new random code
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + (randGen() > 128 ? "55" : "") + (randGen() > 128 ? "60005155" : ""); |
|
||||
// insert new data in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["data"] = "0x" + randomCode; |
|
||||
// insert new value in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["value"] = toString(randGenUniformInt()); |
|
||||
// insert new gasLimit in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["gasLimit"] = "0x" + toHex(toCompactBigEndian((int)randGenUniformInt())); |
|
||||
// fill test
|
|
||||
doStateTests(v); |
|
||||
// stream to output for further handling by the bash script
|
|
||||
cout << json_spirit::write_string(v, true); |
|
||||
} |
|
||||
|
|
||||
void doRandomCodeAlgo() |
|
||||
{ |
|
||||
g_logVerbosity = 0; |
|
||||
dev::test::RandomCodeOptions options; |
|
||||
options.setWeight(dev::eth::Instruction::STOP, 10); //default 50
|
|
||||
options.setWeight(dev::eth::Instruction::SSTORE, 70); |
|
||||
options.setWeight(dev::eth::Instruction::CALL, 75); |
|
||||
options.addAddress(Address("0xffffffffffffffffffffffffffffffffffffffff")); |
|
||||
options.addAddress(Address("0x1000000000000000000000000000000000000000")); |
|
||||
options.addAddress(Address("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")); //coinbase
|
|
||||
options.addAddress(Address("0x945304eb96065b2a98b57a48a06ae28d285a71b5")); |
|
||||
options.addAddress(Address("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")); |
|
||||
options.smartCodeProbability = 35; |
|
||||
string randomCode = dev::test::RandomCode::generate(10, options); |
|
||||
string randomData = dev::test::RandomCode::generate(10, options); |
|
||||
|
|
||||
mValue v; |
|
||||
read_string(c_testExample, v); |
|
||||
|
|
||||
// insert new random code
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode; |
|
||||
|
|
||||
// insert new data in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["data"] = "0x" + randomData; |
|
||||
|
|
||||
// insert new value in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["value"] = dev::test::RandomCode::randomUniIntHex(); |
|
||||
|
|
||||
// insert new gasLimit in tx
|
|
||||
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["gasLimit"] = dev::test::RandomCode::randomUniIntHex(); |
|
||||
|
|
||||
// fill test
|
|
||||
doStateTests(v); |
|
||||
|
|
||||
// stream to output for further handling by the bash script
|
|
||||
cout << json_spirit::write_string(v, true); |
|
||||
} |
|
||||
|
|
||||
void doStateTests(json_spirit::mValue& _v) |
|
||||
{ |
|
||||
eth::VMFactory::setKind(eth::VMKind::Interpreter); |
|
||||
|
|
||||
for (auto& i: _v.get_obj()) |
|
||||
{ |
|
||||
//cerr << i.first << endl;
|
|
||||
mObject& o = i.second.get_obj(); |
|
||||
|
|
||||
assert(o.count("env") > 0); |
|
||||
assert(o.count("pre") > 0); |
|
||||
assert(o.count("transaction") > 0); |
|
||||
bytes output; |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
test::ImportTest importer(o, true); |
|
||||
eth::State theState = importer.m_statePre; |
|
||||
try |
|
||||
{ |
|
||||
output = theState.execute(test::lastHashes(importer.m_environment.currentBlock.number()), importer.m_transaction).output; |
|
||||
} |
|
||||
catch (Exception const& _e) |
|
||||
{ |
|
||||
cnote << "state execution did throw an exception: " << diagnostic_information(_e); |
|
||||
theState.commit(); |
|
||||
} |
|
||||
catch (std::exception const& _e) |
|
||||
{ |
|
||||
cnote << "state execution did throw an exception: " << _e.what(); |
|
||||
} |
|
||||
#if ETH_FATDB |
|
||||
importer.exportTest(output, theState); |
|
||||
#else |
|
||||
cout << "You can not fill tests when FATDB is switched off"; |
|
||||
#endif |
|
||||
} |
|
||||
catch(...) |
|
||||
{ |
|
||||
cnote << "Error filling test, probably..."; |
|
||||
} |
|
||||
} |
|
||||
} |
|
@ -1,206 +0,0 @@ |
|||||
/*
|
|
||||
This file is part of cpp-ethereum. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
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. |
|
||||
|
|
||||
You should have received a copy of the GNU General Public License |
|
||||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
||||
*/ |
|
||||
/** @file createRandomTest.cpp
|
|
||||
* @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com> |
|
||||
* @date 2014 |
|
||||
* Creating a random virtual machine test. |
|
||||
*/ |
|
||||
|
|
||||
#include <string> |
|
||||
#include <iostream> |
|
||||
#include <chrono> |
|
||||
|
|
||||
#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> |
|
||||
#include <libevmcore/Instruction.h> |
|
||||
#include <libevm/VMFactory.h> |
|
||||
#include <test/libevm/vm.h> |
|
||||
#include <test/TestHelper.h> |
|
||||
|
|
||||
using namespace std; |
|
||||
using namespace json_spirit; |
|
||||
using namespace dev; |
|
||||
|
|
||||
void doMyTests(json_spirit::mValue& _v); |
|
||||
|
|
||||
int main(int argc, char *argv[]) |
|
||||
{ |
|
||||
g_logVerbosity = 0; |
|
||||
|
|
||||
// create random code
|
|
||||
|
|
||||
boost::random::mt19937 gen; |
|
||||
|
|
||||
auto now = chrono::steady_clock::now().time_since_epoch(); |
|
||||
auto timeSinceEpoch = chrono::duration_cast<chrono::nanoseconds>(now).count(); |
|
||||
gen.seed(static_cast<unsigned int>(timeSinceEpoch)); |
|
||||
boost::random::uniform_int_distribution<> lengthOfCodeDist(2, 16); |
|
||||
boost::random::uniform_int_distribution<> opcodeDist(0, 255); |
|
||||
boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x40, 0x45); |
|
||||
boost::random::variate_generator<boost::mt19937&, |
|
||||
boost::random::uniform_int_distribution<> > randGen(gen, opcodeDist); |
|
||||
boost::random::variate_generator<boost::mt19937&, |
|
||||
boost::random::uniform_int_distribution<> > randGenBlockInfoOpcode(gen, BlockInfoOpcodeDist); |
|
||||
|
|
||||
int lengthOfCode = lengthOfCodeDist(gen); |
|
||||
string randomCode; |
|
||||
|
|
||||
for (int i = 0; i < lengthOfCode; ++i) |
|
||||
{ |
|
||||
if (i < 8 && (randGen() < 192)) |
|
||||
{ |
|
||||
randomCode += toHex(toCompactBigEndian((uint8_t)randGenBlockInfoOpcode())); |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
uint8_t opcode = randGen(); |
|
||||
// disregard all invalid commands, except of one (0x0c)
|
|
||||
if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 250))) |
|
||||
randomCode += toHex(toCompactBigEndian(opcode)); |
|
||||
else |
|
||||
i--; |
|
||||
} |
|
||||
|
|
||||
const string s =\ |
|
||||
"{\n\
|
|
||||
\"randomVMtest\": {\n\
|
|
||||
\"env\" : {\n\
|
|
||||
\"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\
|
|
||||
\"currentNumber\" : \"300\",\n\
|
|
||||
\"currentGasLimit\" : \"1000000\",\n\
|
|
||||
\"currentDifficulty\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\
|
|
||||
\"currentTimestamp\" : 2,\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; |
|
||||
read_string(s, v); |
|
||||
|
|
||||
// insert new random code
|
|
||||
v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + (randGen() > 128 ? "55" : ""); |
|
||||
|
|
||||
// execute code in vm
|
|
||||
doMyTests(v); |
|
||||
|
|
||||
// stream to output for further handling by the bash script
|
|
||||
cout << json_spirit::write_string(v, true); |
|
||||
|
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
void doMyTests(json_spirit::mValue& _v) |
|
||||
{ |
|
||||
eth::VMFactory::setKind(eth::VMKind::Interpreter); |
|
||||
|
|
||||
for (auto& i: _v.get_obj()) |
|
||||
{ |
|
||||
cnote << i.first; |
|
||||
mObject& o = i.second.get_obj(); |
|
||||
|
|
||||
assert(o.count("env") > 0); |
|
||||
assert(o.count("pre") > 0); |
|
||||
assert(o.count("exec") > 0); |
|
||||
|
|
||||
dev::test::FakeExtVM fev; |
|
||||
fev.importEnv(o["env"].get_obj()); |
|
||||
fev.importState(o["pre"].get_obj()); |
|
||||
|
|
||||
o["pre"] = mValue(fev.exportState()); |
|
||||
|
|
||||
fev.importExec(o["exec"].get_obj()); |
|
||||
if (fev.code.empty()) |
|
||||
{ |
|
||||
fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); |
|
||||
fev.code = fev.thisTxCode; |
|
||||
} |
|
||||
|
|
||||
bytes output; |
|
||||
auto vm = eth::VMFactory::create(); |
|
||||
|
|
||||
bool vmExceptionOccured = false; |
|
||||
try |
|
||||
{ |
|
||||
output = vm->exec(fev.gas, fev, fev.simpleTrace()); |
|
||||
} |
|
||||
catch (eth::VMException const& _e) |
|
||||
{ |
|
||||
cnote << "VM did throw an exception: " << diagnostic_information(_e); |
|
||||
vmExceptionOccured = true; |
|
||||
} |
|
||||
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()); |
|
||||
if (!vmExceptionOccured) |
|
||||
{ |
|
||||
o["post"] = mValue(fev.exportState()); |
|
||||
o["callcreates"] = fev.exportCallCreates(); |
|
||||
o["out"] = toHex(output, 2, HexPrefix::Add); |
|
||||
o["gas"] = toCompactHex(fev.gas, HexPrefix::Add, 1); |
|
||||
o["logs"] = test::exportLog(fev.sub.logs); |
|
||||
} |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue