2014-10-16 14:06:20 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
2014-10-17 13:05:13 +00:00
|
|
|
|
2014-10-16 14:06:20 +00:00
|
|
|
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.
|
2014-10-17 13:05:13 +00:00
|
|
|
|
2014-10-16 14:06:20 +00:00
|
|
|
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.
|
2014-10-17 13:05:13 +00:00
|
|
|
|
2014-10-16 14:06:20 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-17 13:05:13 +00:00
|
|
|
*/
|
2014-10-16 14:06:20 +00:00
|
|
|
/** @file jsonrpc.cpp
|
|
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
2014-10-13 09:22:28 +00:00
|
|
|
|
2014-10-24 14:14:00 +00:00
|
|
|
#if ETH_JSONRPC && 1
|
2014-10-13 09:22:28 +00:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-10-14 15:03:13 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-10-13 09:22:28 +00:00
|
|
|
#include <libdevcore/Log.h>
|
|
|
|
#include <libdevcore/CommonIO.h>
|
2014-10-13 10:28:53 +00:00
|
|
|
#include <libdevcore/CommonJS.h>
|
2014-10-13 09:22:28 +00:00
|
|
|
#include <libwebthree/WebThree.h>
|
2014-10-24 12:21:20 +00:00
|
|
|
#include <libweb3jsonrpc/WebThreeStubServer.h>
|
|
|
|
#include <libweb3jsonrpc/CorsHttpServer.h>
|
2014-10-13 09:22:28 +00:00
|
|
|
#include <jsonrpc/connectors/httpserver.h>
|
2014-10-13 10:28:53 +00:00
|
|
|
#include <jsonrpc/connectors/httpclient.h>
|
2014-10-16 17:03:41 +00:00
|
|
|
#include <set>
|
2014-10-13 09:22:28 +00:00
|
|
|
#include "JsonSpiritHeaders.h"
|
2014-10-13 14:24:34 +00:00
|
|
|
#include "TestHelper.h"
|
2014-10-16 17:03:41 +00:00
|
|
|
#include "webthreestubclient.h"
|
2014-10-13 09:22:28 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::eth;
|
|
|
|
namespace js = json_spirit;
|
|
|
|
|
2014-10-17 13:05:13 +00:00
|
|
|
namespace jsonrpc_tests
|
|
|
|
{
|
2014-10-13 09:22:28 +00:00
|
|
|
|
2014-10-13 13:08:15 +00:00
|
|
|
string name = "Ethereum(++) tests";
|
|
|
|
string dbPath;
|
2014-10-16 17:03:41 +00:00
|
|
|
auto s = set<string>{"eth", "shh"};
|
|
|
|
dev::p2p::NetworkPreferences np(30303, std::string(), false);
|
|
|
|
dev::WebThreeDirect web3(name, dbPath, true, s, np);
|
2014-10-13 09:22:28 +00:00
|
|
|
|
2014-10-16 17:03:41 +00:00
|
|
|
auto_ptr<WebThreeStubServer> jsonrpcServer;
|
|
|
|
auto_ptr<WebThreeStubClient> jsonrpcClient;
|
2014-10-13 09:22:28 +00:00
|
|
|
|
|
|
|
struct JsonrpcFixture {
|
2014-10-16 14:54:27 +00:00
|
|
|
JsonrpcFixture()
|
|
|
|
{
|
|
|
|
cnote << "setup jsonrpc";
|
|
|
|
|
|
|
|
web3.setIdealPeerCount(5);
|
|
|
|
web3.ethereum()->setForceMining(true);
|
2014-10-20 08:01:14 +00:00
|
|
|
jsonrpcServer = auto_ptr<WebThreeStubServer>(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {}));
|
2014-10-16 14:54:27 +00:00
|
|
|
jsonrpcServer->StartListening();
|
|
|
|
|
2014-10-16 17:03:41 +00:00
|
|
|
jsonrpcClient = auto_ptr<WebThreeStubClient>(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080")));
|
2014-10-16 14:54:27 +00:00
|
|
|
}
|
|
|
|
~JsonrpcFixture()
|
|
|
|
{
|
|
|
|
cnote << "teardown jsonrpc";
|
|
|
|
}
|
2014-10-13 09:22:28 +00:00
|
|
|
};
|
2014-10-16 14:54:27 +00:00
|
|
|
|
2014-10-13 09:22:28 +00:00
|
|
|
BOOST_GLOBAL_FIXTURE(JsonrpcFixture)
|
|
|
|
|
2014-10-13 13:08:15 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_balanceAt)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc balanceAt...";
|
|
|
|
dev::KeyPair key = KeyPair::create();
|
|
|
|
auto address = key.address();
|
2014-10-22 09:47:45 +00:00
|
|
|
string balance = jsonrpcClient->balanceAt(toJS(address));
|
2014-10-16 14:54:27 +00:00
|
|
|
BOOST_CHECK_EQUAL(toJS(web3.ethereum()->balanceAt(address)), balance);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_call)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_coinbase)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc coinbase...";
|
|
|
|
string coinbase = jsonrpcClient->coinbase();
|
|
|
|
BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address());
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_countAt)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc countAt...";
|
|
|
|
dev::KeyPair key = KeyPair::create();
|
|
|
|
auto address = key.address();
|
2014-10-22 09:47:45 +00:00
|
|
|
double countAt = jsonrpcClient->countAt(toJS(address));
|
2014-10-16 14:54:27 +00:00
|
|
|
BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address, 0));
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc defaultBlock...";
|
|
|
|
int defaultBlock = jsonrpcClient->defaultBlock();
|
|
|
|
BOOST_CHECK_EQUAL(defaultBlock, web3.ethereum()->getDefault());
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
2014-10-16 14:54:27 +00:00
|
|
|
|
2014-10-13 13:08:15 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc gasPrice...";
|
|
|
|
string gasPrice = jsonrpcClient->gasPrice();
|
|
|
|
BOOST_CHECK_EQUAL(gasPrice, toJS(10 * dev::eth::szabo));
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_isListening)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc isListening...";
|
2014-10-16 14:06:20 +00:00
|
|
|
|
|
|
|
web3.startNetwork();
|
|
|
|
bool listeningOn = jsonrpcClient->listening();
|
|
|
|
BOOST_CHECK_EQUAL(listeningOn, web3.isNetworkStarted());
|
|
|
|
|
|
|
|
web3.stopNetwork();
|
|
|
|
bool listeningOff = jsonrpcClient->listening();
|
|
|
|
BOOST_CHECK_EQUAL(listeningOff, web3.isNetworkStarted());
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_isMining)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc isMining...";
|
2014-10-13 13:08:15 +00:00
|
|
|
|
2014-10-16 14:54:27 +00:00
|
|
|
web3.ethereum()->startMining();
|
|
|
|
bool miningOn = jsonrpcClient->mining();
|
|
|
|
BOOST_CHECK_EQUAL(miningOn, web3.ethereum()->isMining());
|
2014-10-13 13:08:15 +00:00
|
|
|
|
2014-10-16 14:54:27 +00:00
|
|
|
web3.ethereum()->stopMining();
|
|
|
|
bool miningOff = jsonrpcClient->mining();
|
|
|
|
BOOST_CHECK_EQUAL(miningOff, web3.ethereum()->isMining());
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 08:27:48 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_accounts)
|
2014-10-13 09:22:28 +00:00
|
|
|
{
|
2014-10-20 08:27:48 +00:00
|
|
|
cnote << "Testing jsonrpc accounts...";
|
2014-10-16 14:54:27 +00:00
|
|
|
std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()};
|
2014-10-20 08:27:48 +00:00
|
|
|
jsonrpcServer->setAccounts(keys);
|
|
|
|
Json::Value k = jsonrpcClient->accounts();
|
|
|
|
jsonrpcServer->setAccounts({});
|
2014-10-16 14:54:27 +00:00
|
|
|
BOOST_CHECK_EQUAL(k.isArray(), true);
|
|
|
|
BOOST_CHECK_EQUAL(k.size(), keys.size());
|
2014-10-20 08:27:48 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-10-13 09:22:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 13:08:15 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_number)
|
|
|
|
{
|
2014-10-16 01:46:47 +00:00
|
|
|
cnote << "Testing jsonrpc number...";
|
|
|
|
int number = jsonrpcClient->number();
|
|
|
|
BOOST_CHECK_EQUAL(number, web3.ethereum()->number() + 1);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 14:24:34 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_number2)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
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);
|
2014-10-13 14:24:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 13:08:15 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_peerCount)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc peerCount...";
|
2014-10-16 14:06:20 +00:00
|
|
|
int peerCount = jsonrpcClient->peerCount();
|
|
|
|
BOOST_CHECK_EQUAL(web3.peerCount(), peerCount);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_setListening)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc setListening...";
|
2014-10-16 14:06:20 +00:00
|
|
|
|
|
|
|
jsonrpcClient->setListening(true);
|
|
|
|
BOOST_CHECK_EQUAL(web3.isNetworkStarted(), true);
|
|
|
|
|
|
|
|
jsonrpcClient->setListening(false);
|
|
|
|
BOOST_CHECK_EQUAL(web3.isNetworkStarted(), false);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_setMining)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc setMining...";
|
2014-10-13 13:08:15 +00:00
|
|
|
|
2014-10-16 14:54:27 +00:00
|
|
|
jsonrpcClient->setMining(true);
|
|
|
|
BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), true);
|
2014-10-13 13:08:15 +00:00
|
|
|
|
2014-10-16 14:54:27 +00:00
|
|
|
jsonrpcClient->setMining(false);
|
|
|
|
BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), false);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_stateAt)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc stateAt...";
|
|
|
|
dev::KeyPair key = KeyPair::create();
|
|
|
|
auto address = key.address();
|
2014-10-22 09:47:45 +00:00
|
|
|
string stateAt = jsonrpcClient->stateAt(toJS(address), "0");
|
2014-10-16 14:54:27 +00:00
|
|
|
BOOST_CHECK_EQUAL(toJS(web3.ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(jsonrpc_transact)
|
|
|
|
{
|
2014-10-16 14:54:27 +00:00
|
|
|
cnote << "Testing jsonrpc transact...";
|
|
|
|
dev::KeyPair key = KeyPair::create();
|
|
|
|
auto address = key.address();
|
|
|
|
auto receiver = KeyPair::create();
|
|
|
|
|
|
|
|
web3.ethereum()->setAddress(address);
|
2014-10-20 08:27:48 +00:00
|
|
|
jsonrpcServer->setAccounts({key});
|
2014-10-16 14:54:27 +00:00
|
|
|
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;
|
2014-10-20 08:27:48 +00:00
|
|
|
t["from"] = toJS(address);
|
2014-10-16 14:54:27 +00:00
|
|
|
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);
|
2014-10-20 08:27:48 +00:00
|
|
|
jsonrpcServer->setAccounts({});
|
2014-10-16 14:54:27 +00:00
|
|
|
dev::eth::mine(*(web3.ethereum()), 1);
|
|
|
|
auto balance2 = web3.ethereum()->balanceAt(receiver.address());
|
2014-10-20 08:27:48 +00:00
|
|
|
|
2014-10-16 14:54:27 +00:00
|
|
|
BOOST_REQUIRE(balance2 > 0);
|
|
|
|
BOOST_CHECK_EQUAL(txAmount, balance2);
|
2014-10-13 13:08:15 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 09:22:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 13:42:29 +00:00
|
|
|
#endif
|