From a70b84cb3df614377e6b79e6e53ca74731d95bef Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 10:35:12 +0100 Subject: [PATCH 01/10] libtestutils --- CMakeLists.txt | 1 + libtestutils/BlockChainLoader.cpp | 101 ++++++++++++++++++++++++++ libtestutils/BlockChainLoader.h | 51 +++++++++++++ libtestutils/CMakeLists.txt | 44 +++++++++++ libtestutils/Common.cpp | 80 ++++++++++++++++++++ libtestutils/Common.h | 44 +++++++++++ libtestutils/FixedClient.cpp | 41 +++++++++++ libtestutils/FixedClient.h | 55 ++++++++++++++ libtestutils/FixedWebThreeServer.cpp | 2 + libtestutils/FixedWebThreeServer.h | 32 ++++++++ libtestutils/ShortLivingDirectory.cpp | 44 +++++++++++ libtestutils/ShortLivingDirectory.h | 45 ++++++++++++ libtestutils/StateLoader.cpp | 57 +++++++++++++++ libtestutils/StateLoader.h | 42 +++++++++++ 14 files changed, 639 insertions(+) create mode 100644 libtestutils/BlockChainLoader.cpp create mode 100644 libtestutils/BlockChainLoader.h create mode 100644 libtestutils/CMakeLists.txt create mode 100644 libtestutils/Common.cpp create mode 100644 libtestutils/Common.h create mode 100644 libtestutils/FixedClient.cpp create mode 100644 libtestutils/FixedClient.h create mode 100644 libtestutils/FixedWebThreeServer.cpp create mode 100644 libtestutils/FixedWebThreeServer.h create mode 100644 libtestutils/ShortLivingDirectory.cpp create mode 100644 libtestutils/ShortLivingDirectory.h create mode 100644 libtestutils/StateLoader.cpp create mode 100644 libtestutils/StateLoader.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e7e89a5a7..70cd3cb66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,7 @@ add_subdirectory(libevm) add_subdirectory(libethereum) add_subdirectory(libwebthree) +add_subdirectory(libtestutils) add_subdirectory(test) if (NOT JUSTTESTS) diff --git a/libtestutils/BlockChainLoader.cpp b/libtestutils/BlockChainLoader.cpp new file mode 100644 index 000000000..224e40ec5 --- /dev/null +++ b/libtestutils/BlockChainLoader.cpp @@ -0,0 +1,101 @@ +/* + 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 . + */ +/** @file BlockChainLoader.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include "BlockChainLoader.h" +#include "StateLoader.h" +#include "Common.h" + +using namespace std; +using namespace dev; +using namespace dev::test; +using namespace dev::eth; + +namespace dev +{ +namespace test +{ +dev::eth::BlockInfo toBlockInfo(Json::Value const& _json); +bytes toGenesisBlock(Json::Value const& _json); + +} +} + +dev::eth::BlockInfo dev::test::toBlockInfo(Json::Value const& _json) +{ + RLPStream rlpStream; + auto size = _json.getMemberNames().size(); + rlpStream.appendList(_json["hash"].empty() ? size : (size - 1)); + rlpStream << fromHex(_json["parentHash"].asString()); + rlpStream << fromHex(_json["uncleHash"].asString()); + rlpStream << fromHex(_json["coinbase"].asString()); + rlpStream << fromHex(_json["stateRoot"].asString()); + rlpStream << fromHex(_json["transactionsTrie"].asString()); + rlpStream << fromHex(_json["receiptTrie"].asString()); + rlpStream << fromHex(_json["bloom"].asString()); + rlpStream << bigint(_json["difficulty"].asString()); + rlpStream << bigint(_json["number"].asString()); + rlpStream << bigint(_json["gasLimit"].asString()); + rlpStream << bigint(_json["gasUsed"].asString()); + rlpStream << bigint(_json["timestamp"].asString()); + rlpStream << fromHex(_json["extraData"].asString()); + rlpStream << fromHex(_json["mixHash"].asString()); + rlpStream << fromHex(_json["nonce"].asString()); + + BlockInfo result; + RLP rlp(rlpStream.out()); + result.populateFromHeader(rlp, IgnoreNonce); + return result; +} + +bytes dev::test::toGenesisBlock(Json::Value const& _json) +{ + BlockInfo bi = toBlockInfo(_json); + RLPStream rlpStream; + bi.streamRLP(rlpStream, WithNonce); + + RLPStream fullStream(3); + fullStream.appendRaw(rlpStream.out()); + fullStream.appendRaw(RLPEmptyList); + fullStream.appendRaw(RLPEmptyList); + bi.verifyInternals(&fullStream.out()); + + return fullStream.out(); +} + +BlockChainLoader::BlockChainLoader(Json::Value const& _json) +{ + // load pre state + StateLoader sl(_json["pre"]); + m_state = sl.state(); + + // load genesisBlock + m_bc.reset(new BlockChain(toGenesisBlock(_json["genesisBlockHeader"]), m_dir.path(), true)); + + // load blocks + for (auto const& block: _json["blocks"]) + { + bytes rlp = fromHex(block["rlp"].asString()); + m_bc->import(rlp, m_state.db()); + } + + // sync state + m_state.sync(*m_bc); +} diff --git a/libtestutils/BlockChainLoader.h b/libtestutils/BlockChainLoader.h new file mode 100644 index 000000000..a3528045c --- /dev/null +++ b/libtestutils/BlockChainLoader.h @@ -0,0 +1,51 @@ +/* + 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 . + */ +/** @file BlockChainLoader.h + * @author Marek Kotewicz + * @date 2015 + */ + +#pragma once +#include +#include +#include +#include +#include "ShortLivingDirectory.h" + +namespace dev +{ +namespace test +{ + +/** + * @brief - loads the blockchain from json, creates temporary directory to store it, removes this temporary directory on dealloc + */ +class BlockChainLoader +{ +public: + BlockChainLoader(Json::Value const& _json); + eth::BlockChain& bc() { return *m_bc; } + eth::State state() { return m_state; } + +private: + ShortLivingDirectory m_dir; + std::auto_ptr m_bc; + eth::State m_state; +}; + +} +} diff --git a/libtestutils/CMakeLists.txt b/libtestutils/CMakeLists.txt new file mode 100644 index 000000000..f79daa47c --- /dev/null +++ b/libtestutils/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_policy(SET CMP0015 NEW) +# this policy was introduced in cmake 3.0 +# remove if, once 3.0 will be used on unix +if (${CMAKE_MAJOR_VERSION} GREATER 2) + # old policy do not use MACOSX_RPATH + cmake_policy(SET CMP0042 OLD) +endif() +set(CMAKE_AUTOMOC OFF) + +aux_source_directory(. SRC_LIST) + +include_directories(BEFORE ${JSONCPP_INCLUDE_DIRS}) +include_directories(BEFORE ..) +include_directories(${MHD_INCLUDE_DIRS}) +include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) +include_directories(${LEVELDB_INCLUDE_DIRS}) +include_directories(${Boost_INCLUDE_DIRS}) + +set(EXECUTABLE testutils) + +file(GLOB HEADERS "*.h") + +if (ETH_STATIC) + add_library(${EXECUTABLE} STATIC ${SRC_LIST} ${HEADERS}) +else() + add_library(${EXECUTABLE} SHARED ${SRC_LIST} ${HEADERS}) +endif() + +target_link_libraries(${EXECUTABLE} ${LEVELDB_LIBRARIES}) +target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES}) +target_link_libraries(${EXECUTABLE} ${JSON_RPC_CPP_SERVER_LIBRARIES}) +target_link_libraries(${EXECUTABLE} ${MHD_LIBRARIES}) + +target_link_libraries(${EXECUTABLE} webthree) +target_link_libraries(${EXECUTABLE} secp256k1) +target_link_libraries(${EXECUTABLE} solidity) + +if (NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")) + target_link_libraries(${EXECUTABLE} serpent) +endif() + +install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) +install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) + diff --git a/libtestutils/Common.cpp b/libtestutils/Common.cpp new file mode 100644 index 000000000..f15a2da12 --- /dev/null +++ b/libtestutils/Common.cpp @@ -0,0 +1,80 @@ +/* + 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 . + */ +/** @file Common.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include +#include +#include +#include +#include "Common.h" + +using namespace std; +using namespace dev; +using namespace dev::test; + +std::string dev::test::getTestPath() +{ + string testPath; + const char* ptestPath = getenv("ETHEREUM_TEST_PATH"); + + if (ptestPath == NULL) + { + ctest << " could not find environment variable ETHEREUM_TEST_PATH \n"; + testPath = "../../../tests"; + } + else + testPath = ptestPath; + + return testPath; +} + +int dev::test::randomNumber() +{ + static std::mt19937 randomGenerator(time(0)); + randomGenerator.seed(std::random_device()()); + return std::uniform_int_distribution(1)(randomGenerator); +} + +Json::Value dev::test::loadJsonFromFile(std::string const& _path) +{ + Json::Reader reader; + Json::Value result; + string s = asString(dev::contents(_path)); + if (!s.length()) + ctest << "Contents of " + _path + " is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?"; + else + ctest << "FIXTURE: loaded test from file: " << _path; + + reader.parse(s, result); + return result; +} + +std::string dev::test::toTestFilePath(std::string const& _filename) +{ + return getTestPath() + "/" + _filename + ".json"; +} + +std::string dev::test::getRandomPath() +{ + std::stringstream stream; + stream << getDataDir() << "/EthereumTests/" << randomNumber(); + return stream.str(); +} + diff --git a/libtestutils/Common.h b/libtestutils/Common.h new file mode 100644 index 000000000..4757a3b7a --- /dev/null +++ b/libtestutils/Common.h @@ -0,0 +1,44 @@ +/* + 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 . + */ +/** @file Common.h + * @author Marek Kotewicz + * @date 2015 + */ + +#pragma once + +#include +#include +#include + +namespace dev +{ +namespace test +{ + +struct TestChannel: public LogChannel { static const char* name() { return "TEST"; } }; +#define ctest dev::LogOutputStream() + +std::string getTestPath(); +int randomNumber(); +Json::Value loadJsonFromFile(std::string const& _path); +std::string toTestFilePath(std::string const& _filename); +std::string getRandomPath(); + +} + +} diff --git a/libtestutils/FixedClient.cpp b/libtestutils/FixedClient.cpp new file mode 100644 index 000000000..a58d4ae0e --- /dev/null +++ b/libtestutils/FixedClient.cpp @@ -0,0 +1,41 @@ +/* + 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 . + */ +/** @file FixedClient.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include "FixedClient.h" + +using namespace dev; +using namespace dev::eth; +using namespace dev::test; + +eth::State FixedClient::asOf(BlockNumber _h) const +{ + ReadGuard l(x_stateDB); + if (_h == PendingBlock || _h == LatestBlock) + return m_state; + + return State(m_state.db(), bc(), bc().numberHash(_h)); +} + +eth::State FixedClient::asOf(h256 _h) const +{ + ReadGuard l(x_stateDB); + return State(m_state.db(), bc(), _h); +} diff --git a/libtestutils/FixedClient.h b/libtestutils/FixedClient.h new file mode 100644 index 000000000..ce628c651 --- /dev/null +++ b/libtestutils/FixedClient.h @@ -0,0 +1,55 @@ +/* + 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 . + */ +/** @file FixedClient.h + * @author Marek Kotewicz + * @date 2015 + */ + +#pragma once + +#include +#include +#include + +namespace dev +{ +namespace test +{ + +class FixedClient: public dev::eth::ClientBase +{ +public: + FixedClient(eth::BlockChain const& _bc, eth::State _state) : m_bc(_bc), m_state(_state) {} + virtual ~FixedClient() {} + + // stub + virtual void flushTransactions() override {} + virtual eth::BlockChain const& bc() const override { return m_bc; } + virtual eth::State asOf(eth::BlockNumber _h) const override; + virtual eth::State asOf(h256 _h) const override; + virtual eth::State preMine() const override { ReadGuard l(x_stateDB); return m_state; } + virtual eth::State postMine() const override { ReadGuard l(x_stateDB); return m_state; } + virtual void prepareForTransaction() override {} + +private: + eth::BlockChain const& m_bc; + eth::State m_state; + mutable SharedMutex x_stateDB; ///< Lock on the state DB, effectively a lock on m_postMine. +}; + +} +} diff --git a/libtestutils/FixedWebThreeServer.cpp b/libtestutils/FixedWebThreeServer.cpp new file mode 100644 index 000000000..6a1b13b11 --- /dev/null +++ b/libtestutils/FixedWebThreeServer.cpp @@ -0,0 +1,2 @@ + +#include "FixedWebThreeServer.h" diff --git a/libtestutils/FixedWebThreeServer.h b/libtestutils/FixedWebThreeServer.h new file mode 100644 index 000000000..aab12c461 --- /dev/null +++ b/libtestutils/FixedWebThreeServer.h @@ -0,0 +1,32 @@ + + +#pragma once + +#include +#include + +class FixedWebThreeServer: public dev::WebThreeStubServerBase, public dev::WebThreeStubDatabaseFace +{ +public: + FixedWebThreeServer(jsonrpc::AbstractServerConnector& _conn, std::vector const& _accounts, dev::eth::Interface* _client): WebThreeStubServerBase(_conn, _accounts), m_client(_client) {}; + +private: + dev::eth::Interface* client() override { return m_client; } + std::shared_ptr face() override { BOOST_THROW_EXCEPTION(dev::InterfaceNotSupported("dev::shh::Interface")); } + dev::WebThreeNetworkFace* network() override { BOOST_THROW_EXCEPTION(dev::InterfaceNotSupported("dev::WebThreeNetworkFace")); } + dev::WebThreeStubDatabaseFace* db() override { return this; } + std::string get(std::string const& _name, std::string const& _key) override + { + std::string k(_name + "/" + _key); + return m_db[k]; + } + void put(std::string const& _name, std::string const& _key, std::string const& _value) override + { + std::string k(_name + "/" + _key); + m_db[k] = _value; + } + +private: + dev::eth::Interface* m_client; + std::map m_db; +}; diff --git a/libtestutils/ShortLivingDirectory.cpp b/libtestutils/ShortLivingDirectory.cpp new file mode 100644 index 000000000..56fc101a7 --- /dev/null +++ b/libtestutils/ShortLivingDirectory.cpp @@ -0,0 +1,44 @@ +/* + 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 . + */ +/** @file ShortLivingDirectory.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include +#include "ShortLivingDirectory.h" +#include "Common.h" + +using namespace std; +using namespace dev; +using namespace dev::test; + +ShortLivingDirectory::ShortLivingDirectory() +{ + m_path = getRandomPath(); + boost::filesystem::create_directories(m_path); +} + +ShortLivingDirectory::ShortLivingDirectory(std::string const& _path) : m_path(_path) +{ + boost::filesystem::create_directories(m_path); +} + +ShortLivingDirectory::~ShortLivingDirectory() +{ + boost::filesystem::remove_all(m_path); +} diff --git a/libtestutils/ShortLivingDirectory.h b/libtestutils/ShortLivingDirectory.h new file mode 100644 index 000000000..6f572c2bc --- /dev/null +++ b/libtestutils/ShortLivingDirectory.h @@ -0,0 +1,45 @@ +/* + 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 . + */ +/** @file ShortLivingDirectory.h + * @author Marek Kotewicz + * @date 2015 + */ + +#pragma once + +#include + +namespace dev +{ +namespace test +{ + +class ShortLivingDirectory +{ +public: + ShortLivingDirectory(); + ShortLivingDirectory(std::string const& _path); + ~ShortLivingDirectory(); + + std::string path(){ return m_path; } + +private: + std::string m_path; +}; + +} +} diff --git a/libtestutils/StateLoader.cpp b/libtestutils/StateLoader.cpp new file mode 100644 index 000000000..0561e40f1 --- /dev/null +++ b/libtestutils/StateLoader.cpp @@ -0,0 +1,57 @@ +/* + 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 . + */ +/** @file StateLoader.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include "StateLoader.h" + +using namespace std; +using namespace dev; +using namespace dev::eth; +using namespace dev::test; + +StateLoader::StateLoader(Json::Value const& _json) +: m_state(Address(), OverlayDB(), BaseState::Empty) +{ + for (string const& name: _json.getMemberNames()) + { + Json::Value o = _json[name]; + + Address address = Address(name); + bytes code = fromHex(o["code"].asString().substr(2)); + + if (code.size()) + { + m_state.m_cache[address] = Account(u256(o["balance"].asString()), Account::ContractConception); + m_state.m_cache[address].setCode(code); + } + else + m_state.m_cache[address] = Account(u256(o["balance"].asString()), Account::NormalCreation); + + for (string const& j: o["storage"].getMemberNames()) + m_state.setStorage(address, u256(j), u256(o["storage"][j].asString())); + + for (auto i = 0; i < u256(o["nonce"].asString()); ++i) + m_state.noteSending(address); + + m_state.ensureCached(address, false, false); + } + + m_state.commit(); +} diff --git a/libtestutils/StateLoader.h b/libtestutils/StateLoader.h new file mode 100644 index 000000000..90f806714 --- /dev/null +++ b/libtestutils/StateLoader.h @@ -0,0 +1,42 @@ +/* + 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 . + */ +/** @file StateLoader.h + * @author Marek Kotewicz + * @date 2015 + */ + +#pragma once + +#include +#include + +namespace dev +{ +namespace test +{ + +class StateLoader +{ +public: + StateLoader(Json::Value const& _json); + eth::State state() { return m_state; } + +private: + eth::State m_state; +}; +} +} From e78b641290963eac4462d46a41b5eb90dfa79764 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 13:37:43 +0100 Subject: [PATCH 02/10] fixed link dependencies for libtestutils --- libtestutils/CMakeLists.txt | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/libtestutils/CMakeLists.txt b/libtestutils/CMakeLists.txt index f79daa47c..202b9823c 100644 --- a/libtestutils/CMakeLists.txt +++ b/libtestutils/CMakeLists.txt @@ -11,9 +11,7 @@ aux_source_directory(. SRC_LIST) include_directories(BEFORE ${JSONCPP_INCLUDE_DIRS}) include_directories(BEFORE ..) -include_directories(${MHD_INCLUDE_DIRS}) include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) -include_directories(${LEVELDB_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS}) set(EXECUTABLE testutils) @@ -26,18 +24,10 @@ else() add_library(${EXECUTABLE} SHARED ${SRC_LIST} ${HEADERS}) endif() -target_link_libraries(${EXECUTABLE} ${LEVELDB_LIBRARIES}) +target_link_libraries(${EXECUTABLE} ${Boost_FILESYSTEM_LIBRARIES}) target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES}) -target_link_libraries(${EXECUTABLE} ${JSON_RPC_CPP_SERVER_LIBRARIES}) -target_link_libraries(${EXECUTABLE} ${MHD_LIBRARIES}) - -target_link_libraries(${EXECUTABLE} webthree) -target_link_libraries(${EXECUTABLE} secp256k1) -target_link_libraries(${EXECUTABLE} solidity) - -if (NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")) - target_link_libraries(${EXECUTABLE} serpent) -endif() +target_link_libraries(${EXECUTABLE} ethereum) +target_link_libraries(${EXECUTABLE} web3jsonrpc) install( TARGETS ${EXECUTABLE} RUNTIME DESTINATION bin ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) From 77b8e7796ac1fb4ef1cf1a19f17da80dbb2dd454 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 13:47:10 +0100 Subject: [PATCH 03/10] added missing headers --- libtestutils/FixedWebThreeServer.cpp | 20 ++++++++++++++++++++ libtestutils/FixedWebThreeServer.h | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/libtestutils/FixedWebThreeServer.cpp b/libtestutils/FixedWebThreeServer.cpp index 6a1b13b11..c72a106c6 100644 --- a/libtestutils/FixedWebThreeServer.cpp +++ b/libtestutils/FixedWebThreeServer.cpp @@ -1,2 +1,22 @@ +/* + 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 . + */ +/** @file FixedWebThreeStubServer.cpp + * @author Marek Kotewicz + * @date 2015 + */ #include "FixedWebThreeServer.h" diff --git a/libtestutils/FixedWebThreeServer.h b/libtestutils/FixedWebThreeServer.h index aab12c461..53db2f2b2 100644 --- a/libtestutils/FixedWebThreeServer.h +++ b/libtestutils/FixedWebThreeServer.h @@ -1,4 +1,23 @@ +/* + 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 . + */ +/** @file FixedWebThreeStubServer.h + * @author Marek Kotewicz + * @date 2015 + */ #pragma once From 2a892a60da6f99dfa94625dcbbc666e9cade4da7 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 13:57:07 +0100 Subject: [PATCH 04/10] pr fixes for ShortLivingDirectory --- libtestutils/ShortLivingDirectory.cpp | 13 ++++++------- libtestutils/ShortLivingDirectory.h | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/libtestutils/ShortLivingDirectory.cpp b/libtestutils/ShortLivingDirectory.cpp index 56fc101a7..48e1d643d 100644 --- a/libtestutils/ShortLivingDirectory.cpp +++ b/libtestutils/ShortLivingDirectory.cpp @@ -20,21 +20,20 @@ */ #include +#include #include "ShortLivingDirectory.h" -#include "Common.h" using namespace std; using namespace dev; using namespace dev::test; -ShortLivingDirectory::ShortLivingDirectory() -{ - m_path = getRandomPath(); - boost::filesystem::create_directories(m_path); -} - ShortLivingDirectory::ShortLivingDirectory(std::string const& _path) : m_path(_path) { + // we never ever want to delete a directory (including all its contents) that we did not create ourselves. + if (boost::filesystem::exists(m_path)) { + BOOST_THROW_EXCEPTION(FileError()); + } + boost::filesystem::create_directories(m_path); } diff --git a/libtestutils/ShortLivingDirectory.h b/libtestutils/ShortLivingDirectory.h index 6f572c2bc..434120e04 100644 --- a/libtestutils/ShortLivingDirectory.h +++ b/libtestutils/ShortLivingDirectory.h @@ -22,6 +22,7 @@ #pragma once #include +#include "Common.h" namespace dev { @@ -31,11 +32,10 @@ namespace test class ShortLivingDirectory { public: - ShortLivingDirectory(); - ShortLivingDirectory(std::string const& _path); + ShortLivingDirectory(std::string const& _path = getRandomPath()); ~ShortLivingDirectory(); - std::string path(){ return m_path; } + std::string const& path() { return m_path; } private: std::string m_path; From 42b3160f13d8c50de0a8fbac2dbaaaa4df37464d Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 13:59:02 +0100 Subject: [PATCH 05/10] pr fixes for StateLoader --- libtestutils/StateLoader.cpp | 3 +-- libtestutils/StateLoader.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/libtestutils/StateLoader.cpp b/libtestutils/StateLoader.cpp index 0561e40f1..b56475b5a 100644 --- a/libtestutils/StateLoader.cpp +++ b/libtestutils/StateLoader.cpp @@ -26,8 +26,7 @@ using namespace dev; using namespace dev::eth; using namespace dev::test; -StateLoader::StateLoader(Json::Value const& _json) -: m_state(Address(), OverlayDB(), BaseState::Empty) +StateLoader::StateLoader(Json::Value const& _json) : m_state(Address(), OverlayDB(), BaseState::Empty) { for (string const& name: _json.getMemberNames()) { diff --git a/libtestutils/StateLoader.h b/libtestutils/StateLoader.h index 90f806714..10d7251cc 100644 --- a/libtestutils/StateLoader.h +++ b/libtestutils/StateLoader.h @@ -33,7 +33,7 @@ class StateLoader { public: StateLoader(Json::Value const& _json); - eth::State state() { return m_state; } + eth::State const& state() { return m_state; } private: eth::State m_state; From 14115d96d57b8d469151f0ed60aa6fe5188d8bba Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 14:19:09 +0100 Subject: [PATCH 06/10] brief docs for libtestutils classes --- libtestutils/BlockChainLoader.h | 3 ++- libtestutils/FixedClient.h | 4 ++++ libtestutils/FixedWebThreeServer.h | 6 ++++++ libtestutils/ShortLivingDirectory.h | 5 +++++ libtestutils/StateLoader.h | 3 +++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libtestutils/BlockChainLoader.h b/libtestutils/BlockChainLoader.h index a3528045c..ed110ff6f 100644 --- a/libtestutils/BlockChainLoader.h +++ b/libtestutils/BlockChainLoader.h @@ -32,7 +32,8 @@ namespace test { /** - * @brief - loads the blockchain from json, creates temporary directory to store it, removes this temporary directory on dealloc + * @brief Should be used to load test blockchain from json file + * Loads the blockchain from json, creates temporary directory to store it, removes the directory on dealloc */ class BlockChainLoader { diff --git a/libtestutils/FixedClient.h b/libtestutils/FixedClient.h index ce628c651..f0a7c54f6 100644 --- a/libtestutils/FixedClient.h +++ b/libtestutils/FixedClient.h @@ -30,6 +30,10 @@ namespace dev namespace test { +/** + * @brief mvp implementation of ClientBase + * Doesn't support mining interface + */ class FixedClient: public dev::eth::ClientBase { public: diff --git a/libtestutils/FixedWebThreeServer.h b/libtestutils/FixedWebThreeServer.h index 53db2f2b2..33ccbf71e 100644 --- a/libtestutils/FixedWebThreeServer.h +++ b/libtestutils/FixedWebThreeServer.h @@ -24,6 +24,12 @@ #include #include +/** + * @brief dummy JSON-RPC api implementation + * Should be used for test purposes only + * Supports eth && db interfaces + * Doesn't support shh && net interfaces + */ class FixedWebThreeServer: public dev::WebThreeStubServerBase, public dev::WebThreeStubDatabaseFace { public: diff --git a/libtestutils/ShortLivingDirectory.h b/libtestutils/ShortLivingDirectory.h index 434120e04..bab6a1c20 100644 --- a/libtestutils/ShortLivingDirectory.h +++ b/libtestutils/ShortLivingDirectory.h @@ -29,6 +29,11 @@ namespace dev namespace test { +/** + * @brief temporary directory implementation + * It creates temporary directory in the given path. On dealloc it removes the directory + * @throws if the given path already exists, throws an exception + */ class ShortLivingDirectory { public: diff --git a/libtestutils/StateLoader.h b/libtestutils/StateLoader.h index 10d7251cc..e8f955440 100644 --- a/libtestutils/StateLoader.h +++ b/libtestutils/StateLoader.h @@ -29,6 +29,9 @@ namespace dev namespace test { +/** + * @brief Friend of State, loads State from given JSON object + */ class StateLoader { public: From c226dc2451ef4f4571b4fbabde35c237758b6861 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 16:05:50 +0100 Subject: [PATCH 07/10] fixed bracers --- libtestutils/ShortLivingDirectory.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libtestutils/ShortLivingDirectory.cpp b/libtestutils/ShortLivingDirectory.cpp index 48e1d643d..f60e12a86 100644 --- a/libtestutils/ShortLivingDirectory.cpp +++ b/libtestutils/ShortLivingDirectory.cpp @@ -30,9 +30,8 @@ using namespace dev::test; ShortLivingDirectory::ShortLivingDirectory(std::string const& _path) : m_path(_path) { // we never ever want to delete a directory (including all its contents) that we did not create ourselves. - if (boost::filesystem::exists(m_path)) { + if (boost::filesystem::exists(m_path)) BOOST_THROW_EXCEPTION(FileError()); - } boost::filesystem::create_directories(m_path); } From e9b968b1b0b2d3a04ab8de80cf2a893dc0ab9123 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 19:30:52 +0100 Subject: [PATCH 08/10] fixed FixedClient --- libtestutils/FixedClient.cpp | 11 +---------- libtestutils/FixedClient.h | 4 ++-- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/libtestutils/FixedClient.cpp b/libtestutils/FixedClient.cpp index a58d4ae0e..052141039 100644 --- a/libtestutils/FixedClient.cpp +++ b/libtestutils/FixedClient.cpp @@ -25,16 +25,7 @@ using namespace dev; using namespace dev::eth; using namespace dev::test; -eth::State FixedClient::asOf(BlockNumber _h) const -{ - ReadGuard l(x_stateDB); - if (_h == PendingBlock || _h == LatestBlock) - return m_state; - - return State(m_state.db(), bc(), bc().numberHash(_h)); -} - -eth::State FixedClient::asOf(h256 _h) const +eth::State FixedClient::asOf(h256 const& _h) const { ReadGuard l(x_stateDB); return State(m_state.db(), bc(), _h); diff --git a/libtestutils/FixedClient.h b/libtestutils/FixedClient.h index f0a7c54f6..daca444fb 100644 --- a/libtestutils/FixedClient.h +++ b/libtestutils/FixedClient.h @@ -43,8 +43,8 @@ public: // stub virtual void flushTransactions() override {} virtual eth::BlockChain const& bc() const override { return m_bc; } - virtual eth::State asOf(eth::BlockNumber _h) const override; - virtual eth::State asOf(h256 _h) const override; + using ClientBase::asOf; + virtual eth::State asOf(h256 const& _h) const override; virtual eth::State preMine() const override { ReadGuard l(x_stateDB); return m_state; } virtual eth::State postMine() const override { ReadGuard l(x_stateDB); return m_state; } virtual void prepareForTransaction() override {} From 701df0bf55629740d3522eecd54e8b33cc6a6c4d Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 19:36:07 +0100 Subject: [PATCH 09/10] const --- libtestutils/BlockChainLoader.cpp | 1 - libtestutils/BlockChainLoader.h | 6 +++--- libtestutils/ShortLivingDirectory.h | 2 +- libtestutils/StateLoader.h | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/libtestutils/BlockChainLoader.cpp b/libtestutils/BlockChainLoader.cpp index 224e40ec5..eafab190e 100644 --- a/libtestutils/BlockChainLoader.cpp +++ b/libtestutils/BlockChainLoader.cpp @@ -34,7 +34,6 @@ namespace test { dev::eth::BlockInfo toBlockInfo(Json::Value const& _json); bytes toGenesisBlock(Json::Value const& _json); - } } diff --git a/libtestutils/BlockChainLoader.h b/libtestutils/BlockChainLoader.h index ed110ff6f..728860727 100644 --- a/libtestutils/BlockChainLoader.h +++ b/libtestutils/BlockChainLoader.h @@ -39,9 +39,9 @@ class BlockChainLoader { public: BlockChainLoader(Json::Value const& _json); - eth::BlockChain& bc() { return *m_bc; } - eth::State state() { return m_state; } - + eth::BlockChain const& bc() const { return *m_bc; } + eth::State const& state() const { return m_state; } + private: ShortLivingDirectory m_dir; std::auto_ptr m_bc; diff --git a/libtestutils/ShortLivingDirectory.h b/libtestutils/ShortLivingDirectory.h index bab6a1c20..10b542f06 100644 --- a/libtestutils/ShortLivingDirectory.h +++ b/libtestutils/ShortLivingDirectory.h @@ -40,7 +40,7 @@ public: ShortLivingDirectory(std::string const& _path = getRandomPath()); ~ShortLivingDirectory(); - std::string const& path() { return m_path; } + std::string const& path() const { return m_path; } private: std::string m_path; diff --git a/libtestutils/StateLoader.h b/libtestutils/StateLoader.h index e8f955440..e5843d0b4 100644 --- a/libtestutils/StateLoader.h +++ b/libtestutils/StateLoader.h @@ -36,7 +36,7 @@ class StateLoader { public: StateLoader(Json::Value const& _json); - eth::State const& state() { return m_state; } + eth::State const& state() const { return m_state; } private: eth::State m_state; From c12aa4decde863898119ac77c42e984c6c012c47 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 19:41:17 +0100 Subject: [PATCH 10/10] TransientDirectory --- libtestutils/BlockChainLoader.h | 4 ++-- .../{ShortLivingDirectory.cpp => TransientDirectory.cpp} | 8 ++++---- .../{ShortLivingDirectory.h => TransientDirectory.h} | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) rename libtestutils/{ShortLivingDirectory.cpp => TransientDirectory.cpp} (85%) rename libtestutils/{ShortLivingDirectory.h => TransientDirectory.h} (88%) diff --git a/libtestutils/BlockChainLoader.h b/libtestutils/BlockChainLoader.h index 728860727..6cb04c53c 100644 --- a/libtestutils/BlockChainLoader.h +++ b/libtestutils/BlockChainLoader.h @@ -24,7 +24,7 @@ #include #include #include -#include "ShortLivingDirectory.h" +#include "TransientDirectory.h" namespace dev { @@ -43,7 +43,7 @@ public: eth::State const& state() const { return m_state; } private: - ShortLivingDirectory m_dir; + TransientDirectory m_dir; std::auto_ptr m_bc; eth::State m_state; }; diff --git a/libtestutils/ShortLivingDirectory.cpp b/libtestutils/TransientDirectory.cpp similarity index 85% rename from libtestutils/ShortLivingDirectory.cpp rename to libtestutils/TransientDirectory.cpp index f60e12a86..48beca7e3 100644 --- a/libtestutils/ShortLivingDirectory.cpp +++ b/libtestutils/TransientDirectory.cpp @@ -14,20 +14,20 @@ You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ -/** @file ShortLivingDirectory.cpp +/** @file TransientDirectory.cpp * @author Marek Kotewicz * @date 2015 */ #include #include -#include "ShortLivingDirectory.h" +#include "TransientDirectory.h" using namespace std; using namespace dev; using namespace dev::test; -ShortLivingDirectory::ShortLivingDirectory(std::string const& _path) : m_path(_path) +TransientDirectory::TransientDirectory(std::string const& _path) : m_path(_path) { // we never ever want to delete a directory (including all its contents) that we did not create ourselves. if (boost::filesystem::exists(m_path)) @@ -36,7 +36,7 @@ ShortLivingDirectory::ShortLivingDirectory(std::string const& _path) : m_path(_p boost::filesystem::create_directories(m_path); } -ShortLivingDirectory::~ShortLivingDirectory() +TransientDirectory::~TransientDirectory() { boost::filesystem::remove_all(m_path); } diff --git a/libtestutils/ShortLivingDirectory.h b/libtestutils/TransientDirectory.h similarity index 88% rename from libtestutils/ShortLivingDirectory.h rename to libtestutils/TransientDirectory.h index 10b542f06..328b4c92b 100644 --- a/libtestutils/ShortLivingDirectory.h +++ b/libtestutils/TransientDirectory.h @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ -/** @file ShortLivingDirectory.h +/** @file TransientDirectory.h * @author Marek Kotewicz * @date 2015 */ @@ -34,11 +34,11 @@ namespace test * It creates temporary directory in the given path. On dealloc it removes the directory * @throws if the given path already exists, throws an exception */ -class ShortLivingDirectory +class TransientDirectory { public: - ShortLivingDirectory(std::string const& _path = getRandomPath()); - ~ShortLivingDirectory(); + TransientDirectory(std::string const& _path = getRandomPath()); + ~TransientDirectory(); std::string const& path() const { return m_path; }