From c1eaa1c0f5ebb78ed0234cf62ae849ca2c861e51 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 27 Oct 2014 10:31:49 +0100 Subject: [PATCH] common changes in tests --- libweb3jsonrpc/WebThreeStubServer.cpp | 2 +- libweb3jsonrpc/WebThreeStubServer.h | 2 +- libweb3jsonrpc/abstractwebthreestubserver.h | 4 +- libweb3jsonrpc/spec.json | 2 +- test/jsonrpc.cpp | 78 +++++++++------------ test/webthreestubclient.h | 6 +- 6 files changed, 42 insertions(+), 52 deletions(-) diff --git a/libweb3jsonrpc/WebThreeStubServer.cpp b/libweb3jsonrpc/WebThreeStubServer.cpp index db5aa9a03..ba9e5105f 100644 --- a/libweb3jsonrpc/WebThreeStubServer.cpp +++ b/libweb3jsonrpc/WebThreeStubServer.cpp @@ -374,7 +374,7 @@ std::string WebThreeStubServer::stateAt(string const& _address, string const& _s return client() ? toJS(client()->stateAt(jsToAddress(_address), jsToU256(_storage), block)) : ""; } -Json::Value WebThreeStubServer::transact(Json::Value const& _json) +std::string WebThreeStubServer::transact(Json::Value const& _json) { std::string ret; if (!client()) diff --git a/libweb3jsonrpc/WebThreeStubServer.h b/libweb3jsonrpc/WebThreeStubServer.h index c2f85c6ff..b26b161c5 100644 --- a/libweb3jsonrpc/WebThreeStubServer.h +++ b/libweb3jsonrpc/WebThreeStubServer.h @@ -61,7 +61,7 @@ public: virtual bool setListening(bool const& _listening); virtual bool setMining(bool const& _mining); virtual std::string stateAt(std::string const& _address, std::string const& _storage); - virtual Json::Value transact(Json::Value const& _json); + virtual std::string transact(Json::Value const& _json); virtual Json::Value transactionByHash(std::string const& _hash, int const& _i); virtual Json::Value transactionByNumber(int const& _number, int const& _i); virtual Json::Value uncleByHash(std::string const& _hash, int const& _i); diff --git a/libweb3jsonrpc/abstractwebthreestubserver.h b/libweb3jsonrpc/abstractwebthreestubserver.h index d7239c032..417d034cd 100644 --- a/libweb3jsonrpc/abstractwebthreestubserver.h +++ b/libweb3jsonrpc/abstractwebthreestubserver.h @@ -36,7 +36,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("setListening", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::setListeningI); this->bindAndAddMethod(new jsonrpc::Procedure("setMining", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_BOOLEAN, "param1",jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::setMiningI); this->bindAndAddMethod(new jsonrpc::Procedure("stateAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::stateAtI); - this->bindAndAddMethod(new jsonrpc::Procedure("transact", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_ARRAY, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::transactI); + this->bindAndAddMethod(new jsonrpc::Procedure("transact", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::transactI); this->bindAndAddMethod(new jsonrpc::Procedure("transactionByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::transactionByHashI); this->bindAndAddMethod(new jsonrpc::Procedure("transactionByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_INTEGER,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::transactionByNumberI); this->bindAndAddMethod(new jsonrpc::Procedure("uncleByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::uncleByHashI); @@ -214,7 +214,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer{"eth", "shh"}; dev::p2p::NetworkPreferences np(30303, std::string(), false); dev::WebThreeDirect web3(name, dbPath, true, s, np); -auto_ptr jsonrpcServer; -auto_ptr jsonrpcClient; +unique_ptr jsonrpcServer; +unique_ptr jsonrpcClient; struct JsonrpcFixture { JsonrpcFixture() @@ -60,10 +60,10 @@ struct JsonrpcFixture { web3.setIdealPeerCount(5); web3.ethereum()->setForceMining(true); - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {})); + jsonrpcServer = unique_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {})); jsonrpcServer->StartListening(); - jsonrpcClient = auto_ptr(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080"))); + jsonrpcClient = unique_ptr(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080"))); } ~JsonrpcFixture() { @@ -73,35 +73,6 @@ struct JsonrpcFixture { 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..."; @@ -161,13 +132,6 @@ BOOST_AUTO_TEST_CASE(jsonrpc_accounts) } 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(); @@ -219,15 +183,35 @@ BOOST_AUTO_TEST_CASE(jsonrpc_stateAt) BOOST_AUTO_TEST_CASE(jsonrpc_transact) { cnote << "Testing jsonrpc transact..."; + string coinbase = jsonrpcClient->coinbase(); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address()); + dev::KeyPair key = KeyPair::create(); auto address = key.address(); auto receiver = KeyPair::create(); - web3.ethereum()->setAddress(address); + + coinbase = jsonrpcClient->coinbase(); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address()); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), address); + jsonrpcServer->setAccounts({key}); - dev::eth::mine(*(web3.ethereum()), 1); auto balance = web3.ethereum()->balanceAt(address, 0); - BOOST_REQUIRE(balance > 0); + string balanceString = jsonrpcClient->balanceAt(toJS(address)); + double countAt = jsonrpcClient->countAt(toJS(address)); + + BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address)); + BOOST_CHECK_EQUAL(countAt, 0); + BOOST_CHECK_EQUAL(toJS(balance), balanceString); + BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "0"); + + dev::eth::mine(*(web3.ethereum()), 1); + balance = web3.ethereum()->balanceAt(address, 0); + balanceString = jsonrpcClient->balanceAt(toJS(address)); + + BOOST_CHECK_EQUAL(toJS(balance), balanceString); + BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "1500000000000000000"); + auto txAmount = balance / 2u; auto gasPrice = 10 * dev::eth::szabo; auto gas = dev::eth::c_txGas; @@ -243,9 +227,15 @@ BOOST_AUTO_TEST_CASE(jsonrpc_transact) jsonrpcClient->transact(t); jsonrpcServer->setAccounts({}); dev::eth::mine(*(web3.ethereum()), 1); + + countAt = jsonrpcClient->countAt(toJS(address)); auto balance2 = web3.ethereum()->balanceAt(receiver.address()); + string balanceString2 = jsonrpcClient->balanceAt(toJS(receiver.address())); - BOOST_REQUIRE(balance2 > 0); + BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address)); + BOOST_CHECK_EQUAL(countAt, 1); + BOOST_CHECK_EQUAL(toJS(balance2), balanceString2); + BOOST_CHECK_EQUAL(jsToDecimal(balanceString2), "750000000000000000"); BOOST_CHECK_EQUAL(txAmount, balance2); } diff --git a/test/webthreestubclient.h b/test/webthreestubclient.h index 4bd8afda1..c3a392f50 100644 --- a/test/webthreestubclient.h +++ b/test/webthreestubclient.h @@ -311,14 +311,14 @@ p.append(param2); } - Json::Value transact(const Json::Value& param1) throw (jsonrpc::JsonRpcException) + std::string 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; + if (result.isString()) + return result.asString(); else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());