27 #include <boost/test/unit_test.hpp> 28 #include <boost/lexical_cast.hpp> 33 #include <libweb3jsonrpc/WebThreeStubServer.h> 34 #include <jsonrpccpp/server/connectors/httpserver.h> 35 #include <jsonrpccpp/client/connectors/httpclient.h> 37 #include "../JsonSpiritHeaders.h" 45 using namespace dev::eth;
49 unique_ptr<WebThreeStubServer> jsonrpcServer;
56 static bool setup =
false;
62 web3 =
new WebThreeDirect(
"eth tests",
"",
true, {
"eth",
"shh"}, nprefs);
64 web3->setIdealPeerCount(5);
65 web3->ethereum()->setForceMining(
true);
66 auto server =
new jsonrpc::HttpServer(8080);
67 jsonrpcServer = unique_ptr<WebThreeStubServer>(
new WebThreeStubServer(*server, *web3, {}));
68 jsonrpcServer->setIdentities({});
69 jsonrpcServer->StartListening();
70 auto client =
new jsonrpc::HttpClient(
"http://localhost:8080");
75 string fromAscii(
string _s)
85 cnote <<
"Testing jsonrpc defaultBlock...";
86 int defaultBlock = jsonrpcClient->eth_defaultBlock();
92 cnote <<
"Testing jsonrpc gasPrice...";
93 string gasPrice = jsonrpcClient->eth_gasPrice();
99 cnote <<
"Testing jsonrpc isListening...";
101 web3->startNetwork();
102 bool listeningOn = jsonrpcClient->eth_listening();
106 bool listeningOff = jsonrpcClient->eth_listening();
112 cnote <<
"Testing jsonrpc isMining...";
114 web3->ethereum()->startSealing();
115 bool miningOn = jsonrpcClient->eth_mining();
118 web3->ethereum()->stopSealing();
119 bool miningOff = jsonrpcClient->eth_mining();
125 cnote <<
"Testing jsonrpc accounts...";
126 std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()};
127 jsonrpcServer->setAccounts(keys);
129 jsonrpcServer->setAccounts({});
134 auto it = std::find_if(keys.begin(), keys.end(), [i](
dev::KeyPair const& keyPair)
136 return jsToAddress(i.asString()) == keyPair.address();
144 cnote <<
"Testing jsonrpc number2...";
145 int number = jsonrpcClient->eth_number();
148 int numberAfter = jsonrpcClient->eth_number();
155 cnote <<
"Testing jsonrpc peerCount...";
156 int peerCount = jsonrpcClient->eth_peerCount();
162 cnote <<
"Testing jsonrpc setListening...";
164 jsonrpcClient->eth_setListening(
true);
167 jsonrpcClient->eth_setListening(
false);
173 cnote <<
"Testing jsonrpc setMining...";
175 jsonrpcClient->eth_setMining(
true);
178 jsonrpcClient->eth_setMining(
false);
184 cnote <<
"Testing jsonrpc stateAt...";
187 string stateAt = jsonrpcClient->eth_stateAt(
toJS(
address),
"0");
193 cnote <<
"Testing jsonrpc transact...";
194 string coinbase = jsonrpcClient->eth_coinbase();
199 auto receiver = KeyPair::create();
200 web3->ethereum()->setAuthor(
address);
202 coinbase = jsonrpcClient->eth_coinbase();
206 jsonrpcServer->setAccounts({key});
208 string balanceString = jsonrpcClient->eth_balanceAt(
toJS(
address));
209 double countAt = jsonrpcClient->eth_countAt(
toJS(
address));
218 balanceString = jsonrpcClient->eth_balanceAt(
toJS(
address));
224 auto gasPrice = 10 * dev::eth::szabo;
225 auto gas = EVMSchedule().txGas;
230 t[
"to"] =
toJS(receiver.address());
232 t[
"gas"] =
toJS(gas);
233 t[
"gasPrice"] =
toJS(gasPrice);
235 jsonrpcClient->eth_transact(t);
236 jsonrpcServer->setAccounts({});
239 countAt = jsonrpcClient->eth_countAt(
toJS(
address));
240 auto balance2 = web3->ethereum()->balanceAt(receiver.address());
241 string balanceString2 = jsonrpcClient->eth_balanceAt(
toJS(receiver.address()));
253 cnote <<
"Testing jsonrpc contract...";
254 KeyPair kp = KeyPair::create();
255 web3->ethereum()->setAuthor(kp.address());
256 jsonrpcServer->setAccounts({kp});
260 char const* sourceCode =
"contract test {\n" 261 " function f(uint a) returns(uint d) { return a * 7; }\n" 264 string compiled = jsonrpcClient->eth_solidity(sourceCode);
267 create[
"code"] = compiled;
268 string contractAddress = jsonrpcClient->eth_transact(create);
272 call[
"to"] = contractAddress;
273 call[
"data"] =
"0x00000000000000000000000000000000000000000000000000000000000000001";
274 string result = jsonrpcClient->eth_call(call);
275 BOOST_CHECK_EQUAL(result,
"0x0000000000000000000000000000000000000000000000000000000000000007");
280 cnote <<
"Testing jsonrpc contract storage...";
281 KeyPair kp = KeyPair::create();
282 web3->ethereum()->setAuthor(kp.address());
283 jsonrpcServer->setAccounts({kp});
287 char const* sourceCode = R
"( 290 function writeHello(uint value) returns(bool d){ 297 string compiled = jsonrpcClient->eth_solidity(sourceCode);
300 create[
"code"] = compiled;
301 string contractAddress = jsonrpcClient->eth_transact(create);
305 transact[
"to"] = contractAddress;
306 transact[
"data"] =
"0x00000000000000000000000000000000000000000000000000000000000000003";
307 jsonrpcClient->eth_transact(transact);
310 Json::Value storage = jsonrpcClient->eth_storageAt(contractAddress);
313 for (
auto name: storage.getMemberNames())
321 cnote <<
"Testing jsonrpc sha3...";
322 string testString =
"multiply(uint256)";
325 auto hexValue = fromAscii(testString);
326 string result = jsonrpcClient->web3_sha3(hexValue);
328 BOOST_CHECK_EQUAL(
"0xc6888fa159d67f77c2f3d7a402e199802766bd7e8d4d1ecd2274fc920265d56a", result);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
#define BOOST_AUTO_TEST_CASE(funcName)
std::string jsToDecimal(std::string const &_s)
Simple class that represents a "key pair".
std::hash for asio::adress
int Add(word *C, const word *A, const word *B, size_t N)
std::string toJS(FixedHash< S > const &_h)
Address jsToAddress(std::string const &_s)
Leniently convert string to Address (h160). Accepts integers, "0x" prefixing, non-exact length...
std::vector< byte > bytes
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
unique_ptr< WebThreeStubClient > jsonrpcClient
Address const & address() const
Retrieve the associated address of the public key.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
This file is generated by jsonrpcstub, DO NOT CHANGE IT MANUALLY!
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
#define BOOST_AUTO_TEST_SUITE_END()
struct evm_uint160be address(struct evm_env *env)
u256 jsToU256(std::string const &_s)
void mine(Client &c, int numBlocks)
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Helper functions to work with json::spirit and test files.