forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonHelper.cpp
577 lines (506 loc) · 16.8 KB
/
JsonHelper.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
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 JsonHelper.cpp
* @authors:
* Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "JsonHelper.h"
#include <libevmcore/Instruction.h>
#include <libethcore/SealEngine.h>
#include <libethereum/Client.h>
#include <libethereum/NodeConnParamsManagerApi.h>
#include <libwebthree/WebThree.h>
#include <libethcore/CommonJS.h>
#include <libethcore/ICAP.h>
#include <libwhisper/Message.h>
#include <libwhisper/WhisperHost.h>
#include <jsonrpccpp/common/exception.h>
using namespace std;
using namespace dev;
using namespace eth;
namespace dev
{
Json::Value toJson(unordered_map<u256, u256> const& _storage)
{
Json::Value res(Json::objectValue);
for (auto i: _storage)
res[toJS(i.first)] = toJS(i.second);
return res;
}
Json::Value toJson(map<h256, pair<u256, u256>> const& _storage)
{
Json::Value res(Json::objectValue);
for (auto i: _storage)
res[toJS(u256(i.second.first))] = toJS(i.second.second);
return res;
}
Json::Value toJson(Address const& _address)
{
return toJS(_address);
}
// ////////////////////////////////////////////////////////////////////////////////
// p2p
// ////////////////////////////////////////////////////////////////////////////////
namespace p2p
{
Json::Value toJson(p2p::PeerSessionInfo const& _p)
{
//@todo localAddress
//@todo protocols
Json::Value ret;
ret["id"] = _p.id.hex();
ret["name"] = _p.clientVersion;
ret["network"]["remoteAddress"] = _p.host + ":" + toString(_p.port);
ret["lastPing"] = (int)chrono::duration_cast<chrono::milliseconds>(_p.lastPing).count();
for (auto const& i: _p.notes)
ret["notes"][i.first] = i.second;
for (auto const& i: _p.caps)
ret["caps"].append(i.first + "/" + toString((unsigned)i.second));
return ret;
}
}
// ////////////////////////////////////////////////////////////////////////////////
// eth
// ////////////////////////////////////////////////////////////////////////////////
namespace eth
{
Json::Value toJson(dev::eth::BlockHeader const& _bi, SealEngineFace* _sealer)
{
Json::Value res;
if (_bi)
{
DEV_IGNORE_EXCEPTIONS(res["hash"] = toJS(_bi.hash()));
res["parentHash"] = toJS(_bi.parentHash());
res["sha3Uncles"] = toJS(_bi.sha3Uncles());
res["author"] = toJS(_bi.author());
res["stateRoot"] = toJS(_bi.stateRoot());
res["transactionsRoot"] = toJS(_bi.transactionsRoot());
res["receiptsRoot"] = toJS(_bi.receiptsRoot());
res["number"] = toJS(_bi.number());
res["gasUsed"] = toJS(_bi.gasUsed());
res["gasLimit"] = toJS(_bi.gasLimit());
res["extraData"] = toJS(_bi.extraData());
res["logsBloom"] = toJS(_bi.logBloom());
res["timestamp"] = toJS(_bi.timestamp());
res["difficulty"] = toJS(_bi.difficulty());
// TODO: remove once JSONRPC spec is updated to use "author" over "miner".
res["miner"] = toJS(_bi.author());
if (_sealer)
for (auto const& i: _sealer->jsInfo(_bi))
res[i.first] = i.second;
}
return res;
}
Json::Value toJson(dev::eth::Transaction const& _t, std::pair<h256, unsigned> _location, BlockNumber _blockNumber)
{
Json::Value res;
if (_t)
{
res["hash"] = toJS(_t.sha3());
res["input"] = toJS(_t.data());
res["to"] = _t.isCreation() ? Json::Value() : toJS(_t.receiveAddress());
res["from"] = toJS(_t.safeSender());
res["gas"] = toJS(_t.gas());
res["gasPrice"] = toJS(_t.gasPrice());
res["nonce"] = toJS(_t.randomid());
res["value"] = toJS(_t.value());
res["blockHash"] = toJS(_location.first);
res["transactionIndex"] = toJS(_location.second);
res["blockNumber"] = toJS(_blockNumber);
}
return res;
}
Json::Value toJson(dev::eth::BlockHeader const& _bi, BlockDetails const& _bd, UncleHashes const& _us, Transactions const& _ts, SealEngineFace* _face)
{
Json::Value res = toJson(_bi, _face);
if (_bi)
{
res["totalDifficulty"] = toJS(_bd.totalDifficulty);
res["uncles"] = Json::Value(Json::arrayValue);
for (h256 h: _us)
res["uncles"].append(toJS(h));
res["transactions"] = Json::Value(Json::arrayValue);
for (unsigned i = 0; i < _ts.size(); i++)
res["transactions"].append(toJson(_ts[i], std::make_pair(_bi.hash(), i), (BlockNumber)_bi.number()));
}
return res;
}
Json::Value toJson(dev::eth::BlockHeader const& _bi, BlockDetails const& _bd, UncleHashes const& _us, TransactionHashes const& _ts, SealEngineFace* _face)
{
Json::Value res = toJson(_bi, _face);
if (_bi)
{
res["totalDifficulty"] = toJS(_bd.totalDifficulty);
res["uncles"] = Json::Value(Json::arrayValue);
for (h256 h: _us)
res["uncles"].append(toJS(h));
res["transactions"] = Json::Value(Json::arrayValue);
for (h256 const& t: _ts)
res["transactions"].append(toJS(t));
}
return res;
}
Json::Value toJson(dev::eth::TransactionSkeleton const& _t)
{
Json::Value res;
res["to"] = _t.creation ? Json::Value() : toJS(_t.to);
res["from"] = toJS(_t.from);
res["gas"] = toJS(_t.gas);
res["gasPrice"] = toJS(_t.gasPrice);
res["value"] = toJS(_t.value);
res["data"] = toJS(_t.data, 32);
return res;
}
Json::Value toJson(dev::eth::TransactionReceipt const& _t)
{
Json::Value res;
res["stateRoot"] = toJS(_t.stateRoot());
res["gasUsed"] = toJS(_t.gasUsed());
res["contractAddress"] = toJS(_t.contractAddress());//新增的
res["bloom"] = toJS(_t.bloom());
res["log"] = dev::toJson(_t.log());
return res;
}
Json::Value toJson(dev::eth::LocalisedTransactionReceipt const& _t)
{
Json::Value res;
res["transactionHash"] = toJS(_t.hash());
res["transactionIndex"] = _t.transactionIndex();
res["blockHash"] = toJS(_t.blockHash());
res["blockNumber"] = _t.blockNumber();
res["cumulativeGasUsed"] = toJS(_t.gasUsed()); // TODO: check if this is fine
res["gasUsed"] = toJS(_t.gasUsed());
res["contractAddress"] = toJS(_t.contractAddress());
res["logs"] = dev::toJson(_t.localisedLogs());
return res;
}
Json::Value toJson(dev::eth::Transaction const& _t)
{
Json::Value res;
res["to"] = _t.isCreation() ? Json::Value() : toJS(_t.to());
res["from"] = toJS(_t.from());
res["gas"] = toJS(_t.gas());
res["gasPrice"] = toJS(_t.gasPrice());
res["value"] = toJS(_t.value());
res["data"] = toJS(_t.data(), 32);
res["nonce"] = toJS(_t.randomid());
res["hash"] = toJS(_t.sha3(WithSignature));
res["sighash"] = toJS(_t.sha3(WithoutSignature));
res["r"] = toJS(_t.signature().r);
res["s"] = toJS(_t.signature().s);
res["v"] = toJS(_t.signature().v);
return res;
}
Json::Value toJson(dev::eth::LocalisedTransaction const& _t)
{
Json::Value res;
if (_t)
{
res["hash"] = toJS(_t.sha3());
res["input"] = toJS(_t.data());
res["to"] = _t.isCreation() ? Json::Value() : toJS(_t.receiveAddress());
res["from"] = toJS(_t.safeSender());
res["gas"] = toJS(_t.gas());
res["gasPrice"] = toJS(_t.gasPrice());
res["nonce"] = toJS(_t.randomid());
res["value"] = toJS(_t.value());
res["blockHash"] = toJS(_t.blockHash());
res["transactionIndex"] = toJS(_t.transactionIndex());
res["blockNumber"] = toJS(_t.blockNumber());
}
return res;
}
Json::Value toJson(dev::eth::NodeConnParams const& _t)
{
Json::Value res;
res["NodeId"] = _t._sNodeId;
res["AgencyInfo"] = _t._sAgencyInfo;
res["IP"] = _t._sIP;
res["Port"] = _t._iPort;
res["IdentityType"] = _t._iIdentityType;
res["AgencyDesc"] = _t._sAgencyDesc;
return res;
}
Json::Value toJson(dev::eth::LocalisedLogEntry const& _e)
{
Json::Value res;
if (_e.isSpecial)
res = toJS(_e.special);
else
{
res = toJson(static_cast<dev::eth::LogEntry const&>(_e));
res["polarity"] = _e.polarity == BlockPolarity::Live ? true : false;
if (_e.mined)
{
res["type"] = "mined";
res["blockNumber"] = _e.blockNumber;
res["blockHash"] = toJS(_e.blockHash);
res["logIndex"] = _e.logIndex;
res["transactionHash"] = toJS(_e.transactionHash);
res["transactionIndex"] = _e.transactionIndex;
}
else
{
res["type"] = "pending";
res["blockNumber"] = Json::Value(Json::nullValue);
res["blockHash"] = Json::Value(Json::nullValue);
res["logIndex"] = Json::Value(Json::nullValue);
res["transactionHash"] = Json::Value(Json::nullValue);
res["transactionIndex"] = Json::Value(Json::nullValue);
}
}
return res;
}
Json::Value toJson(dev::eth::LogEntry const& _e)
{
Json::Value res;
res["data"] = toJS(_e.data);
res["address"] = toJS(_e.address);
res["topics"] = Json::Value(Json::arrayValue);
for (auto const& t: _e.topics)
res["topics"].append(toJS(t));
return res;
}
Json::Value toJson(std::unordered_map<h256, dev::eth::LocalisedLogEntries> const& _entriesByBlock, vector<h256> const& _order)
{
Json::Value res(Json::arrayValue);
for (auto const& i: _order)
{
auto entries = _entriesByBlock.at(i);
Json::Value currentBlock(Json::objectValue);
LocalisedLogEntry entry = entries[0];
if (entry.mined)
{
currentBlock["blockNumber"] = entry.blockNumber;
currentBlock["blockHash"] = toJS(entry.blockHash);
currentBlock["type"] = "mined";
}
else
currentBlock["type"] = "pending";
currentBlock["polarity"] = entry.polarity == BlockPolarity::Live ? true : false;
currentBlock["logs"] = Json::Value(Json::arrayValue);
for (LocalisedLogEntry const& e: entries)
{
Json::Value log(Json::objectValue);
log["logIndex"] = e.logIndex;
log["transactionIndex"] = e.transactionIndex;
log["transactionHash"] = toJS(e.transactionHash);
log["address"] = toJS(e.address);
log["data"] = toJS(e.data);
log["topics"] = Json::Value(Json::arrayValue);
for (auto const& t: e.topics)
log["topics"].append(toJS(t));
currentBlock["logs"].append(log);
}
res.append(currentBlock);
}
return res;
}
Json::Value toJsonByBlock(LocalisedLogEntries const& _entries)
{
vector<h256> order;
unordered_map <h256, LocalisedLogEntries> entriesByBlock;
for (dev::eth::LocalisedLogEntry const& e: _entries)
{
if (e.isSpecial) // skip special log
continue;
if (entriesByBlock.count(e.blockHash) == 0)
{
entriesByBlock[e.blockHash] = LocalisedLogEntries();
order.push_back(e.blockHash);
}
entriesByBlock[e.blockHash].push_back(e);
}
return toJson(entriesByBlock, order);
}
TransactionSkeleton toTransactionSkeleton(Json::Value const& _json)
{
TransactionSkeleton ret;
if (!_json.isObject() || _json.empty())
return ret;
if (!_json["from"].empty())
ret.from = jsToAddress(_json["from"].asString());
if ((!_json["to"].empty())&& (!_json["to"].asString().empty()) && _json["to"].asString() != "0x")
ret.to = jsToAddress(_json["to"].asString());
else
ret.creation = true;
if (!_json["value"].empty())
ret.value = jsToU256(_json["value"].asString());
if (!_json["gas"].empty())
ret.gas = jsToU256(_json["gas"].asString());
if (!_json["gasPrice"].empty())
ret.gasPrice = jsToU256(_json["gasPrice"].asString());
if (!_json["data"].empty()) // ethereum.js has preconstructed the data array
ret.data = jsToBytes(_json["data"].asString(), OnFailed::Throw);
if (!_json["code"].empty())
ret.data = jsToBytes(_json["code"].asString(), OnFailed::Throw);
if (!_json["randomid"].empty())
ret.randomid = jsToU256(_json["randomid"].asString());
//增加blocklimit 参数
if (!_json["blockLimit"].empty())
ret.blockLimit = jsToU256(_json["blockLimit"].asString());
return ret;
}
dev::eth::LogFilter toLogFilter(Json::Value const& _json)
{
dev::eth::LogFilter filter;
if (!_json.isObject() || _json.empty())
return filter;
// check only !empty. it should throw exceptions if input params are incorrect
if (!_json["fromBlock"].empty())
filter.withEarliest(jsToFixed<32>(_json["fromBlock"].asString()));
if (!_json["toBlock"].empty())
filter.withLatest(jsToFixed<32>(_json["toBlock"].asString()));
if (!_json["address"].empty())
{
if (_json["address"].isArray())
for (auto i : _json["address"])
filter.address(jsToAddress(i.asString()));
else
filter.address(jsToAddress(_json["address"].asString()));
}
if (!_json["topics"].empty())
for (unsigned i = 0; i < _json["topics"].size(); i++)
{
if (_json["topics"][i].isArray())
{
for (auto t: _json["topics"][i])
if (!t.isNull())
filter.topic(i, jsToFixed<32>(t.asString()));
}
else if (!_json["topics"][i].isNull()) // if it is anything else then string, it should and will fail
filter.topic(i, jsToFixed<32>(_json["topics"][i].asString()));
}
return filter;
}
// TODO: this should be removed once we decide to remove backward compatibility with old log filters
dev::eth::LogFilter toLogFilter(Json::Value const& _json, Interface const& _client) // commented to avoid warning. Uncomment once in use @ PoC-7.
{
dev::eth::LogFilter filter;
if (!_json.isObject() || _json.empty())
return filter;
// check only !empty. it should throw exceptions if input params are incorrect
if (!_json["fromBlock"].empty())
filter.withEarliest(_client.hashFromNumber(jsToBlockNumber(_json["fromBlock"].asString())));
if (!_json["toBlock"].empty())
filter.withLatest(_client.hashFromNumber(jsToBlockNumber(_json["toBlock"].asString())));
if (!_json["address"].empty())
{
if (_json["address"].isArray())
for (auto i : _json["address"])
filter.address(jsToAddress(i.asString()));
else
filter.address(jsToAddress(_json["address"].asString()));
}
if (!_json["topics"].empty())
for (unsigned i = 0; i < _json["topics"].size(); i++)
{
if (_json["topics"][i].isArray())
{
for (auto t: _json["topics"][i])
if (!t.isNull())
filter.topic(i, jsToFixed<32>(t.asString()));
}
else if (!_json["topics"][i].isNull()) // if it is anything else then string, it should and will fail
filter.topic(i, jsToFixed<32>(_json["topics"][i].asString()));
}
return filter;
}
}
// ////////////////////////////////////////////////////////////////////////////////////
// shh
// ////////////////////////////////////////////////////////////////////////////////////
namespace shh
{
Json::Value toJson(h256 const& _h, shh::Envelope const& _e, shh::Message const& _m)
{
Json::Value res;
res["hash"] = toJS(_h);
res["expiry"] = toJS(_e.expiry());
res["sent"] = toJS(_e.sent());
res["ttl"] = toJS(_e.ttl());
res["workProved"] = toJS(_e.workProved());
res["topics"] = Json::Value(Json::arrayValue);
for (auto const& t: _e.topic())
res["topics"].append(toJS(t));
res["payload"] = toJS(_m.payload());
res["from"] = toJS(_m.from());
res["to"] = toJS(_m.to());
return res;
}
shh::Message toMessage(Json::Value const& _json)
{
shh::Message ret;
if (!_json["from"].empty())
ret.setFrom(jsToPublic(_json["from"].asString()));
if (!_json["to"].empty())
ret.setTo(jsToPublic(_json["to"].asString()));
if (!_json["payload"].empty())
ret.setPayload(jsToBytes(_json["payload"].asString()));
return ret;
}
shh::Envelope toSealed(Json::Value const& _json, shh::Message const& _m, Secret const& _from)
{
unsigned ttl = 50;
unsigned workToProve = 50;
shh::BuildTopic bt;
if (!_json["ttl"].empty())
ttl = jsToInt(_json["ttl"].asString());
if (!_json["workToProve"].empty())
workToProve = jsToInt(_json["workToProve"].asString());
if (!_json["topics"].empty())
for (auto i: _json["topics"])
{
if (i.isArray())
{
for (auto j: i)
if (!j.isNull())
bt.shift(jsToBytes(j.asString()));
}
else if (!i.isNull()) // if it is anything else then string, it should and will fail
bt.shift(jsToBytes(i.asString()));
}
return _m.seal(_from, bt, ttl, workToProve);
}
pair<shh::Topics, Public> toWatch(Json::Value const& _json)
{
shh::BuildTopic bt;
Public to;
if (!_json["to"].empty())
to = jsToPublic(_json["to"].asString());
if (!_json["topics"].empty())
for (auto i: _json["topics"])
bt.shift(jsToBytes(i.asString()));
return make_pair(bt, to);
}
}
// ////////////////////////////////////////////////////////////////////////////////////
// rpc
// ////////////////////////////////////////////////////////////////////////////////////
namespace rpc
{
h256 h256fromHex(string const& _s)
{
try
{
return h256(_s);
}
catch (boost::exception const&)
{
throw jsonrpc::JsonRpcException("Invalid hex-encoded string: " + _s);
}
}
}
}