aboutsummaryrefslogtreecommitdiffstats
path: root/jsonrpc.cpp
blob: 42b1a5ebb86aa24a49869b415cfd679f9d273214 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
    This file is part of cpp-ethereum.

    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.

    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.

    You should have received a copy of the GNU General Public License
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file jsonrpc.cpp
 * @author Marek Kotewicz <marek@ethdev.com>
 * @date 2014
 */

// @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
#if ETH_JSONRPC && 0

#include <boost/test/unit_test.hpp>
#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 <json/json.h>
#include <jsonrpccpp/server/connectors/httpserver.h>
#include <jsonrpccpp/client/connectors/httpclient.h>
#include <set>
#include "JsonSpiritHeaders.h"
#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()
    {
        static bool setup = false;
        if (setup)
            return;
        setup = true;

        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);
        jsonrpcServer = unique_ptr<WebThreeStubServer>(new WebThreeStubServer(*server, *web3, {}));
        jsonrpcServer->setIdentities({});
        jsonrpcServer->StartListening();
        auto client = new jsonrpc::HttpClient("http://localhost:8080");
        jsonrpcClient = unique_ptr<WebThreeStubClient>(new WebThreeStubClient(*client));
    }
};

string fromAscii(string _s)
{
    bytes b = asBytes(_s);
    return "0x" + toHex(b);
}

BOOST_FIXTURE_TEST_SUITE(environment, Setup)

BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock)
{
    cnote << "Testing jsonrpc defaultBlock...";
    int defaultBlock = jsonrpcClient->eth_defaultBlock();
    BOOST_CHECK_EQUAL(defaultBlock, web3->ethereum()->getDefault());
}

BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice)
{
    cnote << "Testing jsonrpc gasPrice...";
    string gasPrice = jsonrpcClient->eth_gasPrice();
    BOOST_CHECK_EQUAL(gasPrice, toJS(10 * dev::eth::szabo));
}

BOOST_AUTO_TEST_CASE(jsonrpc_isListening)
{
    cnote << "Testing jsonrpc isListening...";

    web3->startNetwork();
    bool listeningOn = jsonrpcClient->eth_listening();
    BOOST_CHECK_EQUAL(listeningOn, web3->isNetworkStarted());
    
    web3->stopNetwork();
    bool listeningOff = jsonrpcClient->eth_listening();
    BOOST_CHECK_EQUAL(listeningOff, web3->isNetworkStarted());
}

BOOST_AUTO_TEST_CASE(jsonrpc_isMining)
{
    cnote << "Testing jsonrpc isMining...";

    web3->ethereum()->startMining();
    bool miningOn = jsonrpcClient->eth_mining();
    BOOST_CHECK_EQUAL(miningOn, web3->ethereum()->isMining());

    web3->ethereum()->stopMining();
    bool miningOff = jsonrpcClient->eth_mining();
    BOOST_CHECK_EQUAL(miningOff, web3->ethereum()->isMining());
}

BOOST_AUTO_TEST_CASE(jsonrpc_accounts)
{
    cnote << "Testing jsonrpc accounts...";
    std::vector <dev::KeyPair> keys = {KeyPair::create(), KeyPair::create()};
    jsonrpcServer->setAccounts(keys);
    Json::Value k = jsonrpcClient->eth_accounts();
    jsonrpcServer->setAccounts({});
    BOOST_CHECK_EQUAL(k.isArray(), true);
    BOOST_CHECK_EQUAL(k.size(),  keys.size());
    for (auto &i:k)
    {
        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)
{
    cnote << "Testing jsonrpc number2...";
    int number = jsonrpcClient->eth_number();
    BOOST_CHECK_EQUAL(number, web3->ethereum()->number() + 1);
    dev::eth::mine(*(web3->ethereum()), 1);
    int numberAfter = jsonrpcClient->eth_number();
    BOOST_CHECK_EQUAL(number + 1, numberAfter);
    BOOST_CHECK_EQUAL(numberAfter, web3->ethereum()->number() + 1);
}

BOOST_AUTO_TEST_CASE(jsonrpc_peerCount)
{
    cnote << "Testing jsonrpc peerCount...";
    int peerCount = jsonrpcClient->eth_peerCount();
    BOOST_CHECK_EQUAL(web3->peerCount(), peerCount);
}

BOOST_AUTO_TEST_CASE(jsonrpc_setListening)
{
    cnote << "Testing jsonrpc setListening...";

    jsonrpcClient->eth_setListening(true);
    BOOST_CHECK_EQUAL(web3->isNetworkStarted(), true);
    
    jsonrpcClient->eth_setListening(false);
    BOOST_CHECK_EQUAL(web3->isNetworkStarted(), false);
}

BOOST_AUTO_TEST_CASE(jsonrpc_setMining)
{
    cnote << "Testing jsonrpc setMining...";

    jsonrpcClient->eth_setMining(true);
    BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), true);

    jsonrpcClient->eth_setMining(false);
    BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), false);
}

BOOST_AUTO_TEST_CASE(jsonrpc_stateAt)
{
    cnote << "Testing jsonrpc stateAt...";
    dev::KeyPair key = KeyPair::create();
    auto address = key.address();
    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)
{
    cnote << "Testing jsonrpc transact...";
    string coinbase = jsonrpcClient->eth_coinbase();
    BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address());
    
    dev::KeyPair key = KeyPair::create();
    auto address = key.address();
    auto receiver = KeyPair::create();
    web3->ethereum()->setAddress(address);

    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);
    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);
    balanceString = jsonrpcClient->eth_balanceAt(toJS(address));
    
    BOOST_CHECK_EQUAL(toJS(balance), balanceString);
    BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "1500000000000000000");
    
    auto txAmount = balance / 2u;
    auto gasPrice = 10 * dev::eth::szabo;
    auto gas = dev::eth::c_txGas;
    
    Json::Value t;
    t["from"] = toJS(address);
    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);
    jsonrpcServer->setAccounts({});
    dev::eth::mine(*(web3->ethereum()), 1);
    
    countAt = jsonrpcClient->eth_countAt(toJS(address));
    auto balance2 = web3->ethereum()->balanceAt(receiver.address());
    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");
    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");
}

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);
    // bracers are required, cause msvc couldnt handle this macro in for statement
    for (auto name: storage.getMemberNames())
    {
        BOOST_CHECK_EQUAL(storage[name].asString(), "0x03");
    }
}

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);
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()

#endif