solidity/jsonrpc.cpp

245 lines
7.4 KiB
C++
Raw Normal View History

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
#if ETH_JSONRPC
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>
#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>
#include <set>
2014-10-13 09:22:28 +00:00
#include "JsonSpiritHeaders.h"
2014-10-13 14:24:34 +00:00
#include "TestHelper.h"
#include "webthreestubclient.h"
2014-10-13 09:22:28 +00:00
BOOST_AUTO_TEST_SUITE(jsonrpc)
2014-10-13 09:22:28 +00:00
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace js = json_spirit;
2014-11-04 14:24:02 +00:00
WebThreeDirect *web3;
2014-10-27 09:31:49 +00:00
unique_ptr<WebThreeStubServer> jsonrpcServer;
unique_ptr<WebThreeStubClient> jsonrpcClient;
2014-10-13 09:22:28 +00:00
2014-11-04 14:24:02 +00:00
struct Setup
{
Setup()
2014-10-16 14:54:27 +00:00
{
2014-11-04 14:24:02 +00:00
static bool setup = false;
if (setup)
return;
setup = true;
2014-10-16 14:54:27 +00:00
2014-11-04 14:24:02 +00:00
dev::p2p::NetworkPreferences nprefs(30303, std::string(), false);
web3 = new WebThreeDirect("Ethereum(++) tests", "", true, {"eth", "shh"}, nprefs);
web3->setIdealPeerCount(5);
web3->ethereum()->setForceMining(true);
jsonrpcServer = unique_ptr<WebThreeStubServer>(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), *web3, {}));
2014-10-30 12:06:45 +00:00
jsonrpcServer->setIdentities({});
2014-10-16 14:54:27 +00:00
jsonrpcServer->StartListening();
2014-10-27 09:31:49 +00:00
jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080")));
2014-10-16 14:54:27 +00:00
}
2014-10-13 09:22:28 +00:00
};
2014-10-16 14:54:27 +00:00
2014-11-04 14:24:02 +00:00
BOOST_FIXTURE_TEST_SUITE(environment, Setup)
2014-10-13 09:22:28 +00:00
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->eth_defaultBlock();
2014-11-04 14:24:02 +00:00
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->eth_gasPrice();
2014-10-16 14:54:27 +00:00
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
2014-11-04 14:24:02 +00:00
web3->startNetwork();
bool listeningOn = jsonrpcClient->eth_listening();
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(listeningOn, web3->isNetworkStarted());
2014-10-16 14:06:20 +00:00
2014-11-04 14:24:02 +00:00
web3->stopNetwork();
bool listeningOff = jsonrpcClient->eth_listening();
2014-11-04 14:24:02 +00:00
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-11-04 14:24:02 +00:00
web3->ethereum()->startMining();
bool miningOn = jsonrpcClient->eth_mining();
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(miningOn, web3->ethereum()->isMining());
2014-10-13 13:08:15 +00:00
2014-11-04 14:24:02 +00:00
web3->ethereum()->stopMining();
bool miningOff = jsonrpcClient->eth_mining();
2014-11-04 14:24:02 +00:00
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->eth_accounts();
2014-10-20 08:27:48 +00:00
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)
{
2014-10-31 16:13:06 +00:00
auto it = std::find_if(keys.begin(), keys.end(), [i](dev::KeyPair const& keyPair)
{
2014-10-20 08:27:48 +00:00
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-13 14:24:34 +00:00
{
2014-10-16 14:54:27 +00:00
cnote << "Testing jsonrpc number2...";
int number = jsonrpcClient->eth_number();
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(number, web3->ethereum()->number() + 1);
dev::eth::mine(*(web3->ethereum()), 1);
int numberAfter = jsonrpcClient->eth_number();
2014-10-16 14:54:27 +00:00
BOOST_CHECK_EQUAL(number + 1, numberAfter);
2014-11-04 14:24:02 +00:00
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...";
int peerCount = jsonrpcClient->eth_peerCount();
2014-11-04 14:24:02 +00:00
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->eth_setListening(true);
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(web3->isNetworkStarted(), true);
2014-10-16 14:06:20 +00:00
jsonrpcClient->eth_setListening(false);
2014-11-04 14:24:02 +00:00
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
jsonrpcClient->eth_setMining(true);
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), true);
2014-10-13 13:08:15 +00:00
jsonrpcClient->eth_setMining(false);
2014-11-04 14:24:02 +00:00
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();
string stateAt = jsonrpcClient->eth_stateAt(toJS(address), "0");
2014-11-04 14:24:02 +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...";
string coinbase = jsonrpcClient->eth_coinbase();
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address());
2014-10-27 09:31:49 +00:00
2014-10-16 14:54:27 +00:00
dev::KeyPair key = KeyPair::create();
auto address = key.address();
auto receiver = KeyPair::create();
2014-11-04 14:24:02 +00:00
web3->ethereum()->setAddress(address);
2014-10-27 09:31:49 +00:00
coinbase = jsonrpcClient->eth_coinbase();
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address());
2014-10-27 09:31:49 +00:00
BOOST_CHECK_EQUAL(jsToAddress(coinbase), address);
2014-10-20 08:27:48 +00:00
jsonrpcServer->setAccounts({key});
2014-11-04 14:24:02 +00:00
auto balance = web3->ethereum()->balanceAt(address, 0);
string balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
double countAt = jsonrpcClient->eth_countAt(toJS(address));
2014-10-27 09:31:49 +00:00
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address));
2014-10-27 09:31:49 +00:00
BOOST_CHECK_EQUAL(countAt, 0);
BOOST_CHECK_EQUAL(toJS(balance), balanceString);
BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "0");
2014-11-04 14:24:02 +00:00
dev::eth::mine(*(web3->ethereum()), 1);
balance = web3->ethereum()->balanceAt(address, 0);
balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
2014-10-27 09:31:49 +00:00
BOOST_CHECK_EQUAL(toJS(balance), balanceString);
BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "1500000000000000000");
2014-10-16 14:54:27 +00:00
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->eth_transact(t);
2014-10-20 08:27:48 +00:00
jsonrpcServer->setAccounts({});
2014-11-04 14:24:02 +00:00
dev::eth::mine(*(web3->ethereum()), 1);
2014-10-27 09:31:49 +00:00
countAt = jsonrpcClient->eth_countAt(toJS(address));
2014-11-04 14:24:02 +00:00
auto balance2 = web3->ethereum()->balanceAt(receiver.address());
string balanceString2 = jsonrpcClient->eth_balanceAt(toJS(receiver.address()));
2014-10-20 08:27:48 +00:00
2014-11-04 14:24:02 +00:00
BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address));
2014-10-27 09:31:49 +00:00
BOOST_CHECK_EQUAL(countAt, 1);
BOOST_CHECK_EQUAL(toJS(balance2), balanceString2);
BOOST_CHECK_EQUAL(jsToDecimal(balanceString2), "750000000000000000");
2014-10-16 14:54:27 +00:00
BOOST_CHECK_EQUAL(txAmount, balance2);
2014-10-13 13:08:15 +00:00
}
2014-11-04 14:24:02 +00:00
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
2014-10-14 13:42:29 +00:00
#endif