forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminEth.cpp
298 lines (271 loc) · 7.53 KB
/
AdminEth.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
#include <jsonrpccpp/common/exception.h>
#include <libdevcore/CommonJS.h>
#include <libethcore/KeyManager.h>
#include <libethcore/ICAP.h>
#include <libethereum/Client.h>
#include <libethereum/Executive.h>
//#include <libethashseal/EthashClient.h>
#include "AdminEth.h"
#include "SessionManager.h"
#include "JsonHelper.h"
using namespace std;
using namespace dev;
using namespace dev::rpc;
using namespace dev::eth;
AdminEth::AdminEth(eth::Client& _eth, eth::TrivialGasPricer& _gp, eth::KeyManager& _keyManager, SessionManager& _sm):
m_eth(_eth),
m_gp(_gp),
m_keyManager(_keyManager),
m_sm(_sm)
{}
bool AdminEth::admin_eth_setMining(bool _on, string const& _session)
{
RPC_ADMIN;
if (_on)
m_eth.startSealing();
else
m_eth.stopSealing();
return true;
}
Json::Value AdminEth::admin_eth_blockQueueStatus(string const& _session)
{
RPC_ADMIN;
Json::Value ret;
BlockQueueStatus bqs = m_eth.blockQueue().status();
ret["importing"] = (int)bqs.importing;
ret["verified"] = (int)bqs.verified;
ret["verifying"] = (int)bqs.verifying;
ret["unverified"] = (int)bqs.unverified;
ret["future"] = (int)bqs.future;
ret["unknown"] = (int)bqs.unknown;
ret["bad"] = (int)bqs.bad;
return ret;
}
bool AdminEth::admin_eth_setAskPrice(string const& _wei, string const& _session)
{
RPC_ADMIN;
m_gp.setAsk(jsToU256(_wei));
return true;
}
bool AdminEth::admin_eth_setBidPrice(string const& _wei, string const& _session)
{
RPC_ADMIN;
m_gp.setBid(jsToU256(_wei));
return true;
}
Json::Value AdminEth::admin_eth_findBlock(string const& _blockHash, string const& _session)
{
RPC_ADMIN;
h256 h(_blockHash);
if (m_eth.blockChain().isKnown(h))
return toJson(m_eth.blockChain().info(h));
switch(m_eth.blockQueue().blockStatus(h))
{
case QueueStatus::Ready:
return "ready";
case QueueStatus::Importing:
return "importing";
case QueueStatus::UnknownParent:
return "unknown parent";
case QueueStatus::Bad:
return "bad";
default:
return "unknown";
}
}
string AdminEth::admin_eth_blockQueueFirstUnknown(string const& _session)
{
RPC_ADMIN;
return m_eth.blockQueue().firstUnknown().hex();
}
bool AdminEth::admin_eth_blockQueueRetryUnknown(string const& _session)
{
RPC_ADMIN;
m_eth.retryUnknown();
return true;
}
Json::Value AdminEth::admin_eth_allAccounts(string const& _session)
{
RPC_ADMIN;
Json::Value ret;
u256 total = 0;
u256 pendingtotal = 0;
Address beneficiary;
for (auto const& address: m_keyManager.accounts())
{
auto pending = m_eth.balanceAt(address, PendingBlock);
auto latest = m_eth.balanceAt(address, LatestBlock);
Json::Value a;
if (address == beneficiary)
a["beneficiary"] = true;
a["address"] = toJS(address);
a["balance"] = toJS(latest);
a["nicebalance"] = formatBalance(latest);
a["pending"] = toJS(pending);
a["nicepending"] = formatBalance(pending);
ret["accounts"][m_keyManager.accountName(address)] = a;
total += latest;
pendingtotal += pending;
}
ret["total"] = toJS(total);
ret["nicetotal"] = formatBalance(total);
ret["pendingtotal"] = toJS(pendingtotal);
ret["nicependingtotal"] = formatBalance(pendingtotal);
return ret;
}
Json::Value AdminEth::admin_eth_newAccount(Json::Value const& _info, string const& _session)
{
RPC_ADMIN;
if (!_info.isMember("name"))
throw jsonrpc::JsonRpcException("No member found: name");
string name = _info["name"].asString();
auto s = ICAP::createDirect();
h128 uuid;
if (_info.isMember("password"))
{
string password = _info["password"].asString();
string hint = _info["passwordHint"].asString();
uuid = m_keyManager.import(s, name, password, hint);
}
else
uuid = m_keyManager.import(s, name);
Json::Value ret;
ret["account"] = toJS(toAddress(s));
ret["uuid"] = toUUID(uuid);
return ret;
}
bool AdminEth::admin_eth_setMiningBenefactor(string const& _uuidOrAddress, string const& _session)
{
RPC_ADMIN;
return miner_setEtherbase(_uuidOrAddress);
}
Json::Value AdminEth::admin_eth_inspect(string const& _address, string const& _session)
{
RPC_ADMIN;
if (!isHash<Address>(_address))
throw jsonrpc::JsonRpcException("Invalid address given.");
Json::Value ret;
auto h = Address(fromHex(_address));
ret["storage"] = toJson(m_eth.storageAt(h, PendingBlock));
ret["balance"] = toJS(m_eth.balanceAt(h, PendingBlock));
ret["nonce"] = toJS(m_eth.countAt(h, PendingBlock));
ret["code"] = toJS(m_eth.codeAt(h, PendingBlock));
return ret;
}
h256 AdminEth::blockHash(string const& _blockNumberOrHash) const
{
if (isHash<h256>(_blockNumberOrHash))
return h256(_blockNumberOrHash.substr(_blockNumberOrHash.size() - 64, 64));
try
{
return m_eth.blockChain().numberHash(stoul(_blockNumberOrHash));
}
catch (...)
{
throw jsonrpc::JsonRpcException("Invalid argument");
}
}
Json::Value AdminEth::admin_eth_reprocess(string const& _blockNumberOrHash, string const& _session)
{
RPC_ADMIN;
Json::Value ret;
PopulationStatistics ps;
m_eth.block(blockHash(_blockNumberOrHash), &ps);
ret["enact"] = ps.enact;
ret["verify"] = ps.verify;
ret["total"] = ps.verify + ps.enact;
return ret;
}
Json::Value AdminEth::admin_eth_vmTrace(string const& _blockNumberOrHash, int _txIndex, string const& _session)
{
RPC_ADMIN;
Json::Value ret;
if (_txIndex < 0)
throw jsonrpc::JsonRpcException("Negative index");
Block block = m_eth.block(blockHash(_blockNumberOrHash));
if ((unsigned)_txIndex < block.pending().size())
{
Transaction t = block.pending()[_txIndex];
State s(State::Null);
Executive e(s, block, _txIndex, m_eth.blockChain());
try
{
StandardTrace st;
st.setShowMnemonics();
e.initialize(t);
if (!e.execute())
e.go(st.onOp());
e.finalize();
Json::Reader().parse(st.json(), ret);
}
catch(Exception const& _e)
{
LOG(WARNING) << diagnostic_information(_e);
}
}
return ret;
}
Json::Value AdminEth::admin_eth_getReceiptByHashAndIndex(string const& _blockNumberOrHash, int _txIndex, string const& _session)
{
RPC_ADMIN;
if (_txIndex < 0)
throw jsonrpc::JsonRpcException("Negative index");
auto h = blockHash(_blockNumberOrHash);
if (!m_eth.blockChain().isKnown(h))
throw jsonrpc::JsonRpcException("Invalid/unknown block.");
auto rs = m_eth.blockChain().receipts(h);
if ((unsigned)_txIndex >= rs.receipts.size())
throw jsonrpc::JsonRpcException("Index too large.");
return toJson(rs.receipts[_txIndex]);
}
bool AdminEth::miner_start(int)
{
m_eth.startSealing();
return true;
}
bool AdminEth::miner_stop()
{
m_eth.stopSealing();
return true;
}
bool AdminEth::miner_setEtherbase(string const& _uuidOrAddress)
{
Address a;
h128 uuid = fromUUID(_uuidOrAddress);
if (uuid)
a = m_keyManager.address(uuid);
else if (isHash<Address>(_uuidOrAddress))
a = Address(_uuidOrAddress);
else
throw jsonrpc::JsonRpcException("Invalid UUID or address");
if (m_setMiningBenefactor)
m_setMiningBenefactor(a);
else
m_eth.setAuthor(a);
return true;
}
bool AdminEth::miner_setExtra(string const& _extraData)
{
m_eth.setExtraData(asBytes(_extraData));
return true;
}
bool AdminEth::miner_setGasPrice(string const& _gasPrice)
{
m_gp.setAsk(jsToU256(_gasPrice));
return true;
}
string AdminEth::miner_hashrate()
{
return "1";
/*
EthashClient const* client = nullptr;
try
{
client = asEthashClient(&m_eth);
}
catch (...)
{
throw jsonrpc::JsonRpcException("Hashrate not available - blockchain does not support mining.");
}
return toJS(client->hashrate());*/
}