diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | jsonrpc.cpp | 254 | ||||
-rw-r--r-- | webthreestubclient.h | 399 |
3 files changed, 655 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6aa99b61..9d5e6691 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ target_link_libraries(testeth secp256k1) target_link_libraries(testeth gmp) target_link_libraries(testeth solidity) target_link_libraries(testeth ${CRYPTOPP_LS}) +target_link_libraries(testeth webthree) +target_link_libraries(testeth ethrpc) target_link_libraries(createRandomTest ethereum) target_link_libraries(createRandomTest ethcore) diff --git a/jsonrpc.cpp b/jsonrpc.cpp new file mode 100644 index 00000000..ff2e3a5b --- /dev/null +++ b/jsonrpc.cpp @@ -0,0 +1,254 @@ +/* + 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 jsonrpc.cpp + * @author Marek Kotewicz <marek@ethdev.com> + * @date 2014 + */ + +#if 1 + +#include <boost/test/unit_test.hpp> +#include <boost/lexical_cast.hpp> +#include <libdevcore/Log.h> +#include <libdevcore/CommonIO.h> +#include <libdevcore/CommonJS.h> +#include <libwebthree/WebThree.h> +#include <libethrpc/WebThreeStubServer.h> +#include <libethrpc/CorsHttpServer.h> +#include <jsonrpc/connectors/httpserver.h> +#include <jsonrpc/connectors/httpclient.h> +#include <set> +#include "JsonSpiritHeaders.h" +#include "TestHelper.h" +#include "webthreestubclient.h" + +using namespace std; +using namespace dev; +using namespace dev::eth; +namespace js = json_spirit; + +namespace jsonrpc_tests +{ + +string name = "Ethereum(++) tests"; +string dbPath; +auto s = set<string>{"eth", "shh"}; +dev::p2p::NetworkPreferences np(30303, std::string(), false); +dev::WebThreeDirect web3(name, dbPath, true, s, np); + +auto_ptr<WebThreeStubServer> jsonrpcServer; +auto_ptr<WebThreeStubClient> jsonrpcClient; + +struct JsonrpcFixture { + JsonrpcFixture() + { + cnote << "setup jsonrpc"; + + web3.setIdealPeerCount(5); + web3.ethereum()->setForceMining(true); + jsonrpcServer = auto_ptr<WebThreeStubServer>(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {})); + jsonrpcServer->StartListening(); + + jsonrpcClient = auto_ptr<WebThreeStubClient>(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080"))); + } + ~JsonrpcFixture() + { + cnote << "teardown jsonrpc"; + } +}; + +BOOST_GLOBAL_FIXTURE(JsonrpcFixture) + +BOOST_AUTO_TEST_CASE(jsonrpc_balanceAt) +{ + cnote << "Testing jsonrpc balanceAt..."; + dev::KeyPair key = KeyPair::create(); + auto address = key.address(); + string balance = jsonrpcClient->balanceAt(toJS(address)); + BOOST_CHECK_EQUAL(toJS(web3.ethereum()->balanceAt(address)), balance); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_call) +{ +} + +BOOST_AUTO_TEST_CASE(jsonrpc_coinbase) +{ + cnote << "Testing jsonrpc coinbase..."; + string coinbase = jsonrpcClient->coinbase(); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address()); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_countAt) +{ + cnote << "Testing jsonrpc countAt..."; + dev::KeyPair key = KeyPair::create(); + auto address = key.address(); + double countAt = jsonrpcClient->countAt(toJS(address)); + BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address, 0)); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock) +{ + cnote << "Testing jsonrpc defaultBlock..."; + int defaultBlock = jsonrpcClient->defaultBlock(); + BOOST_CHECK_EQUAL(defaultBlock, web3.ethereum()->getDefault()); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice) +{ + cnote << "Testing jsonrpc gasPrice..."; + string gasPrice = jsonrpcClient->gasPrice(); + BOOST_CHECK_EQUAL(gasPrice, toJS(10 * dev::eth::szabo)); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_isListening) +{ + cnote << "Testing jsonrpc isListening..."; + + web3.startNetwork(); + bool listeningOn = jsonrpcClient->listening(); + BOOST_CHECK_EQUAL(listeningOn, web3.isNetworkStarted()); + + web3.stopNetwork(); + bool listeningOff = jsonrpcClient->listening(); + BOOST_CHECK_EQUAL(listeningOff, web3.isNetworkStarted()); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_isMining) +{ + cnote << "Testing jsonrpc isMining..."; + + web3.ethereum()->startMining(); + bool miningOn = jsonrpcClient->mining(); + BOOST_CHECK_EQUAL(miningOn, web3.ethereum()->isMining()); + + web3.ethereum()->stopMining(); + bool miningOff = jsonrpcClient->mining(); + BOOST_CHECK_EQUAL(miningOff, web3.ethereum()->isMining()); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_accounts) +{ + cnote << "Testing jsonrpc accounts..."; + std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()}; + jsonrpcServer->setAccounts(keys); + Json::Value k = jsonrpcClient->accounts(); + jsonrpcServer->setAccounts({}); + BOOST_CHECK_EQUAL(k.isArray(), true); + BOOST_CHECK_EQUAL(k.size(), keys.size()); + for (auto &i:k) + { + auto it = std::find_if(keys.begin(), keys.end(), [i](dev::KeyPair const& keyPair){ + return jsToAddress(i.asString()) == keyPair.address(); + }); + BOOST_CHECK_EQUAL(it != keys.end(), true); + } +} + +BOOST_AUTO_TEST_CASE(jsonrpc_number) +{ + cnote << "Testing jsonrpc number..."; + int number = jsonrpcClient->number(); + BOOST_CHECK_EQUAL(number, web3.ethereum()->number() + 1); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_number2) +{ + cnote << "Testing jsonrpc number2..."; + int number = jsonrpcClient->number(); + BOOST_CHECK_EQUAL(number, web3.ethereum()->number() + 1); + dev::eth::mine(*(web3.ethereum()), 1); + int numberAfter = jsonrpcClient->number(); + BOOST_CHECK_EQUAL(number + 1, numberAfter); + BOOST_CHECK_EQUAL(numberAfter, web3.ethereum()->number() + 1); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_peerCount) +{ + cnote << "Testing jsonrpc peerCount..."; + int peerCount = jsonrpcClient->peerCount(); + BOOST_CHECK_EQUAL(web3.peerCount(), peerCount); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_setListening) +{ + cnote << "Testing jsonrpc setListening..."; + + jsonrpcClient->setListening(true); + BOOST_CHECK_EQUAL(web3.isNetworkStarted(), true); + + jsonrpcClient->setListening(false); + BOOST_CHECK_EQUAL(web3.isNetworkStarted(), false); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_setMining) +{ + cnote << "Testing jsonrpc setMining..."; + + jsonrpcClient->setMining(true); + BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), true); + + jsonrpcClient->setMining(false); + BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), false); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_stateAt) +{ + cnote << "Testing jsonrpc stateAt..."; + dev::KeyPair key = KeyPair::create(); + auto address = key.address(); + string stateAt = jsonrpcClient->stateAt(toJS(address), "0"); + BOOST_CHECK_EQUAL(toJS(web3.ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt); +} + +BOOST_AUTO_TEST_CASE(jsonrpc_transact) +{ + cnote << "Testing jsonrpc transact..."; + dev::KeyPair key = KeyPair::create(); + auto address = key.address(); + auto receiver = KeyPair::create(); + + web3.ethereum()->setAddress(address); + jsonrpcServer->setAccounts({key}); + dev::eth::mine(*(web3.ethereum()), 1); + auto balance = web3.ethereum()->balanceAt(address, 0); + BOOST_REQUIRE(balance > 0); + auto txAmount = balance / 2u; + auto gasPrice = 10 * dev::eth::szabo; + auto gas = dev::eth::c_txGas; + + Json::Value t; + t["from"] = toJS(address); + t["value"] = jsToDecimal(toJS(txAmount)); + t["to"] = toJS(receiver.address()); + t["data"] = toJS(bytes()); + t["gas"] = toJS(gas); + t["gasPrice"] = toJS(gasPrice); + + jsonrpcClient->transact(t); + jsonrpcServer->setAccounts({}); + dev::eth::mine(*(web3.ethereum()), 1); + auto balance2 = web3.ethereum()->balanceAt(receiver.address()); + + BOOST_REQUIRE(balance2 > 0); + BOOST_CHECK_EQUAL(txAmount, balance2); +} + +} + +#endif diff --git a/webthreestubclient.h b/webthreestubclient.h new file mode 100644 index 00000000..4bd8afda --- /dev/null +++ b/webthreestubclient.h @@ -0,0 +1,399 @@ +/** + * THIS FILE IS GENERATED BY jsonrpcstub, DO NOT CHANGE IT!!!!! + */ + +#ifndef _WEBTHREESTUBCLIENT_H_ +#define _WEBTHREESTUBCLIENT_H_ + +#include <jsonrpc/rpc.h> + +class WebThreeStubClient +{ + public: + WebThreeStubClient(jsonrpc::AbstractClientConnector* conn) + { + this->client = new jsonrpc::Client(conn); + } + ~WebThreeStubClient() + { + delete this->client; + } + + Json::Value accounts() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("accounts",p); + if (result.isArray()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string balanceAt(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("balanceAt",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value blockByHash(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("blockByHash",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value blockByNumber(const int& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("blockByNumber",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string call(const Json::Value& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("call",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool changed(const int& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("changed",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string codeAt(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("codeAt",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string coinbase() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("coinbase",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string compile(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("compile",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + double countAt(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("countAt",p); + if (result.isDouble()) + return result.asDouble(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + int defaultBlock() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("defaultBlock",p); + if (result.isInt()) + return result.asInt(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string gasPrice() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("gasPrice",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value getMessages(const int& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("getMessages",p); + if (result.isArray()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool listening() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("listening",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool mining() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("mining",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + int newFilter(const Json::Value& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("newFilter",p); + if (result.isInt()) + return result.asInt(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + int newFilterString(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("newFilterString",p); + if (result.isInt()) + return result.asInt(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + int number() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("number",p); + if (result.isInt()) + return result.asInt(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + int peerCount() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("peerCount",p); + if (result.isInt()) + return result.asInt(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool setCoinbase(const std::string& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("setCoinbase",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool setListening(const bool& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("setListening",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool setMining(const bool& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("setMining",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + std::string stateAt(const std::string& param1, const std::string& param2) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); + + Json::Value result = this->client->CallMethod("stateAt",p); + if (result.isString()) + return result.asString(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value transact(const Json::Value& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("transact",p); + if (result.isArray()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value transactionByHash(const std::string& param1, const int& param2) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); + + Json::Value result = this->client->CallMethod("transactionByHash",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value transactionByNumber(const int& param1, const int& param2) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); + + Json::Value result = this->client->CallMethod("transactionByNumber",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value uncleByHash(const std::string& param1, const int& param2) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); + + Json::Value result = this->client->CallMethod("uncleByHash",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + Json::Value uncleByNumber(const int& param1, const int& param2) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); +p.append(param2); + + Json::Value result = this->client->CallMethod("uncleByNumber",p); + if (result.isObject()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + bool uninstallFilter(const int& param1) throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p.append(param1); + + Json::Value result = this->client->CallMethod("uninstallFilter",p); + if (result.isBool()) + return result.asBool(); + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + + private: + jsonrpc::Client* client; +}; +#endif //_WEBTHREESTUBCLIENT_H_ |