From 6bace568d01c90b8d2ee7d43eed840c10955a2c4 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 17:04:18 +0100 Subject: [PATCH 1/6] use ETHEREUM_TEST_PATH in all tests --- test/genesis.cpp | 13 ++++++++++++- test/hexPrefix.cpp | 13 ++++++++++++- test/rlp.cpp | 13 ++++++++++++- test/trie.cpp | 13 ++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/test/genesis.cpp b/test/genesis.cpp index 5f17d2762..16ffb1859 100644 --- a/test/genesis.cpp +++ b/test/genesis.cpp @@ -35,9 +35,20 @@ namespace js = json_spirit; BOOST_AUTO_TEST_CASE(genesis_tests) { + const char* ptestPath = getenv("ETHEREUM_TEST_PATH"); + string testPath; + + if (ptestPath == NULL) + { + cnote << " could not find environment variable ETHEREUM_TEST_PATH \n"; + testPath = "../../../tests"; + } + else + testPath = ptestPath; + cnote << "Testing Genesis block..."; js::mValue v; - string s = asString(contents("../../../tests/genesishashestest.json")); + string s = asString(contents(testPath + "/genesishashestest.json")); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'genesishashestest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); diff --git a/test/hexPrefix.cpp b/test/hexPrefix.cpp index fb2fbc826..99207ab97 100644 --- a/test/hexPrefix.cpp +++ b/test/hexPrefix.cpp @@ -33,9 +33,20 @@ namespace js = json_spirit; BOOST_AUTO_TEST_CASE(hexPrefix_test) { + const char* ptestPath = getenv("ETHEREUM_TEST_PATH"); + string testPath; + + if (ptestPath == NULL) + { + cnote << " could not find environment variable ETHEREUM_TEST_PATH \n"; + testPath = "../../../tests"; + } + else + testPath = ptestPath; + cnote << "Testing Hex-Prefix-Encode..."; js::mValue v; - string s = asString(contents("../../../tests/hexencodetest.json")); + string s = asString(contents(testPath + "/hexencodetest.json")); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content from 'hexencodetest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) diff --git a/test/rlp.cpp b/test/rlp.cpp index 69360ad66..608a8499e 100644 --- a/test/rlp.cpp +++ b/test/rlp.cpp @@ -61,7 +61,18 @@ namespace dev static void getRLPTestCases(js::mValue& v) { - string s = asString(contents("../../../tests/rlptest.json")); + const char* ptestPath = getenv("ETHEREUM_TEST_PATH"); + string testPath; + + if (ptestPath == NULL) + { + cnote << " could not find environment variable ETHEREUM_TEST_PATH \n"; + testPath = "../../../tests"; + } + else + testPath = ptestPath; + + string s = asString(contents(testPath + "/rlptest.json")); BOOST_REQUIRE_MESSAGE( s.length() > 0, "Contents of 'rlptest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); diff --git a/test/trie.cpp b/test/trie.cpp index 899eb1f60..f8ebd10c8 100644 --- a/test/trie.cpp +++ b/test/trie.cpp @@ -49,9 +49,20 @@ static unsigned fac(unsigned _i) BOOST_AUTO_TEST_CASE(trie_tests) { + const char* ptestPath = getenv("ETHEREUM_TEST_PATH"); + string testPath; + + if (ptestPath == NULL) + { + cnote << " could not find environment variable ETHEREUM_TEST_PATH \n"; + testPath = "../../../tests"; + } + else + testPath = ptestPath; + cnote << "Testing Trie..."; js::mValue v; - string s = asString(contents("../../../tests/trietest.json")); + string s = asString(contents(testPath + "/trietest.json")); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?"); js::read_string(s, v); for (auto& i: v.get_obj()) From bc1e4ceee23b924bc89c385aae136eb97a52d749 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 18:59:42 +0100 Subject: [PATCH 2/6] Add VMTRACE to user defined vm test Conflicts: test/vm.cpp --- test/vm.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++----- test/vm.h | 3 +++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/test/vm.cpp b/test/vm.cpp index fe4863041..c0452ef78 100644 --- a/test/vm.cpp +++ b/test/vm.cpp @@ -20,9 +20,9 @@ * vm test functions. */ -#include "vm.h" -#include #include +#include +#include "vm.h" //#define FILL_TESTS @@ -63,6 +63,7 @@ h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFun bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc const&, Address _myAddressOverride, Address _codeAddressOverride) { + u256 contractgas = 0xffff; Transaction t; @@ -422,8 +423,27 @@ void FakeExtVM::importCallCreates(mArray& _callcreates) } } -// THIS IS BROKEN AND NEEDS TO BE REMOVED. -h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _origin, SubState* o_sub, Manifest* o_ms, OnOpFunc const& _onOp, unsigned _level) +OnOpFunc FakeExtVM::simpleTrace() +{ + return [](uint64_t steps, Instruction inst, bigint newMemSize, bigint gasCost, void* voidVM, void const* voidExt) + { + FakeExtVM const& ext = *(FakeExtVM const*)voidExt; + VM& vm = *(VM*)voidVM; + + ostringstream o; + o << endl << " STACK" << endl; + for (auto i: vm.stack()) + o << (h256)i << endl; + o << " MEMORY" << endl << memDump(vm.memory()); + o << " STORAGE" << endl; + for (auto const& i: ext.state().storage(ext.myAddress)) + o << showbase << hex << i.first << ": " << i.second << endl; + dev::LogOutputStream(true) << o.str(); + dev::LogOutputStream(false) << " | " << dec << ext.depth << " | " << ext.myAddress << " | #" << steps << " | " << hex << setw(4) << setfill('0') << vm.curPC() << " : " << instructionInfo(inst).name << " | " << dec << vm.gas() << " | -" << dec << gasCost << " | " << newMemSize << "x32" << " ]"; + }; +} + +h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _origin, std::set
* o_suicides, Manifest* o_ms, OnOpFunc const& _onOp, unsigned _level) { (void)o_sub; @@ -520,7 +540,7 @@ void doTests(json_spirit::mValue& v, bool _fillin) VM vm(fev.gas); try { - output = vm.go(fev).toVector(); + output = vm.go(fev, fev.simpleTrace()).toVector(); } catch (Exception const& _e) { @@ -756,3 +776,30 @@ BOOST_AUTO_TEST_CASE(vmSystemOperationsTest) { dev::test::executeTests("vmSystemOperationsTest"); } + +BOOST_AUTO_TEST_CASE(userDefinedFile) +{ + + if (boost::unit_test::framework::master_test_suite().argc == 2) + { + string filename = boost::unit_test::framework::master_test_suite().argv[1]; + g_logVerbosity = 12; + try + { + cnote << "Testing VM..." << "user defined test"; + json_spirit::mValue v; + string s = asString(contents(filename)); + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + filename + " is empty. "); + json_spirit::read_string(s, v); + dev::test::doTests(v, false); + } + catch (Exception const& _e) + { + BOOST_ERROR("Failed VM Test with Exception: " << diagnostic_information(_e)); + } + catch (std::exception const& _e) + { + BOOST_ERROR("Failed VM Test with Exception: " << _e.what()); + } + } +} diff --git a/test/vm.h b/test/vm.h index 58ff58cd9..bf44c4b42 100644 --- a/test/vm.h +++ b/test/vm.h @@ -80,6 +80,9 @@ public: json_spirit::mArray exportCallCreates(); void importCallCreates(json_spirit::mArray& _callcreates); + static eth::OnOpFunc simpleTrace(); + FakeState state() const { return m_s; } + std::map, bytes>> addresses; eth::Transactions callcreates; bytes thisTxData; From 8cf0e2bd9ceb55b6170e32a19403fc9fcacf800e Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 19:01:10 +0100 Subject: [PATCH 3/6] VMTRACE for internal calls Conflicts: test/vm.cpp --- test/vm.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/vm.cpp b/test/vm.cpp index c0452ef78..fe2aff8cd 100644 --- a/test/vm.cpp +++ b/test/vm.cpp @@ -509,6 +509,7 @@ h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _end + namespace dev { namespace test { void doTests(json_spirit::mValue& v, bool _fillin) From 875d526590ee8e4ff0645125559fb453cc1a6418 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 22:21:30 +0100 Subject: [PATCH 4/6] log VMTRACE in file for user defined test --- test/vm.cpp | 31 ++++++------------------------- test/vm.h | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/test/vm.cpp b/test/vm.cpp index fe2aff8cd..feb2bbaaa 100644 --- a/test/vm.cpp +++ b/test/vm.cpp @@ -21,7 +21,6 @@ */ #include -#include #include "vm.h" //#define FILL_TESTS @@ -63,7 +62,6 @@ h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFun bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc const&, Address _myAddressOverride, Address _codeAddressOverride) { - u256 contractgas = 0xffff; Transaction t; @@ -132,7 +130,7 @@ bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, m_ms.internal.resize(m_ms.internal.size() + 1); - auto ret = m_s.call(_receiveAddress,_codeAddressOverride ? _codeAddressOverride : _receiveAddress, _myAddressOverride ? _myAddressOverride : myAddress, _value, gasPrice, _data, _gas, _out, origin, &sub, &(m_ms.internal.back()), OnOpFunc(), 1); + auto ret = m_s.call(_receiveAddress,_codeAddressOverride ? _codeAddressOverride : _receiveAddress, _myAddressOverride ? _myAddressOverride : myAddress, _value, gasPrice, _data, _gas, _out, origin, &sub, &(m_ms.internal.back()), simpleTrace(), 1); if (!m_ms.internal.back().from) m_ms.internal.pop_back(); @@ -423,27 +421,8 @@ void FakeExtVM::importCallCreates(mArray& _callcreates) } } -OnOpFunc FakeExtVM::simpleTrace() -{ - return [](uint64_t steps, Instruction inst, bigint newMemSize, bigint gasCost, void* voidVM, void const* voidExt) - { - FakeExtVM const& ext = *(FakeExtVM const*)voidExt; - VM& vm = *(VM*)voidVM; - - ostringstream o; - o << endl << " STACK" << endl; - for (auto i: vm.stack()) - o << (h256)i << endl; - o << " MEMORY" << endl << memDump(vm.memory()); - o << " STORAGE" << endl; - for (auto const& i: ext.state().storage(ext.myAddress)) - o << showbase << hex << i.first << ": " << i.second << endl; - dev::LogOutputStream(true) << o.str(); - dev::LogOutputStream(false) << " | " << dec << ext.depth << " | " << ext.myAddress << " | #" << steps << " | " << hex << setw(4) << setfill('0') << vm.curPC() << " : " << instructionInfo(inst).name << " | " << dec << vm.gas() << " | -" << dec << gasCost << " | " << newMemSize << "x32" << " ]"; - }; -} - -h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _origin, std::set
* o_suicides, Manifest* o_ms, OnOpFunc const& _onOp, unsigned _level) +// THIS IS BROKEN AND NEEDS TO BE REMOVED. +h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _origin, SubState* o_sub, Manifest* o_ms, OnOpFunc const& _onOp, unsigned _level) { (void)o_sub; @@ -541,7 +520,7 @@ void doTests(json_spirit::mValue& v, bool _fillin) VM vm(fev.gas); try { - output = vm.go(fev, fev.simpleTrace()).toVector(); + output = vm.go(fev, fev.simpleTrace()).toVector(); } catch (Exception const& _e) { @@ -784,6 +763,7 @@ BOOST_AUTO_TEST_CASE(userDefinedFile) if (boost::unit_test::framework::master_test_suite().argc == 2) { string filename = boost::unit_test::framework::master_test_suite().argv[1]; + int currentVerbosity = g_logVerbosity; g_logVerbosity = 12; try { @@ -802,5 +782,6 @@ BOOST_AUTO_TEST_CASE(userDefinedFile) { BOOST_ERROR("Failed VM Test with Exception: " << _e.what()); } + g_logVerbosity = currentVerbosity; } } diff --git a/test/vm.h b/test/vm.h index bf44c4b42..1718cd6f0 100644 --- a/test/vm.h +++ b/test/vm.h @@ -28,6 +28,7 @@ along with cpp-ethereum. If not, see . #include #include "JsonSpiritHeaders.h" #include +#include #include #include #include @@ -36,6 +37,7 @@ along with cpp-ethereum. If not, see . #include #include + namespace dev { namespace test { struct FakeExtVMFailure : virtual Exception {}; @@ -44,7 +46,7 @@ class FakeState: public eth::State { public: /// Execute a contract-creation transaction. - h160 createNewAddress(Address _newAddress, Address _txSender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _originAddress = {}, eth::SubState* o_suicides = nullptr, eth::Manifest* o_ms = nullptr, eth::OnOpFunc const& _onOp = {}, unsigned _level = 0); + h160 createNewAddress(Address _newAddress, Address _txSender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _originAddress = {}, eth::SubState* o_sub = nullptr, eth::Manifest* o_ms = nullptr, eth::OnOpFunc const& _onOp = {}, unsigned _level = 0); }; class FakeExtVM: public eth::ExtVMFace @@ -80,7 +82,9 @@ public: json_spirit::mArray exportCallCreates(); void importCallCreates(json_spirit::mArray& _callcreates); - static eth::OnOpFunc simpleTrace(); + template + eth::OnOpFunc simpleTrace(); + FakeState state() const { return m_s; } std::map, bytes>> addresses; @@ -94,4 +98,32 @@ private: eth::Manifest m_ms; }; +template +eth::OnOpFunc FakeExtVM::simpleTrace() +{ + return [](uint64_t steps, eth::Instruction inst, bigint newMemSize, bigint gasCost, void* voidVM, void const* voidExt) + { + ExtVMType const& ext = *(ExtVMType const*)voidExt; + eth::VM& vm = *(eth::VM*)voidVM; + + std::ostringstream o; + o << std::endl << " STACK" << std::endl; + for (auto i: vm.stack()) + o << (h256)i << std::endl; + o << " MEMORY" << std::endl << memDump(vm.memory()); + o << " STORAGE" << std::endl; + for (auto const& i: ext.state().storage(ext.myAddress)) + o << std::showbase << std::hex << i.first << ": " << i.second << std::endl; + dev::LogOutputStream(true) << o.str(); + dev::LogOutputStream(false) << " | " << std::dec << ext.depth << " | " << ext.myAddress << " | #" << steps << " | " << std::hex << std::setw(4) << std::setfill('0') << vm.curPC() << " : " << instructionInfo(inst).name << " | " << std::dec << vm.gas() << " | -" << std::dec << gasCost << " | " << newMemSize << "x32" << " ]"; + + if (eth::VMTraceChannel::verbosity <= g_logVerbosity) + { + std::ofstream f; + f.open("./vmtrace.log", std::ofstream::app); + f << o.str(); + f << " | " << std::dec << ext.depth << " | " << ext.myAddress << " | #" << steps << " | " << std::hex << std::setw(4) << std::setfill('0') << vm.curPC() << " : " << instructionInfo(inst).name << " | " << std::dec << vm.gas() << " | -" << std::dec << gasCost << " | " << newMemSize << "x32"; + } + }; +} } } // Namespace Close From 3332043ec2159493c6650dbe455b3705d4219b60 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 22:33:23 +0100 Subject: [PATCH 5/6] Update vm.cpp --- test/vm.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/vm.cpp b/test/vm.cpp index feb2bbaaa..40a0a862b 100644 --- a/test/vm.cpp +++ b/test/vm.cpp @@ -486,9 +486,6 @@ h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _end return _newAddress; } - - - namespace dev { namespace test { void doTests(json_spirit::mValue& v, bool _fillin) From 314b070d364e7d6783241e8b7e1b318df5528544 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Mon, 27 Oct 2014 22:33:50 +0100 Subject: [PATCH 6/6] Update vm.h --- test/vm.h | 1 - 1 file changed, 1 deletion(-) diff --git a/test/vm.h b/test/vm.h index 1718cd6f0..f3aae694a 100644 --- a/test/vm.h +++ b/test/vm.h @@ -37,7 +37,6 @@ along with cpp-ethereum. If not, see . #include #include - namespace dev { namespace test { struct FakeExtVMFailure : virtual Exception {};