Browse Source

common changes in tests

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
c1eaa1c0f5
  1. 2
      libweb3jsonrpc/WebThreeStubServer.cpp
  2. 2
      libweb3jsonrpc/WebThreeStubServer.h
  3. 4
      libweb3jsonrpc/abstractwebthreestubserver.h
  4. 2
      libweb3jsonrpc/spec.json
  5. 78
      test/jsonrpc.cpp
  6. 6
      test/webthreestubclient.h

2
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())

2
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);

4
libweb3jsonrpc/abstractwebthreestubserver.h

@ -36,7 +36,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer<AbstractWebThr
this->bindAndAddMethod(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<AbstractWebThr
virtual bool setListening(const bool& param1) = 0;
virtual bool setMining(const bool& param1) = 0;
virtual std::string stateAt(const std::string& param1, const std::string& param2) = 0;
virtual Json::Value transact(const Json::Value& param1) = 0;
virtual std::string transact(const Json::Value& param1) = 0;
virtual Json::Value transactionByHash(const std::string& param1, const int& param2) = 0;
virtual Json::Value transactionByNumber(const int& param1, const int& param2) = 0;
virtual Json::Value uncleByHash(const std::string& param1, const int& param2) = 0;

2
libweb3jsonrpc/spec.json

@ -16,7 +16,7 @@
{ "method": "countAt", "params": [""], "order": [], "returns" : 0.0},
{ "method": "codeAt", "params": [""], "order": [], "returns": ""},
{ "method": "transact", "params": [{}], "order": [], "returns": []},
{ "method": "transact", "params": [{}], "order": [], "returns": ""},
{ "method": "call", "params": [{}], "order": [], "returns": ""},
{ "method": "blockByHash", "params": [""],"order": [], "returns": {}},

78
test/jsonrpc.cpp

@ -50,8 +50,8 @@ 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;
unique_ptr<WebThreeStubServer> jsonrpcServer;
unique_ptr<WebThreeStubClient> jsonrpcClient;
struct JsonrpcFixture {
JsonrpcFixture()
@ -60,10 +60,10 @@ struct JsonrpcFixture {
web3.setIdealPeerCount(5);
web3.ethereum()->setForceMining(true);
jsonrpcServer = auto_ptr<WebThreeStubServer>(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {}));
jsonrpcServer = unique_ptr<WebThreeStubServer>(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {}));
jsonrpcServer->StartListening();
jsonrpcClient = auto_ptr<WebThreeStubClient>(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080")));
jsonrpcClient = unique_ptr<WebThreeStubClient>(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);
}

6
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());

Loading…
Cancel
Save