From f69dd0a4ba29e88b82508c7e724b7c8bc3076496 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 25 Mar 2015 10:41:01 +0100 Subject: [PATCH] ethrpctest executable --- CMakeLists.txt | 1 + ethrpctest/CMakeLists.txt | 35 ++++++++ ethrpctest/CommandLineInterface.cpp | 129 ++++++++++++++++++++++++++++ ethrpctest/CommandLineInterface.h | 46 ++++++++++ ethrpctest/main.cpp | 34 ++++++++ 5 files changed, 245 insertions(+) create mode 100644 ethrpctest/CMakeLists.txt create mode 100644 ethrpctest/CommandLineInterface.cpp create mode 100644 ethrpctest/CommandLineInterface.h create mode 100644 ethrpctest/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 70cd3cb66..59fdde07e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -178,6 +178,7 @@ endif() if (JSONRPC) add_subdirectory(libweb3jsonrpc) + add_subdirectory(ethrpctest) endif() add_subdirectory(secp256k1) diff --git a/ethrpctest/CMakeLists.txt b/ethrpctest/CMakeLists.txt new file mode 100644 index 000000000..5d3fef542 --- /dev/null +++ b/ethrpctest/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_policy(SET CMP0015 NEW) +set(CMAKE_AUTOMOC OFF) + +aux_source_directory(. SRC_LIST) + +include_directories(BEFORE ${JSONCPP_INCLUDE_DIRS}) +include_directories(BEFORE ..) +include_directories(${Boost_INCLUDE_DIRS}) +include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) + +set(EXECUTABLE ethrpctest) + +file(GLOB HEADERS "*.h") + +add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) + +add_dependencies(${EXECUTABLE} BuildInfo.h) + +target_link_libraries(${EXECUTABLE} ${Boost_REGEX_LIBRARIES}) + +if (READLINE_FOUND) + target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES}) +endif() + +target_link_libraries(${EXECUTABLE} ${Boost_FILESYSTEM_LIBRARIES}) +target_link_libraries(${EXECUTABLE} ${Boost_PROGRAM_OPTIONS_LIBRARIES}) +target_link_libraries(${EXECUTABLE} testutils) +target_link_libraries(${EXECUTABLE} web3jsonrpc) + +if (DEFINED WIN32 AND NOT DEFINED CMAKE_COMPILER_IS_MINGW) + add_custom_command(TARGET ${EXECUTABLE} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy ${MHD_DLL_RELEASE} "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}") +endif() + +install( TARGETS ${EXECUTABLE} DESTINATION bin ) + diff --git a/ethrpctest/CommandLineInterface.cpp b/ethrpctest/CommandLineInterface.cpp new file mode 100644 index 000000000..c4b3d8f27 --- /dev/null +++ b/ethrpctest/CommandLineInterface.cpp @@ -0,0 +1,129 @@ +/* + 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 CommandLineInterface.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "CommandLineInterface.h" +#include "BuildInfo.h" + +using namespace std; +using namespace dev; +using namespace dev::eth; +using namespace dev::test; +namespace po = boost::program_options; + +bool CommandLineInterface::parseArguments(int argc, char** argv) +{ + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("json", po::value>()->required(), "input file") + ("test", po::value>()->required(), "test case name"); + + // All positional options should be interpreted as input files + po::positional_options_description p; + + // parse the compiler arguments + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); + + if (m_args.count("help")) + { + cout << desc; + return false; + } + + po::notify(m_args); + } + catch (po::error const& _exception) + { + cout << _exception.what() << endl; + return false; + } + + return true; +} + +bool CommandLineInterface::processInput() +{ + string infile = m_args["json"].as>()[0]; + + auto path = boost::filesystem::path(infile); + if (!boost::filesystem::exists(path)) + { + cout << "Non existant input file \"" << infile << "\"" << endl; + return false; + } + + string test = m_args["test"].as>()[0]; + Json::Value j = dev::test::loadJsonFromFile(path.string()); + + if (j[test].empty()) + { + cout << "Non existant test case \"" << infile << "\"" << endl; + return false; + } + + if (!j[test].isObject()) + { + cout << "Incorrect JSON file \"" << infile << "\"" << endl; + return false; + } + + m_json = j[test]; + + return true; +} + +bool g_exit = false; + +void sighandler(int) +{ + g_exit = true; +} + +void CommandLineInterface::actOnInput() +{ + BlockChainLoader bcl(m_json); + FixedClient client(bcl.bc(), bcl.state()); + unique_ptr jsonrpcServer; + auto server = new jsonrpc::HttpServer(8080, "", "", 2); + jsonrpcServer.reset(new FixedWebThreeServer(*server, {}, &client)); + jsonrpcServer->StartListening(); + + signal(SIGABRT, &sighandler); + signal(SIGTERM, &sighandler); + signal(SIGINT, &sighandler); + + while (!g_exit) + this_thread::sleep_for(chrono::milliseconds(1000)); +} diff --git a/ethrpctest/CommandLineInterface.h b/ethrpctest/CommandLineInterface.h new file mode 100644 index 000000000..b57e1df5e --- /dev/null +++ b/ethrpctest/CommandLineInterface.h @@ -0,0 +1,46 @@ +/* + 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 . +*/ +/** CommandLineInterface.h + * @author Marek Kotewicz + * @date 2015 + */ +#pragma once + +#include +#include + +class CommandLineInterface +{ +public: + CommandLineInterface() {} + + /// Parse command line arguments and return false if we should not continue + bool parseArguments(int argc, char** argv); + /// Parse input file and check if test exists + bool processInput(); + /// Start FixedJsonRpcServer + void actOnInput(); + +private: + + /// Compiler arguments variable map + boost::program_options::variables_map m_args; + + /// loaded json test case + Json::Value m_json; +}; + diff --git a/ethrpctest/main.cpp b/ethrpctest/main.cpp new file mode 100644 index 000000000..3d710dd5b --- /dev/null +++ b/ethrpctest/main.cpp @@ -0,0 +1,34 @@ +/* + 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 . +*/ +/** main.cpp + * @author Marek Kotewicz + * @date 2015 + */ + +#include "CommandLineInterface.h" + +int main(int argc, char** argv) +{ + CommandLineInterface cli; + if (!cli.parseArguments(argc, argv)) + return 1; + if (!cli.processInput()) + return 1; + cli.actOnInput(); + + return 0; +}