solidity/jsonrpc.cpp

335 lines
10 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
2014-12-22 02:16:38 +00:00
// @debris disabled as tests fail with:
// unknown location(0): fatal error in "jsonrpc_setMining": std::exception: Exception -32003 : Client connector error: : libcurl error: 28
// /home/gav/Eth/cpp-ethereum/test/jsonrpc.cpp(169): last checkpoint
2015-01-09 16:33:16 +00:00
#if ETH_JSONRPC && 0
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>
2014-11-18 10:46:57 +00:00
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <jsonrpccpp/client/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;
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);
auto server = new jsonrpc::HttpServer(8080);
2014-11-18 11:22:10 +00:00
jsonrpcServer = unique_ptr<WebThreeStubServer>(new WebThreeStubServer(*server, *web3, {}));
2014-10-30 12:06:45 +00:00
jsonrpcServer->setIdentities({});
2014-10-16 14:54:27 +00:00
jsonrpcServer->StartListening();
2014-11-18 11:22:10 +00:00
auto client = new jsonrpc::HttpClient("http://localhost:8080");
jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(*client));
2014-10-16 14:54:27 +00:00
}
2014-10-13 09:22:28 +00:00
};
2014-10-16 14:54:27 +00:00
2015-01-09 15:12:56 +00:00
string fromAscii(string _s)
{
bytes b = asBytes(_s);
return "0x" + toHex(b);
}
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
}
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);
2014-11-04 14:24:02 +00:00
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);
2014-11-04 14:24:02 +00:00
Json::Value call;
call["to"] = contractAddress;
call["data"] = "0x00000000000000000000000000000000000000000000000000000000000000001";
string result = jsonrpcClient->eth_call(call);
BOOST_CHECK_EQUAL(result, "0x0000000000000000000000000000000000000000000000000000000000000007");
}
2014-11-13 17:52:21 +00:00
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);
2014-11-04 14:24:02 +00:00
2014-11-13 17:52:21 +00:00
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);
// bracers are required, cause msvc couldnt handle this macro in for statement
2014-11-13 17:52:21 +00:00
for (auto name: storage.getMemberNames())
{
2014-11-13 17:52:21 +00:00
BOOST_CHECK_EQUAL(storage[name].asString(), "0x03");
}
2014-11-13 17:52:21 +00:00
}
2015-01-09 15:12:56 +00:00
BOOST_AUTO_TEST_CASE(sha3)
{
cnote << "Testing jsonrpc sha3...";
string testString = "multiply(uint256)";
h256 expected = dev::sha3(testString);
auto hexValue = fromAscii(testString);
string result = jsonrpcClient->web3_sha3(hexValue);
BOOST_CHECK_EQUAL(toJS(expected), result);
BOOST_CHECK_EQUAL("0xc6888fa159d67f77c2f3d7a402e199802766bd7e8d4d1ecd2274fc920265d56a", result);
}
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