You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
9.3 KiB

10 years ago
/*
This file is part of cpp-ethereum.
10 years ago
10 years ago
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.
10 years ago
10 years ago
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.
10 years ago
10 years ago
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
10 years ago
*/
10 years ago
/** @file jsonrpc.cpp
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
10 years ago
#if ETH_JSONRPC && 0
#include <boost/test/unit_test.hpp>
10 years ago
#include <boost/lexical_cast.hpp>
#include <libdevcore/Log.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/CommonJS.h>
#include <libwebthree/WebThree.h>
#include <libweb3jsonrpc/WebThreeStubServer.h>
#include <libweb3jsonrpc/CorsHttpServer.h>
#include <jsonrpc/connectors/httpserver.h>
#include <jsonrpc/connectors/httpclient.h>
#include <set>
#include "JsonSpiritHeaders.h"
10 years ago
#include "TestHelper.h"
#include "webthreestubclient.h"
BOOST_AUTO_TEST_SUITE(jsonrpc)
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace js = json_spirit;
WebThreeDirect *web3;
unique_ptr<WebThreeStubServer> jsonrpcServer;
unique_ptr<WebThreeStubClient> jsonrpcClient;
struct Setup
{
Setup()
10 years ago
{
static bool setup = false;
if (setup)
return;
setup = true;
10 years ago
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, {}));
10 years ago
jsonrpcServer->setIdentities({});
10 years ago
jsonrpcServer->StartListening();
jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080")));
10 years ago
}
};
10 years ago
BOOST_FIXTURE_TEST_SUITE(environment, Setup)
BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
{
10 years ago
cnote << "Testing jsonrpc defaultBlock...";
10 years ago
int defaultBlock = jsonrpcClient->eth_defaultBlock();
BOOST_CHECK_EQUAL(defaultBlock, web3->ethereum()->getDefault());
}
10 years ago
BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice)
{
10 years ago
cnote << "Testing jsonrpc gasPrice...";
10 years ago
string gasPrice = jsonrpcClient->eth_gasPrice();
10 years ago
BOOST_CHECK_EQUAL(gasPrice, toJS(10 * dev::eth::szabo));
}
BOOST_AUTO_TEST_CASE(jsonrpc_isListening)
{
10 years ago
cnote << "Testing jsonrpc isListening...";
10 years ago
web3->startNetwork();
10 years ago
bool listeningOn = jsonrpcClient->eth_listening();
BOOST_CHECK_EQUAL(listeningOn, web3->isNetworkStarted());
10 years ago
web3->stopNetwork();
10 years ago
bool listeningOff = jsonrpcClient->eth_listening();
BOOST_CHECK_EQUAL(listeningOff, web3->isNetworkStarted());
}
BOOST_AUTO_TEST_CASE(jsonrpc_isMining)
{
10 years ago
cnote << "Testing jsonrpc isMining...";
web3->ethereum()->startMining();
10 years ago
bool miningOn = jsonrpcClient->eth_mining();
BOOST_CHECK_EQUAL(miningOn, web3->ethereum()->isMining());
web3->ethereum()->stopMining();
10 years ago
bool miningOff = jsonrpcClient->eth_mining();
BOOST_CHECK_EQUAL(miningOff, web3->ethereum()->isMining());
}
BOOST_AUTO_TEST_CASE(jsonrpc_accounts)
{
cnote << "Testing jsonrpc accounts...";
10 years ago
std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()};
jsonrpcServer->setAccounts(keys);
10 years ago
Json::Value k = jsonrpcClient->eth_accounts();
jsonrpcServer->setAccounts({});
10 years ago
BOOST_CHECK_EQUAL(k.isArray(), true);
BOOST_CHECK_EQUAL(k.size(), keys.size());
for (auto &i:k)
{
10 years ago
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)
10 years ago
{
10 years ago
cnote << "Testing jsonrpc number2...";
10 years ago
int number = jsonrpcClient->eth_number();
BOOST_CHECK_EQUAL(number, web3->ethereum()->number() + 1);
dev::eth::mine(*(web3->ethereum()), 1);
10 years ago
int numberAfter = jsonrpcClient->eth_number();
10 years ago
BOOST_CHECK_EQUAL(number + 1, numberAfter);
BOOST_CHECK_EQUAL(numberAfter, web3->ethereum()->number() + 1);
10 years ago
}
BOOST_AUTO_TEST_CASE(jsonrpc_peerCount)
{
10 years ago
cnote << "Testing jsonrpc peerCount...";
10 years ago
int peerCount = jsonrpcClient->eth_peerCount();
BOOST_CHECK_EQUAL(web3->peerCount(), peerCount);
}
BOOST_AUTO_TEST_CASE(jsonrpc_setListening)
{
10 years ago
cnote << "Testing jsonrpc setListening...";
10 years ago
10 years ago
jsonrpcClient->eth_setListening(true);
BOOST_CHECK_EQUAL(web3->isNetworkStarted(), true);
10 years ago
10 years ago
jsonrpcClient->eth_setListening(false);
BOOST_CHECK_EQUAL(web3->isNetworkStarted(), false);
}
BOOST_AUTO_TEST_CASE(jsonrpc_setMining)
{
10 years ago
cnote << "Testing jsonrpc setMining...";
10 years ago
jsonrpcClient->eth_setMining(true);
BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), true);
10 years ago
jsonrpcClient->eth_setMining(false);
BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), false);
}
BOOST_AUTO_TEST_CASE(jsonrpc_stateAt)
{
10 years ago
cnote << "Testing jsonrpc stateAt...";
dev::KeyPair key = KeyPair::create();
auto address = key.address();
10 years ago
string stateAt = jsonrpcClient->eth_stateAt(toJS(address), "0");
BOOST_CHECK_EQUAL(toJS(web3->ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt);
}
BOOST_AUTO_TEST_CASE(jsonrpc_transact)
{
10 years ago
cnote << "Testing jsonrpc transact...";
10 years ago
string coinbase = jsonrpcClient->eth_coinbase();
BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address());
10 years ago
dev::KeyPair key = KeyPair::create();
auto address = key.address();
auto receiver = KeyPair::create();
web3->ethereum()->setAddress(address);
10 years ago
coinbase = jsonrpcClient->eth_coinbase();
BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address());
BOOST_CHECK_EQUAL(jsToAddress(coinbase), address);
jsonrpcServer->setAccounts({key});
auto balance = web3->ethereum()->balanceAt(address, 0);
10 years ago
string balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
double countAt = jsonrpcClient->eth_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);
10 years ago
balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
BOOST_CHECK_EQUAL(toJS(balance), balanceString);
BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "1500000000000000000");
10 years ago
auto txAmount = balance / 2u;
auto gasPrice = 10 * dev::eth::szabo;
auto gas = dev::eth::c_txGas;
Json::Value t;
t["from"] = toJS(address);
10 years ago
t["value"] = jsToDecimal(toJS(txAmount));
t["to"] = toJS(receiver.address());
t["data"] = toJS(bytes());
t["gas"] = toJS(gas);
t["gasPrice"] = toJS(gasPrice);
10 years ago
jsonrpcClient->eth_transact(t);
jsonrpcServer->setAccounts({});
dev::eth::mine(*(web3->ethereum()), 1);
10 years ago
countAt = jsonrpcClient->eth_countAt(toJS(address));
auto balance2 = web3->ethereum()->balanceAt(receiver.address());
10 years ago
string balanceString2 = jsonrpcClient->eth_balanceAt(toJS(receiver.address()));
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");
10 years ago
BOOST_CHECK_EQUAL(txAmount, balance2);
}
BOOST_AUTO_TEST_CASE(simple_contract)
{
cnote << "Testing jsonrpc contract...";
KeyPair kp = KeyPair::create();
web3->ethereum()->setAddress(kp.address());
jsonrpcServer->setAccounts({kp});
dev::eth::mine(*(web3->ethereum()), 1);
char const* sourceCode = "contract test {\n"
" function f(uint a) returns(uint d) { return a * 7; }\n"
"}\n";
string compiled = jsonrpcClient->eth_solidity(sourceCode);
Json::Value create;
create["code"] = compiled;
string contractAddress = jsonrpcClient->eth_transact(create);
dev::eth::mine(*(web3->ethereum()), 1);
Json::Value call;
call["to"] = contractAddress;
call["data"] = "0x00000000000000000000000000000000000000000000000000000000000000001";
string result = jsonrpcClient->eth_call(call);
BOOST_CHECK_EQUAL(result, "0x0000000000000000000000000000000000000000000000000000000000000007");
}
10 years ago
BOOST_AUTO_TEST_CASE(contract_storage)
{
cnote << "Testing jsonrpc contract storage...";
KeyPair kp = KeyPair::create();
web3->ethereum()->setAddress(kp.address());
jsonrpcServer->setAccounts({kp});
dev::eth::mine(*(web3->ethereum()), 1);
char const* sourceCode = R"(
contract test {
uint hello;
function writeHello(uint value) returns(bool d){
hello = value;
return true;
}
}
)";
string compiled = jsonrpcClient->eth_solidity(sourceCode);
Json::Value create;
create["code"] = compiled;
string contractAddress = jsonrpcClient->eth_transact(create);
dev::eth::mine(*(web3->ethereum()), 1);
Json::Value transact;
transact["to"] = contractAddress;
transact["data"] = "0x00000000000000000000000000000000000000000000000000000000000000003";
jsonrpcClient->eth_transact(transact);
dev::eth::mine(*(web3->ethereum()), 1);
Json::Value storage = jsonrpcClient->eth_storageAt(contractAddress);
BOOST_CHECK_EQUAL(storage.getMemberNames().size(), 1);
for (auto name: storage.getMemberNames())
BOOST_CHECK_EQUAL(storage[name].asString(), "0x03");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
10 years ago
#endif