Fabcoin Core  0.16.2
P2P Digital Currency
jsonrpc.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 // @debris disabled as tests fail with:
23 // unknown location(0): fatal error in "jsonrpc_setMining": std::exception: Exception -32003 : Client connector error: : libcurl error: 28
24 // /home/gav/Eth/cpp-ethereum/test/jsonrpc.cpp(169): last checkpoint
25 #if ETH_JSONRPC && 0
26 
27 #include <boost/test/unit_test.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <libdevcore/Log.h>
30 #include <libdevcore/CommonIO.h>
31 #include <libethcore/CommonJS.h>
32 #include <libwebthree/WebThree.h>
33 #include <libweb3jsonrpc/WebThreeStubServer.h>
34 #include <jsonrpccpp/server/connectors/httpserver.h>
35 #include <jsonrpccpp/client/connectors/httpclient.h>
36 #include <set>
37 #include "../JsonSpiritHeaders.h"
39 #include "WebThreeStubClient.h"
40 
41 BOOST_FIXTURE_TEST_SUITE(jsonrpc, TestOutputHelper)
42 
43 using namespace std;
44 using namespace dev;
45 using namespace dev::eth;
46 namespace js = json_spirit;
47 
48 WebThreeDirect* web3;
49 unique_ptr<WebThreeStubServer> jsonrpcServer;
51 
52 struct Setup
53 {
54  Setup()
55  {
56  static bool setup = false;
57  if (setup)
58  return;
59  setup = true;
60 
61  dev::p2p::NetworkPreferences nprefs(30303, std::string(), false);
62  web3 = new WebThreeDirect("eth tests", "", true, {"eth", "shh"}, nprefs);
63 
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");
71  jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(*client));
72  }
73 };
74 
75 string fromAscii(string _s)
76 {
77  bytes b = asBytes(_s);
78  return toHex(b, 2, HexPrefix::Add);
79 }
80 
81 BOOST_FIXTURE_TEST_SUITE(environment, Setup)
82 
83 BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
84 {
85  cnote << "Testing jsonrpc defaultBlock...";
86  int defaultBlock = jsonrpcClient->eth_defaultBlock();
87  BOOST_CHECK_EQUAL(defaultBlock, web3->ethereum()->getDefault());
88 }
89 
90 BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice)
91 {
92  cnote << "Testing jsonrpc gasPrice...";
93  string gasPrice = jsonrpcClient->eth_gasPrice();
94  BOOST_CHECK_EQUAL(gasPrice, toJS(10 * dev::eth::szabo));
95 }
96 
97 BOOST_AUTO_TEST_CASE(jsonrpc_isListening)
98 {
99  cnote << "Testing jsonrpc isListening...";
100 
101  web3->startNetwork();
102  bool listeningOn = jsonrpcClient->eth_listening();
103  BOOST_CHECK_EQUAL(listeningOn, web3->isNetworkStarted());
104 
105  web3->stopNetwork();
106  bool listeningOff = jsonrpcClient->eth_listening();
107  BOOST_CHECK_EQUAL(listeningOff, web3->isNetworkStarted());
108 }
109 
110 BOOST_AUTO_TEST_CASE(jsonrpc_isMining)
111 {
112  cnote << "Testing jsonrpc isMining...";
113 
114  web3->ethereum()->startSealing();
115  bool miningOn = jsonrpcClient->eth_mining();
116  BOOST_CHECK_EQUAL(miningOn, web3->ethereum()->isMining());
117 
118  web3->ethereum()->stopSealing();
119  bool miningOff = jsonrpcClient->eth_mining();
120  BOOST_CHECK_EQUAL(miningOff, web3->ethereum()->isMining());
121 }
122 
123 BOOST_AUTO_TEST_CASE(jsonrpc_accounts)
124 {
125  cnote << "Testing jsonrpc accounts...";
126  std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()};
127  jsonrpcServer->setAccounts(keys);
128  Json::Value k = jsonrpcClient->eth_accounts();
129  jsonrpcServer->setAccounts({});
130  BOOST_CHECK_EQUAL(k.isArray(), true);
131  BOOST_CHECK_EQUAL(k.size(), keys.size());
132  for (auto &i:k)
133  {
134  auto it = std::find_if(keys.begin(), keys.end(), [i](dev::KeyPair const& keyPair)
135  {
136  return jsToAddress(i.asString()) == keyPair.address();
137  });
138  BOOST_CHECK_EQUAL(it != keys.end(), true);
139  }
140 }
141 
142 BOOST_AUTO_TEST_CASE(jsonrpc_number)
143 {
144  cnote << "Testing jsonrpc number2...";
145  int number = jsonrpcClient->eth_number();
146  BOOST_CHECK_EQUAL(number, web3->ethereum()->number() + 1);
147  dev::eth::mine(*(web3->ethereum()), 1);
148  int numberAfter = jsonrpcClient->eth_number();
149  BOOST_CHECK_EQUAL(number + 1, numberAfter);
150  BOOST_CHECK_EQUAL(numberAfter, web3->ethereum()->number() + 1);
151 }
152 
153 BOOST_AUTO_TEST_CASE(jsonrpc_peerCount)
154 {
155  cnote << "Testing jsonrpc peerCount...";
156  int peerCount = jsonrpcClient->eth_peerCount();
157  BOOST_CHECK_EQUAL(web3->peerCount(), peerCount);
158 }
159 
160 BOOST_AUTO_TEST_CASE(jsonrpc_setListening)
161 {
162  cnote << "Testing jsonrpc setListening...";
163 
164  jsonrpcClient->eth_setListening(true);
165  BOOST_CHECK_EQUAL(web3->isNetworkStarted(), true);
166 
167  jsonrpcClient->eth_setListening(false);
168  BOOST_CHECK_EQUAL(web3->isNetworkStarted(), false);
169 }
170 
171 BOOST_AUTO_TEST_CASE(jsonrpc_setMining)
172 {
173  cnote << "Testing jsonrpc setMining...";
174 
175  jsonrpcClient->eth_setMining(true);
176  BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), true);
177 
178  jsonrpcClient->eth_setMining(false);
179  BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), false);
180 }
181 
182 BOOST_AUTO_TEST_CASE(jsonrpc_stateAt)
183 {
184  cnote << "Testing jsonrpc stateAt...";
185  dev::KeyPair key = KeyPair::create();
186  auto address = key.address();
187  string stateAt = jsonrpcClient->eth_stateAt(toJS(address), "0");
188  BOOST_CHECK_EQUAL(toJS(web3->ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt);
189 }
190 
191 BOOST_AUTO_TEST_CASE(jsonrpc_transact)
192 {
193  cnote << "Testing jsonrpc transact...";
194  string coinbase = jsonrpcClient->eth_coinbase();
195  BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->author());
196 
197  dev::KeyPair key = KeyPair::create();
198  auto address = key.address();
199  auto receiver = KeyPair::create();
200  web3->ethereum()->setAuthor(address);
201 
202  coinbase = jsonrpcClient->eth_coinbase();
203  BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->author());
205 
206  jsonrpcServer->setAccounts({key});
207  auto balance = web3->ethereum()->balanceAt(address, 0);
208  string balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
209  double countAt = jsonrpcClient->eth_countAt(toJS(address));
210 
211  BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address));
212  BOOST_CHECK_EQUAL(countAt, 0);
213  BOOST_CHECK_EQUAL(toJS(balance), balanceString);
214  BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "0");
215 
216  dev::eth::mine(*(web3->ethereum()), 1);
217  balance = web3->ethereum()->balanceAt(address, 0);
218  balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
219 
220  BOOST_CHECK_EQUAL(toJS(balance), balanceString);
221  BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "1500000000000000000");
222 
223  auto txAmount = balance / 2u;
224  auto gasPrice = 10 * dev::eth::szabo;
225  auto gas = EVMSchedule().txGas;
226 
227  Json::Value t;
228  t["from"] = toJS(address);
229  t["value"] = jsToDecimal(toJS(txAmount));
230  t["to"] = toJS(receiver.address());
231  t["data"] = toJS(bytes());
232  t["gas"] = toJS(gas);
233  t["gasPrice"] = toJS(gasPrice);
234 
235  jsonrpcClient->eth_transact(t);
236  jsonrpcServer->setAccounts({});
237  dev::eth::mine(*(web3->ethereum()), 1);
238 
239  countAt = jsonrpcClient->eth_countAt(toJS(address));
240  auto balance2 = web3->ethereum()->balanceAt(receiver.address());
241  string balanceString2 = jsonrpcClient->eth_balanceAt(toJS(receiver.address()));
242 
243  BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address));
244  BOOST_CHECK_EQUAL(countAt, 1);
245  BOOST_CHECK_EQUAL(toJS(balance2), balanceString2);
246  BOOST_CHECK_EQUAL(jsToDecimal(balanceString2), "750000000000000000");
247  BOOST_CHECK_EQUAL(txAmount, balance2);
248 }
249 
250 
251 BOOST_AUTO_TEST_CASE(simple_contract)
252 {
253  cnote << "Testing jsonrpc contract...";
254  KeyPair kp = KeyPair::create();
255  web3->ethereum()->setAuthor(kp.address());
256  jsonrpcServer->setAccounts({kp});
257 
258  dev::eth::mine(*(web3->ethereum()), 1);
259 
260  char const* sourceCode = "contract test {\n"
261  " function f(uint a) returns(uint d) { return a * 7; }\n"
262  "}\n";
263 
264  string compiled = jsonrpcClient->eth_solidity(sourceCode);
265 
267  create["code"] = compiled;
268  string contractAddress = jsonrpcClient->eth_transact(create);
269  dev::eth::mine(*(web3->ethereum()), 1);
270 
272  call["to"] = contractAddress;
273  call["data"] = "0x00000000000000000000000000000000000000000000000000000000000000001";
274  string result = jsonrpcClient->eth_call(call);
275  BOOST_CHECK_EQUAL(result, "0x0000000000000000000000000000000000000000000000000000000000000007");
276 }
277 
278 BOOST_AUTO_TEST_CASE(contract_storage)
279 {
280  cnote << "Testing jsonrpc contract storage...";
281  KeyPair kp = KeyPair::create();
282  web3->ethereum()->setAuthor(kp.address());
283  jsonrpcServer->setAccounts({kp});
284 
285  dev::eth::mine(*(web3->ethereum()), 1);
286 
287  char const* sourceCode = R"(
288  contract test {
289  uint hello;
290  function writeHello(uint value) returns(bool d){
291  hello = value;
292  return true;
293  }
294  }
295  )";
296 
297  string compiled = jsonrpcClient->eth_solidity(sourceCode);
298 
299  Json::Value create;
300  create["code"] = compiled;
301  string contractAddress = jsonrpcClient->eth_transact(create);
302  dev::eth::mine(*(web3->ethereum()), 1);
303 
304  Json::Value transact;
305  transact["to"] = contractAddress;
306  transact["data"] = "0x00000000000000000000000000000000000000000000000000000000000000003";
307  jsonrpcClient->eth_transact(transact);
308  dev::eth::mine(*(web3->ethereum()), 1);
309 
310  Json::Value storage = jsonrpcClient->eth_storageAt(contractAddress);
311  BOOST_CHECK_EQUAL(storage.getMemberNames().size(), 1);
312  // bracers are required, cause msvc couldnt handle this macro in for statement
313  for (auto name: storage.getMemberNames())
314  {
315  BOOST_CHECK_EQUAL(storage[name].asString(), "0x03");
316  }
317 }
318 
320 {
321  cnote << "Testing jsonrpc sha3...";
322  string testString = "multiply(uint256)";
323  h256 expected = dev::sha3(testString);
324 
325  auto hexValue = fromAscii(testString);
326  string result = jsonrpcClient->web3_sha3(hexValue);
327  BOOST_CHECK_EQUAL(toJS(expected), result);
328  BOOST_CHECK_EQUAL("0xc6888fa159d67f77c2f3d7a402e199802766bd7e8d4d1ecd2274fc920265d56a", result);
329 }
330 
333 
334 #endif
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
#define BOOST_AUTO_TEST_CASE(funcName)
Definition: object.cpp:15
std::string jsToDecimal(std::string const &_s)
Definition: CommonJS.h:122
Simple class that represents a "key pair".
Definition: Common.h:150
std::hash for asio::adress
Definition: Common.h:323
int Add(word *C, const word *A, const word *B, size_t N)
Definition: integer.cpp:2143
Config::Value_type Value
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
Address jsToAddress(std::string const &_s)
Leniently convert string to Address (h160). Accepts integers, "0x" prefixing, non-exact length...
Definition: CommonJS.h:43
const char * name
Definition: rest.cpp:36
std::vector< byte > bytes
Definition: Common.h:75
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
FixedHash< 32 > h256
Definition: FixedHash.h:340
Definition: shhrpc.cpp:69
unique_ptr< WebThreeStubClient > jsonrpcClient
Definition: shhrpc.cpp:65
#define b(i, j)
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:173
WebThreeDirect * web3
Definition: shhrpc.cpp:54
#define cnote
Definition: Log.h:303
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
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.
Definition: SHA3.cpp:214
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
u256 jsToU256(std::string const &_s)
Definition: CommonJS.h:110
void mine(Client &c, int numBlocks)
Definition: TestHelper.cpp:39
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Definition: CommonData.h:79
Helper functions to work with json::spirit and test files.